rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
56 lines • 2.54 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateTableQuery = void 0;
const SqlComponent_1 = require("./SqlComponent");
const ValueComponent_1 = require("./ValueComponent");
const SimpleSelectQuery_1 = require("./SimpleSelectQuery");
const Clause_1 = require("./Clause");
const SelectValueCollector_1 = require("../transformers/SelectValueCollector");
// Represents a CREATE TABLE query model
// Supports temporary tables and AS SELECT ...
class CreateTableQuery extends SqlComponent_1.SqlComponent {
constructor(params) {
var _a;
super();
this.tableName = new ValueComponent_1.IdentifierString(params.tableName);
this.isTemporary = (_a = params.isTemporary) !== null && _a !== void 0 ? _a : false;
this.asSelectQuery = params.asSelectQuery;
}
/**
* Returns a SelectQuery that selects all columns from this table.
*/
getSelectQuery() {
let selectItems;
if (this.asSelectQuery) {
// Use SelectValueCollector to get columns from asSelectQuery
const collector = new SelectValueCollector_1.SelectValueCollector();
const values = collector.collect(this.asSelectQuery);
selectItems = values.map(val => new Clause_1.SelectItem(val.value, val.name));
}
else {
// fallback: wildcard
selectItems = [new Clause_1.SelectItem(new ValueComponent_1.RawString("*"))];
}
return new SimpleSelectQuery_1.SimpleSelectQuery({
selectClause: new Clause_1.SelectClause(selectItems),
fromClause: new Clause_1.FromClause(new Clause_1.SourceExpression(new Clause_1.TableSource(null, this.tableName.name), null), null // joins
),
});
}
/**
* Returns a SelectQuery that counts all rows in this table.
*/
getCountQuery() {
return new SimpleSelectQuery_1.SimpleSelectQuery({
selectClause: new Clause_1.SelectClause([
new Clause_1.SelectItem(new ValueComponent_1.FunctionCall(null, "count", new ValueComponent_1.ColumnReference(null, "*"), null))
]),
fromClause: new Clause_1.FromClause(new Clause_1.SourceExpression(new Clause_1.TableSource(null, this.tableName.name), null), null // joins
),
});
}
}
exports.CreateTableQuery = CreateTableQuery;
/** SqlComponent kind symbol for visitor pattern */
CreateTableQuery.kind = Symbol("CreateTableQuery");
//# sourceMappingURL=CreateTableQuery.js.map
;