Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ <h1 class="title">

try {
parsed = SQLParser.parse(input);
console.log(parsed);
} catch (e) {
document.getElementById("output").value = e.message;
}
Expand All @@ -89,6 +90,7 @@ <h1 class="title">
try {
output = builder.convert();
} catch (e) {
console.log(e);
output = e.message;
}

Expand Down
111 changes: 61 additions & 50 deletions js/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ class Generator {
for (let i = 0; i < args.length; i++) {
let arg = args[i];

if (typeof arg === 'number') {
if (typeof arg === 'number' || arg === true || arg === false || (typeof arg == 'string' && arg.startsWith('function'))) {
res.push(arg);
} else if (Array.isArray(arg)) {
res.push(`[${this.generateArgs(arg).join(',')}]`)
} else if (arg instanceof Generator) {
res.push(arg.generate(1, false));
} else if (arg === true || arg === false || (typeof arg == 'string' && arg.startsWith('function'))) {
res.push(arg);
} else {
res.push(`'${arg}'`);
}
Expand Down Expand Up @@ -131,7 +129,6 @@ class Builder {

select() {
if (this.input.fields.length == 1 && this.input.fields[0].constructor.name == 'Star') {
console.log('star');
return;
}

Expand Down Expand Up @@ -190,22 +187,20 @@ class Builder {
}
}

orderBy()
{
orderBy() {
if (this.input.order !== null) {
for (const order of this.input.order.orderings) {
this.generator.addFunction('orderBy', [
this.parseQualfiedIdFromLiteralValue(order.value),
order.direction,
this.parseQualfiedIdFromLiteralValue(order.value),
order.direction,
]);
}
}
}

groupBy()
{
groupBy() {
if (this.input.group != null) {
this.generator.addFunction('groupBy',...this.input.group.fields.map(f => this.parseQualfiedIdFromLiteralValue(f)));
this.generator.addFunction('groupBy', ...this.input.group.fields.map(f => this.parseQualfiedIdFromLiteralValue(f)));

if (this.input.group.having != null) {
this.generator.addFunction('having', [
Expand All @@ -219,45 +214,59 @@ class Builder {

parseWhere(op, pre_operation = null) {
let func_name = '';
let args = [this.parseQualfiedIdFromLiteralValue(op.left)];

switch (op.operation) {
case '=':
func_name = 'where';
args.push(op.right.value);
break;
case 'IS':
if (op.right.value == null) {
func_name = 'whereNull';
} else {
let args = [];

if (op.constructor.name == 'UnaryOp') {
if (op.operator == 'EXISTS') {
func_name = 'whereExists';
} else {
func_name = 'whereNotExists';
}

args.push(`function($query) {
${(new Builder(op.operand.select)).convert(7, false)}
}`)
} else {
args.push(this.parseQualfiedIdFromLiteralValue(op.left));

switch (op.operation) {
case '=':
func_name = 'where';
args.push(op.right.value);
}

break;
case 'IS NOT':
if (op.right.value == null) {
func_name = 'whereNotNull'
} else {
break;
case 'IS':
if (op.right.value == null) {
func_name = 'whereNull';
} else {
func_name = 'where';
args.push(op.right.value);
}

break;
case 'IS NOT':
if (op.right.value == null) {
func_name = 'whereNotNull'
} else {
func_name = 'where';
args.push(op.right.value);
}

break;
case 'IN':
func_name = 'whereIn';
args.push(op.right.value.map(v => v.value));

break;
case '<':
case '!=':
case '<>':
func_name = 'where';
args.push(op.operation);
args.push(op.right.value);
}

break;
case 'IN':
func_name = 'whereIn';
args.push(op.right.value.map(v => v.value));

break;
case '<':
case '!=':
case '<>':
func_name = 'where';
args.push(op.operation);
args.push(op.right.value);
break;
default:
throw new Error(`Operator ${op.operation} not supported.`)
break;
default:
throw new Error(`Operator ${op.operation} not supported.`)
}
}

return new Meta(this.formatFuncName(pre_operation, func_name), args);
Expand All @@ -283,23 +292,25 @@ class Builder {
if (literal.nested == false) {
qualfied_id = literal.value;
} else {
qualfied_id = `${this.alias_table_map[literal.values[0]]}.${literal.values[1]}`;
qualfied_id = `${this.alias_table_map[literal.values[0]] ? this.alias_table_map[literal.values[0]] : literal.values[0]}.${literal.values[1]}`;
}

return qualfied_id;
}

convert() {
convert(tab_count = 1, add_get = true) {
this.source();
this.joins();
this.where(this.generator, this.input.where !== null ? this.input.where.conditions : null)
this.select();
this.orderBy();
this.groupBy();

this.generator.addFunction('get');
if (add_get) {
this.generator.addFunction('get');
}

return this.generator.generate();
return this.generator.generate(tab_count);
}
}

Expand Down
4 changes: 2 additions & 2 deletions js/sql-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@

SUB_SELECT_OP = ['IN', 'NOT IN', 'ANY', 'ALL', 'SOME'];

SUB_SELECT_UNARY_OP = ['EXISTS'];
SUB_SELECT_UNARY_OP = ['EXISTS', 'NOT EXISTS'];

SQL_CONDITIONALS = ['AND', 'OR'];

Expand Down Expand Up @@ -1241,4 +1241,4 @@ if (typeof module !== 'undefined' && require.main === module) {
if(typeof define === 'function' && define.amd) {
define(function() { return SQLParser });
} else { root.SQLParser = SQLParser }
}(this));
}(this));