Skip to content

Commit 58b7f14

Browse files
author
zhaoge
committed
feat(mysql): collect entity's attribute(comment,colType,alias)
1 parent 418e38e commit 58b7f14

File tree

5 files changed

+141
-52
lines changed

5 files changed

+141
-52
lines changed

src/grammar/mysql/MySqlParser.g4

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ functionParameter
442442
;
443443

444444
routineOption
445-
: KW_COMMENT STRING_LITERAL # routineComment
445+
: KW_COMMENT comment=STRING_LITERAL # routineComment
446446
| KW_LANGUAGE KW_SQL # routineLanguage
447447
| KW_NOT? KW_DETERMINISTIC # routineBehavior
448448
| (KW_CONTAINS KW_SQL | KW_NO KW_SQL | KW_READS KW_SQL KW_DATA | KW_MODIFIES KW_SQL KW_DATA) # routineData
@@ -483,7 +483,7 @@ constraintSymbol
483483
;
484484

485485
columnDefinition
486-
: dataType columnConstraint*
486+
: colType=dataType columnConstraint*
487487
;
488488

489489
columnConstraint
@@ -494,7 +494,7 @@ columnConstraint
494494
| (KW_AUTO_INCREMENT | KW_ON KW_UPDATE currentTimestamp) # autoIncrementColumnConstraint
495495
| KW_PRIMARY? KW_KEY # primaryKeyColumnConstraint
496496
| KW_UNIQUE KW_KEY? # uniqueKeyColumnConstraint
497-
| KW_COMMENT STRING_LITERAL # commentColumnConstraint
497+
| KW_COMMENT comment=STRING_LITERAL # commentColumnConstraint
498498
| KW_COLUMN_FORMAT colformat=(KW_FIXED | KW_DYNAMIC | KW_DEFAULT) # formatColumnConstraint
499499
| KW_STORAGE storageval=(KW_DISK | KW_MEMORY | KW_DEFAULT) # storageColumnConstraint
500500
| referenceDefinition # referenceColumnConstraint
@@ -536,7 +536,7 @@ tableOption
536536
| KW_DEFAULT? charSet '='? (charsetName | KW_DEFAULT) # tableOptionCharset
537537
| (KW_CHECKSUM | KW_PAGE_CHECKSUM) '='? boolValue=('0' | '1') # tableOptionChecksum
538538
| KW_DEFAULT? KW_COLLATE '='? collationName # tableOptionCollate
539-
| KW_COMMENT '='? STRING_LITERAL # tableOptionComment
539+
| KW_COMMENT '='? comment=STRING_LITERAL # tableOptionComment
540540
| KW_COMPRESSION '='? (STRING_LITERAL | ID) # tableOptionCompression
541541
| KW_CONNECTION '='? STRING_LITERAL # tableOptionConnection
542542
| (KW_DATA | KW_INDEX) KW_DIRECTORY '='? STRING_LITERAL # tableOptionDataDirectory

