Comparing version 0.7.0 to 0.7.1
117
monologue.js
@@ -60,2 +60,7 @@ (function(exports) { | ||
Monologue.prototype.select = function select( col, tbl ) { | ||
if( typeof tbl === 'undefined' ) { | ||
tbl = col; | ||
col = '*'; | ||
} | ||
if( this.opt.backquote ) { | ||
@@ -213,17 +218,50 @@ tbl = this.backquote(tbl); | ||
Monologue.prototype.where = function where( wh, sep ) { | ||
this.parts.where = this.condition(wh, sep, this.parts.where); | ||
this.last_condition = this.where; | ||
return this; | ||
}; | ||
/** | ||
*/ | ||
Monologue.prototype.having = function having( hav, sep ) { | ||
this.parts.having = this.condition(hav, sep, this.parts.having); | ||
this.last_condition = this.having; | ||
return this; | ||
}; | ||
Monologue.prototype.condition = function condition(cond, sep, part) { | ||
sep = ( typeof sep === "undefined" ? "AND" : sep ); | ||
sep = ( sep.length > 0 ? " " + sep + " " : sep ); | ||
wh = this.condition(wh, sep); | ||
if( cond instanceof Array && Object(cond[0]) === cond[0] ) { | ||
cond.forEach(function(v, k, arr) { | ||
arr[k] = this.stringify( v ).join(' AND '); | ||
}.bind(this)); | ||
// check if a previous where statement has been set and glue it all together | ||
this.parts.where = ( this.parts.where.length > 0 | ||
? this.parts.where + sep + wh | ||
: wh ); | ||
// join an array of objects with OR | ||
cond = '(' + cond.join(' OR ') + ')'; | ||
} | ||
this.last_condition = this.where; | ||
else if( cond instanceof Array ) { | ||
cond = cond.join(' OR '); | ||
} | ||
return this; | ||
else if( cond === Object(cond) ) { | ||
// stringify the where statements | ||
cond = this.stringify( cond ).join( sep ); | ||
} | ||
part = ( part.length > 0 ? part + sep + cond : cond ); | ||
return part; | ||
}; | ||
Monologue.prototype.and = function and( wh ) { | ||
@@ -294,45 +332,2 @@ return this.where( wh, 'AND' ); | ||
Monologue.prototype.having = function having( hav, sep ) { | ||
sep = ( typeof sep === "undefined" ? "AND" : sep ); | ||
sep = ( sep.length > 0 ? " " + sep + " " : sep ); | ||
hav = this.condition(hav, sep); | ||
// check if a previous statement has been set and glue it all together | ||
this.parts.having = ( this.parts.having.length > 0 | ||
? this.parts.having + sep + hav | ||
: hav ); | ||
this.last_condition = this.having; | ||
return this; | ||
}; | ||
Monologue.prototype.condition = function condition(cond, sep) { | ||
if( cond instanceof Array && Object(cond[0]) === cond[0] ) { | ||
cond.forEach(function(v, k, arr) { | ||
arr[k] = this.stringify( v ).join(' AND '); | ||
}.bind(this)); | ||
// join an array of objects with OR | ||
cond = '(' + cond.join(' OR ') + ')'; | ||
} | ||
else if( cond instanceof Array ) { | ||
cond = cond.join(' OR '); | ||
} | ||
else if( cond === Object(cond) ) { | ||
// stringify the where statements | ||
cond = this.stringify( cond ).join( sep ); | ||
} | ||
return cond; | ||
}; | ||
/** | ||
*/ | ||
Monologue.prototype.order = function order( ord, dir ) { | ||
@@ -404,2 +399,17 @@ dir = dir || 'ASC'; | ||
//*** not sure if these 2 blocks belong. they make queries read more | ||
//*** naturally but by adding complexity... | ||
//*** sample usage | ||
// mono().select('dinner').where('meat').not('pork').sql() | ||
// mono().select('dinner').where('meat').not(null).sql() | ||
// else if( typeof p === 'string' ) { | ||
// this.where( this.format(p), '!=' ); | ||
// } | ||
// else if( p === null ) { | ||
// this.where( ' IS NOT NULL', '' ); | ||
// } | ||
else { | ||
@@ -706,3 +716,8 @@ this.where( ' NOT', '' ); | ||
// return '`' + col.replace(/(\.|\s+)/g, '`$1`') + '`'; | ||
//*** this could be turned on after some extensive testing, might be | ||
//*** handy when column prefixes must be handled manually | ||
//*** the regex seems to be working nicley in prelim tests though | ||
// return '`' + col.replace(/(?!\w)(\.)(?=[a-z])+/gi, '`$1`') + '`'; | ||
return '`' + pre + col + '`'; | ||
@@ -709,0 +724,0 @@ } |
{ | ||
"name": "monologue", | ||
"version": "0.7.0", | ||
"version": "0.7.1", | ||
"description": "Streamlined MySQL query building", | ||
@@ -30,4 +30,4 @@ "main": "./monologue.js", | ||
"devDependencies": { | ||
"nodeunit": "^0.9.1" | ||
"nodeunit": "^0.11.0" | ||
} | ||
} |
@@ -8,2 +8,6 @@ Monologue - Streamlined query building | ||
***NOTICE*** | ||
Starting in version 0.8.0, Monologue will only be compatible with 4.latest and up to take advantage of ES6 features. Do not upgrade if you are still using 0.12 or lower. | ||
***Breaking changes for 0.7.0*** | ||
@@ -30,2 +34,7 @@ | ||
`.select()` was updated in 0.7.1 to *optionally* accept a table name only, making '*' the implicit column selection. Example: | ||
// 'SELECT * FROM `users` WHERE `email` = 'some@example.com' | ||
monologue().select('users').where({email: 'some@example.com'}).sql() | ||
A new method was added in 0.7.0, taking advantage of the recent API changes. It's pretty self-explanatory: | ||
@@ -32,0 +41,0 @@ |
58607
6
534
251