Skip to content
Browse files

- add test

- support NULL Case ( IS NULL || IS NOT NULL )
  • Loading branch information...
1 parent 0c018fd commit f0eaa143f3b1afae7d4aef1365775faf2f7b1b0a shot committed Feb 15, 2012
Showing with 296 additions and 60 deletions.
  1. +4 −0 Changes
  2. +66 −58 lib/sql-generator/index.js
  3. +8 −2 package.json
  4. +15 −0 test/0-main.js
  5. +136 −0 test/1-select.js
  6. +21 −0 test/2-insert.js
  7. +27 −0 test/3-update.js
  8. +19 −0 test/4-delete.js
View
4 Changes
@@ -1,5 +1,9 @@
Revision history for node.js extension sql-generator
+0.0.5 Tue Feb 14 23:55:53 JST 2012
+ - add test
+ - support NULL Case ( IS NULL || IS NOT NULL )
+
0.0.4 Thu Feb 9 18:58:29 JST 2012
- support for columns of type ARRAY in INSERT and UPDATE
View
124 lib/sql-generator/index.js
@@ -49,9 +49,10 @@ var SqlGenerator = function( type ) {
sqls.push( 'OFFSET ' + opt['offset'] );
return sqls.join(' ');
};
- self._gen_where = function( where, values ) {
+ self._gen_where = function( tmp_where, values ) {
var w;
- if( where ) {
+ if( tmp_where ) {
+ var where = JSON.parse( JSON.stringify( tmp_where ) );
if( where instanceof Array ) {
var ws = new Array;
for( var i = 0; i < where.length; i ++ ) {
@@ -70,13 +71,17 @@ var SqlGenerator = function( type ) {
}
return w;
};
- self._recurse_where = function( values, where, key ) {
+ self._recurse_where = function( values, where, key, bop ) {
+ if( !bop )
+ bop = '=';
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 ) );
+ vs.push( self._recurse_where( values, where[i], key, bop ) );
return '( ' + vs.join(' OR ') + ' )';
}
+ else if( ( where === null || where === undefined ) && key )
+ return key + ' IS NULL';
else if( typeof where == 'object' ) {
var ws = new Array;
for( var op in where ) {
@@ -114,66 +119,69 @@ var SqlGenerator = function( type ) {
}
}
else if( op.toLowerCase() == 'array' ) {
- if( typeof where[op] == 'object' ) {
- var ary_fn;
- var ary_op;
- var sec;
- ( function() {
- var flag = false;
- if( typeof where[op] == 'object' )
- for( var ary_key in where[op] )
- if( ary_key.toLowerCase().match( /(any|all)$/ ) ) {
- ary_fn = RegExp.$1.toUpperCase();
- if( ary_key.match( /^(!=|>=|<=|>|<|=) / ) )
- ary_op = RegExp.$1;
- else
- ary_op = '=';
- sec = where[op][ary_key];
- flag = true;
- break;
- }
- if( !flag ) {
- ary_fn = 'ANY';
- ary_op = '=';
- sec = where[op];
- }
- } )();
- if( typeof sec['sql'] == 'object'
- && sec['sql']['sql'] && sec['sql']['values'] ) {
- var sql = sec['sql']['sql'];
- var tmp_values = new Array();
- for( var i = sec['sql']['values'].length - 1; i >= 0; i -- ) {
- tmp_values.unshift( sec['sql']['values'][i] );
- var new_sql = sql.replace( '\$' + ( i + 1 ), '$' + ( values.length + i + 1 ) );
- sql = new_sql;
- }
- for( var i = 0; i < tmp_values.length; i ++ )
- values.push( tmp_values[i] );
- ws.push( '(' + sql + ') ' + ary_op + ' ' + ary_fn + '(' + key + ')' );
- }
- else if( typeof sec == 'string' && sec.match('^(SELECT|select)') )
- ws.push( '(' + sec + ') ' + ary_op + ' ' + ary_fn + '(' + key + ')' );
- else if( sec instanceof Array ) {
- var vs = new Array();
- sec.forEach( function( v ) {
- values.push( v );
- vs.push( '$' + values.length + ' ' + ary_op + ' ' + ary_fn + '(' + key + ')' );
- } );
- ws.push( '(' + vs.join(' OR ') + ')' );
+ if( typeof where[op] != 'object' ) {
+ where[op] = { ANY: where[op] };
+ }
+ var ary_fn;
+ var ary_op;
+ var sec;
+ ( function() {
+ var flag = false;
+ if( typeof where[op] == 'object' )
+ for( var ary_key in where[op] )
+ if( ary_key.toLowerCase().match( /(any|all)$/ ) ) {
+ ary_fn = RegExp.$1.toUpperCase();
+ if( ary_key.match( /^(!=|>=|<=|>|<|=) / ) )
+ ary_op = RegExp.$1;
+ else
+ ary_op = '=';
+ sec = where[op][ary_key];
+ flag = true;
+ break;
+ }
+ if( !flag ) {
+ ary_fn = 'ANY';
+ ary_op = '=';
+ sec = where[op];
}
- else {
- values.push( sec );
- ws.push( '$' + values.length + ' ' + ary_op + ' ' + ary_fn + '(' + key + ')' );
+ } )();
+ if( typeof sec['sql'] == 'object'
+ && sec['sql']['sql'] && sec['sql']['values'] ) {
+ var sql = sec['sql']['sql'];
+ var tmp_values = new Array();
+ for( var i = sec['sql']['values'].length - 1; i >= 0; i -- ) {
+ tmp_values.unshift( sec['sql']['values'][i] );
+ var new_sql = sql.replace( '\$' + ( i + 1 ), '$' + ( values.length + i + 1 ) );
+ sql = new_sql;
}
+ for( var i = 0; i < tmp_values.length; i ++ )
+ values.push( tmp_values[i] );
+ ws.push( '(' + sql + ') ' + ary_op + ' ' + ary_fn + '(' + key + ')' );
+ }
+ else if( typeof sec == 'string' && sec.match('^(SELECT|select)') )
+ ws.push( '(' + sec + ') ' + ary_op + ' ' + ary_fn + '(' + key + ')' );
+ else if( sec instanceof Array ) {
+ var vs = new Array();
+ sec.forEach( function( v ) {
+ values.push( v );
+ vs.push( '$' + values.length + ' ' + ary_op + ' ' + ary_fn + '(' + key + ')' );
+ } );
+ ws.push( '(' + vs.join(' OR ') + ')' );
}
else {
- values.push( where[op] );
- ws.push( '$' + values.length + ' = ANY(' + key + ')' );
+ values.push( sec );
+ ws.push( '$' + values.length + ' ' + ary_op + ' ' + ary_fn + '(' + key + ')' );
}
}
else if( key ) {
- values.push( where[op] );
- ws.push( key + ' ' + op.toUpperCase() + ' $' + values.length );
+ 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 );
+ }
+ else
+ ws.push( key + ' ' + op.toUpperCase() + ' NULL' );
}
else {
ws.push( self._recurse_where( values, where[op], op ) );
@@ -183,7 +191,7 @@ var SqlGenerator = function( type ) {
}
else {
values.push( where );
- return key + ' = $' + values.length;
+ return key + ' ' + bop + ' $' + values.length;
}
};
self._gen_select_column = function( columns ) {
View
10 package.json
@@ -1,13 +1,19 @@
{ "name": "sql-generator",
- "version": "0.0.4",
+ "version": "0.0.5",
"description": "SQL Generator",
"keywords" : ["sql", "database", "rdbms"],
"homepage": "https://github.com/Shanon/node-sql-generator",
"repository" : {
"type" : "git",
"url" : "git://github.com/Shanon/node-sql-generator.git"
},
+ "scripts" : {
+ "test": "nodeunit test"
+ },
"author" : "Shota Takayama <takayama@shanon.co.jp>",
"main" : "./lib",
- "engines" : { "node": ">= 0.4.11" }
+ "engines" : { "node": ">= 0.4.11" },
+ "devDependencies" : {
+ "nodeunit": "*"
+ }
}
View
15 test/0-main.js
@@ -0,0 +1,15 @@
+var SqlG;
+
+exports['test_load_module'] = function( test, assert ) {
+ if( !assert ) { assert = test; assert.finish = function() { this.done() } }
+ SqlG = require('sql-generator');
+ assert.ok( typeof SqlG === 'function');
+ if( typeof test.finish == 'function' ) test.finish();
+};
+exports['test_create_object'] = function( test, assert ) {
+ if( !assert ) { assert = test; assert.finish = function() { this.done() } }
+ var sqlg = new SqlG();
+ assert.ok( typeof sqlg === 'object');
+ if( typeof test.finish == 'function' ) test.finish();
+};
+
View
136 test/1-select.js
@@ -0,0 +1,136 @@
+var util = require('util');
+
+var SqlG = require('sql-generator');
+var sqlg = new SqlG();
+
+
+exports['test_simple'] = function( test, assert ) {
+ if( !assert ) { assert = test; assert.finish = function() { this.done() } }
+
+ assert.deepEqual( { sql: 'SELECT * FROM test', values: [] },
+ sqlg.select('test') );
+
+ assert.deepEqual( { sql: 'SELECT * FROM test', values: [] },
+ sqlg.select('test','*') );
+
+ sqlg.select( 'test', '*', {}, {}, function( sql, values ) {
+ assert.deepEqual( { sql: 'SELECT * FROM test', values: [] },
+ { sql: sql, values: values } );
+ } );
+ if( typeof test.finish == 'function' ) test.finish();
+};
+exports['test_multi_table'] = function( test, assert ) {
+ if( !assert ) { assert = test; assert.finish = function() { this.done() } }
+
+ assert.deepEqual( { sql: 'SELECT * FROM test, sample', values: [] },
+ sqlg.select( ['test','sample'] ) );
+
+ assert.deepEqual( { sql: 'SELECT * FROM test AS t, sample', values: [] },
+ sqlg.select( [ { 'test': 't' }, 'sample'] ) );
+
+ if( typeof test.finish == 'function' ) test.finish();
+};
+exports['test_columns_specified'] = function( test, assert ) {
+ if( !assert ) { assert = test; assert.finish = function() { this.done() } }
+
+ assert.deepEqual( { sql: 'SELECT id, foo, bar FROM test', values: [] },
+ sqlg.select('test','id, foo, bar') );
+
+
+ assert.deepEqual( { sql: 'SELECT id, hoge, fuga FROM test', values: [] },
+ sqlg.select('test', ['id', 'hoge', 'fuga' ] ) );
+
+ assert.deepEqual( { sql: 'SELECT test.id AS id, test.fuga AS fuga FROM test', values: [] },
+ sqlg.select('test', { 'test.id': 'id', 'test.fuga': 'fuga' } ) );
+
+ assert.deepEqual( { sql: 'SELECT test.id AS id, test.fuga AS fuga, test2.id AS 2_id FROM test', values: [] },
+ sqlg.select('test', [ { 'test.id': 'id', 'test.fuga': 'fuga' }, 'test2.id AS 2_id' ] ) );
+
+ if( typeof test.finish == 'function' ) test.finish();
+};
+exports['test_parse_where'] = function( test, assert ) {
+ if( !assert ) { assert = test; assert.finish = function() { this.done() } }
+
+ assert.deepEqual( { sql: 'SELECT * FROM test WHERE a = FALSE AND b = $1', values: [] },
+ sqlg.select('test', '*', 'a = FALSE AND b = $1' ) );
+
+ assert.deepEqual( { sql: 'SELECT * FROM test WHERE a = $1 AND b = $2 AND flag = $3', values: [ 1, 'str', false ] },
+ sqlg.select('test','*', { a: 1, b: 'str', flag: false } ) );
+
+ sqlg.select('test','*', { a: 1, b: 'str', flag: false }, {}, function( sql, values ) {
+ assert.deepEqual( { sql: 'SELECT * FROM test WHERE a = $1 AND b = $2 AND flag = $3', values: [ 1, 'str', false ] },
+ { sql: sql, values: values } );
+ } );
+ assert.deepEqual( { sql: 'SELECT * FROM test WHERE foo = $1 OR bar = $2 OR buz = $3', values: [ 1, 2, 3 ] },
+ sqlg.select('test','*', [ { foo: 1 }, { bar: 2 }, { buz: 3 } ] ) );
+
+ assert.deepEqual( { sql: 'SELECT * FROM test WHERE ( foo = $1 OR foo = $2 OR foo = $3 )', values: [ 1, 2, 3 ] },
+ sqlg.select('test','*', { foo: [ 1, 2, 3 ] } ) );
+
+ assert.deepEqual( { sql: 'SELECT * FROM test WHERE foo IS NULL', values: [] },
+ sqlg.select('test','*', { foo: null } ) );
+
+ assert.deepEqual( { sql: 'SELECT * FROM test WHERE ( foo != $1 OR foo != $2 OR foo != $3 ) AND foo == NULL', values: [ 1, 2, 3 ] },
+ sqlg.select( 'test','*', { foo: { '!=': [ 1, 2, 3 ], '==': null } } ) );
+
+ assert.deepEqual( { sql: 'SELECT * FROM test WHERE ( foo = $1 OR foo != $2 )', values: [ 1, 2 ] },
+ sqlg.select('test','*', { foo: [ 1, { '!=': 2 } ] } ) );
+
+ 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 IN ( $1, $2, $3 )', values: [ 1, 2, 3 ] },
+ sqlg.select('test','*', { foo: { in: [ 1, 2, 3 ] } } ) );
+
+ assert.deepEqual( { sql: 'SELECT * FROM test WHERE foo = $1 AND sample_id IN ( SELECT id FROM sample WHERE name = $2 )', values: [ 1, 'hoge' ] },
+ sqlg.select('test','*', { foo: 1, sample_id: { in: { sql: { sql: 'SELECT id FROM sample WHERE name = $1', values: [ 'hoge' ] } } } } ) );
+
+ assert.deepEqual( { sql: 'SELECT * FROM test WHERE sample_id IN ( SELECT id FROM sample WHERE disable=FALSE ) ', values: [] },
+ sqlg.select('test','*', { sample_id: { in: 'SELECT id FROM sample WHERE disable=FALSE' } } ) );
+
+ assert.deepEqual( { sql: 'SELECT * FROM test WHERE $1 = ANY(ary)', values: [ 'hoge' ] },
+ sqlg.select('test','*', { ary: { ARRAY: 'hoge' } } ) );
+
+ assert.deepEqual( { sql: 'SELECT * FROM test WHERE ($1 = ANY(ary_num) OR $2 = ANY(ary_num) OR $3 = ANY(ary_num))', values: [ 1, 2, 3 ] },
+ sqlg.select('test','*', { ary_num: { ARRAY: [ 1, 2, 3] } } ) );
+
+ assert.deepEqual( { sql: 'SELECT * FROM test WHERE $1 = ANY(ary_num)', values: [ 1 ] },
+ sqlg.select('test','*', { ary_num: { ARRAY: { ANY: 1 } } } ) );
+
+ assert.deepEqual( { sql: 'SELECT * FROM test WHERE $1 != ALL(ary_num)', values: [ 1 ] },
+ sqlg.select('test','*', { ary_num: { ARRAY: { '!= ALL': 1 } } } ) );
+
+ assert.deepEqual( { sql: 'SELECT * FROM test WHERE (SELECT id FROM sample WHERE disable = $1) = ANY(ary_sample_id)', values: [ false ] },
+ sqlg.select('test','*', { ary_sample_id: { ARRAY: { sql: { sql: 'SELECT id FROM sample WHERE disable = $1', values: [ false ] } } } } ) );
+
+ 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();
+};
+
+exports['test_parse_options'] = function( test, assert ) {
+ if( !assert ) { assert = test; assert.finish = function() { this.done() } }
+
+ assert.deepEqual( { sql: 'SELECT * FROM test GROUP BY foo', values: [] },
+ sqlg.select('test','*', null, { group_by: 'foo'} ) );
+
+ assert.deepEqual( { sql: 'SELECT * FROM test GROUP BY foo, bar', values: [] },
+ sqlg.select('test','*', null, { group_by: [ 'foo', 'bar' ] } ) );
+
+ assert.deepEqual( { sql: 'SELECT * FROM test ORDER BY foo', values: [] },
+ sqlg.select('test','*', null, { order: 'foo' } ) );
+
+ assert.deepEqual( { sql: 'SELECT * FROM test ORDER BY foo, bar', values: [] },
+ sqlg.select('test','*', null, { order: [ 'foo', 'bar' ] } ) );
+
+ assert.deepEqual( { sql: 'SELECT * FROM test LIMIT 100', values: [] },
+ sqlg.select('test','*', null, { limit: 100 } ) );
+
+ assert.deepEqual( { sql: 'SELECT * FROM test OFFSET 100', values: [] },
+ sqlg.select('test','*', null, { offset: 100 } ) );
+ assert.deepEqual( { sql: 'SELECT * FROM test GROUP BY foo ORDER BY bar LIMIT 10 OFFSET 100', values: [] },
+ sqlg.select('test','*', null, { group_by: 'foo', order: 'bar', limit: 10, offset: 100 } ) );
+
+ if( typeof test.finish == 'function' ) test.finish();
+};
View
21 test/2-insert.js
@@ -0,0 +1,21 @@
+var SqlG = require('sql-generator');
+var sqlg = new SqlG();
+
+exports['test_insert'] = function( test, assert ) {
+ if( !assert ) { assert = test; assert.finish = function() { this.done() } }
+
+ assert.deepEqual( { sql: 'INSERT INTO test ( foo, bar ) VALUES ( $1, $2 )', values: [ 1, 2 ] },
+ sqlg.insert( 'test', { foo: 1, bar: 2 } ) );
+ sqlg.insert( 'test', { foo: 1, bar: 2 }, function( sql, values ) {
+ assert.deepEqual( { sql: 'INSERT INTO test ( foo, bar ) VALUES ( $1, $2 )', values: [ 1, 2 ] },
+ { sql: sql, values: values } );
+ } );
+
+ assert.deepEqual( { sql: 'INSERT INTO test ( foo ) VALUES ( $1 )', values: [ '{1,2}' ] },
+ sqlg.insert( 'test', { foo: [ 1, 2 ] } ) );
+
+ assert.deepEqual( { sql: 'INSERT INTO test ( foo ) VALUES ( ARRAY[$1, $2] )', values: [ 'a', 'b' ] },
+ sqlg.insert( 'test', { foo: [ 'a', 'b' ] } ) );
+
+ if( typeof test.finish == 'function' ) test.finish();
+};
View
27 test/3-update.js
@@ -0,0 +1,27 @@
+var SqlG = require('sql-generator');
+var sqlg = new SqlG();
+
+exports['test_update'] = function( test, assert ) {
+ if( !assert ) { assert = test; assert.finish = function() { this.done() } }
+
+ assert.deepEqual( { sql: 'UPDATE test SET foo = $1, bar = $2', values: [ 1, 2 ] },
+ sqlg.update( 'test', null, { foo: 1, bar: 2 } ) );
+
+ sqlg.update( 'test', null, { foo: 1, bar: 2 }, function( sql, values ) {
+ assert.deepEqual( { sql: 'UPDATE test SET foo = $1, bar = $2', values: [ 1, 2 ] },
+ { sql: sql, values: values } );
+ } );
+
+
+ assert.deepEqual( { sql: 'UPDATE test SET foo = $1, bar = $2 WHERE id = $3', values: [ 1, 2, 10 ] },
+ sqlg.update( 'test', { id: 10 }, { foo: 1, bar: 2 } ) );
+
+
+ assert.deepEqual( { sql: 'UPDATE test SET foo = $1', values: [ '{1,2}' ] },
+ sqlg.update( 'test', null, { foo: [ 1, 2 ] } ) );
+
+ assert.deepEqual( { sql: 'UPDATE test SET foo = ARRAY[$1, $2]', values: [ 'a', 'b' ] },
+ sqlg.update( 'test', null, { foo: [ 'a', 'b' ] } ) );
+
+ if( typeof test.finish == 'function' ) test.finish();
+};
View
19 test/4-delete.js
@@ -0,0 +1,19 @@
+var SqlG = require('sql-generator');
+var sqlg = new SqlG();
+
+exports['test_delete'] = function( test, assert ) {
+ if( !assert ) { assert = test; assert.finish = function() { this.done() } }
+
+ assert.deepEqual( { sql: 'DELETE FROM test', values: [] },
+ sqlg.delete( 'test', null ) );
+
+ sqlg.delete( 'test', null, function( sql, values ) {
+ assert.deepEqual( { sql: 'DELETE FROM test', values: [] },
+ { sql: sql, values: values } );
+ } );
+
+ assert.deepEqual( { sql: 'DELETE FROM test WHERE id = $1', values: [ 10 ] },
+ sqlg.delete( 'test', { id: 10 } ) );
+
+ if( typeof test.finish == 'function' ) test.finish();
+};

0 comments on commit f0eaa14

Please sign in to comment.
Something went wrong with that request. Please try again.