rawsql-ts
Version:
High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
275 lines • 12 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AlterSequenceStatement = exports.CreateSequenceStatement = exports.AnalyzeStatement = exports.ExplainStatement = exports.ExplainOption = exports.DropConstraintStatement = exports.AlterTableStatement = exports.AlterTableAlterColumnDefault = exports.AlterTableAddColumn = exports.AlterTableDropColumn = exports.AlterTableDropConstraint = exports.AlterTableAddConstraint = exports.CreateIndexStatement = exports.IndexColumnDefinition = exports.DropIndexStatement = exports.DropTableStatement = void 0;
const SqlComponent_1 = require("./SqlComponent");
const ValueComponent_1 = require("./ValueComponent");
function cloneIdentifierWithComments(identifier) {
const clone = new ValueComponent_1.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.
*/
class DropTableStatement extends SqlComponent_1.SqlComponent {
constructor(params) {
var _a, _b;
super();
this.tables = params.tables.map(table => new ValueComponent_1.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;
}
}
exports.DropTableStatement = DropTableStatement;
DropTableStatement.kind = Symbol("DropTableStatement");
/**
* DROP INDEX statement representation.
*/
class DropIndexStatement extends SqlComponent_1.SqlComponent {
constructor(params) {
var _a, _b, _c;
super();
this.indexNames = params.indexNames.map(index => new ValueComponent_1.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;
}
}
exports.DropIndexStatement = DropIndexStatement;
DropIndexStatement.kind = Symbol("DropIndexStatement");
/**
* Column definition within CREATE INDEX clause.
*/
class IndexColumnDefinition extends SqlComponent_1.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;
}
}
exports.IndexColumnDefinition = IndexColumnDefinition;
IndexColumnDefinition.kind = Symbol("IndexColumnDefinition");
/**
* CREATE INDEX statement representation.
*/
class CreateIndexStatement extends SqlComponent_1.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 ValueComponent_1.QualifiedName(params.indexName.namespaces, params.indexName.name);
this.tableName = new ValueComponent_1.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;
}
}
exports.CreateIndexStatement = CreateIndexStatement;
CreateIndexStatement.kind = Symbol("CreateIndexStatement");
/**
* ALTER TABLE ... ADD CONSTRAINT action.
*/
class AlterTableAddConstraint extends SqlComponent_1.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;
}
}
exports.AlterTableAddConstraint = AlterTableAddConstraint;
AlterTableAddConstraint.kind = Symbol("AlterTableAddConstraint");
/**
* ALTER TABLE ... DROP CONSTRAINT action.
*/
class AlterTableDropConstraint extends SqlComponent_1.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;
}
}
exports.AlterTableDropConstraint = AlterTableDropConstraint;
AlterTableDropConstraint.kind = Symbol("AlterTableDropConstraint");
/**
* ALTER TABLE ... DROP COLUMN action.
*/
class AlterTableDropColumn extends SqlComponent_1.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;
}
}
exports.AlterTableDropColumn = AlterTableDropColumn;
AlterTableDropColumn.kind = Symbol("AlterTableDropColumn");
/**
* ALTER TABLE ... ADD COLUMN action.
*/
class AlterTableAddColumn extends SqlComponent_1.SqlComponent {
constructor(params) {
var _a;
super();
this.column = params.column;
this.ifNotExists = (_a = params.ifNotExists) !== null && _a !== void 0 ? _a : false;
}
}
exports.AlterTableAddColumn = AlterTableAddColumn;
AlterTableAddColumn.kind = Symbol("AlterTableAddColumn");
/**
* ALTER TABLE ... ALTER COLUMN ... SET/DROP DEFAULT action.
*/
class AlterTableAlterColumnDefault extends SqlComponent_1.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.");
}
}
}
exports.AlterTableAlterColumnDefault = AlterTableAlterColumnDefault;
AlterTableAlterColumnDefault.kind = Symbol("AlterTableAlterColumnDefault");
/**
* ALTER TABLE statement representation with constraint-centric actions.
*/
class AlterTableStatement extends SqlComponent_1.SqlComponent {
constructor(params) {
var _a, _b;
super();
this.table = new ValueComponent_1.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);
}
}
exports.AlterTableStatement = AlterTableStatement;
AlterTableStatement.kind = Symbol("AlterTableStatement");
/**
* Standalone DROP CONSTRAINT statement representation.
*/
class DropConstraintStatement extends SqlComponent_1.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;
}
}
exports.DropConstraintStatement = DropConstraintStatement;
DropConstraintStatement.kind = Symbol("DropConstraintStatement");
/**
* Option entry within an EXPLAIN statement.
*/
class ExplainOption extends SqlComponent_1.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;
}
}
exports.ExplainOption = ExplainOption;
ExplainOption.kind = Symbol("ExplainOption");
/**
* EXPLAIN statement representation.
*/
class ExplainStatement extends SqlComponent_1.SqlComponent {
constructor(params) {
super();
this.options = params.options ? params.options.map(option => new ExplainOption(option)) : null;
this.statement = params.statement;
}
}
exports.ExplainStatement = ExplainStatement;
ExplainStatement.kind = Symbol("ExplainStatement");
/**
* ANALYZE statement representation.
*/
class AnalyzeStatement extends SqlComponent_1.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 ValueComponent_1.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;
}
}
}
exports.AnalyzeStatement = AnalyzeStatement;
AnalyzeStatement.kind = Symbol("AnalyzeStatement");
/**
* CREATE SEQUENCE statement representation.
*/
class CreateSequenceStatement extends SqlComponent_1.SqlComponent {
constructor(params) {
var _a;
super();
this.sequenceName = new ValueComponent_1.QualifiedName(params.sequenceName.namespaces, params.sequenceName.name);
this.ifNotExists = (_a = params.ifNotExists) !== null && _a !== void 0 ? _a : false;
this.clauses = params.clauses ? [...params.clauses] : [];
}
}
exports.CreateSequenceStatement = CreateSequenceStatement;
CreateSequenceStatement.kind = Symbol("CreateSequenceStatement");
/**
* ALTER SEQUENCE statement representation.
*/
class AlterSequenceStatement extends SqlComponent_1.SqlComponent {
constructor(params) {
var _a;
super();
this.sequenceName = new ValueComponent_1.QualifiedName(params.sequenceName.namespaces, params.sequenceName.name);
this.ifExists = (_a = params.ifExists) !== null && _a !== void 0 ? _a : false;
this.clauses = params.clauses ? [...params.clauses] : [];
}
}
exports.AlterSequenceStatement = AlterSequenceStatement;
AlterSequenceStatement.kind = Symbol("AlterSequenceStatement");
//# sourceMappingURL=DDLStatements.js.map