Comparing version 0.0.5 to 0.0.6
165
monologue.js
(function() { | ||
var rx = /[^a-zA-Z0-9_]/g, | ||
root = this, | ||
yoe = { | ||
qparts = { | ||
query: '', | ||
@@ -24,5 +24,4 @@ where: '', | ||
/** | ||
* Creates a select statement using properties defined before calling method | ||
* @return string the generated query string | ||
*/ | ||
@@ -33,3 +32,3 @@ | ||
yoe.query = "SELECT " + c + " FROM " + t; | ||
qparts.query = "SELECT " + c + " FROM " + t; | ||
@@ -39,2 +38,6 @@ return this; | ||
/** | ||
*/ | ||
Monologue.prototype.where = function( w, separator ) { | ||
@@ -57,4 +60,4 @@ separator = ( typeof separator === "undefined" ? "AND" : separator ); | ||
// check if a previous where statement has been set and glue it all together | ||
yoe.where = ( yoe.where.length > 0 | ||
? yoe.where + " " + separator + " " + w | ||
qparts.where = ( qparts.where.length > 0 | ||
? qparts.where + " " + separator + " " + w | ||
: w ); | ||
@@ -65,2 +68,6 @@ | ||
/** | ||
*/ | ||
Monologue.prototype.having = function( h, separator ) { | ||
@@ -83,4 +90,4 @@ separator = ( typeof separator === "undefined" ? "AND" : separator ); | ||
// check if a previous where statement has been set and glue it all together | ||
yoe.having = ( yoe.having.length > 0 | ||
? yoe.having + " " + separator + " " + h | ||
qparts.having = ( qparts.having.length > 0 | ||
? qparts.having + " " + separator + " " + h | ||
: h ); | ||
@@ -91,2 +98,6 @@ | ||
/** | ||
*/ | ||
Monologue.prototype.in = function( ins ) | ||
@@ -110,2 +121,6 @@ { | ||
/** | ||
*/ | ||
Monologue.prototype.like = function( like, separator ) { | ||
@@ -132,2 +147,6 @@ separator = separator || "AND"; | ||
/** | ||
*/ | ||
Monologue.prototype.between = function( one, two ) | ||
@@ -155,3 +174,7 @@ { | ||
// group, direction | ||
/** | ||
* group, direction | ||
*/ | ||
Monologue.prototype.group = function( g, d ) { | ||
@@ -163,3 +186,3 @@ d = d || 'ASC'; | ||
yoe.group.push( g + " " + d ); | ||
qparts.group.push( g + " " + d ); | ||
@@ -169,3 +192,7 @@ return this; | ||
// order, direction | ||
/** | ||
* order, direction | ||
*/ | ||
Monologue.prototype.order = function( o, d ) { | ||
@@ -177,3 +204,3 @@ d = d || 'ASC'; | ||
yoe.order.push( o + " " + d ); | ||
qparts.order.push( o + " " + d ); | ||
@@ -183,6 +210,10 @@ return this; | ||
// limit, offset | ||
/** | ||
* limit, offset | ||
*/ | ||
Monologue.prototype.limit = function( l, o ) | ||
{ | ||
yoe.limit = ( typeof o === "undefined" ? l : o + ", " + l ); | ||
qparts.limit = ( typeof o === "undefined" ? l : o + ", " + l ); | ||
return this; | ||
@@ -198,14 +229,14 @@ } | ||
{ | ||
if( yoe.where.length > 0 ) | ||
yoe.query = yoe.query + " WHERE " + yoe.where; | ||
if( yoe.group.length > 0 ) | ||
yoe.query += ' GROUP BY ' + yoe.group.join(','); | ||
if( yoe.having.length > 0 ) | ||
yoe.query = yoe.query + " HAVING " + yoe.having; | ||
if( yoe.order.length > 0 ) | ||
yoe.query += ' ORDER BY ' + yoe.order.join(','); | ||
if( yoe.limit.length > 0 ) | ||
yoe.query += " LIMIT " + yoe.limit; | ||
if( qparts.where.length > 0 ) | ||
qparts.query += " WHERE " + qparts.where; | ||
if( qparts.group.length > 0 ) | ||
qparts.query += " GROUP BY " + qparts.group.join(','); | ||
if( qparts.having.length > 0 ) | ||
qparts.query += " HAVING " + qparts.having; | ||
if( qparts.order.length > 0 ) | ||
qparts.query += " ORDER BY " + qparts.order.join(','); | ||
if( qparts.limit.length > 0 ) | ||
qparts.query += " LIMIT " + qparts.limit; | ||
return yoe.query; | ||
return qparts.query; | ||
} | ||
@@ -215,67 +246,4 @@ | ||
/** | ||
* Return a generated insert statement from one or more sets of values | ||
* passed as an associative array. Inserting a batch of rows rather than | ||
* multiple consecutive inserts will be WAY faster. | ||
* @param string $table | ||
* @param array $values | ||
* @return string | ||
*/ | ||
// Monologue.prototype.insert = function( $table, array $params ) | ||
// { | ||
// $columns = array(); | ||
// $multi = ( count( $params ) != count( $params, COUNT_RECURSIVE ) ); | ||
// if( $multi ) | ||
// { | ||
// $arr = array(); | ||
// foreach( $params as $k => $void ) | ||
// { | ||
// // sort the keys so that each group of values are inserted in | ||
// // the same order | ||
// ksort( $void ); | ||
// if( ! $columns ) | ||
// { | ||
// $columns = array_keys( $params[0] ); | ||
// sort( $columns ); | ||
// } | ||
// foreach( $void as $pkey => $pvalue ) | ||
// { | ||
// $this->params[$pkey . $k] = $pvalue;//sprintf( ':%s', $pvalue ); | ||
// $arr[] = ":{$pkey}{$k}"; | ||
// } | ||
// $params[$k] = implode( ', ', $arr ); | ||
// $params[$k] = sprintf( '(%s)', $params[$k] ); | ||
// $arr = array(); | ||
// } | ||
// $params = implode( ', ', $params ); | ||
// } | ||
// else | ||
// { | ||
// ksort( $params ); | ||
// $this->params = $params; | ||
// $columns = array_keys( $this->params ); | ||
// $params = asprintf( ':%1$s', $columns ); | ||
// $params = implode( ', ', $params ); | ||
// $params = sprintf( '(%s)', $params ); | ||
// } | ||
// $columns = sprintf( '(%s)', '`' . implode( '`, `', $columns ) . '`' ); | ||
// $this->query = "INSERT INTO $table $columns VALUES $params"; | ||
// return $this; | ||
// } | ||
/** | ||
* Generates an update statement | ||
* @param string $table | ||
* @param array $params | ||
* @param array $where | ||
* @return string | ||
*/ | ||
Monologue.prototype.insert = function( table, params ) { | ||
@@ -318,3 +286,3 @@ if( typeof params !== "string" ) { | ||
columns = " (" + columns.join(', ') + ") VALUES " + values.join(','); | ||
columns = " (" + columns.join(', ') + ") VALUES " + values.join(','); | ||
@@ -327,3 +295,3 @@ } | ||
this.query = "INSERT INTO " + table + columns; | ||
qparts.query = "INSERT INTO " + table + columns; | ||
@@ -335,7 +303,2 @@ return this; | ||
/** | ||
* Generates an update statement | ||
* @param string $table | ||
* @param array $params | ||
* @param array $where | ||
* @return string | ||
*/ | ||
@@ -360,3 +323,3 @@ | ||
this.query = "UPDATE " + table + columns; | ||
qparts.query = "UPDATE " + table + columns; | ||
@@ -368,13 +331,13 @@ return this; | ||
/** | ||
* Creates a select statement using properties defined before calling method | ||
* @return string the generated query string | ||
*/ | ||
Monologue.prototype.delete = function( t ) | ||
Monologue.prototype.delete = function( table, where ) | ||
{ | ||
yoe.table = t; | ||
yoe.query = "DELETE FROM " + yoe.table; | ||
qparts.query = "DELETE FROM " + table; | ||
if( where ) | ||
this.where(where); | ||
return this; | ||
}; | ||
})(); |
{ | ||
"name": "monologue", | ||
"version": "0.0.5", | ||
"description": "Steamlined MySQL query building", | ||
"version": "0.0.6", | ||
"description": "Streamlined MySQL query building", | ||
"main": "./monologue.js", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -25,3 +25,3 @@ Monologue - Streamlined query building | ||
// Now pass mono.query (or the var you assign, see above) and mono.params into your MySQL querying function! | ||
// Now pass query (or the var you assign, see above) and mono.params into your MySQL querying function! | ||
@@ -28,0 +28,0 @@ console.log( query ); // output: SELECT * FROM users WHERE id IN (:__in_1,:__in_2,:__in_3,:__in_4,:__in_5,:__in_6) AND date_time BETWEEN :__between_8_20120912 AND :__between_8_20130121 OR name LIKE :__like_ro GROUP BY type ASC ORDER BY id ASC LIMIT 1000, 300 |
8110
233