Skip to main content

ExpressionBuilder

@webda/ql


Class: ExpressionBuilder

Defined in: packages/ql/src/query.ts:118

ANTLR parse tree visitor that builds an optimized Expression AST from the parsed tokens

Converts the ANTLR parse tree into a flat, evaluatable expression tree. Automatically flattens nested AND/OR expressions of the same type to reduce depth.

Extends​

  • AbstractParseTreeVisitor<Query>

Extended by​

Implements​

  • WebdaQLParserVisitor<any>

Constructors​

Constructor​

new ExpressionBuilder(): ExpressionBuilder

Returns​

ExpressionBuilder

Inherited from​

AbstractParseTreeVisitor<Query>.constructor

Properties​

limit​

limit: number

Defined in: packages/ql/src/query.ts:122

Parsed LIMIT value, if present


offset​

offset: string

Defined in: packages/ql/src/query.ts:126

Parsed OFFSET continuation token, if present


orderBy​

orderBy: OrderBy[]

Defined in: packages/ql/src/query.ts:130

Parsed ORDER BY clauses, if present

Methods​

aggregateResult()​

protected aggregateResult(aggregate, nextResult): Query

Defined in: node_modules/.pnpm/antlr4ts@0.5.0-alpha.4/node_modules/antlr4ts/tree/AbstractParseTreeVisitor.d.ts:77

Aggregates the results of visiting multiple children of a node. After either all children are visited or #shouldVisitNextChild returns false, the aggregate value is returned as the result of #visitChildren.

The default implementation returns nextResult, meaning #visitChildren will return the result of the last child visited (or return the initial value if the node has no children).

Parameters​

aggregate​

Query

The previous aggregate value. In the default implementation, the aggregate value is initialized to #defaultResult, which is passed as the aggregate argument to this method after the first child node is visited.

nextResult​

Query

The result of the immediately preceeding call to visit a child node.

Returns​

Query

The updated aggregate result.

Inherited from​

AbstractParseTreeVisitor.aggregateResult


defaultResult()​

protected defaultResult(): Query

Defined in: packages/ql/src/query.ts:136

Default result when no expression is matched (empty AND, always true)

Returns​

Query

an empty AND query

Overrides​

AbstractParseTreeVisitor.defaultResult


getComparison()​

getComparison(ctx): any[]

Defined in: packages/ql/src/query.ts:247

Recursively flatten nested logical expressions of the same type

ANTLR produces right-recursive trees like a AND (b AND (c AND d)). This method flattens them into [a, b, c, d] so a single LogicalExpression holds all children at one level.

Parameters​

ctx​

AndLogicExpressionContext | OrLogicExpressionContext

an AND or OR logical expression context

Returns​

any[]

flat array of child parse tree nodes


getLimit()​

getLimit(): number

Defined in: packages/ql/src/query.ts:155

Get parsed LIMIT value

Returns​

number

the limit value


getOffset()​

getOffset(): string

Defined in: packages/ql/src/query.ts:147

Get parsed OFFSET continuation token

Returns​

string

the offset token


shouldVisitNextChild()​

protected shouldVisitNextChild(node, currentResult): boolean

Defined in: node_modules/.pnpm/antlr4ts@0.5.0-alpha.4/node_modules/antlr4ts/tree/AbstractParseTreeVisitor.d.ts:102

This method is called after visiting each child in #visitChildren. This method is first called before the first child is visited; at that point currentResult will be the initial value (in the default implementation, the initial value is returned by a call to #defaultResult. This method is not called after the last child is visited.

The default implementation always returns true, indicating that visitChildren should only return after all children are visited. One reason to override this method is to provide a "short circuit" evaluation option for situations where the result of visiting a single child has the potential to determine the result of the visit operation as a whole.

Parameters​

node​

RuleNode

The RuleNode whose children are currently being visited.

currentResult​

Query

The current aggregate result of the children visited to the current point.

Returns​

boolean

true to continue visiting children. Otherwise return false to stop visiting children and immediately return the current aggregate result from #visitChildren.

Inherited from​

AbstractParseTreeVisitor.shouldVisitNextChild


visit()​

visit(tree): Query

Defined in: node_modules/.pnpm/antlr4ts@0.5.0-alpha.4/node_modules/antlr4ts/tree/AbstractParseTreeVisitor.d.ts:17

The default implementation calls ParseTree#accept on the specified tree.

Parameters​

tree​

ParseTree

Returns​

Query

Implementation of​

WebdaQLParserVisitor.visit

Inherited from​

AbstractParseTreeVisitor.visit


visitAndLogicExpression()​

visitAndLogicExpression(ctx): Expression

Defined in: packages/ql/src/query.ts:278

Get the AndExpression, regrouping all the parameters

