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.2.2 to 1.2.3

87

lib/sql2json.js

@@ -206,2 +206,38 @@ const lexer = require('sql-parser').lexer;

sql2json.prototype.parseBetween = function (between) {
const stack = [];
while (this.hasNext()) {
const token = this.next();
switch (obtainType(token)) {
case 'STRING':
stack.push({
value: token[1],
type: 'string'
});
break;
case 'NUMBER':
stack.push({
value: parseFloat(token[1]),
type: 'number'
});
break;
case 'CONDITIONAL':
break;
default:
return stack;
}
if (stack.length === 2) {
const right = stack.pop();
const left = stack.pop();
return {
type: 'between',
value: between,
arguments: [left, right]
};
}
}
};
sql2json.prototype.parseWhere = function () {

@@ -224,2 +260,3 @@ const stack = [];

between = stack.pop().value;
stack.push(this.parseBetween(between));
break;

@@ -272,19 +309,10 @@ case 'NUMBER':

const left = stack.pop();
if (!between) {
stack.push({
type: 'conditional',
value: conditional,
left,
right
});
conditional = null;
} else {
stack.push({
type: 'between',
value: between,
arguments: [left, right]
});
between = null;
conditional = null;
}
stack.push({
type: 'conditional',
value: conditional,
left,
right
});
conditional = null;
}

@@ -308,19 +336,10 @@ conditional = token[1];

const left = stack.pop();
if (!between) {
stack.push({
type: 'conditional',
value: conditional,
left,
right
});
conditional = null;
} else {
stack.push({
type: 'between',
value: between,
arguments: [left, right]
});
between = null;
conditional = null;
}
stack.push({
type: 'conditional',
value: conditional,
left,
right
});
conditional = null;
}

@@ -327,0 +346,0 @@ this.parsed.where = stack.pop();

{
"name": "sql2json",
"version": "1.2.2",
"version": "1.2.3",
"description": "",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -682,3 +682,3 @@ const assert = require('assert');

it('With table', () => {
it('With name function as select', () => {
const response = {

@@ -697,219 +697,260 @@ select: [{

});
it('With between and in', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'table',
where: {
type: 'conditional',
value: 'AND',
left: {
type: 'in',
value: 'confidence',
arguments: [{
type: 'string',
value: 'nominal'
}, {
type: 'string',
value: '0'
}]
},
right: {
type: 'between',
value: 'bright_ti5',
arguments: [{
type: 'number',
value: 1
}, {
type: 'number',
value: 4
}]
}
}
};
const obj = new Sql2json('select * from table where confidence in (\'nominal\',\'0\') AND bright_ti5 between 1 and 4');
const json = obj.toJSON();
json.should.deepEqual(response);
});
});
// describe('GroupBy', () => {
// it('Group by one field', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// group: ['name']
// };
describe('GroupBy', () => {
it('Group by one field', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
group: ['name']
};
// const obj = new Sql2json('select * from tablename group by name');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select * from tablename group by name');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('Group by several fields', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// group: ['name', 'surname']
// };
it('Group by several fields', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
group: ['name', 'surname']
};
// const obj = new Sql2json('select * from tablename group by name, surname');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select * from tablename group by name, surname');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('Group with where', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// group: ['name', 'surname'],
// where: {
// type: 'between',
// value: 'data',
// arguments: [{
// value: 1,
// type: 'number'
// }, {
// value: 3,
// type: 'number'
// }]
// }
// };
it('Group with where', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
group: ['name', 'surname'],
where: {
type: 'between',
value: 'data',
arguments: [{
value: 1,
type: 'number'
}, {
value: 3,
type: 'number'
}]
}
};
// const obj = new Sql2json('select * from tablename where data between 1 and 3 group by name, surname');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
// });
const obj = new Sql2json('select * from tablename where data between 1 and 3 group by name, surname');
const json = obj.toJSON();
json.should.deepEqual(response);
});
});
// describe('OrderBy', () => {
// it('SQL with orderby', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// orderBy: [{
// value: 'name',
// direction: null
// }]
// };
describe('OrderBy', () => {
it('SQL with orderby', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
orderBy: [{
value: 'name',
direction: null
}]
};
// const obj = new Sql2json('select * from tablename order by name');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select * from tablename order by name');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('SQL with orderby and direction', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// orderBy: [{
// value: 'name',
// direction: 'asc'
// }]
// };
it('SQL with orderby and direction', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
orderBy: [{
value: 'name',
direction: 'asc'
}]
};
// const obj = new Sql2json('select * from tablename order by name asc');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select * from tablename order by name asc');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('SQL with several orderby and direction', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// orderBy: [{
// value: 'name',
// direction: 'asc'
// }, {
// value: 'createdAt',
// direction: 'desc'
// }]
// };
it('SQL with several orderby and direction', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
orderBy: [{
value: 'name',
direction: 'asc'
}, {
value: 'createdAt',
direction: 'desc'
}]
};
// const obj = new Sql2json('select * from tablename order by name asc, createdAt desc');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select * from tablename order by name asc, createdAt desc');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('SQL with several orderby and direction 2', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// orderBy: [{
// value: 'name',
// direction: 'asc'
// }, {
// value: 'createdAt',
// direction: null
// }]
// };
it('SQL with several orderby and direction 2', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
orderBy: [{
value: 'name',
direction: 'asc'
}, {
value: 'createdAt',
direction: null
}]
};
// const obj = new Sql2json('select * from tablename order by name asc, createdAt');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
// });
const obj = new Sql2json('select * from tablename order by name asc, createdAt');
const json = obj.toJSON();
json.should.deepEqual(response);
});
});
// describe('Limit and offset', () => {
// it('Limit', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// limit: 5
// };
describe('Limit and offset', () => {
it('Limit', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
limit: 5
};
// const obj = new Sql2json('select * from tablename limit 5');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select * from tablename limit 5');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('Offset', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// limit: 5,
// offset: 10
// };
it('Offset', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
limit: 5,
offset: 10
};
// const obj = new Sql2json('select * from tablename limit 5 offset 10');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
// });
const obj = new Sql2json('select * from tablename limit 5 offset 10');
const json = obj.toJSON();
json.should.deepEqual(response);
});
});
// describe('all', () => {
// it('All', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// group: ['name', 'surname'],
// where: {
// type: 'between',
// value: 'data',
// arguments: [{
// value: 1,
// type: 'number'
// }, {
// value: 3,
// type: 'number'
// }]
// },
// limit: 1,
// orderBy: [{
// value: 'name',
// direction: null
// }]
// };
describe('all', () => {
it('All', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
group: ['name', '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);
// });
// });
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