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.3.0 to 1.4.0

6

lib/json2sql.js

@@ -144,3 +144,7 @@ function json2sql() {}

}
return `SELECT ${parseSelect(data.select)} FROM ${data.from}${data.where ? ` ${parseWhere(data.where)}` : ''}${data.group ? ` ${parseGroupBy(data.group)}` : ''}${data.orderBy ? ` ${parseOrderBy(data.orderBy)}` : ''}${data.limit ? ` LIMIT ${data.limit}` : ''}${data.offset ? ` OFFSET ${data.offset}` : ''}`.trim();
if (!data.delete) {
return `SELECT ${parseSelect(data.select)} FROM ${data.from}${data.where ? ` ${parseWhere(data.where)}` : ''}${data.group ? ` ${parseGroupBy(data.group)}` : ''}${data.orderBy ? ` ${parseOrderBy(data.orderBy)}` : ''}${data.limit ? ` LIMIT ${data.limit}` : ''}${data.offset ? ` OFFSET ${data.offset}` : ''}`.trim();
} else {
return `DELETE FROM ${data.from}${data.where ? ` ${parseWhere(data.where)}` : ''}${data.group ? ` ${parseGroupBy(data.group)}` : ''}${data.orderBy ? ` ${parseOrderBy(data.orderBy)}` : ''}${data.limit ? ` LIMIT ${data.limit}` : ''}${data.offset ? ` OFFSET ${data.offset}` : ''}`.trim();
}
};

@@ -147,0 +151,0 @@

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

const between = /between/gi;
const sqlStatements = /delete/gi;

