|
|
@@ -1,11 +1,16 @@
|
|
|
|
/* global describe, it */
|
|
|
|
/* global describe, it */
|
|
|
|
var assert = require('assert');
|
|
|
|
var assert = require('assert');
|
|
|
|
var knex = require('knex')({client: 'sqlite' });
|
|
|
|
var Knex = require('knex');
|
|
|
|
var knexJsonQuery = require('./');
|
|
|
|
var knexJsonQuery = require('./');
|
|
|
|
require('simple-mocha');
|
|
|
|
require('simple-mocha');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var testData = {
|
|
|
|
|
|
|
|
mysql:{},
|
|
|
|
|
|
|
|
pg:{},
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var conditions= [
|
|
|
|
testData.pg.knex = Knex({client: 'pg' });
|
|
|
|
|
|
|
|
testData.pg.conditions = [
|
|
|
|
/* -----------Tests for and conditions---------- */
|
|
|
|
/* -----------Tests for and conditions---------- */
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: 'handle simple and conditions',
|
|
|
|
name: 'handle simple and conditions',
|
|
|
@@ -14,23 +19,23 @@ var conditions= [
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: 'handle simple and conditions',
|
|
|
|
name: 'handle simple and conditions',
|
|
|
|
input:{"firstName":[["LIKE","H%"],["OR","hemanth"]]},
|
|
|
|
input:{"firstName":[["like","H%"],["OR","hemanth"]]},
|
|
|
|
output: 'select * where (("firstName" LIKE \'H%\' or "firstName" = \'hemanth\'))'
|
|
|
|
output: 'select * where (("firstName" like \'H%\' or "firstName" = \'hemanth\'))'
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: 'handle regex query: MongoQuery',
|
|
|
|
name: 'handle regex query: MongoQuery',
|
|
|
|
input:{"firstName":{ $regex: "H*" }},
|
|
|
|
input:{"firstName":{ $regex: "H*" }},
|
|
|
|
output: 'select * where ("firstName" REGEXP \'H*\')'
|
|
|
|
output: 'select * where ("firstName" regexp \'H*\')'
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: 'handle regex query and should omit $options: MongoQuery',
|
|
|
|
name: 'handle regex query and should omit $options: MongoQuery',
|
|
|
|
input:{"firstName":{ $regex: "H*", $options: 'i' }},
|
|
|
|
input:{"firstName":{ $regex: "H*", $options: 'i' }},
|
|
|
|
output: 'select * where ("firstName" REGEXP \'H*\')'
|
|
|
|
output: 'select * where ("firstName" regexp \'H*\')'
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: 'handle simple and conditions',
|
|
|
|
name: 'handle simple and conditions',
|
|
|
|
input:{ f1: [[ 'A' ],['LIKE', 'B' ]], f2: 20, f3: 30 },
|
|
|
|
input:{ f1: [[ 'A' ],['like', 'B' ]], f2: 20, f3: 30 },
|
|
|
|
output: 'select * where (("f1" = \'A\' and "f1" LIKE \'B\') and "f2" = 20 and "f3" = 30)'
|
|
|
|
output: 'select * where (("f1" = \'A\' and "f1" like \'B\') and "f2" = 20 and "f3" = 30)'
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: 'handle basic operators :: greater-than less-than',
|
|
|
|
name: 'handle basic operators :: greater-than less-than',
|
|
|
@@ -49,13 +54,13 @@ var conditions= [
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: 'handle multiple conditions in one field',
|
|
|
|
name: 'handle multiple conditions in one field',
|
|
|
|
input:{ f1: [ [ 'LIKE', 20 ], 21, [ 'AND_BETWEEN', [ 40, 45 ] ] ], f2: 20, f3: 30 },
|
|
|
|
input:{ f1: [ [ 'like', 20 ], 21, [ 'AND_BETWEEN', [ 40, 45 ] ] ], f2: 20, f3: 30 },
|
|
|
|
output: 'select * where (("f1" LIKE 20 and "f1" = 21 and "f1" between 40 and 45) and "f2" = 20 and "f3" = 30)'
|
|
|
|
output: 'select * where (("f1" like 20 and "f1" = 21 and "f1" between 40 and 45) and "f2" = 20 and "f3" = 30)'
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: 'handle simple and condition with like statement',
|
|
|
|
name: 'handle simple and condition with like statement',
|
|
|
|
input:{ f1: [ 'LIKE', 20 ], f2: 20, f3: 30 },
|
|
|
|
input:{ f1: [ 'like', 20 ], f2: 20, f3: 30 },
|
|
|
|
output: 'select * where ("f1" LIKE 20 and "f2" = 20 and "f3" = 30)'
|
|
|
|
output: 'select * where ("f1" like 20 and "f2" = 20 and "f3" = 30)'
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: 'handle simple and condition with between statement',
|
|
|
|
name: 'handle simple and condition with between statement',
|
|
|
@@ -91,23 +96,23 @@ var conditions= [
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: 'handle multiple conditions in one field',
|
|
|
|
name: 'handle multiple conditions in one field',
|
|
|
|
input:{ f1: [ [ 'LIKE', 20 ], [ 'OR', 21 ], [ 'OR_BETWEEN', [ 40, 45 ] ] ], f2: 20, f3: 30 },
|
|
|
|
input:{ f1: [ [ 'like', 20 ], [ 'OR', 21 ], [ 'OR_BETWEEN', [ 40, 45 ] ] ], f2: 20, f3: 30 },
|
|
|
|
output: 'select * where (("f1" LIKE 20 or "f1" = 21 or "f1" between 40 and 45) and "f2" = 20 and "f3" = 30)'
|
|
|
|
output: 'select * where (("f1" like 20 or "f1" = 21 or "f1" between 40 and 45) and "f2" = 20 and "f3" = 30)'
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: 'handle multiple conditions in one field: MongoQuery',
|
|
|
|
name: 'handle multiple conditions in one field: MongoQuery',
|
|
|
|
input:{ f1: { $like: 20, $or: 21, $or_between: [ 40, 45 ] }, f2: 20, f3: 30 },
|
|
|
|
input:{ f1: { $like: 20, $or: 21, $or_between: [ 40, 45 ] }, f2: 20, f3: 30 },
|
|
|
|
output: 'select * where (("f1" LIKE 20 or "f1" = 21 or "f1" between 40 and 45) and "f2" = 20 and "f3" = 30)'
|
|
|
|
output: 'select * where (("f1" like 20 or "f1" = 21 or "f1" between 40 and 45) and "f2" = 20 and "f3" = 30)'
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: 'handle simple or condition with like statement',
|
|
|
|
name: 'handle simple or condition with like statement',
|
|
|
|
input:[ { f1: ['LIKE',10] }, { f2: 20 }, { f3: 30 } ],
|
|
|
|
input:[ { f1: ['like',10] }, { f2: 20 }, { f3: 30 } ],
|
|
|
|
output: 'select * where (("f1" LIKE 10) or ("f2" = 20) or ("f3" = 30))'
|
|
|
|
output: 'select * where (("f1" like 10) or ("f2" = 20) or ("f3" = 30))'
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: 'handle simple or condition with like statement: MongoQuery',
|
|
|
|
name: 'handle simple or condition with like statement: MongoQuery',
|
|
|
|
input:[ { f1: { $like: 10 } }, { f2: 20 }, { f3: 30 } ],
|
|
|
|
input:[ { f1: { $like: 10 } }, { f2: 20 }, { f3: 30 } ],
|
|
|
|
output: 'select * where (("f1" LIKE 10) or ("f2" = 20) or ("f3" = 30))'
|
|
|
|
output: 'select * where (("f1" like 10) or ("f2" = 20) or ("f3" = 30))'
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: 'handle simple or condition with between statement',
|
|
|
|
name: 'handle simple or condition with between statement',
|
|
|
@@ -146,8 +151,8 @@ var conditions= [
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: 'handle simple or condition with conditional array',
|
|
|
|
name: 'handle simple or condition with conditional array',
|
|
|
|
input: { f1: [['ILIKE', 'awesome'], ['OR_ILIKE', '%super%'] ] },
|
|
|
|
input: { f1: [['ilike', 'awesome'], ['OR_ilike', '%super%'] ] },
|
|
|
|
output: 'select * where (("f1" ILIKE \'awesome\' or "f1" ILIKE \'%super%\'))'
|
|
|
|
output: 'select * where (("f1" ilike \'awesome\' or "f1" ilike \'%super%\'))'
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: 'handle $or grouping',
|
|
|
|
name: 'handle $or grouping',
|
|
|
@@ -157,6 +162,21 @@ var conditions= [
|
|
|
|
];
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
testData.mysql.knex = Knex({client: 'mysql' });
|
|
|
|
|
|
|
|
testData.mysql.conditions = [
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
name: 'Mysql: handle "and" condition with raw statement',
|
|
|
|
|
|
|
|
input: { f1: { $raw: '< `use_limit`' }, f2: 20 },
|
|
|
|
|
|
|
|
output: 'select * where (`f1` < `use_limit` and `f2` = 20)'
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Object.keys( testData ).forEach(function(driver){
|
|
|
|
|
|
|
|
var data = testData[driver];
|
|
|
|
|
|
|
|
var conditions = data.conditions;
|
|
|
|
|
|
|
|
var knex = data.knex;
|
|
|
|
|
|
|
|
|
|
|
|
describe('SQL query generation from json query', function(){
|
|
|
|
describe('SQL query generation from json query', function(){
|
|
|
|
conditions.forEach(function(v){
|
|
|
|
conditions.forEach(function(v){
|
|
|
|
it( 'should ' + v.name , function(){
|
|
|
|
it( 'should ' + v.name , function(){
|
|
|
@@ -166,3 +186,5 @@ describe('SQL query generation from json query', function(){
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|