By default the parser is doing a AND (b AND (c AND d)) creating 3 depth expressions This visitor simplify to a AND b AND c AND d with only one Expression

Parameters​

ctx​

AndLogicExpressionContext

the AND logic expression context

Returns​

Expression

the flattened AndExpression

Implementation of​

WebdaQLParserVisitor.visitAndLogicExpression


visitAtomExpression()​

visitAtomExpression(ctx): Expression

Defined in: packages/ql/src/query.ts:377

A bare atom used as an expression: only TRUE / FALSE are valid

Parameters​

ctx​

AtomExpressionContext

the atom expression context

Returns​

Expression

the constant expression

Throws​

for identifiers, numbers and strings

Implementation of​

WebdaQLParserVisitor.visitAtomExpression


visitBinaryComparisonExpression()​

visitBinaryComparisonExpression(ctx): ComparisonExpression<ComparisonOperator>

Defined in: packages/ql/src/query.ts:290

Implement the BinaryComparison with all methods managed

Parameters​

ctx​

BinaryComparisonExpressionContext

the binary comparison context

Returns​

ComparisonExpression<ComparisonOperator>

the comparison expression

Implementation of​

WebdaQLParserVisitor.visitBinaryComparisonExpression


visitBooleanLiteral()​

visitBooleanLiteral(ctx): boolean

Defined in: packages/ql/src/query.ts:399

Read the boolean literal

Parameters​

ctx​

BooleanLiteralContext

the boolean literal context

Returns​

boolean

the boolean value

Implementation of​

WebdaQLParserVisitor.visitBooleanLiteral


visitChildren()​

visitChildren(node): Query

Defined in: node_modules/.pnpm/antlr4ts@0.5.0-alpha.4/node_modules/antlr4ts/tree/AbstractParseTreeVisitor.d.ts:33

The default implementation initializes the aggregate result to #defaultResult defaultResult(). Before visiting each child, it calls #shouldVisitNextChild shouldVisitNextChild; if the result is false no more children are visited and the current aggregate result is returned. After visiting a child, the aggregate result is updated by calling #aggregateResult aggregateResult with the previous aggregate result and the result of visiting the child.

The default implementation is not safe for use in visitors that modify the tree structure. Visitors that modify the tree should override this method to behave properly in respect to the specific algorithm in use.

Parameters​

node​

RuleNode

Returns​

Query

Implementation of​

WebdaQLParserVisitor.visitChildren

Inherited from​

AbstractParseTreeVisitor.visitChildren


visitContainsExpression()​

visitContainsExpression(ctx): ComparisonExpression<"CONTAINS">

Defined in: packages/ql/src/query.ts:332

Map the a CONTAINS 'b'

Parameters​

ctx​

ContainsExpressionContext

the CONTAINS expression context

Returns​

ComparisonExpression<"CONTAINS">

the comparison expression

Implementation of​

WebdaQLParserVisitor.visitContainsExpression


visitErrorNode()​

visitErrorNode(node): Query

Defined in: node_modules/.pnpm/antlr4ts@0.5.0-alpha.4/node_modules/antlr4ts/tree/AbstractParseTreeVisitor.d.ts:47

The default implementation returns the result of #defaultResult defaultResult.

Parameters​

node​

ErrorNode

Returns​

Query

Implementation of​

WebdaQLParserVisitor.visitErrorNode

Inherited from​

AbstractParseTreeVisitor.visitErrorNode


visitInExpression()​

visitInExpression(ctx): ComparisonExpression<"IN">

Defined in: packages/ql/src/query.ts:321

Map the a IN ['b','c']

Parameters​

ctx​

InExpressionContext

the IN expression context

Returns​

ComparisonExpression<"IN">

the comparison expression

Implementation of​

WebdaQLParserVisitor.visitInExpression


visitIntegerLiteral()​

visitIntegerLiteral(ctx): number

Defined in: packages/ql/src/query.ts:408

Read the number literal

Parameters​

ctx​

IntegerLiteralContext

the integer literal context

Returns​

number

the parsed integer value

Implementation of​

WebdaQLParserVisitor.visitIntegerLiteral


visitIsNotNullExpression()​

visitIsNotNullExpression(ctx): ComparisonExpression<"IS NOT NULL">

Defined in: packages/ql/src/query.ts:352

Map the a IS NOT NULL

Parameters​

ctx​

IsNotNullExpressionContext

the IS NOT NULL expression context

Returns​

ComparisonExpression<"IS NOT NULL">

the comparison expression

Implementation of​

WebdaQLParserVisitor.visitIsNotNullExpression


visitIsNullExpression()​

visitIsNullExpression(ctx): ComparisonExpression<"IS NULL">

Defined in: packages/ql/src/query.ts:343

Map the a IS NULL

Parameters​

ctx​

IsNullExpressionContext

the IS NULL expression context

Returns​

ComparisonExpression<"IS NULL">

the comparison expression

Implementation of​

WebdaQLParserVisitor.visitIsNullExpression


visitLikeExpression()​

visitLikeExpression(ctx): ComparisonExpression<"LIKE">

Defined in: packages/ql/src/query.ts:310

Visit a LIKE expression (e.g. field LIKE '%pattern_')

Parameters​

ctx​

LikeExpressionContext

the LIKE expression context

Returns​

ComparisonExpression<"LIKE">

the comparison expression

Implementation of​

WebdaQLParserVisitor.visitLikeExpression


visitLimitExpression()​

visitLimitExpression(ctx): void

Defined in: packages/ql/src/query.ts:163

Visit a LIMIT clause and store the integer value

Parameters​

ctx​

LimitExpressionContext

the limit expression context

Returns​

void

Implementation of​

WebdaQLParserVisitor.visitLimitExpression


visitNumberLiteral()​

visitNumberLiteral(ctx): number

Defined in: packages/ql/src/query.ts:417

Read a signed or decimal number literal

Parameters​

ctx​

NumberLiteralContext

the number literal context

Returns​

number

the parsed number

Implementation of​

WebdaQLParserVisitor.visitNumberLiteral


visitOffsetExpression()​

visitOffsetExpression(ctx): void

Defined in: packages/ql/src/query.ts:171

Visit an OFFSET clause and store the string continuation token

Parameters​

ctx​

OffsetExpressionContext

the offset expression context

Returns​

void

Implementation of​

WebdaQLParserVisitor.visitOffsetExpression


visitOrderExpression()​

visitOrderExpression(ctx): void

Defined in: packages/ql/src/query.ts:191

Read the order by values

Parameters​

ctx​

OrderExpressionContext

the order expression context

Returns​

void

Implementation of​

WebdaQLParserVisitor.visitOrderExpression


visitOrderFieldExpression()​

visitOrderFieldExpression(ctx): OrderBy

Defined in: packages/ql/src/query.ts:180

Visit a order field expression

Parameters​

ctx​

OrderFieldExpressionContext

the order field expression context

Returns​

OrderBy

the parsed OrderBy

Implementation of​

WebdaQLParserVisitor.visitOrderFieldExpression


visitOrLogicExpression()​

visitOrLogicExpression(ctx): Expression

Defined in: packages/ql/src/query.ts:364

Get the OrExpression, regrouping all the parameters

By default the parser is doing a OR (b OR (c OR d)) creating 3 depth expressions This visitor simplify to a OR b OR c OR d with only one Expression

Parameters​

ctx​

OrLogicExpressionContext

the OR logic expression context

Returns​

Expression

the flattened OrExpression

Implementation of​

WebdaQLParserVisitor.visitOrLogicExpression


visitParameter()​

visitParameter(ctx): never

Defined in: packages/ql/src/query.ts:426

A ? or :name placeholder reaching evaluation was never bound

Parameters​

ctx​

ParameterContext

the parameter context

Returns​

never

Throws​

always: use bind() or pass the parameters to query()

Implementation of​

WebdaQLParserVisitor.visitParameter


visitSetExpression()​

visitSetExpression(ctx): value[]

Defined in: packages/ql/src/query.ts:301

Visit each value of the [..., ..., ...] set

Parameters​

ctx​

SetExpressionContext

the set expression context

Returns​

value[]

the array of values

Implementation of​

WebdaQLParserVisitor.visitSetExpression


visitStringLiteral()​

visitStringLiteral(ctx): string

Defined in: packages/ql/src/query.ts:390

Read a string literal, stripping the surrounding single or double quotes

Parameters​

ctx​

StringLiteralContext

the string literal context

Returns​

string

the unquoted string value

Implementation of​

WebdaQLParserVisitor.visitStringLiteral


visitTerminal()​

visitTerminal(node): Query

Defined in: node_modules/.pnpm/antlr4ts@0.5.0-alpha.4/node_modules/antlr4ts/tree/AbstractParseTreeVisitor.d.ts:40

The default implementation returns the result of #defaultResult defaultResult.

Parameters​

node​

TerminalNode

Returns​

Query

Implementation of​

WebdaQLParserVisitor.visitTerminal

Inherited from​

AbstractParseTreeVisitor.visitTerminal


visitWebdaql()​

visitWebdaql(ctx): Query

Defined in: packages/ql/src/query.ts:205

Visit the root webdaql rule and build the complete Query

Parses filter expression, ORDER BY, LIMIT, and OFFSET clauses. Returns an empty AND expression (always true) when no filter is present.

Parameters​

ctx​

WebdaqlContext

the webdaql parse context

Returns​

Query

the built Query

Implementation of​

WebdaQLParserVisitor.visitWebdaql