dbgate/packages/engines/default/SqlDumper.js

65 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-02-02 18:27:25 +00:00
class SqlDumper {
2020-02-03 18:52:02 +00:00
/** @param driver {import('@dbgate/types').EngineDriver} */
2020-02-02 18:27:25 +00:00
constructor(driver) {
this.s = '';
this.driver = driver;
this.dialect = driver.dialect;
}
putRaw(text) {
this.s += text;
}
2020-02-03 19:34:38 +00:00
putCmd(format, ...args) {
this.put(format, ...args);
2020-02-02 18:27:25 +00:00
this.putRaw(';\n');
}
putFormattedValue(c, value) {
switch (c) {
case 's':
if (value != null) {
this.putRaw(value.toString());
}
break;
case 'f':
{
const { schemaName, pureName } = value;
if (schemaName) {
this.putRaw(this.dialect.quoteIdentifier(schemaName));
this.putRaw('.');
}
this.putRaw(this.dialect.quoteIdentifier(pureName));
}
break;
}
}
/** @param format {string} */
put(format, ...args) {
let i = 0;
let argIndex = 0;
const length = format.length;
while (i < length) {
let c = format[i];
i++;
switch (c) {
case '^':
while (i < length && format[i].match(/[a-z]/i)) {
this.putRaw(format[i].toUpperCase());
i++;
}
break;
case '%':
c = format[i];
i++;
this.putFormattedValue(c, args[argIndex]);
argIndex++;
break;
default:
this.putRaw(c);
break;
}
}
}
}
module.exports = SqlDumper;