lite-ui-sql-parser
Version:
simple node sql parser
34 lines (32 loc) • 1.11 kB
JavaScript
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["exports", "./expr"], factory);
} else if (typeof exports !== "undefined") {
factory(exports, require("./expr"));
} else {
var mod = {
exports: {}
};
factory(mod.exports, global.expr);
global._with = mod.exports;
}
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _expr) {
"use strict";
Object.defineProperty(_exports, "__esModule", {
value: true
});
_exports.withToSql = withToSql;
/**
* @param {Array<Object>} withExpr
*/
function withToSql(withExpr) {
if (!withExpr || withExpr.length === 0) return;
const isRecursive = withExpr[0].recursive ? 'RECURSIVE ' : '';
const withExprStr = withExpr.map(cte => {
const name = `"${cte.name}"`;
const columns = Array.isArray(cte.columns) ? `(${cte.columns.join(', ')})` : '';
return `${name}${columns} AS (${(0, _expr.exprToSQL)(cte.stmt)})`;
}).join(', ');
return `WITH ${isRecursive}${withExprStr}`;
}
});