@@ -10,2 +11,3 @@ const obtainType = function (token) {

between.lastIndex = 0;
sqlStatements.lastIndex = 0;
if (token[0] === 'LITERAL' && postgisFunctions.test(token[1])) {

@@ -15,2 +17,4 @@ return 'FUNCTION';

return 'BETWEEN';
} else if (token[0] === 'LITERAL' && sqlStatements.test(token[1])) {
return 'DELETE';
}

@@ -152,2 +156,6 @@ return token[0];

sql2json.prototype.parseDelete = function () {
this.parsed.delete = true;
};
sql2json.prototype.parseFrom = function () {

@@ -449,2 +457,5 @@ let name = '';

break;
case 'DELETE':
this.parseDelete();
break;
case 'FROM':

@@ -451,0 +462,0 @@ this.parseFrom();

2

package.json
{
"name": "sql2json",
"version": "1.3.0",
"version": "1.4.0",
"description": "",

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

@@ -21,2 +21,36 @@ const assert = require('assert');

describe('Delete', () => {
it('basic delete', () => {
const data = {
delete: true,
from: 'tablename'
};
const response = 'DELETE FROM tablename';
Json2sql.toSQL(data).should.deepEqual(response);
});
it('with where', () => {
const data = {
delete: true,
from: 'tablename',
where: {
type: 'operator',
left: {
value: 'id',
type: 'literal'
},
value: '>',
right: {
value: 2,
type: 'number'
}
}
};
const response = 'DELETE FROM tablename WHERE id > 2';
Json2sql.toSQL(data).should.deepEqual(response);
});
});
describe('From', () => {

@@ -803,3 +837,3 @@

});
it('With cast', () => {

@@ -806,0 +840,0 @@ const data = {

@@ -21,15 +21,10 @@ const assert = require('assert');

describe('From', () => {
it('With table name', () => {
describe('Delete', () => {
it('basic delete', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
delete: true,
from: 'tablename'
};
const obj = new Sql2json('select * from tablename');
const obj = new Sql2json('DELETE FROM tablename');
const json = obj.toJSON();

@@ -39,967 +34,1008 @@ json.should.deepEqual(response);

it('With table name inside quotes', () => {
it('with where', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: '"tablename"'
delete: true,
from: 'tablename',
where: {
type: 'operator',
left: {
value: 'id',
type: 'literal'
},
value: '>',
right: {
value: 2,
type: 'number'
}
}
};
const obj = new Sql2json('select * from "tablename"');
const obj = new Sql2json('DELETE FROM tablename WHERE id > 2');
const json = obj.toJSON();
json.should.deepEqual(response);
});
});
it('With table name inside quotes 2', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: '"ft:table/name"'
};
// describe('From', () => {
const obj = new Sql2json('select * from "ft:table/name"');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('With table name', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename'
// };
it('With table name inside simple quotes', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: '\'ft:tablename\''
};
// const obj = new Sql2json('select * from tablename');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select * from \'ft:tablename\'');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('With table name inside quotes', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: '"tablename"'
// };
it('With table name inside dots', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'public.pepe'
};
// const obj = new Sql2json('select * from "tablename"');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select * from public.pepe');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('With table name inside quotes 2', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: '"ft:table/name"'
// };
});
// const obj = new Sql2json('select * from "ft:table/name"');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
describe('Select', () => {
// it('With table name inside simple quotes', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: '\'ft:tablename\''
// };
it('SQL with wildcard', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename'
};
// const obj = new Sql2json('select * from \'ft:tablename\'');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select * from tablename');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('With table name inside dots', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'public.pepe'
// };
it('SQL with one column', () => {
const response = {
select: [{
value: 'column1',
alias: null,
type: 'literal'
}],
from: 'tablename'
};
// const obj = new Sql2json('select * from public.pepe');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select column1 from tablename');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// });
it('SQL with several columns', () => {
const response = {
select: [{
value: 'column1',
alias: null,
type: 'literal'
}, {
value: 'column2',
alias: null,
type: 'literal'
}],
from: 'tablename'
};
// describe('Select', () => {
const obj = new Sql2json('select column1, column2 from tablename');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('SQL with wildcard', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename'
// };
it('SQL with one column and alias', () => {
const response = {
select: [{
value: 'column1',
alias: 'aliascolumn',
type: 'literal'
}],
from: 'tablename'
};
// const obj = new Sql2json('select * from tablename');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select column1 as aliascolumn from tablename');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('SQL with one column', () => {
// const response = {
// select: [{
// value: 'column1',
// alias: null,
// type: 'literal'
// }],
// from: 'tablename'
// };
it('SQL with several columns and one alias', () => {
const response = {
select: [{
value: 'column1',
alias: 'aliascolumn',
type: 'literal'
}, {
value: 'column2',
alias: null,
type: 'literal'
}, {
value: 'column3',
alias: null,
type: 'literal'
}],
from: 'tablename'
};
// const obj = new Sql2json('select column1 from tablename');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select column1 as aliascolumn, column2, column3 from tablename');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('SQL with several columns', () => {
// const response = {
// select: [{
// value: 'column1',
// alias: null,
// type: 'literal'
// }, {
// value: 'column2',
// alias: null,
// type: 'literal'
// }],
// from: 'tablename'
// };
// const obj = new Sql2json('select column1, column2 from tablename');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
it('SQL with function', () => {
const response = {
select: [{
alias: null,
type: 'function',
value: 'sum',
arguments: [{
value: 'column1',
type: 'literal'
}]
}],
from: 'tablename'
};
// it('SQL with one column and alias', () => {
// const response = {
// select: [{
// value: 'column1',
// alias: 'aliascolumn',
// type: 'literal'
// }],
// from: 'tablename'
// };
const obj = new Sql2json('select sum(column1) from tablename');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// const obj = new Sql2json('select column1 as aliascolumn from tablename');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
it('SQL with function and alias', () => {
const response = {
select: [{
alias: 'total',
type: 'function',
value: 'sum',
arguments: [{
value: 'column1',
type: 'literal'
}]
}],
from: 'tablename'
};
// it('SQL with several columns and one alias', () => {
// const response = {
// select: [{
// value: 'column1',
// alias: 'aliascolumn',
// type: 'literal'
// }, {
// value: 'column2',
// alias: null,
// type: 'literal'
// }, {
// value: 'column3',
// alias: null,
// type: 'literal'
// }],
// from: 'tablename'
// };
const obj = new Sql2json('select sum(column1) as total from tablename');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// const obj = new Sql2json('select column1 as aliascolumn, column2, column3 from tablename');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
it('SQL with gee function', () => {
const response = {
select: [{
alias: 'total',
type: 'function',
value: 'ST_HISTOGRAM',
arguments: []
}],
from: 'tablename'
};
const obj = new Sql2json('select ST_HISTOGRAM() as total from tablename');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('SQL with function', () => {
// const response = {
// select: [{
// alias: null,
// type: 'function',
// value: 'sum',
// arguments: [{
// value: 'column1',
// type: 'literal'
// }]
// }],
// from: 'tablename'
// };
});
// const obj = new Sql2json('select sum(column1) from tablename');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
describe('Where', () => {
// it('SQL with function and alias', () => {
// const response = {
// select: [{
// alias: 'total',
// type: 'function',
// value: 'sum',
// arguments: [{
// value: 'column1',
// type: 'literal'
// }]
// }],
// from: 'tablename'
// };
it('With one comparison', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
where: {
type: 'operator',
left: {
value: 'id',
type: 'literal'
},
value: '>',
right: {
value: 2,
type: 'number'
}
}
};
// const obj = new Sql2json('select sum(column1) as total from tablename');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select * from tablename where id > 2');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('SQL with gee function', () => {
// const response = {
// select: [{
// alias: 'total',
// type: 'function',
// value: 'ST_HISTOGRAM',
// arguments: []
// }],
// from: 'tablename'
// };
it('With one function', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
where: {
type: 'operator',
left: {
value: 'sum',
type: 'function',
alias: null,
arguments: [{
value: 'data',
type: 'literal'
}]
},
value: '>',
right: {
value: 2,
type: 'number'
}
}
};
// const obj = new Sql2json('select ST_HISTOGRAM() as total from tablename');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select * from tablename where sum(data) > 2');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// });
it('With one function of postgis', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
where: {
value: 'ST_Intersects',
type: 'function',
alias: null,
arguments: [{
value: 'the_geom',
type: 'literal'
}, {
value: '{}',
type: 'string'
}]
}
};
// describe('Where', () => {
const obj = new Sql2json('select * from tablename where ST_Intersects(the_geom, \'{}\')');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('With one comparison', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// where: {
// type: 'operator',
// left: {
// value: 'id',
// type: 'literal'
// },
// value: '>',
// right: {
// value: 2,
// type: 'number'
// }
// }
// };
// const obj = new Sql2json('select * from tablename where id > 2');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
it('With child functions of postgis', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
where: {
value: 'ST_Intersects',
type: 'function',
alias: null,
arguments: [{
value: 'the_geom',
type: 'literal'
}, {
value: 'st_asgeojson',
type: 'function',
alias: null,
arguments:  [{
value: '{}',
type: 'string'
}]
}]
}
};
// it('With one function', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// where: {
// type: 'operator',
// left: {
// value: 'sum',
// type: 'function',
// alias: null,
// arguments: [{
// value: 'data',
// type: 'literal'
// }]
// },
// value: '>',
// right: {
// value: 2,
// type: 'number'
// }
// }
// };
const obj = new Sql2json('select * from tablename where ST_Intersects(the_geom, st_asgeojson(\'{}\'))');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// const obj = new Sql2json('select * from tablename where sum(data) > 2');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
// it('With one function of postgis', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// where: {
// value: 'ST_Intersects',
// type: 'function',
// alias: null,
// arguments: [{
// value: 'the_geom',
// type: 'literal'
// }, {
// value: '{}',
// type: 'string'
// }]
// }
// };
it('With and', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
where: {
type: 'conditional',
value: 'and',
left: {
type: 'operator',
left: {
value: 'id',
type: 'literal'
},
value: '>',
right: {
value: 2,
type: 'number'
}
},
right: {
type: 'operator',
left: {
value: 'id',
type: 'literal'
},
value: '<',
right: {
value: 2,
type: 'number'
}
}
}
};
// const obj = new Sql2json('select * from tablename where ST_Intersects(the_geom, \'{}\')');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select * from tablename where id > 2 and id < 2');
const json = obj.toJSON();
json.should.deepEqual(response);
});
it('With several conditionals', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
where: {
type: 'conditional',
value: 'or',
left: {
type: 'conditional',
value: 'and',
left: {
type: 'operator',
value: '>',
left: {
value: 'a',
type: 'literal'
},
right: {
value: 2,
type: 'number'
}
},
right: {
type: 'operator',
value: '<',
left: {
value: 'b',
type: 'literal'
},
right: {
value: 3,
type: 'number'
}
}
},
right: {
type: 'operator',
value: '=',
left: {
value: 'c',
type: 'literal'
},
right: {
value: 2,
type: 'number'
}
}
}
};
// it('With child functions of postgis', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// where: {
// value: 'ST_Intersects',
// type: 'function',
// alias: null,
// arguments: [{
// value: 'the_geom',
// type: 'literal'
// }, {
// value: 'st_asgeojson',
// type: 'function',
// alias: null,
// arguments:  [{
// value: '{}',
// type: 'string'
// }]
// }]
// }
// };
const obj = new Sql2json('SELECT * FROM tablename WHERE a > 2 and b < 3 or c = 2');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// const obj = new Sql2json('select * from tablename where ST_Intersects(the_geom, st_asgeojson(\'{}\'))');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
it('With in numbers', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
where: {
type: 'in',
value: 'data',
arguments: [{
value: 2,
type: 'number'
}, {
value: 3,
type: 'number'
}]
}
};
const obj = new Sql2json('select * from tablename where data in (2, 3)');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('With and', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// where: {
// type: 'conditional',
// value: 'and',
// left: {
// type: 'operator',
// left: {
// value: 'id',
// type: 'literal'
// },
// value: '>',
// right: {
// value: 2,
// type: 'number'
// }
// },
// right: {
// type: 'operator',
// left: {
// value: 'id',
// type: 'literal'
// },
// value: '<',
// right: {
// value: 2,
// type: 'number'
// }
// }
// }
// };
it('With in numbers floats', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
where: {
type: 'in',
value: 'data',
arguments: [{
value: 2.2,
type: 'number'
}, {
value: 3.3,
type: 'number'
}]
}
};
// const obj = new Sql2json('select * from tablename where id > 2 and id < 2');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select * from tablename where data in (2.2, 3.3)');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('With several conditionals', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// where: {
// type: 'conditional',
// value: 'or',
// left: {
// type: 'conditional',
// value: 'and',
// left: {
// type: 'operator',
// value: '>',
// left: {
// value: 'a',
// type: 'literal'
// },
// right: {
// value: 2,
// type: 'number'
// }
// },
// right: {
// type: 'operator',
// value: '<',
// left: {
// value: 'b',
// type: 'literal'
// },
// right: {
// value: 3,
// type: 'number'
// }
// }
// },
// right: {
// type: 'operator',
// value: '=',
// left: {
// value: 'c',
// type: 'literal'
// },
// right: {
// value: 2,
// type: 'number'
// }
// }
// }
// };
it('With in strings', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
where: {
type: 'in',
value: 'data',
arguments: [{
value: 'a',
type: 'string'
}, {
value: 'b',
type: 'string'
}]
}
};
// const obj = new Sql2json('SELECT * FROM tablename WHERE a > 2 and b < 3 or c = 2');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select * from tablename where data in (\'a\', \'b\')');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('With in numbers', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// where: {
// type: 'in',
// value: 'data',
// arguments: [{
// value: 2,
// type: 'number'
// }, {
// value: 3,
// type: 'number'
// }]
// }
// };
it('With betweens', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
where: {
type: 'between',
value: 'data',
arguments: [{
value: 1,
type: 'number'
}, {
value: 3,
type: 'number'
}]
}
};
// const obj = new Sql2json('select * from tablename where data in (2, 3)');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select * from tablename where data between 1 and 3');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('With in numbers floats', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// where: {
// type: 'in',
// value: 'data',
// arguments: [{
// value: 2.2,
// type: 'number'
// }, {
// value: 3.3,
// type: 'number'
// }]
// }
// };
it('With equality', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
where: {
type: 'operator',
left: {
value: 'country_iso',
type: 'literal'
},
value: '=',
right: {
value: 'BRA',
type: 'string'
}
}
};
// const obj = new Sql2json('select * from tablename where data in (2.2, 3.3)');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select * from tablename where country_iso = "BRA"');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('With in strings', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// where: {
// type: 'in',
// value: 'data',
// arguments: [{
// value: 'a',
// type: 'string'
// }, {
// value: 'b',
// type: 'string'
// }]
// }
// };
it('With equality', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
where: {
type: 'operator',
left: {
value: 'country_iso',
type: 'literal'
},
value: '=',
right: {
value: 'BRA',
type: 'string'
}
}
};
// const obj = new Sql2json('select * from tablename where data in (\'a\', \'b\')');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select * from tablename where country_iso = \'BRA\'');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('With betweens', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// where: {
// type: 'between',
// value: 'data',
// arguments: [{
// value: 1,
// type: 'number'
// }, {
// value: 3,
// type: 'number'
// }]
// }
// };
it('With like', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
where: {
type: 'operator',
left: {
value: 'country_iso',
type: 'literal'
},
value: 'LIKE',
right: {
value: 'BRA',
type: 'string'
}
}
};
// const obj = new Sql2json('select * from tablename where data between 1 and 3');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select * from tablename where country_iso LIKE \'BRA\'');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('With equality', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// where: {
// type: 'operator',
// left: {
// value: 'country_iso',
// type: 'literal'
// },
// value: '=',
// right: {
// value: 'BRA',
// type: 'string'
// }
// }
// };
it('With cast', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
where: {
type: 'operator',
left: {
value: 'day::int',
type: 'literal'
},
value: '>',
right: {
value: 2,
type: 'number'
}
}
};
// const obj = new Sql2json('select * from tablename where country_iso = "BRA"');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('select * from tablename where day::int > 2');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('With equality', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// where: {
// type: 'operator',
// left: {
// value: 'country_iso',
// type: 'literal'
// },
// value: '=',
// right: {
// value: 'BRA',
// type: 'string'
// }
// }
// };
it('With name function as select', () => {
const response = {
select: [{
value: 'avg',
alias: null,
type: 'literal'
}],
from: 'cait_2_0_country_ghg_emissions_onlyco2'
};
// const obj = new Sql2json('select * from tablename where country_iso = \'BRA\'');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
const obj = new Sql2json('SELECT avg FROM cait_2_0_country_ghg_emissions_onlyco2');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// it('With like', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// where: {
// type: 'operator',
// left: {
// value: 'country_iso',
// type: 'literal'
// },
// value: 'LIKE',
// right: {
// value: 'BRA',
// type: 'string'
// }
// }
// };
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 tablename where country_iso LIKE \'BRA\'');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
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);
});
// it('With cast', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// where: {
// type: 'operator',
// left: {
// value: 'day::int',
// type: 'literal'
// },
// value: '>',
// right: {
// value: 2,
// type: 'number'
// }
// }
// };
});
// const obj = new Sql2json('select * from tablename where day::int > 2');
// 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: [{
type: 'literal',
value: 'name'
}]
};
// it('With name function as select', () => {
// const response = {
// select: [{
// value: 'avg',
// alias: null,
// type: 'literal'
// }],
// from: 'cait_2_0_country_ghg_emissions_onlyco2'
// };
const obj = new Sql2json('select * from tablename group by name');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// const obj = new Sql2json('SELECT avg FROM cait_2_0_country_ghg_emissions_onlyco2');
// const json = obj.toJSON();
// json.should.deepEqual(response);
// });
it('Group by several fields', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
group: [{
type: 'literal',
value: 'name'
}, {
type: 'literal',
value: 'surname'
}]
};
// 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 tablename group by name, surname');
const json = obj.toJSON();
json.should.deepEqual(response);
});
// 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);
// });
it('Group with where', () => {
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'
}]
}
};
// });
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('GroupBy', () => {
// it('Group by one field', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// group: [{
// type: 'literal',
// value: 'name'
// }]
// };
it('Group with function', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
group: [{
type: 'function',
value: 'ST_GeoHash',
alias: null,
arguments: [{
type: 'literal',
value: 'the_geom_point'
}, {
type: 'number',
value: 8
}]
}],
// 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: [{
// type: 'literal',
// value: 'name'
// }, {
// type: 'literal',
// value: 'surname'
// }]
// };
const obj = new Sql2json('select * from tablename group by ST_GeoHash(the_geom_point, 8)');
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);
// });
describe('OrderBy', () => {
it('SQL with orderby', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
orderBy: [{
value: 'name',
direction: null
}]
};
// it('Group with where', () => {
// 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'
// }]
// }
// };
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 where data between 1 and 3 group by name, surname');
// 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('Group with function', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// group: [{
// type: 'function',
// value: 'ST_GeoHash',
// alias: null,
// arguments: [{
// type: 'literal',
// value: 'the_geom_point'
// }, {
// type: 'number',
// value: 8
// }]
// }],
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'
}]
};
// const obj = new Sql2json('select * from tablename group by ST_GeoHash(the_geom_point, 8)');
// 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);
});
// describe('OrderBy', () => {
// it('SQL with orderby', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// orderBy: [{
// value: 'name',
// 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');
// 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);
});
});
// it('SQL with orderby and direction', () => {
// const response = {
// select: [{
// value: '*',
// alias: null,
// type: 'wildcard'
// }],
// from: 'tablename',
// orderBy: [{
// value: 'name',
// direction: 'asc'
// }]
// };
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 order by name asc');
// 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('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('Offset', () => {
const response = {
select: [{
value: '*',
alias: null,
type: 'wildcard'
}],
from: 'tablename',
limit: 5,
offset: 10
};
// 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 limit 5 offset 10');
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
// }]
// };
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 order by name asc, createdAt');
// 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);
});
});
// 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);
// });
// 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);
// });
// });
// 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