Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- support OR join case ( foo = 1 OR bar = 2 OR buz != 3 )

  • Loading branch information...
commit 5944724669a7e3382d516ffea42f5c8d593db6ab 1 parent f0eaa14
shot authored
View
2  Changes
@@ -1,5 +1,7 @@
Revision history for node.js extension sql-generator
+0.0.6 Mon Apr 16 18:14:10 JST 2012
+ - support OR join Case ( foo = 1 OR bar = 2 OR buz != 3 )
0.0.5 Tue Feb 14 23:55:53 JST 2012
- add test
- support NULL Case ( IS NULL || IS NOT NULL )
View
22 README.md
@@ -50,14 +50,15 @@ SQL Generator for node.js.
var stmt2 = sqlgen.select( 'test_table', // target table
'*', // target columns
- { foo: 1, // foo = 1
- bar: { '>=': 10 }, // bar >= 10
- buz: { '>': 100, '<': 200 }, // buz > 100 AND buz < 200
- hoge: { like: '%john%' }, // hoge LIKE '%john%'
- fuga: { IN: [ 1, 2, 3 ] }, // fuga IN ( 1, 2, 3 )
- moge: [ 6, 7, 8, { '!=': 9 } ], // ( moge = 6 OR moge = 7 OR moge = 8 OR moge != 9 )
- puga: { '-and': [ 1, 2, 3 ] }, // ( puga = 1 AND puga = 2 AND puga = 3 )
- base_table_id: { IN: { sql: stmt } } // where section
+ { foo: 1, // foo = 1
+ bar: { '>=': 10 }, // bar >= 10
+ buz: { '>': 100, '<': 200 }, // buz > 100 AND buz < 200
+ hoge: { like: '%john%' }, // hoge LIKE '%john%'
+ fuga: { IN: [ 1, 2, 3 ] }, // fuga IN ( 1, 2, 3 )
+ moge: [ 6, 7, 8, { '!=': 9 } ], // ( moge = 6 OR moge = 7 OR moge = 8 OR moge != 9 )
+ puga: { '-and': [ 1, 2, 3 ] }, // ( puga = 1 AND puga = 2 AND puga = 3 ),
+ '-or': { red: 1, blue: 2, green: { '!=': 3 } }, // ( red = 1 OR blue = 2 OR green != 3 )
+ base_table_id: { IN: { sql: stmt } } // where section
},
{ order: 'id' } // order section
);
@@ -65,8 +66,9 @@ SQL Generator for node.js.
// stmt2 = { sql: 'SELECT * FROM test_table \
// WHERE foo = $1 AND bar >= $2 AND buz > $3 AND buz < $4 AND hoge LIKE $5 \
// AND fuga IN ( $6, $7, $8 ) AND ( moge = $9 OR moge = $10 OR moge = $11 OR moge != $12 ) \
- // AND base_table_id IN ( SELECT id FROM base_table WHERE id >= $13 ) ORDER BY id',
- // values: [ 1, 10, 100, 200, '%john%', 1, 2, 3, 6, 7, 8, 9, 33 ] };
+ // AND ( red = $13 OR blue = $14 OR green != $15 ) \
+ // AND base_table_id IN ( SELECT id FROM base_table WHERE id >= $16 ) ORDER BY id',
+ // values: [ 1, 10, 100, 200, '%john%', 1, 2, 3, 6, 7, 8, 9, 1, 2, 3, 33 ] };
// columns of type ARRAY
var ary_stmt = sqlgen.select( 'array_table', // target table
View
50 lib/sql-generator/index.js
@@ -71,27 +71,53 @@ var SqlGenerator = function( type ) {
}
return w;
};
- self._recurse_where = function( values, where, key, bop ) {
+ self._recurse_where = function( values, where, key, bop, jk ) {
if( !bop )
bop = '=';
- if( where instanceof Array ) {
+ if( !jk )
+ jk = 'AND';
+ //var util = require('util');
+ //console.log( util.inspect( { values: values, where: where, key: key, bop: bop, jk: jk }, false, 10 ) );
+ if( key && key.toLowerCase() == '-or' ) {
+ var vs = new Array();
+ if( where instanceof Array )
+ where.forEach( function( w ) {
+ vs.push( self._recurse_where( values, w, key, bop, 'OR' ) );
+ } );
+ else if( typeof where == 'object' )
+ Object.keys( where ).forEach( function( w ) {
+ var tmph = {};
+ tmph[ w ] = where[ w ];
+ vs.push( self._recurse_where( values, tmph, null, bop, 'OR' ) );
+ } );
+ return '(' + vs.join(' OR ') + ')';
+ }
+ else if( where instanceof Array ) {
var vs = new Array;
for( var i = 0; i < where.length; i ++ )
- vs.push( self._recurse_where( values, where[i], key, bop ) );
+ vs.push( self._recurse_where( values, where[i], key, bop, jk ) );
return '( ' + vs.join(' OR ') + ' )';
}
- else if( ( where === null || where === undefined ) && key )
- return key + ' IS NULL';
+ else if( ( where === null || where === undefined ) && key ) {
+ if( bop == '=' )
+ return key + ' IS NULL';
+ else
+ return key + ' ' + bop + ' NULL';
+ }
else if( typeof where == 'object' ) {
var ws = new Array;
for( var op in where ) {
+ //console.log( '+++', { ws: ws, op: op, key:key, 'where[op]': where[op] } );
if( op.toLowerCase() == '-and' ) {
var vs = new Array();
where[op].forEach( function( w ) {
- vs.push( self._recurse_where( values, w, key ) );
+ vs.push( self._recurse_where( values, w, key, bop, jk ) );
} );
return '(' + vs.join(' AND ') + ')';
}
+ else if( ['=','!=','>','>=','<','<='].indexOf( op.toLowerCase() ) != -1 ) {
+ ws.push( self._recurse_where( values, where[op], key, op, jk ) );
+ }
else if( op.toLowerCase() == 'in' ) {
if( typeof where[op] == 'object'
&& typeof where[op]['sql'] == 'object'
@@ -175,19 +201,15 @@ var SqlGenerator = function( type ) {
}
else if( key ) {
if( where[op] instanceof Array )
- ws.push( self._recurse_where( values, where[op], key, op ) );
- else if( where[op] ) {
- values.push( where[op] );
- ws.push( key + ' ' + op.toUpperCase() + ' $' + values.length );
- }
+ ws.push( self._recurse_where( values, where[op], key, op, jk ) );
else
- ws.push( key + ' ' + op.toUpperCase() + ' NULL' );
+ ws.push( self._recurse_where( values, where[op], key, op, jk ) );
}
else {
- ws.push( self._recurse_where( values, where[op], op ) );
+ ws.push( self._recurse_where( values, where[op], op, bop, jk ) );
}
}
- return ws.join(' AND ');
+ return ws.join(' '+jk+' ');
}
else {
values.push( where );
View
2  package.json
@@ -1,5 +1,5 @@
{ "name": "sql-generator",
- "version": "0.0.5",
+ "version": "0.0.6",
"description": "SQL Generator",
"keywords" : ["sql", "database", "rdbms"],
"homepage": "https://github.com/Shanon/node-sql-generator",
View
5 test/1-select.js
@@ -79,6 +79,9 @@ exports['test_parse_where'] = function( test, assert ) {
assert.deepEqual( { sql: 'SELECT * FROM test WHERE (foo = $1 AND foo != $2)', values: [ 1, 2 ] },
sqlg.select('test','*', { foo: { '-and': [ 1, { '!=': 2 } ] } } ) );
+ assert.deepEqual( { sql: 'SELECT * FROM test WHERE (foo = $1 OR bar = $2 OR buz != $3)', values: [ 1, 2, 3 ] },
+ sqlg.select('test', '*', { '-or': { foo: 1, bar: 2, buz: { '!=': 3 } } } ) );
+
assert.deepEqual( { sql: 'SELECT * FROM test WHERE foo IN ( $1, $2, $3 )', values: [ 1, 2, 3 ] },
sqlg.select('test','*', { foo: { in: [ 1, 2, 3 ] } } ) );
@@ -105,7 +108,7 @@ exports['test_parse_where'] = function( test, assert ) {
assert.deepEqual( { sql: 'SELECT * FROM test WHERE (SELECT id FROM sample WHERE disable = FALSE) = ANY(ary_sample_id)', values: [] },
sqlg.select('test','*', { ary_sample_id: { ARRAY: 'SELECT id FROM sample WHERE disable = FALSE' } } ) );
-
+
if( typeof test.finish == 'function' ) test.finish();
};
Please sign in to comment.
Something went wrong with that request. Please try again.