UNPKG

rawsql-ts

Version:

High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.

127 lines 5.87 kB
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 REFERENCES clause definition that can be shared between column and table constraints. */ export class ReferenceDefinition extends SqlComponent { constructor(params) { var _a, _b, _c, _d, _e; super(); this.targetTable = params.targetTable; this.columns = params.columns ? [...params.columns] : null; this.matchType = (_a = params.matchType) !== null && _a !== void 0 ? _a : null; this.onDelete = (_b = params.onDelete) !== null && _b !== void 0 ? _b : null; this.onUpdate = (_c = params.onUpdate) !== null && _c !== void 0 ? _c : null; this.deferrable = (_d = params.deferrable) !== null && _d !== void 0 ? _d : null; this.initially = (_e = params.initially) !== null && _e !== void 0 ? _e : null; } } ReferenceDefinition.kind = Symbol("ReferenceDefinition"); /** * Column-level constraint definition. */ export class ColumnConstraintDefinition extends SqlComponent { constructor(params) { super(); this.kind = params.kind; this.constraintName = params.constraintName; this.defaultValue = params.defaultValue; this.checkExpression = params.checkExpression; this.reference = params.reference; this.rawClause = params.rawClause; } } ColumnConstraintDefinition.kind = Symbol("ColumnConstraintDefinition"); /** * Table-level constraint definition. */ export class TableConstraintDefinition extends SqlComponent { constructor(params) { var _a, _b; super(); this.kind = params.kind; this.constraintName = params.constraintName; this.columns = params.columns ? [...params.columns] : null; this.reference = params.reference; this.checkExpression = params.checkExpression; this.rawClause = params.rawClause; this.deferrable = (_a = params.deferrable) !== null && _a !== void 0 ? _a : null; this.initially = (_b = params.initially) !== null && _b !== void 0 ? _b : null; } } TableConstraintDefinition.kind = Symbol("TableConstraintDefinition"); /** * Represents a single column definition within CREATE TABLE. */ export class TableColumnDefinition extends SqlComponent { constructor(params) { super(); this.name = params.name; this.dataType = params.dataType; this.constraints = params.constraints ? [...params.constraints] : []; } } TableColumnDefinition.kind = Symbol("TableColumnDefinition"); // Represents a CREATE TABLE query model that supports column definitions and AS SELECT variants. export class CreateTableQuery extends SqlComponent { constructor(params) { var _a, _b, _c, _d; super(); this.tableName = new IdentifierString(params.tableName); this.namespaces = params.namespaces ? [...params.namespaces] : null; this.isTemporary = (_a = params.isTemporary) !== null && _a !== void 0 ? _a : false; this.ifNotExists = (_b = params.ifNotExists) !== null && _b !== void 0 ? _b : false; this.columns = params.columns ? [...params.columns] : []; this.tableConstraints = params.tableConstraints ? [...params.tableConstraints] : []; this.tableOptions = (_c = params.tableOptions) !== null && _c !== void 0 ? _c : null; this.asSelectQuery = params.asSelectQuery; this.withDataOption = (_d = params.withDataOption) !== null && _d !== void 0 ? _d : null; } /** * Returns a SelectQuery that selects all columns from this table. */ getSelectQuery() { let selectItems; // Prefer explicit AS SELECT query columns when present. if (this.asSelectQuery) { const collector = new SelectValueCollector(); const values = collector.collect(this.asSelectQuery); selectItems = values.map(val => new SelectItem(val.value, val.name)); } else if (this.columns.length > 0) { // Use defined column names when the table definition is DDL-based. selectItems = this.columns.map(column => new SelectItem(new ColumnReference(null, column.name), column.name.name)); } else { // Fallback to wild-card selection when no column metadata is available. selectItems = [new SelectItem(new RawString("*"))]; } // Build a simple SELECT ... FROM table query. const qualifiedName = this.namespaces && this.namespaces.length > 0 ? [...this.namespaces, this.tableName.name].join(".") : this.tableName.name; return new SimpleSelectQuery({ selectClause: new SelectClause(selectItems), fromClause: new FromClause(new SourceExpression(new TableSource(null, qualifiedName), null), null), }); } /** * Returns a SelectQuery that counts all rows in this table. */ getCountQuery() { const qualifiedName = this.namespaces && this.namespaces.length > 0 ? [...this.namespaces, this.tableName.name].join(".") : this.tableName.name; return new SimpleSelectQuery({ selectClause: new SelectClause([ new SelectItem(new FunctionCall(null, "count", new ColumnReference(null, "*"), null)) ]), fromClause: new FromClause(new SourceExpression(new TableSource(null, qualifiedName), null), null), }); } } CreateTableQuery.kind = Symbol("CreateTableQuery"); //# sourceMappingURL=CreateTableQuery.js.map