Permalink
Browse files

mysql make stuff + using new query

  • Loading branch information...
1 parent acf1b08 commit 85ff282de4fe56bc228300bf6a40ab0b9d50316e Karl Seguin committed May 6, 2012
Showing with 40 additions and 78 deletions.
  1. +9 −66 lib/mysql.js
  2. +3 −3 lib/postgres.js
  3. +3 −3 lib/query.js
  4. +3 −2 makefile
  5. +16 −0 test/mysql_helper.coffee
  6. +2 −1 test/mysql_queries_spec.coffee
  7. +2 −2 test/mysql_schema_spec.coffee
  8. +2 −1 test/mysql_spec.coffee
View
@@ -4,42 +4,6 @@ var _ = require('underscore')._;
var mysql = require("mysql");
var Query = require('./query')
-var parseWhere = function(conditions) {
-
- var where = {};
- var params = [];
-
- if(_.isObject(conditions) &! _.isArray(conditions)) {
-
- var operations = {'=': '=', '!': '!=', '>': '>', '<': '<', '>=': '>=', '<=': '<=', '!=' : '<>', '<>': '<>'};
- var _conditions = [];
- var seed = params.length;
-
- _.each(conditions, function(value, key) {
- var parts = key.trim().split(/ +/);
- var property = parts[0];
- var operation = operations[parts[1]] || '=';
-
- if (_.isBoolean(value) || _.isNumber(value)) {
- _conditions.push(util.format('"%s" %s %d', property, operation, value));
- } else if (_.isArray(value)) {
- var arrayConditions = [];
- _.each(value, function(v) {
- arrayConditions.push("?");
- params.push(v);
- });
- _conditions.push(util.format('"%s" %s (%s)', property, operation == '!=' || operation == '<>' ? 'NOT IN' : 'IN', arrayConditions.join(', ')));
- } else {
- _conditions.push(util.format('"%s" %s %s', property, operation, '?'));
- params.push(value);
- }
- });
- where.sql = ' \nWHERE ' + _conditions.join(' \nAND ');
- where.params = params;
- };
- return where;
-};
-
var Table = function(tableName, pk, _db) {
var self = this;
this.name = tableName;
@@ -58,23 +22,15 @@ var Table = function(tableName, pk, _db) {
};
this.find = function() {
- var query = new Query("SELECT * FROM " + this.name, [], this);
- query.parseArgs(arguments);
- return query;
+ return new Query("SELECT * FROM " + this.name, [], this).parseArgs(arguments);
};
+
this.count = function(where) {
- var query = new Query("SELECT COUNT(1) FROM " + this.name, [] ,this);
- if(where) {
- var criteria = parseWhere(where);
- query.sql += criteria.sql;
- query.params.push(criteria.params);
- }
- return query;
- }
+ return new Query("SELECT COUNT(1) FROM " + this.name, [] ,this).where(where);
+ };
+
this.destroy = function() {
- var query = new Query("DELETE FROM " + this.name, [], this);
- query.parseArgs(arguments);
- return query;
+ return new Query("DELETE FROM " + this.name, [], this).parseArgs(arguments);
};
this.insert = function(data) {
@@ -104,22 +60,7 @@ var Table = function(tableName, pk, _db) {
parameters.push(value);
});
var sql = util.format("UPDATE %s SET %s", this.name, f.join(', '));
- if(where){
-
- if(_.isNumber(where)){
- sql+= util.format(" \nWHERE \"%s\" = %d", this.pk, where);
- }else if (_.isString(where)){
- sql+= util.format(" \nWHERE \"%s\" = ?", this.pk);
- parameters.push(where);
- }else{
- where = parseWhere(where);
- sql += where.sql;
- if(where.params.length > 0) parameters.push(where.params);
- }
-
- }
- var query = new Query(sql, parameters, this);
- return query;
+ return new Query(sql, parameters, this).where(where);
};
}
@@ -145,6 +86,8 @@ var MySQL = function(credentials){
where table_schema = '" + credentials.database + "'";
+ this.placeholder = function(seed) { return '?'; }
+
this.execute = function(sql, params, callback) {
var client = mysql.createClient(credentials);
View
@@ -92,9 +92,9 @@ var Postgres = function(connection){
from information_schema.tables ist \
where table_schema NOT IN ('pg_catalog', 'information_schema')";
- this.end = function() {
- pg.end();
- }
+ this.end = function() { pg.end(); };
+
+ this.placeholder = function(seed) { return '$' + seed; }
this.execute = function(sql, params, callback) {
View
@@ -68,7 +68,7 @@ var Query = function(sql, params, table) {
self.sql += util.format(" \nWHERE \"%s\" = %d", self.table.pk, conditions);
return self;
}else if (_.isString(conditions)) {
- self.sql += util.format(" \nWHERE \"%s\" = $%d", self.table.pk, self.params.length + 1);
+ self.sql += util.format(" \nWHERE \"%s\" = %s", self.table.pk, self.db.placeholder(self.params.length+1));
self.params.push(conditions);
return self;
}
@@ -91,12 +91,12 @@ var Query = function(sql, params, table) {
} else if (_.isArray(value)) {
var arrayConditions = [];
_.each(value, function(v) {
- arrayConditions.push('$' + ++seed);
+ arrayConditions.push(self.db.placeholder(++seed));
params.push(v);
});
_conditions.push(util.format('"%s" %s (%s)', property, operation == '!=' || operation == '<>' ? 'NOT IN' : 'IN', arrayConditions.join(', ')));
} else {
- _conditions.push(util.format('"%s" %s %s', property, operation, '$' + ++seed));
+ _conditions.push(util.format('"%s" %s %s', property, operation, self.db.placeholder(++seed)));
params.push(value);
}
});
View
@@ -12,6 +12,7 @@ test-mysql:
setup-pg:
@coffee test/pg_helper.coffee setup
+setup-mysql:
+ @coffee test/mysql_helper.coffee setup
-
-.PHONY: test pg mysql
+.PHONY: test pg mysql setup-pg setup-mysql
@@ -0,0 +1,16 @@
+Path = require('path')
+
+class Helper
+ @connectionString: ->
+ {user : "root", password : "", database : "test"}
+
+ @setup: ->
+ massive = require("../index")
+ massive.connect @connectionString(), (err, db) ->
+ db.createTable('products', {name: 'string'}).execute -> process.exit()
+
+module.exports = Helper
+
+
+if Path.basename(process.argv[1]) == 'mysql_helper.coffee' && process.argv[2] == 'setup'
+ Helper.setup()
@@ -1,11 +1,12 @@
massive = require("../index")
should = require("should")
util = require("util")
+helper = require('./mysql_helper')
describe "MySQL Queries", ->
db = null
before (done) ->
- massive.connect {user : "root", password : "", database : "test"}, (err,_db) ->
+ massive.connect helper.connectionString(), (err,_db) ->
db = _db
done()
@@ -1,12 +1,12 @@
massive = require("../index")
should = require("should")
util = require("util")
-
+helper = require('./mysql_helper')
describe "schema queries", ->
db = null
before (done) ->
- massive.connect {user : "root", password : "", database : "test"}, (err,_db) ->
+ massive.connect helper.connectionString(), (err,_db) ->
db = _db
done()
@@ -1,11 +1,12 @@
massive = require("../index")
should = require("should")
util = require("util")
+helper = require('./mysql_helper')
describe "Connections", ->
db = null
before (done) ->
- massive.connect {user : "root", password : "", database : "test"}, (err,_db) ->
+ massive.connect helper.connectionString(), (err,_db) ->
db = _db
done()

0 comments on commit 85ff282

Please sign in to comment.