UNPKG

lite-ui-sql-parser

Version:
65 lines (59 loc) 2.17 kB
(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports", "./tables", "./expr", "./util", "./select"], factory); } else if (typeof exports !== "undefined") { factory(exports, require("./tables"), require("./expr"), require("./util"), require("./select")); } else { var mod = { exports: {} }; factory(mod.exports, global.tables, global.expr, global.util, global.select); global.insert = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _tables, _expr, _util, _select) { "use strict"; Object.defineProperty(_exports, "__esModule", { value: true }); _exports.insertToSQL = insertToSQL; /** * @param {Array} values * @return {string} */ function valuesToSQL(values) { if (values.type === 'select') return (0, _select.selectToSQL)(values); const clauses = values.map(_expr.exprToSQL); return `(${clauses.join('),(')})`; } function partitionToSQL(partition) { if (!partition) return ''; const partitionArr = ['PARTITION', '(']; if (Array.isArray(partition)) { partitionArr.push(partition.map(_util.identifierToSql).join(', ')); } else { const { value } = partition; partitionArr.push(value.map(_expr.exprToSQL).join(', ')); } partitionArr.push(')'); return partitionArr.filter(_util.hasVal).join(''); } function insertToSQL(stmt) { const { table, prefix = 'into', columns, values, where, partition, returning } = stmt; const clauses = ['INSERT', (0, _util.toUpper)(prefix), (0, _tables.tablesToSQL)(table), partitionToSQL(partition)]; if (Array.isArray(columns)) clauses.push(`(${columns.map(_util.identifierToSql).join(', ')})`); clauses.push((0, _util.commonOptionConnector)(Array.isArray(values) ? 'VALUES' : '', valuesToSQL, values)); clauses.push((0, _util.commonOptionConnector)('WHERE', _expr.exprToSQL, where)); clauses.push((0, _util.returningToSQL)(returning)); return clauses.filter(_util.hasVal).join(' '); } });