Skip to content

Commit d86454c

Browse files
authored
Merge pull request #476 from liuxy0551/feat_genericSql_emptyColumn
feat(generic): entity collector with empty column
2 parents 88d2c4c + 378ce74 commit d86454c

11 files changed

Lines changed: 1945 additions & 1175 deletions

File tree

‎src/grammar/generic/GenericSql.g4‎

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ setQuantifier
8686
;
8787

8888
selectItem
89-
: expression (KW_AS? alias=identifier)?
90-
| qualifiedName '.' ASTERISK
91-
| ASTERISK
89+
: expression (KW_AS? alias=identifier)? # selectExpressionElement
90+
| qualifiedName '.' ASTERISK # selectStarElement
91+
| ASTERISK # selectAllElement
92+
| {this.shouldMatchEmpty()}? emptyColumn # selectEmptyElement
9293
;
9394

9495
fromClause
@@ -118,15 +119,15 @@ relationPrimary
118119
;
119120

120121
whereClause
121-
: KW_WHERE expression
122+
: KW_WHERE (expression | {this.shouldMatchEmpty()}? emptyColumn)
122123
;
123124

124125
groupByClause
125126
: KW_GROUP KW_BY setQuantifier? expression (',' expression)*
126127
;
127128

128129
havingClause
129-
: KW_HAVING expression
130+
: KW_HAVING (expression | {this.shouldMatchEmpty()}? emptyColumn)
130131
;
131132

132133
orderByClause
@@ -177,7 +178,11 @@ tableElement
177178
;
178179

179180
columnDefinition
180-
: columnRef dataType (KW_NOT KW_NULL)? (KW_DEFAULT expression)? (KW_PRIMARY KW_KEY)?
181+
: columnRefCreate colType=dataType (KW_NOT KW_NULL)? (KW_DEFAULT expression)? (KW_PRIMARY KW_KEY)?
182+
;
183+
184+
columnRefCreate
185+
: identifier
181186
;
182187

183188
tableConstraint
@@ -251,17 +256,17 @@ valueExpression
251256
;
252257

253258
primaryExpression
254-
: literal # literalExpression
255-
| qualifiedName '(' (setQuantifier? expression (',' expression)*)? ')' # functionCall
256-
| KW_CASE whenClause+ (KW_ELSE expression)? KW_END # searchedCaseExpression
257-
| KW_CASE expression whenClause+ (KW_ELSE expression)? KW_END # simpleCaseExpression
258-
| KW_CAST '(' expression KW_AS dataType ')' # castExpression
259-
| KW_COALESCE '(' expression (',' expression)* ')' # coalesceExpression
260-
| KW_NULLIF '(' valueExpression ',' valueExpression ')' # nullIfExpression
261-
| '(' expression ')' # parenthesizedExpression
262-
| KW_EXISTS '(' queryStatement ')' # existsExpression
263-
| subqueryExpression # subqueryExpressionDefault
264-
| qualifiedName # columnReference
259+
: literal # literalExpression
260+
| functionName '(' (setQuantifier? expression (',' expression)*)? ')' # functionCall
261+
| KW_CASE whenClause+ (KW_ELSE expression)? KW_END # searchedCaseExpression
262+
| KW_CASE expression whenClause+ (KW_ELSE expression)? KW_END # simpleCaseExpression
263+
| KW_CAST '(' expression KW_AS dataType ')' # castExpression
264+
| KW_COALESCE '(' expression (',' expression)* ')' # coalesceExpression
265+
| KW_NULLIF '(' valueExpression ',' valueExpression ')' # nullIfExpression
266+
| '(' expression ')' # parenthesizedExpression
267+
| KW_EXISTS '(' queryStatement ')' # existsExpression
268+
| subqueryExpression # subqueryExpressionDefault
269+
| columnName # columnReference
265270
;
266271

267272
whenClause
@@ -328,8 +333,22 @@ qualifiedName
328333
: identifier ('.' identifier)*
329334
;
330335

336+
qualifiedNameAllowEmpty
337+
: qualifiedName
338+
| {this.shouldMatchEmpty()}? qualifiedName '.' emptyColumn
339+
;
340+
331341
columnRef
332342
: identifier
343+
| {this.shouldMatchEmpty()}? emptyColumn
344+
;
345+
346+
columnName
347+
: qualifiedNameAllowEmpty
348+
;
349+
350+
emptyColumn
351+
:
333352
;
334353

335354
tableName
@@ -340,6 +359,10 @@ tableNameCreate
340359
: qualifiedName
341360
;
342361

362+
functionName
363+
: qualifiedName
364+
;
365+
343366
// 非保留关键字 — 可以用作标识符的关键字
344367
// 核心结构关键字 (SELECT, FROM, WHERE, CREATE, TABLE, INSERT, UPDATE, DELETE, DROP, ALTER, SET 等) 是保留的,不能用作标识符
345368
nonReserved

