Comparing version 0.9.0 to 0.9.2
158
monologue.js
@@ -70,4 +70,7 @@ (function(exports) { | ||
stmt: '', | ||
select: [], | ||
from: '', | ||
table: '', | ||
sets: {}, | ||
values: '', | ||
sql: '', | ||
@@ -95,3 +98,3 @@ join: [], | ||
if( col !== '*' ) col = this.backquote(col); | ||
if( typeof col !== 'string' ) col = this.backquote(col); | ||
} | ||
@@ -104,3 +107,5 @@ | ||
this.parts.table = tbl; | ||
this.parts.stmt = `SELECT ${col} FROM ${tbl}`; | ||
this.parts.from = `FROM ${tbl}`; | ||
this.parts.select.push(col); | ||
this.parts.stmt = 'SELECT'; | ||
@@ -110,3 +115,43 @@ return this; | ||
Monologue.prototype.count = function count( count, alias ) { | ||
if( typeof alias === 'undefined' ) { | ||
alias = 'count'; | ||
} | ||
this.parts.select.push(`COUNT(${count}) as ${alias}`); | ||
return this; | ||
}; | ||
Monologue.prototype.sum = function sum( sum, alias ) { | ||
if( typeof alias === 'undefined' ) { | ||
alias = 'sum'; | ||
} | ||
this.parts.select.push(`SUM(${sum}) as ${alias}`); | ||
return this; | ||
}; | ||
Monologue.prototype.min = function min( min, alias ) { | ||
if( typeof alias === 'undefined' ) { | ||
alias = 'min'; | ||
} | ||
this.parts.select.push(`MIN(${min}) as ${alias}`); | ||
return this; | ||
}; | ||
Monologue.prototype.max = function max( max, alias ) { | ||
if( typeof alias === 'undefined' ) { | ||
alias = 'max'; | ||
} | ||
this.parts.select.push(`MAX(${max}) as ${alias}`); | ||
return this; | ||
}; | ||
/** | ||
@@ -176,7 +221,7 @@ * direction, table, statement | ||
p = this.backquote( p ).join(','); | ||
var d = this.stringify( d, "", ""); | ||
let vals = this.stringify( d, "", ""); | ||
// stringify should be refactored a bit so this isn't necessary | ||
d.shift(); | ||
vals.shift(); | ||
col = `(${p}) VALUES (${d.join('),(')})`; | ||
col = `(${p}) VALUES (${vals.join('),(')})`; | ||
} | ||
@@ -191,6 +236,6 @@ | ||
let a = this.stringify( p, "", ""); | ||
let vals = this.stringify( p, "", ""); | ||
// col = `(${a.shift()}) VALUES ${a.join(',')}`; | ||
col = `(${a.shift()}) VALUES (${a.join('),(')})`; | ||
col = `(${vals.shift()}) VALUES (${vals.join('),(')})`; | ||
} | ||
@@ -202,3 +247,4 @@ | ||
this.parts.columns = col; | ||
this.parts.values = col; | ||
this.parts.table = tbl; | ||
this.parts.stmt = `INSERT INTO ${tbl} ${col}`; | ||
@@ -209,3 +255,59 @@ | ||
Monologue.prototype.values = function update( p, d ) { | ||
let col = ''; | ||
// if( p instanceof Array && d instanceof Array ) { | ||
// p = this.backquote( p ).join(','); | ||
// var vals = this.stringify( d, "", ""); | ||
// // stringify should be refactored a bit so this isn't necessary | ||
// vals.shift(); | ||
// | ||
// col = `(${vals.join('),(')})`; | ||
// } | ||
// | ||
// // Array is also an object | ||
// else if( typeof p === "object" ) { | ||
// // if it's not a nested array, cheat and make it one | ||
// if( ! ( p instanceof Array ) ) { | ||
// p = [p]; | ||
// } | ||
// | ||
// let vals = this.stringify( p, "", ""); | ||
// | ||
// // col = `(${a.shift()}) VALUES ${a.join(',')}`; | ||
// col = `(${vals.join('),(')})`; | ||
// } | ||
// else if( typeof p === "string" ) { | ||
// col = p; | ||
// } | ||
if( p instanceof Array ) { | ||
// if it's not a nested array, cheat and make it one | ||
// if( ! ( p instanceof Array ) ) { | ||
// p = [p]; | ||
// } | ||
// | ||
let vals = this.stringify( p, "", ""); | ||
// col = `(${a.shift()}) VALUES ${a.join(',')}`; | ||
if( p[0] instanceof Array ) { | ||
vals.shift(); | ||
col = `(${vals.join('),(')})`; | ||
} | ||
else { | ||
col = `(${vals.join(',')})`; | ||
} | ||
} | ||
else if( typeof p === "string" ) { | ||
col = p; | ||
} | ||
this.parts.values = [this.parts.values, col].filter(Boolean).join(','); | ||
this.parts.stmt = `INSERT INTO ${this.parts.table} ${this.parts.values}`; | ||
// console.log(this.parts); | ||
return this; | ||
}; | ||
/** | ||
@@ -528,24 +630,50 @@ * t: table, p: params | ||
this.parts.sql = this.parts.stmt; | ||
let parts = [this.parts.stmt]; | ||
if( this.parts.join.length > 0 ) | ||
if( this.parts.select.length > 0 ) { | ||
this.parts.sql += ' ' + this.parts.select.join(', '); | ||
parts = parts.concat(this.parts.select.join(', ')) | ||
} | ||
if( this.parts.from.length > 0 ) { | ||
this.parts.sql += ' ' + this.parts.from; | ||
parts = parts.concat(this.parts.from); | ||
} | ||
if( this.parts.join.length > 0 ) { | ||
this.parts.sql += ' ' + this.parts.join.join(' '); | ||
parts = parts.concat(this.parts.join.join(' ')); | ||
} | ||
if( this.parts.where.length > 0 ) | ||
if( this.parts.where.length > 0 ) { | ||
this.parts.sql += ' WHERE ' + this.parts.where; | ||
parts = parts.concat('WHERE').concat(this.parts.where); | ||
} | ||
if( this.parts.group.length > 0 ) | ||
if( this.parts.group.length > 0 ) { | ||
this.parts.sql += ' GROUP BY ' + this.parts.group.join(','); | ||
parts = parts.concat('GROUP BY').concat(this.parts.group.join(',')) | ||
} | ||
if( this.parts.having.length > 0 ) | ||
if( this.parts.having.length > 0 ) { | ||
this.parts.sql += ' HAVING ' + this.parts.having; | ||
parts = parts.concat('HAVING').concat(this.parts.having) | ||
} | ||
if( this.parts.order.length > 0 ) | ||
if( this.parts.order.length > 0 ) { | ||
this.parts.sql += ' ORDER BY ' + this.parts.order.join(','); | ||
parts = parts.concat('ORDER BY').concat(this.parts.order.join(',')) | ||
} | ||
if( this.parts.limit.length > 0 ) | ||
if( this.parts.limit.length > 0 ) { | ||
this.parts.sql += ' LIMIT ' + this.parts.limit; | ||
parts = parts.concat('LIMIT').concat(this.parts.limit); | ||
} | ||
if( this.parts.last.length > 0 ) | ||
if( this.parts.last.length > 0 ) { | ||
this.parts.sql += this.parts.last; | ||
parts = parts.concat(this.parts.last); | ||
} | ||
console.log(parts.join(' ')); | ||
return this.parts.sql; | ||
@@ -552,0 +680,0 @@ }; |
{ | ||
"name": "monologue", | ||
"version": "0.9.0", | ||
"version": "0.9.2", | ||
"description": "Streamlined MySQL query building", | ||
@@ -5,0 +5,0 @@ "main": "./monologue.js", |
128
README.md
@@ -10,2 +10,8 @@ Monologue - Streamlined query building | ||
### 0.9.2 | ||
* Added .count, .sum(), .min(), and .max() for streamlining (and testing) aggregate fields in SELECT statements | ||
### 0.9.1 | ||
* Added .values() method for adding additional VALUES to INSERT statements | ||
### 0.9.0 | ||
@@ -35,4 +41,2 @@ * Added .set() method for augmenting/overwriting SET statements when using UPDATE (INSERT coming soon) | ||
// Less than basic SELECT statement | ||
// call the SQL wrappers in any order, see below: where, group, where, order | ||
@@ -52,49 +56,2 @@ // output: SELECT * FROM users WHERE id IN (1,2,3,4,5,6) AND date_time BETWEEN '2012-09-12' AND '2013-01-20' OR name LIKE 'ro%en' GROUP BY type, hamster ASC ORDER BY id ASC LIMIT 1000, 300 | ||
// the different JOIN methods | ||
mono().ljoin( table, statement ); // LEFT JOIN | ||
mono().rjoin( table, statement ); // RIGHT JOIN | ||
mono().lojoin( table, statement ); // LEFT OUTER JOIN | ||
mono().rojoin( table, statement ); // RIGHT OUTER JOIN | ||
// JOIN (default is inner): | ||
// SELECT * FROM users u INNER JOIN posts p ON p.user_id = u.id WHERE category = '67' | ||
monologue() | ||
.select( "*", "users u" ) | ||
.join( "posts p", "p.user_id = u.id" ) | ||
.where( { "category": "67" } ) | ||
.sql(); | ||
// JOIN (LEFT, as argument): | ||
// SELECT * FROM users u LEFT JOIN posts p ON p.user_id = u.id WHERE category = '67' | ||
monologue() | ||
.select( "*", "users u" ) | ||
.join( "LEFT", "posts p", { "p.user_id": "u.id" } ) | ||
.where( { "category": "67" } ) | ||
.sql(); | ||
// SELECT into outfile: the third param (OPTIONALLY ENCLOSED BY) is, as stated, optional. Just pass in the line ending and leave the 4th param out, the rest will be taken care of | ||
// output: SELECT * FROM users WHERE company = 'general motors' INTO OUTFILE '/tmp/datafile' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' | ||
monologue() | ||
.select( "*", "users" ) | ||
.where( { "company": "general motors" } ) | ||
.file( "/tmp/datafile", ",", '"', "\\n" ) | ||
.sql(); | ||
// SELECT into outfile: without third param | ||
// output: SELECT * FROM users WHERE company = 'general motors' INTO OUTFILE '/tmp/datafile' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' | ||
monologue() | ||
.select( "*", "users") | ||
.where( { "company": "general motors" } ) | ||
.file( "/tmp/datafile", ",", "\\n" ) | ||
.sql(); | ||
// INSERT, passing an array of objects | ||
@@ -160,15 +117,2 @@ // output: INSERT INTO users (first_name, password, username) VALUES ('me','1234','test'),('pasta','abcd','example') | ||
// UNION | ||
// Wrappers can be out of order BEFORE the UNION statement, | ||
// wrappers after will be applied to the secondary statment | ||
// 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"}) | ||
.sql(); | ||
// SELECT `username`, `password` FROM `users` WHERE id NOT IN (1,2,3,4) | ||
@@ -232,1 +176,61 @@ mono() | ||
.sql(); | ||
// the different JOIN methods | ||
mono().ljoin( table, statement ); // LEFT JOIN | ||
mono().rjoin( table, statement ); // RIGHT JOIN | ||
mono().lojoin( table, statement ); // LEFT OUTER JOIN | ||
mono().rojoin( table, statement ); // RIGHT OUTER JOIN | ||
// JOIN (default is inner): | ||
// SELECT * FROM users u INNER JOIN posts p ON p.user_id = u.id WHERE category = '67' | ||
monologue() | ||
.select( "*", "users u" ) | ||
.join( "posts p", "p.user_id = u.id" ) | ||
.where( { "category": "67" } ) | ||
.sql(); | ||
// JOIN (LEFT, as argument): | ||
// SELECT * FROM users u LEFT JOIN posts p ON p.user_id = u.id WHERE category = '67' | ||
monologue() | ||
.select( "*", "users u" ) | ||
.join( "LEFT", "posts p", { "p.user_id": "u.id" } ) | ||
.where( { "category": "67" } ) | ||
.sql(); | ||
// UNION | ||
// Wrappers can be out of order BEFORE the UNION statement, | ||
// wrappers after will be applied to the secondary statment | ||
// 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"}) | ||
.sql(); | ||
// SELECT into outfile: the third param (OPTIONALLY ENCLOSED BY) is, as stated, optional. Just pass in the line ending and leave the 4th param out, the rest will be taken care of | ||
// output: SELECT * FROM users WHERE company = 'general motors' INTO OUTFILE '/tmp/datafile' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' | ||
monologue() | ||
.select( "*", "users" ) | ||
.where( { "company": "general motors" } ) | ||
.file( "/tmp/datafile", ",", '"', "\\n" ) | ||
.sql(); | ||
// SELECT into outfile: without third param | ||
// output: SELECT * FROM users WHERE company = 'general motors' INTO OUTFILE '/tmp/datafile' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' | ||
monologue() | ||
.select( "*", "users") | ||
.where( { "company": "general motors" } ) | ||
.file( "/tmp/datafile", ",", "\\n" ) | ||
.sql(); |
42383
4
1227
232