Skip to content
This repository has been archived by the owner on Mar 4, 2019. It is now read-only.

Commit

Permalink
Might have solved the execute issue by passing a client reference alo…
Browse files Browse the repository at this point in the history
…ng from DB to Table to Query. Might be OK. Karl will probably yell at me
  • Loading branch information
subsonic committed May 4, 2012
1 parent c4142bf commit 599a63b
Showing 1 changed file with 35 additions and 36 deletions.
71 changes: 35 additions & 36 deletions lib/postgres_db.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ var Query = function(sql, params, table) {
this.sql = sql; this.sql = sql;
this.params = params || []; this.params = params || [];
this.table = table; this.table = table;

this.db = table.db;

self.on('newListener', function(eventName){ self.on('newListener', function(eventName){
console.log('Adding a listener for ' + eventName); console.log('Adding a listener for ' + eventName);
if(eventName === 'row'){ if(eventName === 'row'){
//fire the query //fire the query
//self.execute(self); //self.execute(self);
//want to execute this! Where do we do it? this.db.execute(self.sql,self.params);
} }
}); });


Expand Down Expand Up @@ -115,7 +116,7 @@ var Table = function(tableName, pk, _db) {


}; };
this.count = function(where) { this.count = function(where) {
var query = new Query("SELECT COUNT(1) FROM " + this.name); var query = new Query("SELECT COUNT(1) FROM " + this.name, [] ,this);
if(where) { if(where) {
var criteria = parseWhere(where); var criteria = parseWhere(where);
query.sql += criteria.sql; query.sql += criteria.sql;
Expand All @@ -142,7 +143,7 @@ var Table = function(tableName, pk, _db) {
parameters.push(_.values(data[i])); parameters.push(_.values(data[i]));
} }
sql += values.join(",\n"); sql += values.join(",\n");
return new Query(sql, _.flatten(parameters)); return new Query(sql, _.flatten(parameters), this);
}; };


this.update = function(fields, where){ this.update = function(fields, where){
Expand Down Expand Up @@ -198,42 +199,40 @@ var PostgresDB = function(connection){
from information_schema.tables ist \ from information_schema.tables ist \
where table_schema NOT IN ('pg_catalog', 'information_schema')"; where table_schema NOT IN ('pg_catalog', 'information_schema')";


}; this.execute = function(sql, params, callback) {
util.inherits(PostgresDB,events.EventEmitter); var self = this;

pg.connect(self.connectionString, function(err,db){
PostgresDB.prototype.execute = function(sql, params, callback) { self.emit("beforeExecute", self);
var self = this; db.query(sql, params,function(err, results){
pg.connect(self.connectionString, function(err,db){
self.emit("beforeExecute", self); if(err && callback) callback(err,null);
db.query(sql, params,function(err, results){ else {
if(err) callback(err,null); if(results && results.rows.length > 0){
else { if(callback)callback(null,results.rows);
if(results.rows.length > 0){ }else{
if(callback)callback(null,results.rows); if(callback)callback(null,results);
}else{ }
if(callback)callback(null,results); self.emit("executed");
} }
self.emit("executed"); });
}
}); });
}); };
} this.run = function(sql,params) {

return new Query(sql,params, this);
PostgresDB.prototype.run = function(sql,params) { };
return new Query(sql,params);
}


PostgresDB.prototype.loadTables = function(callback) { this.loadTables = function(callback) {
var self = this; var self = this;
self.execute(self.tableSQL, [], function(err, tables){ self.execute(self.tableSQL, [], function(err, tables){
_.each(tables, function(table){ _.each(tables, function(table){
var t = new Table(table.name, table.pk, this); var t = new Table(table.name, table.pk, self);
self.tables.push(t); self.tables.push(t);
self[t.name] = t; self[t.name] = t;
});
callback(null,self);
}); });
callback(null,self); };
});
}; };

util.inherits(PostgresDB,events.EventEmitter);


module.exports = PostgresDB; module.exports = PostgresDB;

0 comments on commit 599a63b

Please sign in to comment.