@riao/dbal
Version:
158 lines • 4.41 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SqlBuilder = void 0;
const builder_1 = require("./builder");
class SqlBuilder extends builder_1.Builder {
constructor() {
super(...arguments);
this.sql = '';
this.params = [];
this.usePlaceholders = true;
this.operators = {
closeParens: ')',
openParens: '(',
endStatement: ';',
equals: '=',
like: 'LIKE',
notEqual: '!=',
negate: '!',
lt: '<',
lte: '<=',
gt: '>',
gte: '>=',
and: 'AND',
or: 'OR',
null: 'NULL',
is: 'IS',
in: 'IN',
between: 'BETWEEN',
openEnclosure: '"',
closeEnclosure: '"',
addition: '+',
subtraction: '-',
multiplication: '*',
division: '/',
modulo: '%',
};
}
toDatabaseQuery() {
return {
sql: this.sql.trim(),
params: this.params,
};
}
// ------------------------------------------------------------------------
// SQL utility functions
// ------------------------------------------------------------------------
append(str) {
this.sql += str;
return this;
}
appendParams(params) {
this.params = this.params.concat(params);
}
space() {
this.sql += ' ';
return this;
}
commaSeparate(strings) {
this.sql += strings.join(', ');
return this;
}
openParens() {
this.sql += this.operators.openParens;
return this;
}
closeParens() {
this.sql = this.sql.trimEnd();
this.sql += this.operators.closeParens + ' ';
return this;
}
trimEnd(s) {
this.sql = this.sql.trimEnd();
if (s) {
s = s.trimEnd();
this.sql = this.sql.substring(0, this.sql.length - s.length);
}
return this;
}
endStatement() {
this.sql += this.operators.endStatement + ' ';
return this;
}
// ------------------------------------------------------------------------
// Column/Table reference enclosure
// ------------------------------------------------------------------------
encloseString(str) {
if (!str) {
return str;
}
return (this.operators.openEnclosure + str + this.operators.closeEnclosure);
}
getEnclosedName(str) {
if (str.includes('.')) {
return str
.split('.')
.map((part) => this.encloseString(part))
.join('.');
}
else {
return this.encloseString(str);
}
}
tableName(name) {
this.sql += this.getEnclosedName(name);
return this;
}
columnName(name) {
this.sql += this.getEnclosedName(name);
return this;
}
// ------------------------------------------------------------------------
// Placeholders
// ------------------------------------------------------------------------
enablePlaceholders() {
this.usePlaceholders = true;
return this;
}
disablePlaceholders() {
this.usePlaceholders = false;
return this;
}
appendPlaceholder(value) {
this.sql += '? ';
return this;
}
placeholder(value) {
if (this.usePlaceholders) {
this.appendPlaceholder(value);
this.params.push(value);
}
else {
this.appendValue(value);
}
return this;
}
appendValue(value) {
if (value === null) {
this.appendNull();
}
else if (typeof value === 'string') {
this.appendStringValue(value);
}
else {
this.append(value);
}
return this;
}
appendNull() {
this.append(this.operators.null);
return this;
}
appendStringValue(value) {
this.append(`'${value}'`);
return this;
}
}
exports.SqlBuilder = SqlBuilder;
//# sourceMappingURL=sql-builder.js.map