UNPKG

@mas-soft/mas-core-server

Version:

main application

70 lines 2.59 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var assert = require('assert'); const PLACEHOLDER = '?'; class ParameterizedSQL { constructor(sql, params) { this.toJSON = function () { return { sql: this.sql, params: this.params, }; }; if (!(this instanceof ParameterizedSQL)) { return new ParameterizedSQL(sql, params); } sql = sql || ''; if (arguments.length === 1 && typeof sql === 'object') { this.sql = sql.sql; this.params = sql.params || []; } else { this.sql = sql != null ? sql.toString() : ''; this.params = params || []; } assert(typeof this.sql === 'string', 'sql must be a string'); assert(Array.isArray(this.params), 'params must be an array'); let parts = this.sql.split(PLACEHOLDER); assert(parts.length - 1 === this.params.length, 'The number of ? (' + (parts.length - 1) + ') in the sql (' + this.sql + ') must match the number of params (' + this.params.length + ') ' + this.params); } merge(ps, separator) { if (Array.isArray(ps)) { return ParameterizedSQL.append(this, ParameterizedSQL.join(ps, separator), separator); } else { return ParameterizedSQL.append(this, ps, separator); } } ; static append(currentStmt, stmt, separator) { currentStmt = (currentStmt instanceof ParameterizedSQL) ? currentStmt : new ParameterizedSQL(currentStmt); stmt = (stmt instanceof ParameterizedSQL) ? stmt : new ParameterizedSQL(stmt); separator = typeof separator === 'string' ? separator : ' '; if (currentStmt.sql) { currentStmt.sql += separator; } if (stmt.sql) { currentStmt.sql += stmt.sql; } currentStmt.params = (currentStmt.params || []).concat(stmt.params || []); return currentStmt; } ; static join(sqls, separator) { assert(Array.isArray(sqls), 'sqls must be an array'); let ps = new ParameterizedSQL('', []); for (var i = 0, n = sqls.length; i < n; i++) { this.append(ps, sqls[i], separator); } return ps; } ; } ParameterizedSQL.PLACEHOLDER = PLACEHOLDER; exports.ParameterizedSQL = ParameterizedSQL; //# sourceMappingURL=parametrized-sql.js.map