rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
135 lines • 6.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateTableQuery = exports.TableColumnDefinition = exports.TableConstraintDefinition = exports.ColumnConstraintDefinition = exports.ReferenceDefinition = 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 REFERENCES clause definition that can be shared between column and table constraints.
*/
class ReferenceDefinition extends SqlComponent_1.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;
}
}
exports.ReferenceDefinition = ReferenceDefinition;
ReferenceDefinition.kind = Symbol("ReferenceDefinition");
/**
* Column-level constraint definition.
*/
class ColumnConstraintDefinition extends SqlComponent_1.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;
}
}
exports.ColumnConstraintDefinition = ColumnConstraintDefinition;
ColumnConstraintDefinition.kind = Symbol("ColumnConstraintDefinition");
/**
* Table-level constraint definition.
*/
class TableConstraintDefinition extends SqlComponent_1.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;
}
}
exports.TableConstraintDefinition = TableConstraintDefinition;
TableConstraintDefinition.kind = Symbol("TableConstraintDefinition");
/**
* Represents a single column definition within CREATE TABLE.
*/
class TableColumnDefinition extends SqlComponent_1.SqlComponent {
constructor(params) {
super();
this.name = params.name;
this.dataType = params.dataType;
this.constraints = params.constraints ? [...params.constraints] : [];
}
}
exports.TableColumnDefinition = TableColumnDefinition;
TableColumnDefinition.kind = Symbol("TableColumnDefinition");
// Represents a CREATE TABLE query model that supports column definitions and AS SELECT variants.
class CreateTableQuery extends SqlComponent_1.SqlComponent {
constructor(params) {
var _a, _b, _c, _d;
super();
this.tableName = new ValueComponent_1.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_1.SelectValueCollector();
const values = collector.collect(this.asSelectQuery);
selectItems = values.map(val => new Clause_1.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 Clause_1.SelectItem(new ValueComponent_1.ColumnReference(null, column.name), column.name.name));
}
else {
// Fallback to wild-card selection when no column metadata is available.
selectItems = [new Clause_1.SelectItem(new ValueComponent_1.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_1.SimpleSelectQuery({
selectClause: new Clause_1.SelectClause(selectItems),
fromClause: new Clause_1.FromClause(new Clause_1.SourceExpression(new Clause_1.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_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, qualifiedName), null), null),
});
}
}
exports.CreateTableQuery = CreateTableQuery;
CreateTableQuery.kind = Symbol("CreateTableQuery");
//# sourceMappingURL=CreateTableQuery.js.map