rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
52 lines • 2.24 kB
JavaScript
import { SqlComponent } from "./SqlComponent";
import { ColumnReference, FunctionCall, IdentifierString, RawString } from "./ValueComponent";
import { SimpleSelectQuery } from "./SimpleSelectQuery";
import { SelectClause, SelectItem, FromClause, TableSource, SourceExpression } from "./Clause";
import { SelectValueCollector } from "../transformers/SelectValueCollector";
// Represents a CREATE TABLE query model
// Supports temporary tables and AS SELECT ...
export class CreateTableQuery extends SqlComponent {
constructor(params) {
var _a;
super();
this.tableName = new 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();
const values = collector.collect(this.asSelectQuery);
selectItems = values.map(val => new SelectItem(val.value, val.name));
}
else {
// fallback: wildcard
selectItems = [new SelectItem(new RawString("*"))];
}
return new SimpleSelectQuery({
selectClause: new SelectClause(selectItems),
fromClause: new FromClause(new SourceExpression(new TableSource(null, this.tableName.name), null), null // joins
),
});
}
/**
* Returns a SelectQuery that counts all rows in this table.
*/
getCountQuery() {
return new SimpleSelectQuery({
selectClause: new SelectClause([
new SelectItem(new FunctionCall(null, "count", new ColumnReference(null, "*"), null))
]),
fromClause: new FromClause(new SourceExpression(new TableSource(null, this.tableName.name), null), null // joins
),
});
}
}
/** SqlComponent kind symbol for visitor pattern */
CreateTableQuery.kind = Symbol("CreateTableQuery");
//# sourceMappingURL=CreateTableQuery.js.map