src/lib/mysql/MySqlParser.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6703,7 +6703,7 @@ export class MySqlParser extends SQLParserBase {
67036703
this.state = 1697;
67046704
this.match(MySqlParser.KW_COMMENT);
67056705
this.state = 1698;
6706-
this.match(MySqlParser.STRING_LITERAL);
6706+
(localContext as RoutineCommentContext)._comment = this.match(MySqlParser.STRING_LITERAL);
67076707
}
67086708
break;
67096709
case MySqlParser.KW_LANGUAGE:
@@ -7368,7 +7368,7 @@ export class MySqlParser extends SQLParserBase {
73687368
this.enterOuterAlt(localContext, 1);
73697369
{
73707370
this.state = 1851;
7371-
this.dataType();
7371+
localContext._colType = this.dataType();
73727372
this.state = 1855;
73737373
this.errorHandler.sync(this);
73747374
alternative = this.interpreter.adaptivePredict(this.tokenStream, 172, this.context);
@@ -7518,7 +7518,7 @@ export class MySqlParser extends SQLParserBase {
75187518
this.state = 1877;
75197519
this.match(MySqlParser.KW_COMMENT);
75207520
this.state = 1878;
7521-
this.match(MySqlParser.STRING_LITERAL);
7521+
(localContext as CommentColumnConstraintContext)._comment = this.match(MySqlParser.STRING_LITERAL);
75227522
}
75237523
break;
75247524
case MySqlParser.KW_COLUMN_FORMAT:
@@ -8149,7 +8149,7 @@ export class MySqlParser extends SQLParserBase {
81498149
}
81508150

81518151
this.state = 1998;
8152-
this.match(MySqlParser.STRING_LITERAL);
8152+
(localContext as TableOptionCommentContext)._comment = this.match(MySqlParser.STRING_LITERAL);
81538153
}
81548154
break;
81558155
case 10:
@@ -55075,6 +55075,7 @@ export class RoutineLanguageContext extends RoutineOptionContext {
5507555075
}
5507655076
}
5507755077
export class RoutineCommentContext extends RoutineOptionContext {
55078+
public _comment?: Token | null;
5507855079
public constructor(ctx: RoutineOptionContext) {
5507955080
super(ctx.parent, ctx.invokingState);
5508055081
super.copyFrom(ctx);
@@ -55463,6 +55464,7 @@ export class ConstraintSymbolContext extends antlr.ParserRuleContext {
5546355464

5546455465

5546555466
export class ColumnDefinitionContext extends antlr.ParserRuleContext {
55467+
public _colType?: DataTypeContext;
5546655468
public constructor(parent: antlr.ParserRuleContext | null, invokingState: number) {
5546755469
super(parent, invokingState);
5546855470
}
@@ -55610,6 +55612,7 @@ export class AutoIncrementColumnConstraintContext extends ColumnConstraintContex
5561055612
}
5561155613
}
5561255614
export class CommentColumnConstraintContext extends ColumnConstraintContext {
55615+
public _comment?: Token | null;
5561355616
public constructor(ctx: ColumnConstraintContext) {
5561455617
super(ctx.parent, ctx.invokingState);
5561555618
super.copyFrom(ctx);
@@ -57075,6 +57078,7 @@ export class TableOptionSecondaryEngineAttributeContext extends TableOptionConte
5707557078
}
5707657079
}
5707757080
export class TableOptionCommentContext extends TableOptionContext {
57081+
public _comment?: Token | null;
5707857082
public constructor(ctx: TableOptionContext) {
5707957083
super(ctx.parent, ctx.invokingState);
5708057084
super.copyFrom(ctx);

src/parser/mysql/mysqlEntityCollector.ts

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import type {
1+
import {
22
ColumnCreateTableContext,
33
ColumnNameCreateContext,
44
CopyCreateTableContext,
55
CreateDatabaseContext,
6+
CreateDefinitionContext,
67
CreateFunctionContext,
78
CreateFunctionLoadableContext,
89
CreateViewContext,
@@ -15,12 +16,13 @@ import type {
1516
SingleStatementContext,
1617
TableNameContext,
1718
TableNameCreateContext,
19+
TableSourceContext,
1820
ViewNameContext,
1921
ViewNameCreateContext,
2022
} from '../../lib/mysql/MySqlParser';
2123
import type { MySqlParserListener } from '../../lib/mysql/MySqlParserListener';
24+
import { AttrName, EntityCollector, StmtContextType } from '../common/entityCollector';
2225
import { EntityContextType } from '../common/types';
23-
import { StmtContextType, EntityCollector } from '../common/entityCollector';
2426

2527
export class MySqlEntityCollector extends EntityCollector implements MySqlParserListener {
2628
/** ====== Entity Begin */
@@ -33,11 +35,28 @@ export class MySqlEntityCollector extends EntityCollector implements MySqlParser
3335
}
3436

3537
exitTableName(ctx: TableNameContext) {
36-
this.pushEntity(ctx, EntityContextType.TABLE);
38+
const needCollectAttr = this.getRootStmt()?.stmtContextType === StmtContextType.SELECT_STMT;
39+
this.pushEntity(
40+
ctx,
41+
EntityContextType.TABLE,
42+
needCollectAttr
43+
? [
44+
{
45+
attrName: AttrName.alias,
46+
endContextList: [TableSourceContext.name],
47+
},
48+
]
49+
: undefined
50+
);
3751
}
3852

3953
exitTableNameCreate(ctx: TableNameCreateContext) {
40-
this.pushEntity(ctx, EntityContextType.TABLE_CREATE);
54+
this.pushEntity(ctx, EntityContextType.TABLE_CREATE, [
55+
{
56+
attrName: AttrName.comment,
57+
endContextList: [ColumnCreateTableContext.name],
58+
},
59+
]);
4160
}
4261

4362
exitViewName(ctx: ViewNameContext) {
@@ -49,11 +68,25 @@ export class MySqlEntityCollector extends EntityCollector implements MySqlParser
4968
}
5069

5170
exitFunctionNameCreate(ctx: FunctionNameCreateContext) {
52-
this.pushEntity(ctx, EntityContextType.FUNCTION_CREATE);
71+
this.pushEntity(ctx, EntityContextType.FUNCTION_CREATE, [
72+
{
73+
attrName: AttrName.comment,
74+
endContextList: [CreateFunctionContext.name],
75+
},
76+
]);
5377
}
5478

5579
exitColumnNameCreate(ctx: ColumnNameCreateContext) {
56-
this.pushEntity(ctx, EntityContextType.COLUMN_CREATE);
80+
this.pushEntity(ctx, EntityContextType.COLUMN_CREATE, [
81+
{
82+
attrName: AttrName.comment,
83+
endContextList: [CreateDefinitionContext.name],
84+
},
85+
{
86+
attrName: AttrName.colType,
87+
endContextList: [CreateDefinitionContext.name],
88+
},
89+
]);
5790
}
5891

5992
/** ===== Statement begin */

0 commit comments

Comments
 (0)