2010-10-24 09:26:24 +08:00
|
|
|
var net = require('net');
|
2010-10-25 14:30:14 +08:00
|
|
|
var helper = require(__dirname+'/../test-helper');
|
2011-02-24 09:50:43 +08:00
|
|
|
var Connection = require('connection');
|
2010-10-26 10:43:55 +08:00
|
|
|
var connect = function(callback) {
|
|
|
|
var username = helper.args.user;
|
|
|
|
var database = helper.args.database;
|
|
|
|
var con = new Connection({stream: new net.Stream()});
|
|
|
|
con.on('error', function(error){
|
2010-10-26 10:25:44 +08:00
|
|
|
console.log(error);
|
|
|
|
throw new Error("Connection error");
|
|
|
|
});
|
2011-07-20 05:55:39 +08:00
|
|
|
con.connect(helper.args.port || '5432', helper.args.host || 'localhost');
|
2010-10-26 10:43:55 +08:00
|
|
|
con.once('connect', function() {
|
|
|
|
con.startup({
|
2010-10-24 09:26:24 +08:00
|
|
|
user: username,
|
|
|
|
database: database
|
|
|
|
});
|
2010-10-26 10:43:55 +08:00
|
|
|
con.once('authenticationCleartextPassword', function(){
|
|
|
|
con.password(helper.args.password);
|
2010-10-26 10:25:44 +08:00
|
|
|
});
|
2010-10-26 10:43:55 +08:00
|
|
|
con.once('authenticationMD5Password', function(msg){
|
2011-03-05 04:07:20 +08:00
|
|
|
//need js client even if native client is included
|
|
|
|
var client = require(__dirname +"/../../../lib/client");
|
|
|
|
var inner = client.md5(helper.args.password+helper.args.user);
|
|
|
|
var outer = client.md5(inner + msg.salt.toString('binary'));
|
2010-10-26 10:43:55 +08:00
|
|
|
con.password("md5"+outer);
|
2010-10-26 10:25:44 +08:00
|
|
|
});
|
2010-10-24 09:26:24 +08:00
|
|
|
con.once('readyForQuery', function() {
|
|
|
|
con.query('create temp table ids(id integer)');
|
2010-10-24 10:16:21 +08:00
|
|
|
con.once('readyForQuery', function() {
|
|
|
|
con.query('insert into ids(id) values(1); insert into ids(id) values(2);');
|
|
|
|
con.once('readyForQuery', function() {
|
|
|
|
callback(con);
|
|
|
|
});
|
|
|
|
});
|
2010-10-24 09:26:24 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
connect: connect
|
|
|
|
};
|