Comparing version 1.4.5 to 1.4.6
@@ -71,2 +71,4 @@ function json2sql() {} | ||
return `${parseNodeWhere(node.left)} ${node.value} ${parseNodeWhere(node.right)}`; | ||
case 'bracket': | ||
return `(${parseNodeWhere(node.value)})`; | ||
case 'in': | ||
@@ -73,0 +75,0 @@ args = []; |
@@ -276,2 +276,34 @@ const lexer = require('sql-parser').lexer; | ||
case 'LEFT_PAREN': | ||
stack.push({ | ||
value: this.parseWhere(), | ||
type: 'bracket' | ||
}); | ||
break; | ||
case 'RIGHT_PAREN': | ||
if (operator && stack.length >= 2) { | ||
const right = stack.pop(); | ||
const left = stack.pop(); | ||
stack.push({ | ||
type: 'operator', | ||
value: operator, | ||
left, | ||
right | ||
}); | ||
operator = null; | ||
} | ||
if (stack.length >= 2 && conditional) { | ||
const right = stack.pop(); | ||
const left = stack.pop(); | ||
stack.push({ | ||
type: 'conditional', | ||
value: conditional, | ||
left, | ||
right | ||
}); | ||
conditional = null; | ||
} | ||
return stack.pop(); | ||
break; | ||
case 'LITERAL': | ||
@@ -278,0 +310,0 @@ stack.push({ |
{ | ||
"name": "sql2json", | ||
"version": "1.4.5", | ||
"version": "1.4.6", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -949,2 +949,149 @@ const assert = require('assert'); | ||
}); | ||
describe('brackets', () => { | ||
it('brackets', () => { | ||
const data = { | ||
select: [{ | ||
value: '*', | ||
alias: null, | ||
type: 'wildcard' | ||
}], | ||
from: 'tablename', | ||
where: { | ||
type: 'conditional', | ||
value: 'and', | ||
left: { | ||
type: 'operator', | ||
left: { | ||
value: 'a', | ||
type: 'literal' | ||
}, | ||
value: '>', | ||
right: { | ||
value: 2, | ||
type: 'number' | ||
} | ||
}, | ||
right: { | ||
type:'bracket', | ||
value: { | ||
type: 'conditional', | ||
value: 'or', | ||
left: { | ||
type: 'operator', | ||
left: { | ||
value: 'c', | ||
type: 'literal' | ||
}, | ||
value: '>', | ||
right: { | ||
value: 2, | ||
type: 'number' | ||
} | ||
}, | ||
right: { | ||
type: 'operator', | ||
left: { | ||
value: 'c', | ||
type: 'literal' | ||
}, | ||
value: '<', | ||
right: { | ||
value: 0, | ||
type: 'number' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
const response = 'SELECT * FROM tablename WHERE a > 2 and (c > 2 or c < 0)'; | ||
Json2sql.toSQL(data).should.deepEqual(response); | ||
}); | ||
it('brackets', () => { | ||
const data = { | ||
select: [{ | ||
value: '*', | ||
alias: null, | ||
type: 'wildcard' | ||
}], | ||
from: 'tablename', | ||
where: { | ||
type: 'conditional', | ||
value: 'and', | ||
left: { | ||
type: 'bracket', | ||
value: { | ||
type: 'conditional', | ||
value: 'or', | ||
left:{ | ||
type: 'operator', | ||
left: { | ||
value: 'a', | ||
type: 'literal' | ||
}, | ||
value: '>', | ||
right: { | ||
value: 2, | ||
type: 'number' | ||
} | ||
}, | ||
right: { | ||
type: 'operator', | ||
left: { | ||
value: 'c', | ||
type: 'literal' | ||
}, | ||
value: '<', | ||
right: { | ||
value: 1, | ||
type: 'number' | ||
} | ||
} | ||
} | ||
}, | ||
right: { | ||
type:'bracket', | ||
value: { | ||
type: 'conditional', | ||
value: 'or', | ||
left: { | ||
type: 'operator', | ||
left: { | ||
value: 'c', | ||
type: 'literal' | ||
}, | ||
value: '>', | ||
right: { | ||
value: 2, | ||
type: 'number' | ||
} | ||
}, | ||
right: { | ||
type: 'operator', | ||
left: { | ||
value: 'c', | ||
type: 'literal' | ||
}, | ||
value: '<', | ||
right: { | ||
value: 0, | ||
type: 'number' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
const response = 'SELECT * FROM tablename WHERE (a > 2 or c < 1) and (c > 2 or c < 0)'; | ||
Json2sql.toSQL(data).should.deepEqual(response); | ||
}); | ||
}); | ||
describe('all', () => { | ||
@@ -951,0 +1098,0 @@ it('All', () => { |
@@ -380,9 +380,9 @@ const assert = require('assert'); | ||
arguments: [{ | ||
value: 'rast', | ||
type: 'literal' | ||
}, | ||
{ | ||
value: 1, | ||
type: 'number' | ||
} | ||
value: 'rast', | ||
type: 'literal' | ||
}, | ||
{ | ||
value: 1, | ||
type: 'number' | ||
} | ||
] | ||
@@ -1117,4 +1117,4 @@ }], | ||
describe('all', () => { | ||
it('All', () => { | ||
describe('brackets', () => { | ||
it('brackets', () => { | ||
const response = { | ||
@@ -1127,28 +1127,136 @@ select: [{ | ||
from: 'tablename', | ||
group: [{ | ||
type: 'literal', | ||
value: 'name' | ||
}, { | ||
type: 'literal', | ||
value: 'surname' | ||
where: { | ||
type: 'conditional', | ||
value: 'and', | ||
left: { | ||
type: 'operator', | ||
left: { | ||
value: 'a', | ||
type: 'literal' | ||
}, | ||
value: '>', | ||
right: { | ||
value: 2, | ||
type: 'number' | ||
} | ||
}, | ||
right: { | ||
type:'bracket', | ||
value: { | ||
type: 'conditional', | ||
value: 'or', | ||
left: { | ||
type: 'operator', | ||
left: { | ||
value: 'c', | ||
type: 'literal' | ||
}, | ||
value: '>', | ||
right: { | ||
value: 2, | ||
type: 'number' | ||
} | ||
}, | ||
right: { | ||
type: 'operator', | ||
left: { | ||
value: 'c', | ||
type: 'literal' | ||
}, | ||
value: '<', | ||
right: { | ||
value: 0, | ||
type: 'number' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
const obj = new Sql2json('select * from tablename where a > 2 and (c > 2 or c < 0)'); | ||
const json = obj.toJSON(); | ||
json.should.deepEqual(response); | ||
}); | ||
it('brackets complex', () => { | ||
const response = { | ||
select: [{ | ||
value: '*', | ||
alias: null, | ||
type: 'wildcard' | ||
}], | ||
from: 'tablename', | ||
where: { | ||
type: 'between', | ||
value: 'data', | ||
arguments: [{ | ||
value: 1, | ||
type: 'number' | ||
}, { | ||
value: 3, | ||
type: 'number' | ||
}] | ||
}, | ||
limit: 1, | ||
orderBy: [{ | ||
value: 'name', | ||
direction: null | ||
}] | ||
type: 'conditional', | ||
value: 'and', | ||
left: { | ||
type: 'bracket', | ||
value: { | ||
type: 'conditional', | ||
value: 'or', | ||
left:{ | ||
type: 'operator', | ||
left: { | ||
value: 'a', | ||
type: 'literal' | ||
}, | ||
value: '>', | ||
right: { | ||
value: 2, | ||
type: 'number' | ||
} | ||
}, | ||
right: { | ||
type: 'operator', | ||
left: { | ||
value: 'c', | ||
type: 'literal' | ||
}, | ||
value: '<', | ||
right: { | ||
value: 1, | ||
type: 'number' | ||
} | ||
} | ||
} | ||
}, | ||
right: { | ||
type:'bracket', | ||
value: { | ||
type: 'conditional', | ||
value: 'or', | ||
left: { | ||
type: 'operator', | ||
left: { | ||
value: 'c', | ||
type: 'literal' | ||
}, | ||
value: '>', | ||
right: { | ||
value: 2, | ||
type: 'number' | ||
} | ||
}, | ||
right: { | ||
type: 'operator', | ||
left: { | ||
value: 'c', | ||
type: 'literal' | ||
}, | ||
value: '<', | ||
right: { | ||
value: 0, | ||
type: 'number' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
const obj = new Sql2json('select * from tablename where data between 1 and 3 group by name, surname order by name limit 1'); | ||
const obj = new Sql2json('select * from tablename where (a > 2 or c < 1) and (c > 2 or c < 0)'); | ||
const json = obj.toJSON(); | ||
@@ -1159,2 +1267,42 @@ json.should.deepEqual(response); | ||
// describe('all', () => { | ||
// it('All', () => { | ||
// const response = { | ||
// select: [{ | ||
// value: '*', | ||
// alias: null, | ||
// type: 'wildcard' | ||
// }], | ||
// from: 'tablename', | ||
// group: [{ | ||
// type: 'literal', | ||
// value: 'name' | ||
// }, { | ||
// type: 'literal', | ||
// value: 'surname' | ||
// }], | ||
// where: { | ||
// type: 'between', | ||
// value: 'data', | ||
// arguments: [{ | ||
// value: 1, | ||
// type: 'number' | ||
// }, { | ||
// value: 3, | ||
// type: 'number' | ||
// }] | ||
// }, | ||
// limit: 1, | ||
// orderBy: [{ | ||
// value: 'name', | ||
// direction: null | ||
// }] | ||
// }; | ||
// const obj = new Sql2json('select * from tablename where data between 1 and 3 group by name, surname order by name limit 1'); | ||
// const json = obj.toJSON(); | ||
// json.should.deepEqual(response); | ||
// }); | ||
// }); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
101104
2853