‎src/lib/generic/GenericSql.interp‎

Lines changed: 6 additions & 1 deletion
Large diffs are not rendered by default.

‎src/lib/generic/GenericSqlListener.ts‎

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ import { QueryTermContext } from "./GenericSqlParser.js";
2727
import { QueryPrimaryContext } from "./GenericSqlParser.js";
2828
import { QuerySpecificationContext } from "./GenericSqlParser.js";
2929
import { SetQuantifierContext } from "./GenericSqlParser.js";
30-
import { SelectItemContext } from "./GenericSqlParser.js";
30+
import { SelectExpressionElementContext } from "./GenericSqlParser.js";
31+
import { SelectStarElementContext } from "./GenericSqlParser.js";
32+
import { SelectAllElementContext } from "./GenericSqlParser.js";
33+
import { SelectEmptyElementContext } from "./GenericSqlParser.js";
3134
import { FromClauseContext } from "./GenericSqlParser.js";
3235
import { SimpleRelationContext } from "./GenericSqlParser.js";
3336
import { JoinRelationContext } from "./GenericSqlParser.js";
@@ -49,6 +52,7 @@ import { DeleteStatementContext } from "./GenericSqlParser.js";
4952
import { CreateTableStatementContext } from "./GenericSqlParser.js";
5053
import { TableElementContext } from "./GenericSqlParser.js";
5154
import { ColumnDefinitionContext } from "./GenericSqlParser.js";
55+
import { ColumnRefCreateContext } from "./GenericSqlParser.js";
5256
import { TableConstraintContext } from "./GenericSqlParser.js";
5357
import { AlterTableStatementContext } from "./GenericSqlParser.js";
5458
import { DropTableStatementContext } from "./GenericSqlParser.js";
@@ -95,9 +99,13 @@ import { StringLiteralContext } from "./GenericSqlParser.js";
9599
import { BinaryLiteralContext } from "./GenericSqlParser.js";
96100
import { IdentifierContext } from "./GenericSqlParser.js";
97101
import { QualifiedNameContext } from "./GenericSqlParser.js";
102+
import { QualifiedNameAllowEmptyContext } from "./GenericSqlParser.js";
98103
import { ColumnRefContext } from "./GenericSqlParser.js";
104+
import { ColumnNameContext } from "./GenericSqlParser.js";
105+
import { EmptyColumnContext } from "./GenericSqlParser.js";
99106
import { TableNameContext } from "./GenericSqlParser.js";
100107
import { TableNameCreateContext } from "./GenericSqlParser.js";
108+
import { FunctionNameContext } from "./GenericSqlParser.js";
101109
import { NonReservedContext } from "./GenericSqlParser.js";
102110

103111

@@ -311,15 +319,53 @@ export class GenericSqlListener implements ParseTreeListener {
311319
*/
312320
exitSetQuantifier?: (ctx: SetQuantifierContext) => void;
313321
/**
314-
* Enter a parse tree produced by `GenericSqlParser.selectItem`.
322+
* Enter a parse tree produced by the `selectExpressionElement`
323+
* labeled alternative in `GenericSqlParser.selectItem`.
315324
* @param ctx the parse tree
316325
*/
317-
enterSelectItem?: (ctx: SelectItemContext) => void;
326+
enterSelectExpressionElement?: (ctx: SelectExpressionElementContext) => void;
318327
/**
319-
* Exit a parse tree produced by `GenericSqlParser.selectItem`.
328+
* Exit a parse tree produced by the `selectExpressionElement`
329+
* labeled alternative in `GenericSqlParser.selectItem`.
320330
* @param ctx the parse tree
321331
*/
322-
exitSelectItem?: (ctx: SelectItemContext) => void;
332+
exitSelectExpressionElement?: (ctx: SelectExpressionElementContext) => void;
333+
/**
334+
* Enter a parse tree produced by the `selectStarElement`
335+
* labeled alternative in `GenericSqlParser.selectItem`.
336+
* @param ctx the parse tree
337+
*/
338+
enterSelectStarElement?: (ctx: SelectStarElementContext) => void;
339+
/**
340+
* Exit a parse tree produced by the `selectStarElement`
341+
* labeled alternative in `GenericSqlParser.selectItem`.
342+
* @param ctx the parse tree
343+
*/
344+
exitSelectStarElement?: (ctx: SelectStarElementContext) => void;
345+
/**
346+
* Enter a parse tree produced by the `selectAllElement`
347+
* labeled alternative in `GenericSqlParser.selectItem`.
348+
* @param ctx the parse tree
349+
*/
350+
enterSelectAllElement?: (ctx: SelectAllElementContext) => void;
351+
/**
352+
* Exit a parse tree produced by the `selectAllElement`
353+
* labeled alternative in `GenericSqlParser.selectItem`.
354+
* @param ctx the parse tree
355+
*/
356+
exitSelectAllElement?: (ctx: SelectAllElementContext) => void;
357+
/**
358+
* Enter a parse tree produced by the `selectEmptyElement`
359+
* labeled alternative in `GenericSqlParser.selectItem`.
360+
* @param ctx the parse tree
361+
*/
362+
enterSelectEmptyElement?: (ctx: SelectEmptyElementContext) => void;
363+
/**
364+
* Exit a parse tree produced by the `selectEmptyElement`
365+
* labeled alternative in `GenericSqlParser.selectItem`.
366+
* @param ctx the parse tree
367+
*/
368+
exitSelectEmptyElement?: (ctx: SelectEmptyElementContext) => void;
323369
/**
324370
* Enter a parse tree produced by `GenericSqlParser.fromClause`.
325371
* @param ctx the parse tree
@@ -538,6 +584,16 @@ export class GenericSqlListener implements ParseTreeListener {
538584
* @param ctx the parse tree
539585
*/
540586
exitColumnDefinition?: (ctx: ColumnDefinitionContext) => void;
587+
/**
588+
* Enter a parse tree produced by `GenericSqlParser.columnRefCreate`.
589+
* @param ctx the parse tree
590+
*/
591+
enterColumnRefCreate?: (ctx: ColumnRefCreateContext) => void;
592+
/**
593+
* Exit a parse tree produced by `GenericSqlParser.columnRefCreate`.
594+
* @param ctx the parse tree
595+
*/
596+
exitColumnRefCreate?: (ctx: ColumnRefCreateContext) => void;
541597
/**
542598
* Enter a parse tree produced by `GenericSqlParser.tableConstraint`.
543599
* @param ctx the parse tree
@@ -1064,6 +1120,16 @@ export class GenericSqlListener implements ParseTreeListener {
10641120
* @param ctx the parse tree
10651121
*/
10661122
exitQualifiedName?: (ctx: QualifiedNameContext) => void;
1123+
/**
1124+
* Enter a parse tree produced by `GenericSqlParser.qualifiedNameAllowEmpty`.
1125+
* @param ctx the parse tree
1126+
*/
1127+
enterQualifiedNameAllowEmpty?: (ctx: QualifiedNameAllowEmptyContext) => void;
1128+
/**
1129+
* Exit a parse tree produced by `GenericSqlParser.qualifiedNameAllowEmpty`.
1130+
* @param ctx the parse tree
1131+
*/
1132+
exitQualifiedNameAllowEmpty?: (ctx: QualifiedNameAllowEmptyContext) => void;
10671133
/**
10681134
* Enter a parse tree produced by `GenericSqlParser.columnRef`.
10691135
* @param ctx the parse tree
@@ -1074,6 +1140,26 @@ export class GenericSqlListener implements ParseTreeListener {
10741140
* @param ctx the parse tree
10751141
*/
10761142
exitColumnRef?: (ctx: ColumnRefContext) => void;
1143+
/**
1144+
* Enter a parse tree produced by `GenericSqlParser.columnName`.
1145+
* @param ctx the parse tree
1146+
*/
1147+
enterColumnName?: (ctx: ColumnNameContext) => void;
1148+
/**
1149+
* Exit a parse tree produced by `GenericSqlParser.columnName`.
1150+
* @param ctx the parse tree
1151+
*/
1152+
exitColumnName?: (ctx: ColumnNameContext) => void;
1153+
/**
1154+
* Enter a parse tree produced by `GenericSqlParser.emptyColumn`.
1155+
* @param ctx the parse tree
1156+
*/
1157+
enterEmptyColumn?: (ctx: EmptyColumnContext) => void;
1158+
/**
1159+
* Exit a parse tree produced by `GenericSqlParser.emptyColumn`.
1160+
* @param ctx the parse tree
1161+
*/
1162+
exitEmptyColumn?: (ctx: EmptyColumnContext) => void;
10771163
/**
10781164
* Enter a parse tree produced by `GenericSqlParser.tableName`.
10791165
* @param ctx the parse tree
@@ -1094,6 +1180,16 @@ export class GenericSqlListener implements ParseTreeListener {
10941180
* @param ctx the parse tree
10951181
*/
10961182
exitTableNameCreate?: (ctx: TableNameCreateContext) => void;
1183+
/**
1184+
* Enter a parse tree produced by `GenericSqlParser.functionName`.
1185+
* @param ctx the parse tree
1186+
*/
1187+
enterFunctionName?: (ctx: FunctionNameContext) => void;
1188+
/**
1189+
* Exit a parse tree produced by `GenericSqlParser.functionName`.
1190+
* @param ctx the parse tree
1191+
*/
1192+
exitFunctionName?: (ctx: FunctionNameContext) => void;
10971193
/**
10981194
* Enter a parse tree produced by `GenericSqlParser.nonReserved`.
10991195
* @param ctx the parse tree

0 commit comments

Comments
 (0)