Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sql2json

Package Overview
Dependencies
Maintainers
1
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sql2json - npm Package Compare versions

Comparing version 1.4.5 to 1.4.6

2

lib/json2sql.js

@@ -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({

2

package.json
{
"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);
// });
// });
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc