UNPKG

rawsql-ts

Version:

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

983 lines (982 loc) 64.6 kB
import { PartitionByClause, OrderByClause, OrderByItem, SelectClause, SelectItem, Distinct, DistinctOn, SortDirection, NullsSortDirection, TableSource, SourceExpression, FromClause, JoinClause, JoinOnClause, JoinUsingClause, FunctionSource, SourceAliasExpression, WhereClause, GroupByClause, HavingClause, SubQuerySource, WindowFrameClause, LimitClause, ForClause, OffsetClause, WindowsClause as WindowClause, CommonTable, WithClause, FetchClause, FetchExpression, InsertClause, UpdateClause, SetClause, ReturningClause, SetClauseItem } from "../models/Clause"; import { HintClause } from "../models/HintClause"; import { BinarySelectQuery, SimpleSelectQuery, ValuesQuery } from "../models/SelectQuery"; import { SqlPrintToken, SqlPrintTokenType, SqlPrintTokenContainerType } from "../models/SqlPrintToken"; import { ValueList, ColumnReference, FunctionCall, UnaryExpression, BinaryExpression, LiteralValue, ParameterExpression, SwitchCaseArgument, CaseKeyValuePair, RawString, IdentifierString, ParenExpression, CastExpression, CaseExpression, ArrayExpression, ArrayQueryExpression, ArraySliceExpression, ArrayIndexExpression, BetweenExpression, StringSpecifierExpression, TypeValue, TupleExpression, WindowFrameExpression, QualifiedName, InlineQuery, WindowFrameSpec, WindowFrameBoundStatic, WindowFrameBoundaryValue } from "../models/ValueComponent"; import { ParameterCollector } from "../transformers/ParameterCollector"; import { IdentifierDecorator } from "./IdentifierDecorator"; import { ParameterDecorator } from "./ParameterDecorator"; import { InsertQuery } from "../models/InsertQuery"; import { UpdateQuery } from "../models/UpdateQuery"; import { CreateTableQuery } from "../models/CreateTableQuery"; export var ParameterStyle; (function (ParameterStyle) { ParameterStyle["Anonymous"] = "anonymous"; ParameterStyle["Indexed"] = "indexed"; ParameterStyle["Named"] = "named"; })(ParameterStyle || (ParameterStyle = {})); export const PRESETS = { mysql: { identifierEscape: { start: '`', end: '`' }, parameterSymbol: '?', parameterStyle: ParameterStyle.Anonymous, }, postgres: { identifierEscape: { start: '"', end: '"' }, parameterSymbol: '$', parameterStyle: ParameterStyle.Indexed, }, postgresWithNamedParams: { identifierEscape: { start: '"', end: '"' }, parameterSymbol: ':', parameterStyle: ParameterStyle.Named, }, sqlserver: { identifierEscape: { start: '[', end: ']' }, parameterSymbol: '@', parameterStyle: ParameterStyle.Named, }, sqlite: { identifierEscape: { start: '"', end: '"' }, parameterSymbol: ':', parameterStyle: ParameterStyle.Named, }, oracle: { identifierEscape: { start: '"', end: '"' }, parameterSymbol: ':', parameterStyle: ParameterStyle.Named, }, clickhouse: { identifierEscape: { start: '`', end: '`' }, parameterSymbol: '?', parameterStyle: ParameterStyle.Anonymous, }, firebird: { identifierEscape: { start: '"', end: '"' }, parameterSymbol: '?', parameterStyle: ParameterStyle.Anonymous, }, db2: { identifierEscape: { start: '"', end: '"' }, parameterSymbol: '?', parameterStyle: ParameterStyle.Anonymous, }, snowflake: { identifierEscape: { start: '"', end: '"' }, parameterSymbol: '?', parameterStyle: ParameterStyle.Anonymous, }, cloudspanner: { identifierEscape: { start: '`', end: '`' }, parameterSymbol: '@', parameterStyle: ParameterStyle.Named, }, duckdb: { identifierEscape: { start: '"', end: '"' }, parameterSymbol: '?', parameterStyle: ParameterStyle.Anonymous, }, cockroachdb: { identifierEscape: { start: '"', end: '"' }, parameterSymbol: '$', parameterStyle: ParameterStyle.Indexed, }, athena: { identifierEscape: { start: '"', end: '"' }, parameterSymbol: '?', parameterStyle: ParameterStyle.Anonymous, }, bigquery: { identifierEscape: { start: '`', end: '`' }, parameterSymbol: '@', parameterStyle: ParameterStyle.Named, }, hive: { identifierEscape: { start: '`', end: '`' }, parameterSymbol: '?', parameterStyle: ParameterStyle.Anonymous, }, mariadb: { identifierEscape: { start: '`', end: '`' }, parameterSymbol: '?', parameterStyle: ParameterStyle.Anonymous, }, redshift: { identifierEscape: { start: '"', end: '"' }, parameterSymbol: '$', parameterStyle: ParameterStyle.Indexed, }, flinksql: { identifierEscape: { start: '`', end: '`' }, parameterSymbol: '?', parameterStyle: ParameterStyle.Anonymous, }, mongodb: { identifierEscape: { start: '"', end: '"' }, parameterSymbol: '?', parameterStyle: ParameterStyle.Anonymous, }, }; export class SqlPrintTokenParser { constructor(options) { var _a, _b, _c, _d, _e, _f, _g; this.handlers = new Map(); this.index = 1; if (options === null || options === void 0 ? void 0 : options.preset) { const preset = options.preset; options = Object.assign(Object.assign({}, preset), options); } this.parameterDecorator = new ParameterDecorator({ prefix: typeof (options === null || options === void 0 ? void 0 : options.parameterSymbol) === 'string' ? options.parameterSymbol : (_b = (_a = options === null || options === void 0 ? void 0 : options.parameterSymbol) === null || _a === void 0 ? void 0 : _a.start) !== null && _b !== void 0 ? _b : ':', suffix: typeof (options === null || options === void 0 ? void 0 : options.parameterSymbol) === 'object' ? options.parameterSymbol.end : '', style: (_c = options === null || options === void 0 ? void 0 : options.parameterStyle) !== null && _c !== void 0 ? _c : 'named' }); this.identifierDecorator = new IdentifierDecorator({ start: (_e = (_d = options === null || options === void 0 ? void 0 : options.identifierEscape) === null || _d === void 0 ? void 0 : _d.start) !== null && _e !== void 0 ? _e : '"', end: (_g = (_f = options === null || options === void 0 ? void 0 : options.identifierEscape) === null || _f === void 0 ? void 0 : _f.end) !== null && _g !== void 0 ? _g : '"' }); this.handlers.set(ValueList.kind, (expr) => this.visitValueList(expr)); this.handlers.set(ColumnReference.kind, (expr) => this.visitColumnReference(expr)); this.handlers.set(QualifiedName.kind, (expr) => this.visitQualifiedName(expr)); this.handlers.set(FunctionCall.kind, (expr) => this.visitFunctionCall(expr)); this.handlers.set(UnaryExpression.kind, (expr) => this.visitUnaryExpression(expr)); this.handlers.set(BinaryExpression.kind, (expr) => this.visitBinaryExpression(expr)); this.handlers.set(LiteralValue.kind, (expr) => this.visitLiteralValue(expr)); this.handlers.set(ParameterExpression.kind, (expr) => this.visitParameterExpression(expr)); this.handlers.set(SwitchCaseArgument.kind, (expr) => this.visitSwitchCaseArgument(expr)); this.handlers.set(CaseKeyValuePair.kind, (expr) => this.visitCaseKeyValuePair(expr)); this.handlers.set(RawString.kind, (expr) => this.visitRawString(expr)); this.handlers.set(IdentifierString.kind, (expr) => this.visitIdentifierString(expr)); this.handlers.set(ParenExpression.kind, (expr) => this.visitParenExpression(expr)); this.handlers.set(CastExpression.kind, (expr) => this.visitCastExpression(expr)); this.handlers.set(CaseExpression.kind, (expr) => this.visitCaseExpression(expr)); this.handlers.set(ArrayExpression.kind, (expr) => this.visitArrayExpression(expr)); this.handlers.set(ArrayQueryExpression.kind, (expr) => this.visitArrayQueryExpression(expr)); this.handlers.set(ArraySliceExpression.kind, (expr) => this.visitArraySliceExpression(expr)); this.handlers.set(ArrayIndexExpression.kind, (expr) => this.visitArrayIndexExpression(expr)); this.handlers.set(BetweenExpression.kind, (expr) => this.visitBetweenExpression(expr)); this.handlers.set(StringSpecifierExpression.kind, (expr) => this.visitStringSpecifierExpression(expr)); this.handlers.set(TypeValue.kind, (expr) => this.visitTypeValue(expr)); this.handlers.set(TupleExpression.kind, (expr) => this.visitTupleExpression(expr)); this.handlers.set(InlineQuery.kind, (expr) => this.visitInlineQuery(expr)); this.handlers.set(WindowFrameExpression.kind, (expr) => this.visitWindowFrameExpression(expr)); this.handlers.set(WindowFrameSpec.kind, (expr) => this.visitWindowFrameSpec(expr)); this.handlers.set(WindowFrameBoundStatic.kind, (expr) => this.visitWindowFrameBoundStatic(expr)); this.handlers.set(WindowFrameBoundaryValue.kind, (expr) => this.visitWindowFrameBoundaryValue(expr)); this.handlers.set(PartitionByClause.kind, (expr) => this.visitPartitionByClause(expr)); this.handlers.set(OrderByClause.kind, (expr) => this.visitOrderByClause(expr)); this.handlers.set(OrderByItem.kind, (expr) => this.visitOrderByItem(expr)); // select this.handlers.set(SelectItem.kind, (expr) => this.visitSelectItem(expr)); this.handlers.set(SelectClause.kind, (expr) => this.visitSelectClause(expr)); this.handlers.set(Distinct.kind, (expr) => this.visitDistinct(expr)); this.handlers.set(DistinctOn.kind, (expr) => this.visitDistinctOn(expr)); this.handlers.set(HintClause.kind, (expr) => this.visitHintClause(expr)); // from this.handlers.set(TableSource.kind, (expr) => this.visitTableSource(expr)); this.handlers.set(FunctionSource.kind, (expr) => this.visitFunctionSource(expr)); this.handlers.set(SourceExpression.kind, (expr) => this.visitSourceExpression(expr)); this.handlers.set(SourceAliasExpression.kind, (expr) => this.visitSourceAliasExpression(expr)); this.handlers.set(FromClause.kind, (expr) => this.visitFromClause(expr)); this.handlers.set(JoinClause.kind, (expr) => this.visitJoinClause(expr)); this.handlers.set(JoinOnClause.kind, (expr) => this.visitJoinOnClause(expr)); this.handlers.set(JoinUsingClause.kind, (expr) => this.visitJoinUsingClause(expr)); // where this.handlers.set(WhereClause.kind, (expr) => this.visitWhereClause(expr)); // group this.handlers.set(GroupByClause.kind, (expr) => this.visitGroupByClause(expr)); this.handlers.set(HavingClause.kind, (expr) => this.visitHavingClause(expr)); this.handlers.set(WindowClause.kind, (expr) => this.visitWindowClause(expr)); this.handlers.set(WindowFrameClause.kind, (expr) => this.visitWindowFrameClause(expr)); this.handlers.set(LimitClause.kind, (expr) => this.visitLimitClause(expr)); this.handlers.set(OffsetClause.kind, (expr) => this.visitOffsetClause(expr)); this.handlers.set(FetchClause.kind, (expr) => this.visitFetchClause(expr)); this.handlers.set(FetchExpression.kind, (expr) => this.visitFetchExpression(expr)); this.handlers.set(ForClause.kind, (expr) => this.visitForClause(expr)); // With this.handlers.set(WithClause.kind, (expr) => this.visitWithClause(expr)); this.handlers.set(CommonTable.kind, (expr) => this.visitCommonTable(expr)); // Query this.handlers.set(SimpleSelectQuery.kind, (expr) => this.visitSimpleQuery(expr)); this.handlers.set(SubQuerySource.kind, (expr) => this.visitSubQuerySource(expr)); this.handlers.set(BinarySelectQuery.kind, (expr) => this.visitBinarySelectQuery(expr)); this.handlers.set(ValuesQuery.kind, (expr) => this.visitValuesQuery(expr)); this.handlers.set(TupleExpression.kind, (expr) => this.visitTupleExpression(expr)); this.handlers.set(InsertQuery.kind, (expr) => this.visitInsertQuery(expr)); this.handlers.set(InsertClause.kind, (expr) => this.visitInsertClause(expr)); this.handlers.set(UpdateQuery.kind, (expr) => this.visitUpdateQuery(expr)); this.handlers.set(UpdateClause.kind, (expr) => this.visitUpdateClause(expr)); this.handlers.set(SetClause.kind, (expr) => this.visitSetClause(expr)); this.handlers.set(SetClauseItem.kind, (expr) => this.visitSetClauseItem(expr)); this.handlers.set(ReturningClause.kind, (expr) => this.visitReturningClause(expr)); this.handlers.set(CreateTableQuery.kind, (expr) => this.visitCreateTableQuery(expr)); } /** * Pretty-prints a BinarySelectQuery (e.g., UNION, INTERSECT, EXCEPT). * This will recursively print left and right queries, separated by the operator. * @param arg BinarySelectQuery */ visitBinarySelectQuery(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, ''); token.innerTokens.push(this.visit(arg.left)); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, arg.operator.value, SqlPrintTokenContainerType.BinarySelectQueryOperator)); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(this.visit(arg.right)); return token; } /** * Returns an array of tokens representing a comma followed by a space. * This is a common pattern in SQL pretty-printing. */ static commaSpaceTokens() { return [SqlPrintTokenParser.COMMA_TOKEN, SqlPrintTokenParser.SPACE_TOKEN]; } static argumentCommaSpaceTokens() { return [SqlPrintTokenParser.ARGUMENT_SPLIT_COMMA_TOKEN, SqlPrintTokenParser.SPACE_TOKEN]; } visitQualifiedName(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.QualifiedName); if (arg.namespaces) { for (let i = 0; i < arg.namespaces.length; i++) { token.innerTokens.push(arg.namespaces[i].accept(this)); token.innerTokens.push(SqlPrintTokenParser.DOT_TOKEN); } } token.innerTokens.push(arg.name.accept(this)); return token; } visitPartitionByClause(arg) { // Print as: partition by ... const token = new SqlPrintToken(SqlPrintTokenType.keyword, 'partition by', SqlPrintTokenContainerType.PartitionByClause); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(this.visit(arg.value)); return token; } visitOrderByClause(arg) { // Print as: order by ... const token = new SqlPrintToken(SqlPrintTokenType.keyword, 'order by', SqlPrintTokenContainerType.OrderByClause); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); for (let i = 0; i < arg.order.length; i++) { if (i > 0) token.innerTokens.push(...SqlPrintTokenParser.commaSpaceTokens()); token.innerTokens.push(this.visit(arg.order[i])); } return token; } /** * Print an OrderByItem (expression [asc|desc] [nulls first|last]) */ visitOrderByItem(arg) { // arg: OrderByItem const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.OrderByItem); token.innerTokens.push(this.visit(arg.value)); if (arg.sortDirection && arg.sortDirection !== SortDirection.Ascending) { token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'desc')); } if (arg.nullsPosition) { if (arg.nullsPosition === NullsSortDirection.First) { token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'nulls first')); } else if (arg.nullsPosition === NullsSortDirection.Last) { token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'nulls last')); } } return token; } parse(arg) { // reset parameter index before parsing this.index = 1; const token = this.visit(arg); const paramsRaw = ParameterCollector.collect(arg).sort((a, b) => { var _a, _b; return ((_a = a.index) !== null && _a !== void 0 ? _a : 0) - ((_b = b.index) !== null && _b !== void 0 ? _b : 0); }); const style = this.parameterDecorator.style; if (style === ParameterStyle.Named) { // Named: { name: value, ... } const paramsObj = {}; for (const p of paramsRaw) { const key = p.name.value; if (paramsObj.hasOwnProperty(key)) { if (paramsObj[key] !== p.value) { throw new Error(`Duplicate parameter name '${key}' with different values detected during query composition.`); } // If value is the same, skip (already set) continue; } paramsObj[key] = p.value; } return { token, params: paramsObj }; } else if (style === ParameterStyle.Indexed) { // Indexed: [value1, value2, ...] (sorted by index) const paramsArr = paramsRaw.map(p => p.value); return { token, params: paramsArr }; } else if (style === ParameterStyle.Anonymous) { // Anonymous: [value1, value2, ...] (sorted by index, name is empty) const paramsArr = paramsRaw.map(p => p.value); return { token, params: paramsArr }; } // Fallback (just in case) return { token, params: [] }; } visit(arg) { const handler = this.handlers.get(arg.getKind()); if (handler) { const token = handler(arg); // Add comments to the token if they exist this.addCommentsToToken(token, arg.comments); return token; } throw new Error(`[SqlPrintTokenParser] No handler for kind: ${arg.getKind().toString()}`); } /** * Adds comment tokens to a SqlPrintToken based on the comments array */ addCommentsToToken(token, comments) { if (!comments || comments.length === 0) { return; } // Create CommentBlock containers for each comment const commentBlocks = this.createCommentBlocks(comments); // Insert comment blocks and handle spacing if (commentBlocks.length > 0) { this.insertCommentBlocksWithSpacing(token, commentBlocks); } } /** * Creates CommentBlock containers for the given comments. * Each CommentBlock contains: Comment -> CommentNewline -> Space * This structure supports both oneliner and multiline formatting modes. */ createCommentBlocks(comments) { const commentBlocks = []; for (const comment of comments) { if (comment.trim()) { commentBlocks.push(this.createSingleCommentBlock(comment)); } } return commentBlocks; } /** * Creates a single CommentBlock with the standard structure: * Comment -> CommentNewline -> Space * * This structure supports both formatting modes: * - Multiline mode: Comment + newline (space is filtered as leading space) * - Oneliner mode: Comment + space (commentNewline is skipped) */ createSingleCommentBlock(comment) { const commentBlock = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.CommentBlock); // Add comment token const commentToken = new SqlPrintToken(SqlPrintTokenType.comment, this.formatBlockComment(comment)); commentBlock.innerTokens.push(commentToken); // Add conditional newline token for multiline mode const commentNewlineToken = new SqlPrintToken(SqlPrintTokenType.commentNewline, ''); commentBlock.innerTokens.push(commentNewlineToken); // Add space token for oneliner mode spacing const spaceToken = new SqlPrintToken(SqlPrintTokenType.space, ' '); commentBlock.innerTokens.push(spaceToken); return commentBlock; } /** * Inserts comment blocks into a token and handles spacing logic. * Adds separator spaces for clause-level containers and manages duplicate space removal. */ insertCommentBlocksWithSpacing(token, commentBlocks) { token.innerTokens.unshift(...commentBlocks); // Add a separator space after comments only for certain container types // where comments need to be separated from main content const needsSeparatorSpace = this.shouldAddSeparatorSpace(token.containerType); if (needsSeparatorSpace) { const separatorSpace = new SqlPrintToken(SqlPrintTokenType.space, ' '); token.innerTokens.splice(commentBlocks.length, 0, separatorSpace); // Remove the original space token after our separator if it exists // This prevents duplicate spaces when comments are added if (token.innerTokens.length > commentBlocks.length + 1 && token.innerTokens[commentBlocks.length + 1].type === SqlPrintTokenType.space) { token.innerTokens.splice(commentBlocks.length + 1, 1); } } else { // For containers that don't need separator space, still remove duplicate spaces if (token.innerTokens.length > commentBlocks.length && token.innerTokens[commentBlocks.length].type === SqlPrintTokenType.space) { token.innerTokens.splice(commentBlocks.length, 1); } } } /** * Determines whether a separator space should be added after comments for the given container type. * * Clause-level containers (SELECT, FROM, WHERE, etc.) need separator spaces because: * - Comments appear before the main clause content * - A space is needed to separate comment block from SQL tokens * * Item-level containers (SelectItem, etc.) don't need separator spaces because: * - Comments are inline with the item content * - Spacing is handled by existing token structure */ shouldAddSeparatorSpace(containerType) { return this.isClauseLevelContainer(containerType); } /** * Checks if the container type represents a SQL clause (as opposed to an item within a clause). */ isClauseLevelContainer(containerType) { switch (containerType) { case SqlPrintTokenContainerType.SelectClause: case SqlPrintTokenContainerType.FromClause: case SqlPrintTokenContainerType.WhereClause: case SqlPrintTokenContainerType.GroupByClause: case SqlPrintTokenContainerType.HavingClause: case SqlPrintTokenContainerType.OrderByClause: case SqlPrintTokenContainerType.LimitClause: case SqlPrintTokenContainerType.OffsetClause: case SqlPrintTokenContainerType.WithClause: case SqlPrintTokenContainerType.SimpleSelectQuery: return true; default: return false; } } /** * Formats a comment string as a block comment. */ formatBlockComment(comment) { return `/* ${comment} */`; } visitValueList(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.ValueList); for (let i = 0; i < arg.values.length; i++) { if (i > 0) { token.innerTokens.push(...SqlPrintTokenParser.argumentCommaSpaceTokens()); } token.innerTokens.push(this.visit(arg.values[i])); } return token; } visitColumnReference(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.ColumnReference); token.innerTokens.push(arg.qualifiedName.accept(this)); return token; } visitFunctionCall(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.FunctionCall); token.innerTokens.push(arg.qualifiedName.accept(this)); token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN); if (arg.argument) { token.innerTokens.push(this.visit(arg.argument)); } token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN); if (arg.over) { token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'over')); if (arg.over instanceof IdentifierString) { token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(arg.over.accept(this)); } else { token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN); token.innerTokens.push(this.visit(arg.over)); token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN); } } return token; } visitUnaryExpression(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.UnaryExpression); token.innerTokens.push(this.visit(arg.operator)); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(this.visit(arg.expression)); return token; } visitBinaryExpression(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.BinaryExpression); token.innerTokens.push(this.visit(arg.left)); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.operator, arg.operator.value)); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(this.visit(arg.right)); return token; } visitLiteralValue(arg) { let text; if (typeof arg.value === "string") { if (arg.isDollarString) { // For dollar-quoted strings, return the original format text = arg.value; } else { text = `'${arg.value.replace(/'/g, "''")}'`; } } else if (arg.value === null) { text = "null"; } else { text = arg.value.toString(); } return new SqlPrintToken(SqlPrintTokenType.value, text, SqlPrintTokenContainerType.LiteralValue); } visitParameterExpression(arg) { // Create a parameter token and decorate it using the parameterDecorator arg.index = this.index; const text = this.parameterDecorator.decorate(arg.name.value, arg.index); const token = new SqlPrintToken(SqlPrintTokenType.parameter, text); this.index++; return token; } visitSwitchCaseArgument(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.SwitchCaseArgument); // Add each WHEN/THEN clause for (const kv of arg.cases) { // Create a new line for each WHEN clause token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(kv.accept(this)); } // Add ELSE clause if present if (arg.elseValue) { token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(this.createElseToken(arg.elseValue)); } return token; } createElseToken(elseValue) { // Creates a token for the ELSE clause in a CASE expression. const elseToken = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.ElseClause); // Add the ELSE keyword elseToken.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'else')); // Create a container for the ELSE value to enable proper indentation elseToken.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); const elseValueContainer = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.CaseElseValue); elseValueContainer.innerTokens.push(this.visit(elseValue)); elseToken.innerTokens.push(elseValueContainer); return elseToken; } visitCaseKeyValuePair(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.CaseKeyValuePair); // Create WHEN clause token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'when')); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(this.visit(arg.key)); // Create THEN clause token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'then')); // Create a container for the THEN value to enable proper indentation token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); const thenValueContainer = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.CaseThenValue); thenValueContainer.innerTokens.push(this.visit(arg.value)); token.innerTokens.push(thenValueContainer); return token; } visitRawString(arg) { // Even for non-container tokens, set the container type for context return new SqlPrintToken(SqlPrintTokenType.value, arg.value, SqlPrintTokenContainerType.RawString); } visitIdentifierString(arg) { // Create an identifier token and decorate it using the identifierDecorator const text = arg.name === "*" ? arg.name : this.identifierDecorator.decorate(arg.name); const token = new SqlPrintToken(SqlPrintTokenType.value, text, SqlPrintTokenContainerType.IdentifierString); return token; } visitParenExpression(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.ParenExpression); token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN); token.innerTokens.push(this.visit(arg.expression)); token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN); return token; } visitCastExpression(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.CastExpression); token.innerTokens.push(this.visit(arg.input)); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.operator, '::')); token.innerTokens.push(this.visit(arg.castType)); return token; } visitCaseExpression(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.CaseExpression); // Add the CASE keyword token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'case')); // Add the condition if exists if (arg.condition) { token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(this.visit(arg.condition)); } // Add the WHEN/THEN pairs and ELSE token.innerTokens.push(this.visit(arg.switchCase)); // Add the END keyword token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'end')); return token; } visitArrayExpression(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.ArrayExpression); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'array')); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.parenthesis, '[')); token.innerTokens.push(this.visit(arg.expression)); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.parenthesis, ']')); return token; } visitArrayQueryExpression(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.ArrayExpression); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'array')); // ARRAY(SELECT ...) token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.parenthesis, '(')); token.innerTokens.push(this.visit(arg.query)); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.parenthesis, ')')); return token; } visitArraySliceExpression(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.ArrayExpression); // array expression token.innerTokens.push(this.visit(arg.array)); // opening bracket token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.parenthesis, '[')); // start index (optional) if (arg.startIndex) { token.innerTokens.push(this.visit(arg.startIndex)); } // colon separator token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.operator, ':')); // end index (optional) if (arg.endIndex) { token.innerTokens.push(this.visit(arg.endIndex)); } // closing bracket token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.parenthesis, ']')); return token; } visitArrayIndexExpression(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.ArrayExpression); // array expression token.innerTokens.push(this.visit(arg.array)); // opening bracket token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.parenthesis, '[')); // index token.innerTokens.push(this.visit(arg.index)); // closing bracket token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.parenthesis, ']')); return token; } visitBetweenExpression(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.BetweenExpression); token.innerTokens.push(this.visit(arg.expression)); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); if (arg.negated) { token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'not')); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); } token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'between')); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(this.visit(arg.lower)); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'and')); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(this.visit(arg.upper)); return token; } visitStringSpecifierExpression(arg) { // Combine specifier and value into a single token const specifier = arg.specifier.accept(this).text; const value = arg.value.accept(this).text; return new SqlPrintToken(SqlPrintTokenType.value, specifier + value, SqlPrintTokenContainerType.StringSpecifierExpression); } visitTypeValue(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.TypeValue); token.innerTokens.push(arg.qualifiedName.accept(this)); if (arg.argument) { token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN); token.innerTokens.push(this.visit(arg.argument)); token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN); } return token; } visitTupleExpression(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.TupleExpression); token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN); for (let i = 0; i < arg.values.length; i++) { if (i > 0) { token.innerTokens.push(...SqlPrintTokenParser.argumentCommaSpaceTokens()); } token.innerTokens.push(this.visit(arg.values[i])); } token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN); return token; } visitWindowFrameExpression(arg) { // Compose window frame expression: over(partition by ... order by ... rows ...) const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.WindowFrameExpression); let first = true; if (arg.partition) { token.innerTokens.push(this.visit(arg.partition)); first = false; } if (arg.order) { if (!first) { token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); } else { first = false; } token.innerTokens.push(this.visit(arg.order)); } if (arg.frameSpec) { if (!first) { token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); } else { first = false; } token.innerTokens.push(this.visit(arg.frameSpec)); } return token; } visitWindowFrameSpec(arg) { // This method prints a window frame specification, such as "rows between ... and ..." or "range ...". const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.WindowFrameSpec); // Add frame type (e.g., "rows", "range", "groups") token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, arg.frameType)); if (arg.endBound === null) { // Only start bound: e.g., "rows unbounded preceding" token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(arg.startBound.accept(this)); } else { // Between: e.g., "rows between unbounded preceding and current row" token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'between')); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(arg.startBound.accept(this)); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'and')); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(arg.endBound.accept(this)); } return token; } /** * Prints a window frame boundary value, such as "5 preceding" or "3 following". * @param arg WindowFrameBoundaryValue */ visitWindowFrameBoundaryValue(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.WindowFrameBoundaryValue); token.innerTokens.push(arg.value.accept(this)); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); // true for "FOLLOWING", false for "PRECEDING" token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, arg.isFollowing ? 'following' : 'preceding')); return token; } /** * Prints a static window frame bound, such as "unbounded preceding", "current row", or "unbounded following". * @param arg WindowFrameBoundStatic */ visitWindowFrameBoundStatic(arg) { const token = new SqlPrintToken(SqlPrintTokenType.keyword, arg.bound); return token; } visitSelectItem(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.SelectItem); token.innerTokens.push(this.visit(arg.value)); if (!arg.identifier) { return token; } // No alias needed if it matches the default name if (arg.value instanceof ColumnReference) { const defaultName = arg.value.column.name; if (arg.identifier.name === defaultName) { return token; } } // Add alias if it is different from the default name token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'as')); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(this.visit(arg.identifier)); return token; } visitSelectClause(arg) { const token = new SqlPrintToken(SqlPrintTokenType.keyword, 'select', SqlPrintTokenContainerType.SelectClause); // Handle hints and DISTINCT as part of the keyword line let selectKeywordText = 'select'; // Add hint clauses immediately after SELECT (before DISTINCT) for (const hint of arg.hints) { selectKeywordText += ' ' + this.visit(hint).text; } // Add DISTINCT after hints (if present) if (arg.distinct) { const distinctToken = arg.distinct.accept(this); if (distinctToken.innerTokens && distinctToken.innerTokens.length > 0) { // For compound DISTINCT tokens (like DISTINCT ON), concatenate all parts let distinctText = distinctToken.text; for (const innerToken of distinctToken.innerTokens) { distinctText += this.flattenTokenText(innerToken); } selectKeywordText += ' ' + distinctText; } else { selectKeywordText += ' ' + distinctToken.text; } } // Update the token text to include hints and DISTINCT token.text = selectKeywordText; token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); for (let i = 0; i < arg.items.length; i++) { if (i > 0) { token.innerTokens.push(...SqlPrintTokenParser.commaSpaceTokens()); } token.innerTokens.push(this.visit(arg.items[i])); } return token; } flattenTokenText(token) { let result = token.text; if (token.innerTokens) { for (const innerToken of token.innerTokens) { result += this.flattenTokenText(innerToken); } } return result; } visitHintClause(arg) { const token = new SqlPrintToken(SqlPrintTokenType.value, arg.getFullHint()); return token; } visitDistinct(arg) { const token = new SqlPrintToken(SqlPrintTokenType.keyword, 'distinct'); return token; } visitDistinctOn(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.DistinctOn); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'distinct on')); token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN); token.innerTokens.push(arg.value.accept(this)); token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN); return token; } visitTableSource(arg) { // Print table name with optional namespaces and alias let fullName = ''; if (Array.isArray(arg.namespaces) && arg.namespaces.length > 0) { fullName = arg.namespaces.map(ns => ns.accept(this).text).join('.') + '.'; } fullName += arg.table.accept(this).text; const token = new SqlPrintToken(SqlPrintTokenType.value, fullName); // alias (if present and different from table name) if (arg.identifier && arg.identifier.name !== arg.table.name) { } return token; } visitSourceExpression(arg) { // Print source expression (e.g. "table", "table as t", "schema.table t") const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.SourceExpression); token.innerTokens.push(arg.datasource.accept(this)); if (!arg.aliasExpression) { return token; } if (arg.datasource instanceof TableSource) { // No alias needed if it matches the default name const defaultName = arg.datasource.table.name; if (arg.aliasExpression.table.name === defaultName) { return token; } token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'as')); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); // exclude column aliases token.innerTokens.push(arg.aliasExpression.accept(this)); return token; } else { // For other source types, just print the alias token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'as')); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); // included column aliases token.innerTokens.push(arg.aliasExpression.accept(this)); return token; } } visitFromClause(arg) { const token = new SqlPrintToken(SqlPrintTokenType.keyword, 'from', SqlPrintTokenContainerType.FromClause); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(this.visit(arg.source)); if (arg.joins) { for (let i = 0; i < arg.joins.length; i++) { token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(this.visit(arg.joins[i])); } } return token; } visitJoinClause(arg) { // Print join clause: [joinType] [lateral] [source] [on/using ...] const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.JoinClause); // join type (e.g. inner join, left join, etc) token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, arg.joinType.value)); if (arg.lateral) { token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'lateral')); } token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(this.visit(arg.source)); if (arg.condition) { token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(this.visit(arg.condition)); } return token; } visitJoinOnClause(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.JoinOnClause); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'on')); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(this.visit(arg.condition)); return token; } visitJoinUsingClause(arg) { const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.JoinUsingClause); token.innerTokens.push(new SqlPrintToken(SqlPrintTokenType.keyword, 'using')); token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN); token.innerTokens.push(this.visit(arg.condition)); token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN); return token; } visitFunctionSource(arg) { // Print function source: [functionName]([args]) const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.FunctionSource); token.innerTokens.push(arg.qualifiedName.accept(this)); token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN); if (arg.argument) { token.innerTokens.push(this.visit(arg.argument)); } token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN); return token; } visitSourceAliasExpression(arg) { // Print source alias expression: [source] as [alias] const token = new SqlPrintToken(SqlPrintTokenType.container, '', SqlPrintTokenContainerType.SourceAliasExpression); token.innerTokens.push(this.visit(arg.table)); if (arg.columns) { token.innerTokens.push(SqlPrintTokenParser.PAREN_OPEN_TOKEN); for (let i = 0; i < arg.columns.length; i++) { if (i > 0) { token.innerTokens.push(...SqlPrintTokenParser.argumentCommaSpaceTokens()); } token.innerTokens.push(this.visit(arg.columns[i])); } token.innerTokens.push(SqlPrintTokenParser.PAREN_CLOSE_TOKEN); } return token; } visitWhereClause(arg) { const token = new SqlPrintToken(SqlPrintTokenType.keyword, 'where', SqlPrintTokenContainerType.WhereClause); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); token.innerTokens.push(this.visit(arg.condition)); return token; } visitGroupByClause(arg) { const token = new SqlPrintToken(SqlPrintTokenType.keyword, 'group by', SqlPrintTokenContainerType.GroupByClause); token.innerTokens.push(SqlPrintTokenParser.SPACE_TOKEN); for (let i = 0; i < arg.grouping.length; i++) { if (i > 0) { token.innerTokens.push(...SqlPrintTokenParser.commaSpaceTokens());