sql-parser-mistic
Version:
Lexer and Parser for SQL Syntax
558 lines (438 loc) • 13.1 kB
JavaScript
// Generated by CoffeeScript 1.8.0
(function() {
var ArgumentListValue, BetweenOp, Case, CaseElse, CaseWhen, Field, FunctionValue, Group, Having, Join, Limit, ListValue, LiteralValue, NumberValue, Offset, Op, Order, OrderArgument, ParameterValue, Select, Star, StringValue, SubSelect, Table, UnaryOp, Union, Where, WhitepaceList, indent;
indent = function(str) {
var line;
return ((function() {
var _i, _len, _ref, _results;
_ref = str.split("\n");
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
line = _ref[_i];
_results.push(" " + line);
}
return _results;
})()).join("\n");
};
exports.Select = Select = (function() {
function Select(fields, source, distinct, joins, unions) {
this.fields = fields;
this.source = source;
this.distinct = distinct != null ? distinct : false;
this.joins = joins != null ? joins : [];
this.unions = unions != null ? unions : [];
this.order = null;
this.group = null;
this.where = null;
this.limit = null;
}
Select.prototype.toString = function() {
var join, ret, union, _i, _j, _len, _len1, _ref, _ref1;
ret = ["SELECT " + (this.fields.join(', '))];
ret.push(indent("FROM " + this.source));
_ref = this.joins;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
join = _ref[_i];
ret.push(indent(join.toString()));
}
if (this.where) {
ret.push(indent(this.where.toString()));
}
if (this.group) {
ret.push(indent(this.group.toString()));
}
if (this.order) {
ret.push(indent(this.order.toString()));
}
if (this.limit) {
ret.push(indent(this.limit.toString()));
}
_ref1 = this.unions;
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
union = _ref1[_j];
ret.push(union.toString());
}
return ret.join("\n");
};
return Select;
})();
exports.SubSelect = SubSelect = (function() {
function SubSelect(select, name) {
this.select = select;
this.name = name != null ? name : null;
null;
}
SubSelect.prototype.toString = function() {
var ret;
ret = [];
ret.push('(');
ret.push(indent(this.select.toString()));
ret.push(this.name ? ") " + (this.name.toString()) : ")");
return ret.join("\n");
};
return SubSelect;
})();
exports.Join = Join = (function() {
function Join(right, conditions, side, mode) {
this.right = right;
this.conditions = conditions != null ? conditions : null;
this.side = side != null ? side : null;
this.mode = mode != null ? mode : null;
null;
}
Join.prototype.toString = function() {
var ret;
ret = '';
if (this.side != null) {
ret += "" + this.side + " ";
}
if (this.mode != null) {
ret += "" + this.mode + " ";
}
return ret + ("JOIN " + this.right + "\n") + indent("ON " + this.conditions);
};
return Join;
})();
exports.Union = Union = (function() {
function Union(query, all) {
this.query = query;
this.all = all != null ? all : false;
null;
}
Union.prototype.toString = function() {
var all;
all = this.all ? ' ALL' : '';
return "UNION" + all + "\n" + (this.query.toString());
};
return Union;
})();
exports.LiteralValue = LiteralValue = (function() {
function LiteralValue(value, value2) {
this.value = value;
this.value2 = value2 != null ? value2 : null;
if (this.value2) {
this.nested = true;
this.values = this.value.values;
this.values.push(value2);
} else {
this.nested = false;
this.values = [this.value];
}
}
LiteralValue.prototype.toString = function(quote) {
if (quote == null) {
quote = true;
}
if (quote) {
return "`" + (this.values.join('`.`')) + "`";
} else {
return "" + (this.values.join('.'));
}
};
return LiteralValue;
})();
exports.StringValue = StringValue = (function() {
function StringValue(value, quoteType) {
this.value = value;
this.quoteType = quoteType != null ? quoteType : "''";
null;
}
StringValue.prototype.toString = function() {
var escaped;
escaped = this.quoteType === "'" ? this.value.replace(/(^|[^\\])'/g, "$1''") : this.value;
return "" + this.quoteType + escaped + this.quoteType;
};
return StringValue;
})();
exports.NumberValue = NumberValue = (function() {
function NumberValue(value) {
this.value = Number(value);
}
NumberValue.prototype.toString = function() {
return this.value.toString();
};
return NumberValue;
})();
exports.ListValue = ListValue = (function() {
function ListValue(value) {
this.value = value;
null;
}
ListValue.prototype.toString = function() {
return "(" + (this.value.join(', ')) + ")";
};
return ListValue;
})();
exports.WhitepaceList = WhitepaceList = (function() {
function WhitepaceList(value) {
this.value = value;
null;
}
WhitepaceList.prototype.toString = function() {
return this.value.map(function(value) {
if (value instanceof exports.LiteralValue) {
return value.toString(false);
} else {
return value.toString();
}
}).join(' ');
};
return WhitepaceList;
})();
exports.ParameterValue = ParameterValue = (function() {
function ParameterValue(value) {
this.value = value;
this.index = parseInt(value.substr(1), 10) - 1;
}
ParameterValue.prototype.toString = function() {
return "$" + this.value;
};
return ParameterValue;
})();
exports.ArgumentListValue = ArgumentListValue = (function() {
function ArgumentListValue(value, distinct) {
this.value = value;
this.distinct = distinct != null ? distinct : false;
null;
}
ArgumentListValue.prototype.toString = function() {
if (this.distinct) {
return "DISTINCT " + (this.value.join(', '));
} else {
return "" + (this.value.join(', '));
}
};
return ArgumentListValue;
})();
exports.BooleanValue = LiteralValue = (function() {
function LiteralValue(value) {
this.value = (function() {
switch (value.toLowerCase()) {
case 'true':
return true;
case 'false':
return false;
default:
return null;
}
})();
}
LiteralValue.prototype.toString = function() {
if (this.value != null) {
return this.value.toString().toUpperCase();
} else {
return 'NULL';
}
};
return LiteralValue;
})();
exports.FunctionValue = FunctionValue = (function() {
function FunctionValue(name, _arguments, udf) {
this.name = name;
this["arguments"] = _arguments != null ? _arguments : null;
this.udf = udf != null ? udf : false;
null;
}
FunctionValue.prototype.toString = function() {
if (this["arguments"]) {
return "" + (this.name.toUpperCase()) + "(" + (this["arguments"].toString()) + ")";
} else {
return "" + (this.name.toUpperCase()) + "()";
}
};
return FunctionValue;
})();
exports.Case = Case = (function() {
function Case(whens, _else) {
this.whens = whens;
this["else"] = _else;
}
Case.prototype.toString = function() {
var whensStr;
whensStr = this.whens.map(function(w) {
return w.toString();
}).join(' ');
if (this["else"]) {
return "CASE " + whensStr + " " + (this["else"].toString()) + " END";
} else {
return "CASE " + whensStr + " END";
}
};
return Case;
})();
exports.CaseWhen = CaseWhen = (function() {
function CaseWhen(whenCondition, resCondition) {
this.whenCondition = whenCondition;
this.resCondition = resCondition;
}
CaseWhen.prototype.toString = function() {
return "WHEN " + this.whenCondition + " THEN " + this.resCondition;
};
return CaseWhen;
})();
exports.CaseElse = CaseElse = (function() {
function CaseElse(elseCondition) {
this.elseCondition = elseCondition;
}
CaseElse.prototype.toString = function() {
return "ELSE " + this.elseCondition;
};
return CaseElse;
})();
exports.Order = Order = (function() {
function Order(orderings, offset) {
this.orderings = orderings;
this.offset = offset;
}
Order.prototype.toString = function() {
return ("ORDER BY " + (this.orderings.join(', '))) + (this.offset ? "\n" + this.offset.toString() : "");
};
return Order;
})();
exports.OrderArgument = OrderArgument = (function() {
function OrderArgument(value, direction) {
this.value = value;
this.direction = direction != null ? direction : 'ASC';
null;
}
OrderArgument.prototype.toString = function() {
return "" + this.value + " " + this.direction;
};
return OrderArgument;
})();
exports.Offset = Offset = (function() {
function Offset(row_count, limit) {
this.row_count = row_count;
this.limit = limit;
null;
}
Offset.prototype.toString = function() {
return ("OFFSET " + this.row_count + " ROWS") + (this.limit ? "\nFETCH NEXT " + this.limit + " ROWS ONLY" : "");
};
return Offset;
})();
exports.Limit = Limit = (function() {
function Limit(value, offset) {
this.value = value;
this.offset = offset;
null;
}
Limit.prototype.toString = function() {
return ("LIMIT " + this.value) + (this.offset ? "\nOFFSET " + this.offset : "");
};
return Limit;
})();
exports.Table = Table = (function() {
function Table(name, alias, win, winFn, winArg) {
this.name = name;
this.alias = alias != null ? alias : null;
this.win = win != null ? win : null;
this.winFn = winFn != null ? winFn : null;
this.winArg = winArg != null ? winArg : null;
null;
}
Table.prototype.toString = function() {
if (this.win) {
return "" + this.name + "." + this.win + ":" + this.winFn + "(" + this.winArg + ")";
} else if (this.alias) {
return "" + this.name + " AS " + this.alias;
} else {
return this.name.toString();
}
};
return Table;
})();
exports.Group = Group = (function() {
function Group(fields) {
this.fields = fields;
this.having = null;
}
Group.prototype.toString = function() {
var ret;
ret = ["GROUP BY " + (this.fields.join(', '))];
if (this.having) {
ret.push(this.having.toString());
}
return ret.join("\n");
};
return Group;
})();
exports.Where = Where = (function() {
function Where(conditions) {
this.conditions = conditions;
null;
}
Where.prototype.toString = function() {
return "WHERE " + this.conditions;
};
return Where;
})();
exports.Having = Having = (function() {
function Having(conditions) {
this.conditions = conditions;
null;
}
Having.prototype.toString = function() {
return "HAVING " + this.conditions;
};
return Having;
})();
exports.Op = Op = (function() {
function Op(operation, left, right) {
this.operation = operation;
this.left = left;
this.right = right;
null;
}
Op.prototype.toString = function() {
return "(" + this.left + " " + (this.operation.toUpperCase()) + " " + this.right + ")";
};
return Op;
})();
exports.UnaryOp = UnaryOp = (function() {
function UnaryOp(operator, operand) {
this.operator = operator;
this.operand = operand;
null;
}
UnaryOp.prototype.toString = function() {
return "(" + (this.operator.toUpperCase()) + " " + this.operand + ")";
};
return UnaryOp;
})();
exports.BetweenOp = BetweenOp = (function() {
function BetweenOp(value) {
this.value = value;
null;
}
BetweenOp.prototype.toString = function() {
return "" + (this.value.join(' AND '));
};
return BetweenOp;
})();
exports.Field = Field = (function() {
function Field(field, name) {
this.field = field;
this.name = name != null ? name : null;
null;
}
Field.prototype.toString = function() {
if (this.name) {
return "" + this.field + " AS " + this.name;
} else {
return this.field.toString();
}
};
return Field;
})();
exports.Star = Star = (function() {
function Star() {
null;
}
Star.prototype.toString = function() {
return '*';
};
Star.prototype.star = true;
return Star;
})();
}).call(this);