UNPKG

@vulcan-sql/extension-driver-bq

Version:

BigQuery driver for Vulcan SQL

42 lines 1.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildSQL = exports.isNoOP = exports.addOffset = exports.addLimit = exports.removeEndingSemiColon = void 0; const lodash_1 = require("lodash"); const isNullOrUndefine = (value) => (0, lodash_1.isUndefined)(value) || (0, lodash_1.isNull)(value); const removeEndingSemiColon = (sql) => { return sql.replace(/;([ \n]+)?$/, ''); }; exports.removeEndingSemiColon = removeEndingSemiColon; const addLimit = (sql, limit) => { if (isNullOrUndefine(limit)) return sql; return [sql, `LIMIT`, limit].join(' '); }; exports.addLimit = addLimit; const addOffset = (sql, offset) => { if (isNullOrUndefine(offset)) return sql; return [sql, `OFFSET`, offset].join(' '); }; exports.addOffset = addOffset; // Check if there is no operations const isNoOP = (operations) => { if (!isNullOrUndefine(operations.limit)) return false; if (!isNullOrUndefine(operations.offset)) return false; return true; }; exports.isNoOP = isNoOP; const buildSQL = (sql, operations) => { if ((0, exports.isNoOP)(operations)) return sql; let builtSQL = ''; builtSQL += `SELECT * FROM (${(0, exports.removeEndingSemiColon)(sql)})`; builtSQL = (0, exports.addLimit)(builtSQL, operations.limit); builtSQL = (0, exports.addOffset)(builtSQL, operations.offset); builtSQL += ';'; return builtSQL; }; exports.buildSQL = buildSQL; //# sourceMappingURL=bqlSqlBuilder.js.map