Comparing version 0.2.4 to 0.2.5
@@ -6,2 +6,3 @@ (function() { | ||
function monologue() { | ||
// semi-global object to contain query parts until they are compiled | ||
var global = { | ||
@@ -27,3 +28,3 @@ query: '', | ||
select: function( c, t ) { | ||
if( toString.call( c ) === "[object Array]" ) | ||
if( Array.isArray( c ) ) | ||
c = c.join( ", " ); | ||
@@ -38,10 +39,9 @@ | ||
/** | ||
* d: direction, t: table, f: fields | ||
*/ | ||
join: function( d, t, f ) { | ||
join: function( dir, t, f ) { | ||
if( typeof f === "undefined" ) { | ||
f = t; | ||
t = d; | ||
d = "LEFT"; | ||
t = dir; | ||
dir = "LEFT"; | ||
} | ||
@@ -56,5 +56,6 @@ | ||
f = fields.join(" AND "); | ||
delete fields; | ||
} | ||
global.join.push( " " + d + " JOIN " + t + " ON " + f ); | ||
global.join.push( " " + dir + " JOIN " + t + " ON " + f ); | ||
@@ -66,29 +67,26 @@ return this; | ||
/** | ||
* t: table, p: params | ||
*/ | ||
insert: function( table, params ) { | ||
// I don't know why this would ever NOT be ther case | ||
if( typeof params === "object" ) { | ||
var columns = [], values = [], index = 0; | ||
insert: function( t, p ) { | ||
// I don't know why this would ever NOT be the case | ||
if( typeof p === "object" ) { | ||
var c = []; | ||
// if it's not a multidimensional array, cheat and make it one | ||
if( toString.call( params ) !== "[object Array]" ) { | ||
params = [params]; | ||
// if it's not a nested array, cheat and make it one | ||
if( toString.call( p ) !== "[object Array]" ) { | ||
p = [p]; | ||
} | ||
columns = this.stringify( params, ""); | ||
columns = "(" + columns.shift() + ") VALUES " | ||
+ columns.join(','); | ||
c = this.stringify( p, ""); | ||
c = "(" + c.shift() + ") VALUES " | ||
+ c.join(','); | ||
} | ||
else if( typeof params === "string" ) { | ||
var columns = params; | ||
else if( typeof p === "string" ) { | ||
var c = p; | ||
} | ||
else { | ||
// emit error | ||
} | ||
global.query = "INSERT INTO " + t + " " + c; | ||
global.query = "INSERT INTO " + table + " " + columns; | ||
return this; | ||
@@ -99,12 +97,13 @@ }, | ||
/** | ||
* t: table, p: params | ||
*/ | ||
update: function( table, params ) { | ||
if( typeof params === "object" ) { | ||
var columns = this.stringify(params); | ||
columns = "SET " + columns.join( ', ' ); | ||
update: function( t, p ) { | ||
if( typeof p === "object" ) { | ||
var c = this.stringify( p ); | ||
c = "SET " + c.join( ', ' ); | ||
} | ||
else if( typeof params === "string" ) { | ||
var columns = params; | ||
else if( typeof p === "string" ) { | ||
var c = p; | ||
} | ||
@@ -116,3 +115,3 @@ | ||
global.query = "UPDATE " + table + " " + columns; | ||
global.query = "UPDATE " + t + " " + c; | ||
@@ -126,5 +125,5 @@ return this; | ||
delete: function( table, where ) { | ||
global.query = "DELETE FROM " + table; | ||
return ( where ? this.where( where ) : this ); | ||
delete: function( t, w ) { | ||
global.query = "DELETE FROM " + t; | ||
return ( w ? this.where( w ) : this ); | ||
}, | ||
@@ -141,9 +140,7 @@ | ||
// console.log(w) | ||
if( toString.call( w ) === "[object Object]" ) { | ||
var criteria = this.stringify( w ); | ||
var crit = this.stringify( w ); | ||
// stringify the where statements | ||
w = criteria.join( s ); | ||
w = crit.join( s ); | ||
} | ||
@@ -175,4 +172,4 @@ | ||
like: function( like, separator ) { | ||
separator = separator || "AND"; | ||
like: function( like, sep ) { | ||
sep = sep || "AND"; | ||
@@ -223,4 +220,4 @@ // calling this.where() will take of stringifying, so gluing the | ||
having: function( h, separator ) { | ||
separator = ( typeof separator === "undefined" ? "AND" : separator ); | ||
having: function( h, sep ) { | ||
sep = ( typeof sep === "undefined" ? "AND" : sep ); | ||
@@ -231,3 +228,3 @@ if( typeof h !== "string" ) { | ||
// stringify the having statements | ||
h = criteria.join( " " + separator + " " ); | ||
h = criteria.join( " " + sep + " " ); | ||
} | ||
@@ -238,3 +235,3 @@ | ||
global.having = ( global.having.length > 0 | ||
? global.having + " " + separator + " " + h | ||
? global.having + " " + sep + " " + h | ||
: h ); | ||
@@ -272,3 +269,4 @@ | ||
/** | ||
* f: file path, t: field terminator, e: field enclosure, l: line terminator | ||
* f: file path, t: field terminator, e: field enclosure, | ||
* l: line terminator | ||
*/ | ||
@@ -318,2 +316,3 @@ | ||
/** | ||
* Takes an object or and array of objects and builds a SQL string | ||
* p: params, s: separator, pre: bound param prefix | ||
@@ -329,2 +328,3 @@ */ | ||
global.columns = Object.keys( p[0] ).sort(); | ||
c.push( global.columns ); | ||
@@ -335,6 +335,7 @@ for( var ii = 0, l = p.length; ii < l; ++ii ) { | ||
if( toString.call( p[ii] ) === "[object Object]" ) { | ||
c.push( global.columns ); | ||
c.push( "(" + this.stringify( p[ii], "" ) + ")"); | ||
} | ||
// I can't think of a circumstance where this block | ||
// would ever execute. further testing needed. | ||
else { | ||
@@ -365,2 +366,3 @@ // generate a comma-separated list of fields | ||
/** | ||
* takes a key/value and formats it for use in a bound-param query | ||
*/ | ||
@@ -383,7 +385,5 @@ | ||
if( typeof module !== "undefined" && module.exports ) { | ||
// module.exports = new Monologue; | ||
module.exports = monologue; | ||
} | ||
else { | ||
// root.monologue = new Monologue; | ||
root.monologue = monologue; | ||
@@ -390,0 +390,0 @@ } |
{ | ||
"name": "monologue", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "Streamlined MySQL query building", | ||
@@ -5,0 +5,0 @@ "main": "./monologue.js", |
289
12820