expression parser

This commit is contained in:
Jan Prochazka
2020-03-12 12:46:07 +01:00
parent e2c5c8163c
commit 064121376f
15 changed files with 1306 additions and 100 deletions

View File

@@ -1,57 +1,83 @@
const _ = require('lodash');
const moment = require('moment');
class SqlDumper {
/** @param driver {import('@dbgate/types').EngineDriver} */
constructor(driver) {
this.s = "";
this.s = '';
this.driver = driver;
this.dialect = driver.dialect;
this.indentLevel = 0;
}
endCommand() {
this.putRaw(";\n");
this.putRaw(';\n');
}
putRaw(text) {
this.s += text;
}
escapeString(value) {
const esc = this.dialect.stringEscapeChar;
let res = '';
for (let i = 0; i < value.length; i++) {
const c = value[i];
if (c == esc || c == "'") {
res += esc;
}
res += c;
}
return res;
}
putStringValue(value) {
this.putRaw("'");
this.putRaw(this.escapeString(value));
this.putRaw("'");
}
putValue(value) {
if (_.isString(value)) this.putStringValue(value);
if (_.isNumber(value)) this.putRaw(value.toString());
if (_.isDate(value)) this.putStringValue(moment(value).toISOString());
}
putCmd(format, ...args) {
this.put(format, ...args);
this.endCommand();
}
putFormattedValue(c, value) {
switch (c) {
case "s":
case 's':
if (value != null) {
this.putRaw(value.toString());
}
break;
case "i":
case 'i':
{
this.putRaw(this.dialect.quoteIdentifier(value));
}
break;
case "k":
case 'k':
{
if (value) {
this.putRaw(value.toUpperCase());
}
}
break;
case "f":
case 'f':
{
const { schemaName, pureName } = value;
if (schemaName) {
this.putRaw(this.dialect.quoteIdentifier(schemaName));
this.putRaw(".");
this.putRaw('.');
}
this.putRaw(this.dialect.quoteIdentifier(pureName));
}
break;
case 'v':
this.putValue(value);
break;
}
}
putFormattedList(c, collection) {
if (!collection) return;
this.putCollection(", ", collection, item =>
this.putFormattedValue(c, item)
);
this.putCollection(', ', collection, item => this.putFormattedValue(c, item));
}
/** @param format {string} */
put(format, ...args) {
@@ -62,20 +88,20 @@ class SqlDumper {
let c = format[i];
i++;
switch (c) {
case "^":
case '^':
while (i < length && format[i].match(/[a-z0-9_]/i)) {
this.putRaw(format[i].toUpperCase());
i++;
}
break;
case "%":
case '%':
c = format[i];
i++;
switch (c) {
case "%":
this.putRaw("%");
case '%':
this.putRaw('%');
break;
case ",":
case ',':
c = format[i];
i++;
this.putFormattedList(c, args[argIndex]);
@@ -87,22 +113,22 @@ class SqlDumper {
argIndex++;
break;
case "&":
case '&':
c = format[i];
i++;
switch (c) {
case "&":
this.putRaw("&");
case '&':
this.putRaw('&');
break;
case ">":
case '>':
this.indentLevel++;
break;
case "<":
case '<':
this.indentLevel--;
break;
case "n":
this.putRaw("\n");
this.putRaw(" ".repeat(2 * this.indentLevel));
case 'n':
this.putRaw('\n');
this.putRaw(' '.repeat(2 * this.indentLevel));
break;
}
break;
@@ -114,36 +140,29 @@ class SqlDumper {
}
}
autoIncrement() {
this.put(" ^auto_increment");
this.put(' ^auto_increment');
}
/**
* @param column {import('@dbgate/types').ColumnInfo}
*/
columnDefinition(
column,
{
includeDefault = true,
includeNullable = true,
includeCollate = true
} = {}
) {
columnDefinition(column, { includeDefault = true, includeNullable = true, includeCollate = true } = {}) {
if (column.computedExpression) {
this.put("^as %s", column.computedExpression);
if (column.isPersisted) this.put(" ^persisted");
this.put('^as %s', column.computedExpression);
if (column.isPersisted) this.put(' ^persisted');
return;
}
this.put("%k", column.dataType);
this.put('%k', column.dataType);
if (column.autoIncrement) {
this.autoIncrement();
}
this.putRaw(" ");
this.putRaw(' ');
if (column.isSparse) {
this.put(" ^sparse ");
this.put(' ^sparse ');
}
if (includeNullable) {
this.put(column.notNull ? "^not ^null" : "^null");
this.put(column.notNull ? '^not ^null' : '^null');
}
if (includeDefault && column.defaultValue != null) {
this.columnDefault(column);
@@ -155,13 +174,9 @@ class SqlDumper {
*/
columnDefault(column) {
if (column.defaultConstraint != null) {
this.put(
" ^constraint %i ^default %s ",
column.defaultConstraint,
column.defaultValue
);
this.put(' ^constraint %i ^default %s ', column.defaultConstraint, column.defaultValue);
} else {
this.put(" ^default %s ", column.defaultValue);
this.put(' ^default %s ', column.defaultValue);
}
}
@@ -182,24 +197,24 @@ class SqlDumper {
}
/** @param table {import('@dbgate/types').TableInfo} */
createTable(table) {
this.put("^create ^table %f ( &>&n", table);
this.putCollection(",&n", table.columns, col => {
this.put("%i ", col.columnName);
this.put('^create ^table %f ( &>&n', table);
this.putCollection(',&n', table.columns, col => {
this.put('%i ', col.columnName);
this.columnDefinition(col);
});
if (table.primaryKey) {
this.put(",&n");
this.put(',&n');
if (table.primaryKey.constraintName) {
this.put("^constraint %i", table.primaryKey.constraintName);
this.put('^constraint %i', table.primaryKey.constraintName);
}
this.put(
" ^primary ^key (%,i)",
' ^primary ^key (%,i)',
table.primaryKey.columns.map(x => x.columnName)
);
}
if (table.foreignKeys) {
table.foreignKeys.forEach(fk => {
this.put(",&n");
this.put(',&n');
this.createForeignKeyFore(fk);
});
}
@@ -215,7 +230,7 @@ class SqlDumper {
// first = false;
// CreateCheckCore(cnt);
// }
this.put("&<&n)");
this.put('&<&n)');
this.endCommand();
// foreach (var ix in table.Indexes)
// {
@@ -225,16 +240,15 @@ class SqlDumper {
/** @param fk {import('@dbgate/types').ForeignKeyInfo} */
createForeignKeyFore(fk) {
if (fk.constraintName != null)
this.put("^constraint %i ", fk.constraintName);
if (fk.constraintName != null) this.put('^constraint %i ', fk.constraintName);
this.put(
"^foreign ^key (%,i) ^references %f (%,i)",
'^foreign ^key (%,i) ^references %f (%,i)',
fk.columns.map(x => x.columnName),
{ schemaName: fk.refSchemaName, pureName: fk.refTableName },
fk.columns.map(x => x.refColumnName)
);
if (fk.deleteAction) this.put(" ^on ^delete %k", fk.deleteAction);
if (fk.updateAction) this.put(" ^on ^update %k", fk.updateAction);
if (fk.deleteAction) this.put(' ^on ^delete %k', fk.deleteAction);
if (fk.updateAction) this.put(' ^on ^update %k', fk.updateAction);
}
}