@jakub.knejzlik/ts-query
Version:
TypeScript implementation of SQL builder
63 lines (62 loc) • 2.33 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateTableAsSelect = void 0;
const Query_1 = require("./Query");
const compression_1 = require("./compression");
const mysql_1 = require("./flavors/mysql");
const interfaces_1 = require("./interfaces");
class CreateTableAsSelect {
constructor(_tableName, _select, _orReplace = false, _ifNotExists = false) {
this._tableName = _tableName;
this._select = _select;
this._orReplace = _orReplace;
this._ifNotExists = _ifNotExists;
if (this._orReplace && this._ifNotExists) {
throw new Error("CreateTableAsSelect: orReplace and ifNotExists are mutually exclusive");
}
}
clone() {
return new this.constructor(this._tableName, this._select.clone(), this._orReplace, this._ifNotExists);
}
getOperationType() {
return interfaces_1.MetadataOperationType.CREATE_TABLE_AS;
}
getTableNames() {
return [this._tableName, ...this._select.getTableNames()];
}
toSQL(flavor = new mysql_1.MySQLFlavor()) {
const orReplaceStr = this._orReplace ? "OR REPLACE " : "";
const ifNotExistsStr = this._ifNotExists ? "IF NOT EXISTS " : "";
return `CREATE ${orReplaceStr}TABLE ${ifNotExistsStr}${flavor.escapeTable(this._tableName)} AS ${this._select.toSQL(flavor)}`;
}
// serialization
serialize(opts = { compress: false }) {
const json = JSON.stringify(this.toJSON());
return opts.compress ? (0, compression_1.compressString)(json) : json;
}
toJSON() {
return {
type: interfaces_1.OperationType.CREATE_TABLE_AS,
select: this._select.toJSON(),
tableName: this._tableName,
orReplace: this._orReplace,
ifNotExists: this._ifNotExists,
};
}
static fromJSON({ tableName, select, orReplace, ifNotExists, }) {
return new CreateTableAsSelect(tableName, Query_1.SelectQuery.fromJSON(select), orReplace, ifNotExists);
}
getTableName() {
return this._tableName;
}
getSelect() {
return this._select;
}
getOrReplace() {
return this._orReplace;
}
getIfNotExists() {
return this._ifNotExists;
}
}
exports.CreateTableAsSelect = CreateTableAsSelect;