node-postgres/lib/client.js

224 lines
5.4 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-25 11:52:12 +08:00
this.connection = config.connection || new Connection({stream: config.stream || new net.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;
2010-10-24 09:26:24 +08:00
con.connect(this.port, this.host);
2010-10-25 11:52:12 +08:00
//once connection is established send startup message
2010-10-24 09:26:24 +08:00
con.on('connect', function() {
con.startup({
2010-10-24 09:26:24 +08:00
user: self.user,
database: self.database
});
});
2010-10-25 11:52:12 +08:00
//password request handling
2010-10-24 08:02:13 +08:00
con.on('authenticationCleartextPassword', function() {
con.password(self.password);
});
2010-10-25 11:52:12 +08:00
//password request handling
con.on('authenticationMD5Password', function(msg) {
2010-10-24 09:26:24 +08:00
var inner = Client.md5(self.password + self.user);
var outer = Client.md5(inner + msg.salt.toString('binary'));
var md5password = "md5" + outer;
con.password(md5password);
2010-10-23 07:16:40 +08:00
});
2010-10-25 11:52:12 +08:00
con.on('readyForQuery', function() {
self.readyForQuery = true;
2010-10-24 02:12:49 +08:00
2010-10-25 11:52:12 +08:00
self.pulseQueryQueue();
});
2010-10-26 14:51:12 +08:00
con.on('error', function(error) {
self.emit('error', error);
});
2010-10-24 02:12:49 +08:00
};
2010-10-25 11:52:12 +08:00
p.pulseQueryQueue = function() {
if(this.readyForQuery===true && this.queryQueue.length > 0) {
this.readyForQuery = false;
var query = this.queryQueue.shift();
2010-10-25 12:32:18 +08:00
query.submit(this.connection);
2010-10-11 11:37:30 +08:00
}
};
2010-10-25 11:52:12 +08:00
p.query = function(config) {
//can take in strings or config objects
var query = new Query(config.text ? config : { text: config });
2010-10-25 12:32:18 +08:00
this.queryQueue.push(query);
2010-10-25 11:52:12 +08:00
this.pulseQueryQueue();
2010-10-25 12:32:18 +08:00
return query;
2010-10-24 02:12:49 +08:00
};
2010-10-26 10:24:17 +08:00
p.end = function() {
this.connection.end();
};
2010-10-25 11:52:12 +08:00
Client.md5 = function(string) {
return crypto.createHash('md5').update(string).digest('hex');
2010-10-24 02:12:49 +08:00
};
2010-10-25 12:32:18 +08:00
var Query = function(config) {
this.text = config.text;
this.values = config.values;
this.rows = config.rows;
this.types = config.types;
this.name = config.name;
//for code clarity purposes we'll declare this here though it's not
//set or used until a rowDescription message comes in
this.rowDescription = null;
2010-10-25 12:32:18 +08:00
EventEmitter.call(this);
};
sys.inherits(Query, EventEmitter);
2010-10-25 12:32:18 +08:00
var p = Query.prototype;
p.isBoundCommand = function() {
return (this.values || 0).length > 0 || this.name || this.rows;
};
2010-10-25 12:32:18 +08:00
p.submit = function(connection) {
var self = this;
if(this.isBoundCommand()) {
throw new Error('Bound command not supported yet! :(');
} else {
connection.query(this.text);
}
2010-10-25 12:32:18 +08:00
var handleRowDescription = function(msg) {
self.onRowDescription(msg);
2010-10-25 12:32:18 +08:00
};
var handleDatarow = function(msg) {
self.onDataRow(msg);
2010-10-25 12:32:18 +08:00
};
connection.on('rowDescription', handleRowDescription);
connection.on('dataRow', handleDatarow);
connection.once('readyForQuery', function() {
//remove all listeners
connection.removeListener('rowDescription', handleRowDescription);
connection.removeListener('dataRow', handleDatarow);
self.emit('end');
});
};
2010-10-26 10:24:17 +08:00
p.onRowDescription = function(msg) {
var typeIds = msg.fields.map(function(field) {
return field.dataTypeID;
});
var noParse = function(val) {
return val;
};
this.converters = typeIds.map(function(typeId) {
return Client.dataTypeParser[typeId] || noParse;
});
};
//handles the raw 'dataRow' event from the connection does type coercion
p.onDataRow = function(msg) {
var fields = msg.fields;
var converters = this.converters || [];
var len = msg.fields.length;
for(var i = 0; i < len; i++) {
fields[i] = this.converters[i] (fields[i]);
}
msg.fields = fields;
this.emit('row', msg);
};
2010-10-26 10:24:17 +08:00
2010-10-25 11:52:12 +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;
// }
// };
// var dateParser = {
// fromDbValue: function(isoDate) {
// return Date.parse(isoDate);
// }
// };
Client.dataTypeParser = {
20: parseInt,
21: parseInt,
23: parseInt,
2010-10-27 13:15:58 +08:00
26: parseInt,
1700: parseFloat,
700: parseFloat,
2010-10-27 13:31:34 +08:00
701: parseFloat,
16: function(dbVal) {
return dbVal === 't';
}
// 1083: timeParser,
// 1266: timeParser,
// 1114: dateParser,
// 1184: dateParser
};
2010-10-25 11:52:12 +08:00
// 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
// var dataType = Client.dataTypes[field.dataTypeID];
// if(dataType) {
// row[i] = dataType.fromDbValue(row[i]);
// }
// }
// this.emit('row',row);
// };
2010-10-24 02:12:49 +08:00
2010-10-20 13:21:13 +08:00
//end parsing methods
2010-10-11 11:37:30 +08:00
module.exports = Client;