Permalink
Browse files

removed mocha and should from package.json. Moved require statement f…

…or client inline with connection parser
  • Loading branch information...
subsonic committed May 5, 2012
1 parent 261574a commit 3160aad8a4540ac960f9d0efc02030b4adb0f50e
Showing with 12 additions and 16 deletions.
  1. +2 −12 index.js
  2. +10 −2 lib/postgres.js
  3. +0 −2 package.json
View
@@ -1,5 +1,3 @@
-var pg = require("./lib/postgres");
-var mysql = require("./lib/mysql");
var _ = require("underscore")._;
var isPostgres = function(connection){
@@ -15,20 +13,12 @@ module.exports.connect = function(connection, callback) {
//console.log("Massive connection set to " + connection);
var db;
if(isPostgres(connection)){
+ var pg = require("./lib/postgres");
db = new pg(connection);
}else if (isMySQL(connection)) {
+ var mysql = require("./lib/mysql");
db = new mysql(connection);
}
db.loadTables(callback);
- //_client.connect(connection);
};
-
-// module.exports.Client = _client;
-
-// ['run', 'createTable', 'dropTable'].forEach(function(method) {
-// module.exports[method] = _client[method];
-// });
-
-//here's what I want to do: run "connect" and create a new instance of some type of query
-//that query will be based on the Client
View
@@ -3,6 +3,9 @@ var util = require('util');
var _ = require('underscore')._;
var pg = require("pg");
+//This function examines the passed-in object anc creates a WHERE statement for Postgres. This is pg client-specific.
+//The type of object passed-in is important. If the value is a boolean or number, it will be appended as opposed to
+//using parameters.
var parseWhere = function(conditions) {
var where = {};
@@ -83,30 +86,35 @@ var Query = function(sql, params, table) {
if(args.length > 0 && _.isArray(args[0])){
columns = args[0].join(",");
self.sql = self.sql.replace("*", columns);
+ //if the second arg has {columns : "..."} then columns are also specified
}else if(args.length > 1 && args[1].columns) {
self.sql = self.sql.replace("*", args[1].columns);
-
+ //if the argument is numeric (instead of an object) - default it to a PK lookup
}else if (args.length > 0 && _.isNumber(args[0])){
var criteria = {};
criteria[self.table.pk] = args[0];
where = parseWhere(criteria);
self.sql += where.sql;
self.params = where.params;
-
+ //if the argument is an object, parse a where statement
}else if (args.length > 0 && _.isObject(args[0])){
where = parseWhere(args[0]);
self.sql += where.sql;
self.params = where.params;
}
return self;
}
+
+ //execution uses the Client
self.execute = function(callback) {
self.db.execute(self.sql, self.params, function(err,result,client){
if(callback) callback(err,result,client);
self.emit("executed",client);
})
}
+ //built-in iteration. This fetches the results using a callback
+ //TODO: use the PG client's built in streamer for this
self.each = function(callback) {
self.db.execute(self.sql,self.params, function(err,results){
if(err && callback) callback(err,self.raiseError(err));
View
@@ -4,8 +4,6 @@
, "author": "Rob Conery <rob@tekpub.com>"
, "contributors": [ "Karl Seguin <karl@openmymind.net>" ]
, "devDependencies": {
- "mocha": "*"
- , "should": "*"
, "pg" : "*"
, "mysql" : "*"
, "underscore" : "*"

0 comments on commit 3160aad

Please sign in to comment.