rawsql-ts
Version:
High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
256 lines • 10.3 kB
JavaScript
import { SqlComponent } from "./SqlComponent";
import { QualifiedName, IdentifierString } from "./ValueComponent";
function cloneIdentifierWithComments(identifier) {
const clone = new IdentifierString(identifier.name);
// Preserve positioned comment metadata while cloning identifiers.
if (identifier.positionedComments) {
clone.positionedComments = identifier.positionedComments.map(entry => ({
position: entry.position,
comments: [...entry.comments],
}));
}
else if (identifier.comments && identifier.comments.length > 0) {
// Copy legacy comment arrays when no positioned comments exist.
clone.comments = [...identifier.comments];
}
return clone;
}
/**
* DROP TABLE statement representation.
*/
export class DropTableStatement extends SqlComponent {
constructor(params) {
var _a, _b;
super();
this.tables = params.tables.map(table => new QualifiedName(table.namespaces, table.name));
this.ifExists = (_a = params.ifExists) !== null && _a !== void 0 ? _a : false;
this.behavior = (_b = params.behavior) !== null && _b !== void 0 ? _b : null;
}
}
DropTableStatement.kind = Symbol("DropTableStatement");
/**
* DROP INDEX statement representation.
*/
export class DropIndexStatement extends SqlComponent {
constructor(params) {
var _a, _b, _c;
super();
this.indexNames = params.indexNames.map(index => new QualifiedName(index.namespaces, index.name));
this.ifExists = (_a = params.ifExists) !== null && _a !== void 0 ? _a : false;
this.concurrently = (_b = params.concurrently) !== null && _b !== void 0 ? _b : false;
this.behavior = (_c = params.behavior) !== null && _c !== void 0 ? _c : null;
}
}
DropIndexStatement.kind = Symbol("DropIndexStatement");
/**
* Column definition within CREATE INDEX clause.
*/
export class IndexColumnDefinition extends SqlComponent {
constructor(params) {
var _a, _b, _c, _d;
super();
this.expression = params.expression;
this.sortOrder = (_a = params.sortOrder) !== null && _a !== void 0 ? _a : null;
this.nullsOrder = (_b = params.nullsOrder) !== null && _b !== void 0 ? _b : null;
this.collation = (_c = params.collation) !== null && _c !== void 0 ? _c : null;
this.operatorClass = (_d = params.operatorClass) !== null && _d !== void 0 ? _d : null;
}
}
IndexColumnDefinition.kind = Symbol("IndexColumnDefinition");
/**
* CREATE INDEX statement representation.
*/
export class CreateIndexStatement extends SqlComponent {
constructor(params) {
var _a, _b, _c, _d, _e, _f;
super();
this.unique = (_a = params.unique) !== null && _a !== void 0 ? _a : false;
this.concurrently = (_b = params.concurrently) !== null && _b !== void 0 ? _b : false;
this.ifNotExists = (_c = params.ifNotExists) !== null && _c !== void 0 ? _c : false;
this.indexName = new QualifiedName(params.indexName.namespaces, params.indexName.name);
this.tableName = new QualifiedName(params.tableName.namespaces, params.tableName.name);
this.usingMethod = (_d = params.usingMethod) !== null && _d !== void 0 ? _d : null;
this.columns = params.columns.map(col => {
var _a, _b;
return new IndexColumnDefinition({
expression: col.expression,
sortOrder: col.sortOrder,
nullsOrder: col.nullsOrder,
collation: (_a = col.collation) !== null && _a !== void 0 ? _a : null,
operatorClass: (_b = col.operatorClass) !== null && _b !== void 0 ? _b : null
});
});
this.include = params.include ? [...params.include] : null;
this.where = params.where;
this.withOptions = (_e = params.withOptions) !== null && _e !== void 0 ? _e : null;
this.tablespace = (_f = params.tablespace) !== null && _f !== void 0 ? _f : null;
}
}
CreateIndexStatement.kind = Symbol("CreateIndexStatement");
/**
* ALTER TABLE ... ADD CONSTRAINT action.
*/
export class AlterTableAddConstraint extends SqlComponent {
constructor(params) {
var _a, _b;
super();
this.constraint = params.constraint;
this.ifNotExists = (_a = params.ifNotExists) !== null && _a !== void 0 ? _a : false;
this.notValid = (_b = params.notValid) !== null && _b !== void 0 ? _b : false;
}
}
AlterTableAddConstraint.kind = Symbol("AlterTableAddConstraint");
/**
* ALTER TABLE ... DROP CONSTRAINT action.
*/
export class AlterTableDropConstraint extends SqlComponent {
constructor(params) {
var _a, _b;
super();
this.constraintName = params.constraintName;
this.ifExists = (_a = params.ifExists) !== null && _a !== void 0 ? _a : false;
this.behavior = (_b = params.behavior) !== null && _b !== void 0 ? _b : null;
}
}
AlterTableDropConstraint.kind = Symbol("AlterTableDropConstraint");
/**
* ALTER TABLE ... DROP COLUMN action.
*/
export class AlterTableDropColumn extends SqlComponent {
constructor(params) {
var _a, _b;
super();
this.columnName = params.columnName;
this.ifExists = (_a = params.ifExists) !== null && _a !== void 0 ? _a : false;
this.behavior = (_b = params.behavior) !== null && _b !== void 0 ? _b : null;
}
}
AlterTableDropColumn.kind = Symbol("AlterTableDropColumn");
/**
* ALTER TABLE ... ADD COLUMN action.
*/
export class AlterTableAddColumn extends SqlComponent {
constructor(params) {
var _a;
super();
this.column = params.column;
this.ifNotExists = (_a = params.ifNotExists) !== null && _a !== void 0 ? _a : false;
}
}
AlterTableAddColumn.kind = Symbol("AlterTableAddColumn");
/**
* ALTER TABLE ... ALTER COLUMN ... SET/DROP DEFAULT action.
*/
export class AlterTableAlterColumnDefault extends SqlComponent {
constructor(params) {
var _a, _b;
super();
this.columnName = params.columnName;
this.setDefault = (_a = params.setDefault) !== null && _a !== void 0 ? _a : null;
this.dropDefault = (_b = params.dropDefault) !== null && _b !== void 0 ? _b : false;
// Guard against constructing an action that tries to both set and drop the default.
if (this.setDefault !== null && this.dropDefault) {
throw new Error("[AlterTableAlterColumnDefault] Cannot set and drop a default at the same time.");
}
}
}
AlterTableAlterColumnDefault.kind = Symbol("AlterTableAlterColumnDefault");
/**
* ALTER TABLE statement representation with constraint-centric actions.
*/
export class AlterTableStatement extends SqlComponent {
constructor(params) {
var _a, _b;
super();
this.table = new QualifiedName(params.table.namespaces, params.table.name);
this.only = (_a = params.only) !== null && _a !== void 0 ? _a : false;
this.ifExists = (_b = params.ifExists) !== null && _b !== void 0 ? _b : false;
this.actions = params.actions.map(action => action);
}
}
AlterTableStatement.kind = Symbol("AlterTableStatement");
/**
* Standalone DROP CONSTRAINT statement representation.
*/
export class DropConstraintStatement extends SqlComponent {
constructor(params) {
var _a, _b;
super();
this.constraintName = params.constraintName;
this.ifExists = (_a = params.ifExists) !== null && _a !== void 0 ? _a : false;
this.behavior = (_b = params.behavior) !== null && _b !== void 0 ? _b : null;
}
}
DropConstraintStatement.kind = Symbol("DropConstraintStatement");
/**
* Option entry within an EXPLAIN statement.
*/
export class ExplainOption extends SqlComponent {
constructor(params) {
var _a;
super();
// Clone the option name so explain options keep associated metadata.
this.name = cloneIdentifierWithComments(params.name);
this.value = (_a = params.value) !== null && _a !== void 0 ? _a : null;
}
}
ExplainOption.kind = Symbol("ExplainOption");
/**
* EXPLAIN statement representation.
*/
export class ExplainStatement extends SqlComponent {
constructor(params) {
super();
this.options = params.options ? params.options.map(option => new ExplainOption(option)) : null;
this.statement = params.statement;
}
}
ExplainStatement.kind = Symbol("ExplainStatement");
/**
* ANALYZE statement representation.
*/
export class AnalyzeStatement extends SqlComponent {
constructor(params) {
var _a;
super();
this.verbose = (_a = params === null || params === void 0 ? void 0 : params.verbose) !== null && _a !== void 0 ? _a : false;
this.target = (params === null || params === void 0 ? void 0 : params.target)
? new QualifiedName(params.target.namespaces, params.target.name)
: null;
if (params === null || params === void 0 ? void 0 : params.columns) {
// Clone target columns so position-aware comments remain intact.
this.columns = params.columns.map(cloneIdentifierWithComments);
}
else {
this.columns = null;
}
}
}
AnalyzeStatement.kind = Symbol("AnalyzeStatement");
/**
* CREATE SEQUENCE statement representation.
*/
export class CreateSequenceStatement extends SqlComponent {
constructor(params) {
var _a;
super();
this.sequenceName = new QualifiedName(params.sequenceName.namespaces, params.sequenceName.name);
this.ifNotExists = (_a = params.ifNotExists) !== null && _a !== void 0 ? _a : false;
this.clauses = params.clauses ? [...params.clauses] : [];
}
}
CreateSequenceStatement.kind = Symbol("CreateSequenceStatement");
/**
* ALTER SEQUENCE statement representation.
*/
export class AlterSequenceStatement extends SqlComponent {
constructor(params) {
var _a;
super();
this.sequenceName = new QualifiedName(params.sequenceName.namespaces, params.sequenceName.name);
this.ifExists = (_a = params.ifExists) !== null && _a !== void 0 ? _a : false;
this.clauses = params.clauses ? [...params.clauses] : [];
}
}
AlterSequenceStatement.kind = Symbol("AlterSequenceStatement");
//# sourceMappingURL=DDLStatements.js.map