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

Commit

Permalink
removed mocha and should from package.json. Moved require statement f…
Browse files Browse the repository at this point in the history
…or client inline with connection parser
  • Loading branch information
subsonic committed May 5, 2012
1 parent 261574a commit 3160aad
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
14 changes: 2 additions & 12 deletions index.js
@@ -1,5 +1,3 @@
var pg = require("./lib/postgres");
var mysql = require("./lib/mysql");
var _ = require("underscore")._;

var isPostgres = function(connection){
Expand All @@ -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
12 changes: 10 additions & 2 deletions lib/postgres.js
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 0 additions & 2 deletions package.json
Expand Up @@ -4,8 +4,6 @@
, "author": "Rob Conery <rob@tekpub.com>"
, "contributors": [ "Karl Seguin <karl@openmymind.net>" ]
, "devDependencies": {
"mocha": "*"
, "should": "*"
, "pg" : "*"
, "mysql" : "*"
, "underscore" : "*"
Expand Down

0 comments on commit 3160aad

Please sign in to comment.