Comparing version 0.3.2 to 0.3.3
164
monologue.js
@@ -5,3 +5,3 @@ (function(exports) { | ||
function monologue(opt) { | ||
var dict = { escape: true, quote: false }; | ||
var dict = { escape: true }; | ||
@@ -17,5 +17,2 @@ if( typeof opt === "undefined" ) { | ||
// hard-coded prevention for now | ||
opt.quote = false; | ||
// semi-global object to contain query parts until they are compiled | ||
@@ -62,7 +59,7 @@ var global = { | ||
select: function( c, t ) { | ||
if( Array.isArray( c ) ) | ||
c = c.join( ", " ); | ||
select: function( col, tbl ) { | ||
if( Array.isArray( col ) ) | ||
col = col.join( ", " ); | ||
global.query = "SELECT " + c + " FROM " + t; | ||
global.query = "SELECT " + col + " FROM " + tbl; | ||
@@ -74,23 +71,23 @@ return this; | ||
/** | ||
* direction, table, fields | ||
* direction, table, statement | ||
*/ | ||
join: function( dir, t, f ) { | ||
join: function( dir, tbl, stmt ) { | ||
// default to inner join if unspecified (parity with mysql) | ||
if( typeof f === "undefined" ) { | ||
f = t; | ||
t = dir; | ||
if( typeof stmt === "undefined" ) { | ||
stmt = tbl; | ||
tbl = dir; | ||
dir = "INNER"; | ||
} | ||
if( typeof f === "object" ) { | ||
if( typeof stmt === "object" ) { | ||
var fields = []; | ||
for( var ii in f ) { | ||
fields.push( ii + " = " + f[ii] ); | ||
for( var ii in stmt ) { | ||
fields.push( ii + " = " + stmt[ii] ); | ||
} | ||
f = fields.join(" AND "); | ||
stmt = fields.join(" AND "); | ||
} | ||
global.join.push( " " + dir + " JOIN " + t + " ON " + f ); | ||
global.join.push( " " + dir + " JOIN " + tbl + " ON " + stmt ); | ||
@@ -105,7 +102,7 @@ return this; | ||
insert: function( t, p ) { | ||
insert: function( tbl, p ) { | ||
var col = ''; | ||
// I don't know why this would ever NOT be the case | ||
if( typeof p === "object" ) { | ||
var c = []; | ||
// if it's not a nested array, cheat and make it one | ||
@@ -116,11 +113,11 @@ if( ! Array.isArray( p ) ) { | ||
c = this.stringify( p, ""); | ||
c = "(" + c.shift() + ") VALUES " + c.join(','); | ||
var a = this.stringify( p, ""); | ||
col = "(" + a.shift() + ") VALUES " + a.join(','); | ||
} | ||
else if( typeof p === "string" ) { | ||
var c = p; | ||
col = p; | ||
} | ||
global.query = "INSERT INTO " + t + " " + c; | ||
global.query = "INSERT INTO " + tbl + " " + col; | ||
@@ -135,10 +132,11 @@ return this; | ||
update: function( t, p ) { | ||
update: function( tbl, p ) { | ||
var col = ''; | ||
if( typeof p === "object" ) { | ||
var c = this.stringify( p ); | ||
c = "SET " + c.join( ', ' ); | ||
col = "SET " + this.stringify( p ).join( ', ' ); | ||
} | ||
else if( typeof p === "string" ) { | ||
var c = p; | ||
col = p; | ||
} | ||
@@ -150,3 +148,3 @@ | ||
global.query = "UPDATE " + t + " " + c; | ||
global.query = "UPDATE " + tbl + " " + col; | ||
@@ -160,5 +158,5 @@ return this; | ||
delete: function( t, w ) { | ||
global.query = "DELETE FROM " + t; | ||
return ( w ? this.where( w ) : this ); | ||
delete: function( tbl, wh ) { | ||
global.query = "DELETE FROM " + tbl; | ||
return ( wh ? this.where( wh ) : this ); | ||
}, | ||
@@ -168,14 +166,11 @@ | ||
/** | ||
* w: where statement, s: separator | ||
*/ | ||
where: function( w, s ) { | ||
s = ( typeof s === "undefined" ? "AND" : s ); | ||
s = ( s.length > 0 ? " " + s + " " : s ); | ||
where: function( wh, sep ) { | ||
sep = ( typeof sep === "undefined" ? "AND" : sep ); | ||
sep = ( sep.length > 0 ? " " + sep + " " : sep ); | ||
if( toString.call( w ) === "[object Object]" ) { | ||
var crit = this.stringify( w ); | ||
if( toString.call( wh ) === "[object Object]" ) { | ||
// stringify the where statements | ||
w = crit.join( s ); | ||
wh = this.stringify( wh ).join( sep ); | ||
} | ||
@@ -186,4 +181,4 @@ | ||
global.where = ( global.where.length > 0 | ||
? global.where + s + w | ||
: w ); | ||
? global.where + sep + wh | ||
: wh ); | ||
@@ -198,6 +193,6 @@ return this; | ||
in: function( ins ) { | ||
var i = this.stringify( ins, '' ); | ||
ins = this.stringify( ins, '' ); | ||
// returns "this" | ||
return this.where( " IN (" + i + ")", "" ); | ||
return this.where( " IN (" + ins + ")", "" ); | ||
}, | ||
@@ -260,12 +255,11 @@ | ||
/** | ||
* g: group, d: direction | ||
*/ | ||
group: function( g, d ) { | ||
d = d || 'ASC'; | ||
group: function( grp, dir ) { | ||
dir = dir || 'ASC'; | ||
if( Array.isArray( g ) ) | ||
g = g.join( ', ' ); | ||
if( Array.isArray( grp ) ) | ||
grp = grp.join( ', ' ); | ||
global.group.push( g + " " + d ); | ||
global.group.push( grp + " " + dir ); | ||
@@ -277,13 +271,11 @@ return this; | ||
/** | ||
* h: having | ||
*/ | ||
having: function( h, sep ) { | ||
having: function( hav, sep ) { | ||
sep = ( typeof sep === "undefined" ? "AND" : sep ); | ||
sep = ( sep.length > 0 ? " " + sep + " " : sep ); | ||
if( typeof h !== "string" ) { | ||
var criteria = this.stringify(h); | ||
if( typeof hav !== "string" ) { | ||
// stringify the having statements | ||
h = criteria.join( " " + sep + " " ); | ||
hav = this.stringify(hav).join( sep ); | ||
} | ||
@@ -294,4 +286,4 @@ | ||
global.having = ( global.having.length > 0 | ||
? global.having + " " + sep + " " + h | ||
: h ); | ||
? global.having + sep + hav | ||
: hav ); | ||
@@ -303,12 +295,11 @@ return this; | ||
/** | ||
* o: order, d: direction | ||
*/ | ||
order: function( o, d ) { | ||
d = d || 'ASC'; | ||
order: function( ord, dir ) { | ||
dir = dir || 'ASC'; | ||
if( Array.isArray( o ) ) | ||
o = o.join( ', ' ); | ||
if( Array.isArray( ord ) ) | ||
ord = ord.join( ', ' ); | ||
global.order.push( o + " " + d ); | ||
global.order.push( ord + " " + dir ); | ||
@@ -320,9 +311,8 @@ return this; | ||
/** | ||
* l: limit, o: offset | ||
*/ | ||
limit: function( l, o ) { | ||
global.limit = ( typeof o === "undefined" | ||
? '' + l | ||
: o + ", " + l ); | ||
limit: function( lim, off ) { | ||
global.limit = ( typeof off === "undefined" | ||
? '' + lim | ||
: off + ", " + lim ); | ||
return this; | ||
@@ -394,3 +384,3 @@ }, | ||
* Takes an object or and array of objects and builds a SQL string | ||
* p: params, s: separator, pre: bound param prefix | ||
* p: params, s: separator | ||
*/ | ||
@@ -411,7 +401,3 @@ | ||
var ret = ( opt.quote | ||
? "`" + global.columns.join('`, `') + "`" | ||
: global.columns.join(', ') ) | ||
c.push( ret ); | ||
c.push( global.columns.join(', ') ); | ||
} | ||
@@ -435,5 +421,3 @@ | ||
if( Array.isArray( p[col[jj]] ) ) { | ||
var n = ( opt.quote | ||
? '`' + col[jj] + '`' | ||
: col[jj] ) | ||
var n = col[jj] | ||
+ " IN (" + this.stringify( p[col[jj]] ) + ")"; | ||
@@ -473,9 +457,23 @@ | ||
if( opt.quote ) { | ||
k = '`' + k + '`'; | ||
// spit out the bound param name | ||
return ( s.length > 0 ? k + " " + s + " " : '' ) + r; | ||
}, | ||
backquote: function( col ) { | ||
if( Array.isArray(col) ) { | ||
return col.map(function(v) { | ||
return '`' + v + '`'; | ||
}); | ||
} | ||
// spit out the bound param name | ||
return ( s.length > 0 ? "" + k + " " + s + " " : '' ) + r; | ||
else if( col === Object(col) ){ | ||
return Object.keys(col).map(function(v) { | ||
return '`' + v + '`'; | ||
}); | ||
} | ||
else { | ||
return '`' + col + '`'; | ||
} | ||
}, | ||
@@ -482,0 +480,0 @@ |
{ | ||
"name": "monologue", | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"description": "Streamlined MySQL query building", | ||
@@ -5,0 +5,0 @@ "main": "./monologue.js", |
@@ -16,2 +16,21 @@ Monologue - Streamlined query building | ||
**API** | ||
A new method has been introduced to deal with situations where backquoting is required. An experimental approach was tested in previous versions where backquoting could be done inline, but this proved to be buggy and impossible to perform accurately. However, you can now do it manually with monologue.backquote(). The method accepts 3 types of data: an array of column names, an object, or a string. An array will result in each element being backquoted. An object will return an array of backquoted keys. A string will be returned backquoted. | ||
Example: | ||
// result: [ '`email`', '`password`', '`type`' ] | ||
monologue().backquote(['email', 'password', 'type']); | ||
// result: [ '`pizza`', '`drink`', '`dessert`' ] | ||
monologue().backquote({ | ||
pizza: "hawaiin bbq chicken", | ||
drink: "chocolate milk", | ||
dessert: "german chocolate cake" | ||
}); | ||
// result: '`cupcake`' | ||
monologue().backquote('cupcake'); | ||
**Usage** | ||
@@ -25,3 +44,4 @@ | ||
// call the SQL wrappers in any order, see below: where, group, where, order | ||
var mono = monologue().select( "*", "users") | ||
var mono = monologue() | ||
.select( "*", "users") | ||
.where( { "id": [1,2,3,4,5,6] } ) // alternative to where("id").in([...]) | ||
@@ -42,3 +62,4 @@ .where( 'date_time' ).between( '2012-09-12', '2013-01-20') | ||
monologue().select( "*", "users u" ) | ||
monologue() | ||
.select( "*", "users u" ) | ||
.join( "posts p", "p.user_id = u.id" ) | ||
@@ -52,3 +73,4 @@ .where( { "category": "67" } ) | ||
monologue().select( "*", "users u" ) | ||
monologue() | ||
.select( "*", "users u" ) | ||
.join( "LEFT", "posts p", { "p.user_id": "u.id" } ) | ||
@@ -63,3 +85,4 @@ .where( { "category": "67" } ) | ||
monologue().select( "*", "users" ) | ||
monologue() | ||
.select( "*", "users" ) | ||
.where( { "company": "general motors" } ) | ||
@@ -73,3 +96,4 @@ .file( "/tmp/datafile", ",", '"', "\\n" ) | ||
monologue().select( "*", "users") | ||
monologue() | ||
.select( "*", "users") | ||
.where( { "company": "general motors" } ) | ||
@@ -83,6 +107,8 @@ .file( "/tmp/datafile", ",", "\\n" ) | ||
monologue().insert( 'users', [ | ||
{ username: 'test', password: '1234', first_name: 'me' }, | ||
{ username: 'example', password: 'abcd', first_name: "pasta" } | ||
] ).query().sql; | ||
monologue() | ||
.insert( 'users', [ | ||
{ username: 'test', password: '1234', first_name: 'me' }, | ||
{ username: 'example', password: 'abcd', first_name: "pasta" } | ||
] ) | ||
.query().sql; | ||
@@ -93,3 +119,7 @@ | ||
monologue().insert( 'users', { username: 'me', password: 'abcd', first_name: "cubert" } ).query().sql; | ||
monologue() | ||
.insert( 'users', { | ||
username: 'me', password: 'abcd', first_name: "cubert" | ||
} ) | ||
.query().sql; | ||
@@ -100,3 +130,6 @@ | ||
monologue().update( "users", {username: "yoyo", email: 'some@email.com', password: "abcdefg"} ).where( {id: 23} ).query().sql; | ||
monologue() | ||
.update( "users", {username: "yoyo", email: 'some@email.com', password: "abcdefg"} ) | ||
.where( {id: 23} ) | ||
.query().sql; | ||
@@ -107,4 +140,7 @@ | ||
monologue().delete( 'users', { username: 'test', password: '1234', first_name: "me" } ).query().sql; | ||
monologue() | ||
.delete( 'users', { username: 'test', password: '1234', first_name: "me" } ) | ||
.query().sql; | ||
// UNION | ||
@@ -114,2 +150,9 @@ // Wrappers can be out of order BEFORE the UNION statement, | ||
// output: SELECT username, email FROM users WHERE company_id = '1234' UNION SELECT screename, email_address FROM app_users WHERE company = 'coName' | ||
monologue().select('username, email', 'users').where({"company_id": "1234"}).union('screename, email_address', 'app_users').where({"company":"coName"}).query().sql | ||
monologue() | ||
.select('username, email', 'users') | ||
.where({"company_id": "1234"}) | ||
.union('screename, email_address', 'app_users') | ||
.where({"company":"coName"}) | ||
.query().sql | ||
Sorry, the diff of this file is not supported yet
42446
148
368