node-postgres/lib/client.js

112 lines
2.5 KiB
JavaScript
Raw Normal View History

2010-10-11 12:02:11 +08:00
var sys = require('sys');
2010-10-24 01:46:27 +08:00
var net = require('net');
2010-10-23 07:16:40 +08:00
var crypto = require('crypto');
2010-10-24 01:46:27 +08:00
var EventEmitter = require('events').EventEmitter;
2010-10-24 01:45:37 +08:00
var utils = require(__dirname + '/utils');
var BufferList = require(__dirname + '/buffer-list');
2010-10-24 08:02:13 +08:00
var Connection = require(__dirname + '/connection');
2010-10-24 01:46:27 +08:00
2010-10-11 11:37:30 +08:00
var Client = function(config) {
EventEmitter.call(this);
config = config || {};
this.user = config.user;
this.database = config.database;
this.port = config.port || 5432;
this.host = config.host;
this.queryQueue = [];
2010-10-11 11:37:30 +08:00
this.stream = config.stream || new net.Stream();
this.connection = new Connection({stream: this.stream});
2010-10-11 11:37:30 +08:00
this.queryQueue = [];
this.password = config.password || '';
2010-10-20 13:34:16 +08:00
this.lastBuffer = false;
this.lastOffset = 0;
this.buffer = null;
this.offset = null;
this.encoding = 'utf8';
2010-10-11 11:37:30 +08:00
};
sys.inherits(Client, EventEmitter);
2010-10-11 11:44:13 +08:00
var p = Client.prototype;
p.connect = function() {
2010-10-11 11:37:30 +08:00
var self = this;
2010-10-24 08:02:13 +08:00
var con = this.connection;
con.on('authenticationCleartextPassword', function() {
con.passwordMessage(self.password);
});
con.on('authenticationMD5Password', function(msg) {
var inner = self.md5(self.password + self.user);
var outer = self.md5(inner + msg.salt.toString('binary'));
var md5password = "md5" + outer;
con.passwordMessage(md5password);
2010-10-23 07:16:40 +08:00
});
2010-10-11 11:37:30 +08:00
};
p.md5 = function(string) {
return crypto.createHash('md5').update(string).digest('hex');
};
2010-10-24 02:12:49 +08:00
var intParser = {
fromDbValue: parseInt
};
var floatParser = {
fromDbValue: parseFloat
};
var timeParser = {
fromDbValue: function(isoTime) {
var when = new Date();
var split = isoTime.split(':');
when.setHours(split[0]);
when.setMinutes(split[1]);
when.setSeconds(split[2].split('-') [0]);
return when;
2010-10-11 11:37:30 +08:00
}
};
2010-10-24 02:12:49 +08:00
var dateParser = {
fromDbValue: function(isoDate) {
return Date.parse(isoDate);
}
};
2010-10-24 03:02:44 +08:00
Client.dataTypes = {
2010-10-24 02:12:49 +08:00
20: intParser,
21: intParser,
23: intParser,
26: intParser,
1700: floatParser,
700: floatParser,
701: floatParser,
1083: timeParser,
1266: timeParser,
1114: dateParser,
1184: dateParser
};
p.processRowDescription = function(description) {
this.fields = description.fields;
};
p.processDataRow = function(dataRow) {
var row = dataRow.fields;
var fields = this.fields || [];
var field, dataType;
for(var i = 0, len = row.length; i < len; i++) {
field = fields[i] || 0
2010-10-24 03:02:44 +08:00
var dataType = Client.dataTypes[field.dataTypeID];
2010-10-24 02:12:49 +08:00
if(dataType) {
row[i] = dataType.fromDbValue(row[i]);
}
}
this.emit('row',row);
};
2010-10-20 13:21:13 +08:00
//end parsing methods
2010-10-11 11:37:30 +08:00
module.exports = Client;