Permalink
Browse files

Might have solved the execute issue by passing a client reference alo…

…ng from DB to Table to Query. Might be OK. Karl will probably yell at me
  • Loading branch information...
1 parent c4142bf commit 599a63b5969452595e1790834db4481a5f1c7ae5 @subsonic subsonic committed May 4, 2012
Showing with 35 additions and 36 deletions.
  1. +35 −36 lib/postgres_db.js
View
@@ -44,13 +44,14 @@ var Query = function(sql, params, table) {
this.sql = sql;
this.params = params || [];
this.table = table;
-
+ this.db = table.db;
+
self.on('newListener', function(eventName){
console.log('Adding a listener for ' + eventName);
if(eventName === 'row'){
//fire the query
//self.execute(self);
- //want to execute this! Where do we do it?
+ this.db.execute(self.sql,self.params);
}
});
@@ -115,7 +116,7 @@ var Table = function(tableName, pk, _db) {
};
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) {
var criteria = parseWhere(where);
query.sql += criteria.sql;
@@ -142,7 +143,7 @@ var Table = function(tableName, pk, _db) {
parameters.push(_.values(data[i]));
}
sql += values.join(",\n");
- return new Query(sql, _.flatten(parameters));
+ return new Query(sql, _.flatten(parameters), this);
};
this.update = function(fields, where){
@@ -198,42 +199,40 @@ var PostgresDB = function(connection){
from information_schema.tables ist \
where table_schema NOT IN ('pg_catalog', 'information_schema')";
-};
-util.inherits(PostgresDB,events.EventEmitter);
-
-PostgresDB.prototype.execute = function(sql, params, callback) {
- var self = this;
- pg.connect(self.connectionString, function(err,db){
- self.emit("beforeExecute", self);
- db.query(sql, params,function(err, results){
- if(err) callback(err,null);
- else {
- if(results.rows.length > 0){
- if(callback)callback(null,results.rows);
- }else{
- if(callback)callback(null,results);
+ this.execute = function(sql, params, callback) {
+ var self = this;
+ pg.connect(self.connectionString, function(err,db){
+ self.emit("beforeExecute", self);
+ db.query(sql, params,function(err, results){
+
+ if(err && callback) callback(err,null);
+ else {
+ if(results && results.rows.length > 0){
+ if(callback)callback(null,results.rows);
+ }else{
+ if(callback)callback(null,results);
+ }
+ self.emit("executed");
}
- self.emit("executed");
- }
+ });
});
- });
-}
-
-PostgresDB.prototype.run = function(sql,params) {
- return new Query(sql,params);
-}
+ };
+ this.run = function(sql,params) {
+ return new Query(sql,params, this);
+ };
-PostgresDB.prototype.loadTables = function(callback) {
- var self = this;
- self.execute(self.tableSQL, [], function(err, tables){
- _.each(tables, function(table){
- var t = new Table(table.name, table.pk, this);
- self.tables.push(t);
- self[t.name] = t;
+ this.loadTables = function(callback) {
+ var self = this;
+ self.execute(self.tableSQL, [], function(err, tables){
+ _.each(tables, function(table){
+ var t = new Table(table.name, table.pk, self);
+ self.tables.push(t);
+ self[t.name] = t;
+ });
+ callback(null,self);
});
- callback(null,self);
- });
+ };
};
-
+util.inherits(PostgresDB,events.EventEmitter);
module.exports = PostgresDB;

0 comments on commit 599a63b

Please sign in to comment.