Comparing version 0.5.0 to 0.6.0
131
monologue.js
@@ -26,2 +26,6 @@ (function(exports) { | ||
// store whether where() or having() was used last | ||
// (I hope this doesn't get ugly) | ||
this.condition = null; | ||
// set the inital parts container | ||
@@ -62,6 +66,7 @@ this.reset(); | ||
if( Array.isArray( col ) ) | ||
if( Array.isArray( col ) ) { | ||
col = col.join( ", " ); | ||
} | ||
this.parts.query = "SELECT " + col + " FROM " + tbl; | ||
this.parts.query = ["SELECT", col, "FROM", tbl].join(' '); | ||
@@ -101,3 +106,3 @@ return this; | ||
this.parts.join.push( " " + dir + " JOIN " + tbl + " ON " + stmt ); | ||
this.parts.join.push( [dir, "JOIN", tbl, "ON", stmt].join(' ') ); | ||
@@ -134,3 +139,3 @@ return this; | ||
this.parts.query = "INSERT INTO " + tbl + " " + col; | ||
this.parts.query = ["INSERT INTO", tbl, col].join(' '); | ||
@@ -160,3 +165,3 @@ return this; | ||
this.parts.query = "UPDATE " + tbl + " " + col; | ||
this.parts.query = ["UPDATE", tbl, col].join(' '); | ||
@@ -215,2 +220,4 @@ return this; | ||
this.condition = this.where; | ||
return this; | ||
@@ -245,5 +252,3 @@ }; | ||
this.parts.where += " LIKE " + like; | ||
return this; | ||
return this.where(" LIKE " + like, ''); | ||
}; | ||
@@ -261,5 +266,3 @@ | ||
this.parts.where += " BETWEEN " + one + " AND " + two; | ||
return this; | ||
return this.where(" BETWEEN " + one + " AND " + two, ''); | ||
}; | ||
@@ -304,2 +307,4 @@ | ||
this.condition = this.having; | ||
return this; | ||
@@ -360,2 +365,62 @@ }; | ||
Monologue.prototype.not = function not(p, sep) { | ||
sep = sep || 'AND'; | ||
sep = ' ' + sep + ' '; | ||
if( typeof p === 'undefined' ) { | ||
this.where( ' NOT', '' ); | ||
} | ||
else if( Array.isArray(p) && Object(p[0]) === p[0] ) { | ||
this.where( p.map(function(v, k) { | ||
return this.stringify(v, '!=') | ||
}.bind(this)).join(sep) ); | ||
} | ||
else if( Array.isArray(p) ) { | ||
this.where( ' NOT' + this.format(p, ''), '' ); | ||
} | ||
else if( Object(p) === p ) { | ||
this.where( this.stringify(p, '!=').join(sep) ); | ||
} | ||
return this; | ||
}; | ||
Monologue.prototype.lt = function lt(p, sep) { | ||
return this.comparison(p, sep, '<'); | ||
}; | ||
Monologue.prototype.lte = function lte(p, sep) { | ||
return this.comparison(p, sep, '<='); | ||
}; | ||
Monologue.prototype.gt = function gt(p, sep) { | ||
return this.comparison(p, sep, '>'); | ||
}; | ||
Monologue.prototype.gte = function gte(p, sep) { | ||
return this.comparison(p, sep, '>='); | ||
}; | ||
Monologue.prototype.comparison = function comparison(p, sep, eq) { | ||
sep = sep || 'AND'; | ||
sep = ' ' + sep + ' '; | ||
if( ! Array.isArray(p) && Object(p) !== p ) { | ||
this.condition.call( this, this.format(p), eq ); | ||
} | ||
// else if( Array.isArray(p) && Object(p[0]) === p[0] ) { | ||
// this.condition.call( this, p.map(function(val, k) { | ||
// return this.stringify(val, eq).join(sep) | ||
// }.bind(this)) ); | ||
// } | ||
else { | ||
this.condition.call( this, this.stringify( p, eq ).join(sep) ); | ||
} | ||
return this; | ||
} | ||
/** | ||
@@ -387,3 +452,3 @@ * f: file path, t: field terminator, e: field enclosure, | ||
if( this.parts.join.length > 0 ) | ||
this.parts.query += this.parts.join.join(''); | ||
this.parts.query += ' ' + this.parts.join.join(' '); | ||
if( this.parts.where.length > 0 ) | ||
@@ -413,3 +478,4 @@ this.parts.query += " WHERE " + this.parts.where; | ||
Monologue.prototype.stringify = function stringify( p, s ) { | ||
Monologue.prototype.stringify = function stringify( p, s, j ) { | ||
j = j || ', '; | ||
s = ( typeof s === "undefined" ? "=" : s ); | ||
@@ -436,6 +502,6 @@ var c = []; | ||
c.push( cols.join(', ') ); | ||
c.push( cols.join(j) ); | ||
} | ||
c.push( "(" + this.stringify( p[ii], "" ).join(',') + ")" ); | ||
c.push( "(" + this.stringify( p[ii], "" ).join(j) + ")" ); | ||
} | ||
@@ -517,2 +583,6 @@ | ||
k = this.backquote(k); | ||
if( v === 'NULL' && s ) { | ||
s = ( s === '=' ? 'IS' : 'IS NOT' ); | ||
} | ||
} | ||
@@ -532,13 +602,30 @@ | ||
Monologue.prototype.escape = function escape( v ) { | ||
if( v === undefined || v === null ) { | ||
Monologue.prototype.escape = function escape( val ) { | ||
if( Array.isArray(val) ) { | ||
return val.map(function(v) { | ||
return this.escape(v); | ||
// maintaining execution scope to avoid setting a var | ||
// (can't wait to upgrade node 4+) | ||
}.bind(this)); | ||
} | ||
else if( Object(val) === val ) { | ||
var obj = {}; | ||
for( var i in val ) { | ||
obj[this.escape(i)] = val[i]; | ||
} | ||
return obj; | ||
} | ||
if( val === undefined || val === null ) { | ||
return 'NULL'; | ||
} | ||
switch( typeof v ) { | ||
case 'boolean': return (v ? 'true' : 'false'); | ||
case 'number': return v + ''; | ||
switch( typeof val ) { | ||
case 'boolean': return (val ? 'true' : 'false'); | ||
case 'number': return val + ''; | ||
} | ||
v = v.replace( /[\0\n\r\b\t\\\'\"\x1a]/g, function( s ) { | ||
val = val.replace( /[\0\n\r\b\t\\\'\"\x1a]/g, function( s ) { | ||
switch( s ) { | ||
@@ -555,3 +642,3 @@ case "\0": return "\\0"; | ||
return "'" + v + "'"; | ||
return "'" + val + "'"; | ||
}; | ||
@@ -558,0 +645,0 @@ |
{ | ||
"name": "monologue", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Streamlined MySQL query building", | ||
@@ -5,0 +5,0 @@ "main": "./monologue.js", |
@@ -14,2 +14,67 @@ Monologue - Streamlined query building | ||
**New features** | ||
New methods were added for doing different types of comparison. File a github issue if you have some feedback, maybe they're stupid/useless, you be the judge: | ||
// new methods: .gt(), .lt(), .gte(), .lte(), and .not() | ||
// SELECT `username`, `password` FROM `users` WHERE id NOT IN (1,2,3,4) | ||
mono() | ||
.select(['username', 'password'], 'users') | ||
.where('id') | ||
.not([1,2,3,4]) | ||
.query().sql; | ||
// SELECT * FROM `campsites` WHERE `reserved` != true AND `fishing` != \'slow\' | ||
mono() | ||
.select('*', 'campsites') | ||
.not({'reserved': true, fishing: 'slow'}) | ||
.query().sql; | ||
// SELECT * FROM `media` WHERE `type` IS NOT NULL AND `file_size` > 0 AND `seconds` > 24325 | ||
mono() | ||
.select('*', 'media') | ||
.not({'type': null}) | ||
.gt({file_size: 0, seconds: 24325}) | ||
.query().sql; | ||
// SELECT `username`, `password` FROM `users` WHERE `username` != \'joe\' AND `username` != \'bob\' | ||
mono() | ||
.select(['username', 'password'], 'users') | ||
.not([{username: 'joe'},{username: 'bob'}]) | ||
.query().sql; | ||
// SELECT * FROM `users` WHERE last_login NOT BETWEEN \'2015-10-01 00:00:00\' AND \'2015-11-30 23:59:59\' | ||
mono() | ||
.select('*', 'users') | ||
.where('last_login') | ||
.not() | ||
.between('2015-10-01 00:00:00', '2015-11-30 23:59:59') | ||
.query().sql; | ||
// SELECT `title`, `post` FROM `posts` WHERE status < 8 | ||
mono() | ||
.select(['title', 'post'], 'posts') | ||
.where('status').lt(8) | ||
.query().sql; | ||
// SELECT * FROM `posts` WHERE `favorited` <= 815 | ||
mono() | ||
.select('*', 'posts') | ||
.where('favorited').lte(815) | ||
.query().sql; | ||
// SELECT `post_id`, `comments` FROM `comments` WHERE `post_id` = 23565 AND date_time > \'2015-12-01 00:00:00\' | ||
mono() | ||
.select(['post_id', 'comments'], 'comments') | ||
.where({post_id: 23565}) | ||
.where('date_time').gt('2015-12-01 00:00:00') | ||
// SELECT sum(id) as count FROM comments HAVING count >= 42 | ||
mono({backquote: false}) | ||
.select(['sum(id) as count'], 'comments') | ||
.having('count').gte(42) | ||
.query().sql; | ||
#API | ||
@@ -16,0 +81,0 @@ |
Sorry, the diff of this file is not supported yet
56207
489
224