plywood
Version:
A query planner and executor
165 lines (164 loc) • 6.51 kB
JavaScript
var SQLDialect = (function () {
function SQLDialect() {
this.escapedTableName = null;
}
SQLDialect.prototype.setTable = function (name) {
if (name) {
this.escapedTableName = name.length === 1 ? name : this.escapeName(name);
}
else {
this.escapedTableName = null;
}
};
SQLDialect.prototype.nullConstant = function () {
return 'NULL';
};
SQLDialect.prototype.emptyGroupBy = function () {
return "GROUP BY ''";
};
SQLDialect.prototype.escapeName = function (name) {
name = name.replace(/"/g, '""');
return '"' + name + '"';
};
SQLDialect.prototype.maybeNamespacedName = function (name) {
var escapedName = this.escapeName(name);
if (this.escapedTableName) {
return this.escapedTableName + '.' + escapedName;
}
else {
return escapedName;
}
};
SQLDialect.prototype.escapeLiteral = function (name) {
if (name === null)
return this.nullConstant();
name = name.replace(/'/g, "''");
return "'" + name + "'";
};
SQLDialect.prototype.booleanToSQL = function (bool) {
return ('' + bool).toUpperCase();
};
SQLDialect.prototype.ipToSQL = function (ip) {
return ip.toString();
};
SQLDialect.prototype.floatDivision = function (numerator, denominator) {
return "(".concat(numerator, "/").concat(denominator, ")");
};
SQLDialect.prototype.numberOrTimeToSQL = function (x) {
if (x === null)
return this.nullConstant();
if (x.toISOString) {
return this.timeToSQL(x);
}
else {
return this.numberToSQL(x);
}
};
SQLDialect.prototype.numberToSQL = function (num) {
if (num === null)
return this.nullConstant();
return '' + num;
};
SQLDialect.prototype.dateToSQLDateString = function (date) {
return date
.toISOString()
.replace('T', ' ')
.replace('Z', '')
.replace(/\.000$/, '')
.replace(/ 00:00:00$/, '');
};
SQLDialect.prototype.aggregateFilterIfNeeded = function (inputSQL, expressionSQL, elseSQL) {
if (elseSQL === void 0) { elseSQL = null; }
var whereIndex = inputSQL.indexOf(' WHERE ');
if (whereIndex === -1)
return expressionSQL;
var filterSQL = inputSQL.substr(whereIndex + 7);
return this.ifThenElseExpression(filterSQL, expressionSQL, elseSQL);
};
SQLDialect.prototype.concatExpression = function (_a, _b) {
throw new Error('must implement');
};
SQLDialect.prototype.containsExpression = function (_a, _b, _insensitive) {
throw new Error('must implement');
};
SQLDialect.prototype.mvContainsExpression = function (_a, _b) {
throw new Error('must implement');
};
SQLDialect.prototype.mvFilterOnlyExpression = function (_a, _b) {
throw new Error('must implement');
};
SQLDialect.prototype.mvOverlapExpression = function (_a, _b) {
throw new Error('must implement');
};
SQLDialect.prototype.substrExpression = function (a, position, length) {
return "SUBSTR(".concat(a, ",").concat(position + 1, ",").concat(length, ")");
};
SQLDialect.prototype.coalesceExpression = function (a, b) {
return "COALESCE(".concat(a, ", ").concat(b, ")");
};
SQLDialect.prototype.countDistinctExpression = function (a, _parameterAttributeName) {
return "COUNT(DISTINCT ".concat(a, ")");
};
SQLDialect.prototype.ifThenElseExpression = function (a, b, c) {
var elsePart = typeof c === 'string' ? " ELSE ".concat(c) : '';
return "CASE WHEN ".concat(a, " THEN ").concat(b).concat(elsePart, " END");
};
SQLDialect.prototype.filterAggregatorExpression = function (aggregate, whereFilter) {
var whereIndex = whereFilter.indexOf('WHERE');
return "".concat(aggregate).concat(whereIndex !== -1 ? "FILTER (".concat(whereFilter.substr(whereIndex), ")") : '');
};
SQLDialect.prototype.isNotDistinctFromExpression = function (a, b) {
var nullConst = this.nullConstant();
if (a === nullConst)
return "".concat(b, " IS ").concat(nullConst);
if (b === nullConst)
return "".concat(a, " IS ").concat(nullConst);
return "(".concat(a, " IS NOT DISTINCT FROM ").concat(b, ")");
};
SQLDialect.prototype.regexpExpression = function (expression, regexp) {
return "(".concat(expression, " REGEXP ").concat(this.escapeLiteral(regexp), ")");
};
SQLDialect.prototype.inExpression = function (operand, start, end, bounds) {
if (start === end && bounds === '[]')
return "".concat(operand, "=").concat(start);
var startSQL = null;
if (start !== this.nullConstant()) {
startSQL = start + (bounds[0] === '[' ? '<=' : '<') + operand;
}
var endSQL = null;
if (end !== this.nullConstant()) {
endSQL = operand + (bounds[1] === ']' ? '<=' : '<') + end;
}
if (startSQL) {
return endSQL ? "(".concat(startSQL, " AND ").concat(endSQL, ")") : startSQL;
}
else {
return endSQL ? endSQL : 'TRUE';
}
};
SQLDialect.prototype.lengthExpression = function (a) {
return "CHAR_LENGTH(".concat(a, ")");
};
SQLDialect.prototype.quantileExpression = function (_str, _quantile, _parameterAttributeName) {
throw new Error('dialect does not implement quantile');
};
SQLDialect.prototype.logExpression = function (base, operand) {
if (base === String(Math.E))
return "LN(".concat(operand, ")");
return "LOG(".concat(base, ",").concat(operand, ")");
};
SQLDialect.prototype.lookupExpression = function (_base, _lookup) {
throw new Error('can not express a lookup as a function');
};
SQLDialect.prototype.ipMatchExpression = function (_colName, _searchString, _ipSearchType) {
throw new Error('must implement');
};
SQLDialect.prototype.ipSearchExpression = function (_colName, _searchString, _ipSearchType) {
throw new Error('must implement');
};
SQLDialect.prototype.ipStringifyExpression = function (_operand) {
throw new Error('must implement');
};
return SQLDialect;
}());
export { SQLDialect };