UNPKG

lite-ui-sql-parser

Version:
70 lines (65 loc) 2.46 kB
(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports", "./expr", "./column", "./with", "./tables", "./util"], factory); } else if (typeof exports !== "undefined") { factory(exports, require("./expr"), require("./column"), require("./with"), require("./tables"), require("./util")); } else { var mod = { exports: {} }; factory(mod.exports, global.expr, global.column, global._with, global.tables, global.util); global.select = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _expr, _column, _with, _tables, _util) { "use strict"; Object.defineProperty(_exports, "__esModule", { value: true }); _exports.selectToSQL = selectToSQL; /** * @param {Object} stmt * @param {?Array} stmt.with * @param {?Array} stmt.options * @param {?string} stmt.distinct * @param {?Array|string} stmt.columns * @param {?Array} stmt.from * @param {?Object} stmt.where * @param {?Array} stmt.groupby * @param {?Object} stmt.having * @param {?Array} stmt.orderby * @param {?Array} stmt.limit * @return {string} */ function selectToSQL(stmt) { const { with: withInfo, options, distinct, columns, from, where, groupby, having, orderby, limit, top } = stmt; const clauses = [(0, _with.withToSql)(withInfo), 'SELECT']; clauses.push((0, _util.topToSQL)(top)); if (Array.isArray(options)) clauses.push(options.join(' ')); clauses.push(distinct, (0, _column.columnsToSQL)(columns, from)); // FROM + joins clauses.push((0, _util.commonOptionConnector)('FROM', _tables.tablesToSQL, from)); clauses.push((0, _util.commonOptionConnector)('WHERE', _expr.exprToSQL, where)); clauses.push((0, _util.connector)('GROUP BY', (0, _expr.getExprListSQL)(groupby).join(', '))); clauses.push((0, _util.commonOptionConnector)('HAVING', _expr.exprToSQL, having)); clauses.push((0, _expr.orderOrPartitionByToSQL)(orderby, 'order by')); if (limit) { const { seperator, value } = limit; clauses.push((0, _util.connector)('LIMIT', value.map(_expr.exprToSQL).join(`${seperator === 'offset' ? ' ' : ''}${seperator.toUpperCase()} `))); } return clauses.filter(_util.hasVal).join(' '); } });