Skip to content

Commit 418e38e

Browse files
author
zhaoge
committed
feat: update interface of attrInfo and alter entitycollect ts file
1 parent c291daf commit 418e38e

File tree

6 files changed

+172
-101
lines changed

6 files changed

+172
-101
lines changed

src/parser/common/entityCollector.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export function isColumnEntityContext(entity: EntityContext): entity is ColumnEn
128128
* what we need when collect attribute information
129129
* */
130130
interface AttrInfo {
131-
attrNameList: AttrName[];
131+
attrName: AttrName;
132132
endContextList: string[];
133133
}
134134

@@ -137,7 +137,7 @@ export function toEntityContext(
137137
type: EntityContextType,
138138
input: string,
139139
belongStmt: StmtContext,
140-
attrInfo?: AttrInfo
140+
attrInfo?: AttrInfo[]
141141
): EntityContext | null {
142142
const word = ctxToText(ctx, input);
143143
if (!word) return null;
@@ -169,9 +169,9 @@ export function toEntityContext(
169169
break;
170170
}
171171
if (attrInfo) {
172-
for (let k = 0; k < attrInfo?.attrNameList?.length; k++) {
173-
const attributeName: AttrName = attrInfo?.attrNameList[k];
174-
const attrToken = findAttribute(ctx, attributeName, attrInfo?.endContextList);
172+
for (let k = 0; k < attrInfo.length; k++) {
173+
const attributeName: AttrName = attrInfo[k]?.attrName;
174+
const attrToken = findAttribute(ctx, attributeName, attrInfo[k]?.endContextList);
175175
if (attrToken) {
176176
const attrVal: WordRange | TextSlice | null = isToken(attrToken)
177177
? tokenToWord(attrToken, input)
@@ -334,7 +334,7 @@ export abstract class EntityCollector {
334334
return stmtContext;
335335
}
336336

337-
protected pushEntity(ctx: ParserRuleContext, type: EntityContextType, attrInfo?: AttrInfo) {
337+
protected pushEntity(ctx: ParserRuleContext, type: EntityContextType, attrInfo?: AttrInfo[]) {
338338
const entityContext = toEntityContext(
339339
ctx,
340340
type,

src/parser/hive/hiveEntityCollector.ts

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ import { EntityContextType } from '../common/types';
2929
export class HiveEntityCollector extends EntityCollector implements HiveSqlParserListener {
3030
/** ====== Entity Begin */
3131
exitTableNameCreate(ctx: TableNameCreateContext) {
32-
this.pushEntity(ctx, EntityContextType.TABLE_CREATE, {
33-
attrNameList: [AttrName.comment],
34-
endContextList: [CreateTableStatementContext.name],
35-
});
32+
this.pushEntity(ctx, EntityContextType.TABLE_CREATE, [
33+
{
34+
attrName: AttrName.comment,
35+
endContextList: [CreateTableStatementContext.name],
36+
},
37+
]);
3638
}
3739

3840
exitTableName(ctx: TableNameContext) {
@@ -41,37 +43,49 @@ export class HiveEntityCollector extends EntityCollector implements HiveSqlParse
4143
ctx,
4244
EntityContextType.TABLE,
4345
needCollectAttr
44-
? {
45-
attrNameList: [AttrName.alias],
46-
endContextList: [TableSourceContext.name, UniqueJoinSourceContext.name],
47-
}
46+
? [
47+
{
48+
attrName: AttrName.alias,
49+
endContextList: [TableSourceContext.name, UniqueJoinSourceContext.name],
50+
},
51+
]
4852
: undefined
4953
);
5054
}
5155

5256
exitColumnNameCreate(ctx: ColumnNameCreateContext) {
53-
this.pushEntity(ctx, EntityContextType.COLUMN_CREATE, {
54-
attrNameList: [AttrName.comment, AttrName.colType],
55-
endContextList: [ColumnNameTypeConstraintContext.name, ColumnNameCommentContext.name],
56-
});
57+
this.pushEntity(ctx, EntityContextType.COLUMN_CREATE, [
58+
{
59+
attrName: AttrName.comment,
60+
endContextList: [ColumnNameCommentContext.name],
61+
},
62+
{
63+
attrName: AttrName.colType,
64+
endContextList: [ColumnNameTypeConstraintContext.name],
65+
},
66+
]);
5767
}
5868

5969
exitViewNameCreate(ctx: ViewNameCreateContext) {
60-
this.pushEntity(ctx, EntityContextType.VIEW_CREATE, {
61-
attrNameList: [AttrName.comment],
62-
endContextList: [CreateViewStatementContext.name],
63-
});
70+
this.pushEntity(ctx, EntityContextType.VIEW_CREATE, [
71+
{
72+
attrName: AttrName.comment,
73+
endContextList: [CreateViewStatementContext.name],
74+
},
75+
]);
6476
}
6577

6678
exitViewName(ctx: ViewNameContext) {
6779
this.pushEntity(ctx, EntityContextType.VIEW);
6880
}
6981

7082
exitDbSchemaNameCreate(ctx: DbSchemaNameCreateContext) {
71-
this.pushEntity(ctx, EntityContextType.DATABASE_CREATE, {
72-
attrNameList: [AttrName.comment],
73-
endContextList: [CreateDatabaseStatementContext.name],
74-
});
83+
this.pushEntity(ctx, EntityContextType.DATABASE_CREATE, [
84+
{
85+
attrName: AttrName.comment,
86+
endContextList: [CreateDatabaseStatementContext.name],
87+
},
88+
]);
7589
}
7690

7791
exitDbSchemaName(ctx: DbSchemaNameContext) {

src/parser/impala/impalaEntityCollector.ts

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,15 @@ import { EntityContextType } from '../common/types';
2929
export class ImpalaEntityCollector extends EntityCollector implements ImpalaSqlParserListener {
3030
/** ===== Entity begin */
3131
exitTableNameCreate(ctx: TableNameCreateContext) {
32-
this.pushEntity(ctx, EntityContextType.TABLE_CREATE, {
33-
attrNameList: [AttrName.comment],
34-
endContextList: [CreateTableSelectContext.name, CreateKuduTableAsSelectContext.name],
35-
});
32+
this.pushEntity(ctx, EntityContextType.TABLE_CREATE, [
33+
{
34+
attrName: AttrName.comment,
35+
endContextList: [
36+
CreateTableSelectContext.name,
37+
CreateKuduTableAsSelectContext.name,
38+
],
39+
},
40+
]);
3641
}
3742

3843
exitTableNamePath(ctx: TableNamePathContext) {
@@ -41,30 +46,44 @@ export class ImpalaEntityCollector extends EntityCollector implements ImpalaSqlP
4146
ctx,
4247
EntityContextType.TABLE,
4348
needCollectAttr
44-
? {
45-
attrNameList: [AttrName.alias],
46-
endContextList: [SampledRelationContext.name],
47-
}
49+
? [
50+
{
51+
attrName: AttrName.alias,
52+
endContextList: [SampledRelationContext.name],
53+
},
54+
]
4855
: undefined
4956
);
5057
}
5158

5259
exitColumnNamePathCreate(ctx: ColumnNamePathCreateContext) {
53-
this.pushEntity(ctx, EntityContextType.COLUMN_CREATE, {
54-
attrNameList: [AttrName.comment, AttrName.colType],
55-
endContextList: [
56-
ColumnDefinitionContext.name,
57-
KuduTableElementContext.name,
58-
ViewColumnItemContext.name,
59-
],
60-
});
60+
this.pushEntity(ctx, EntityContextType.COLUMN_CREATE, [
61+
{
62+
attrName: AttrName.comment,
63+
endContextList: [
64+
ColumnDefinitionContext.name,
65+
KuduTableElementContext.name,
66+
ViewColumnItemContext.name,
67+
],
68+
},
69+
{
70+
attrName: AttrName.colType,
71+
endContextList: [
72+
ColumnDefinitionContext.name,
73+
KuduTableElementContext.name,
74+
ViewColumnItemContext.name,
75+
],
76+
},
77+
]);
6178
}
6279

6380
exitViewNameCreate(ctx: ViewNameCreateContext) {
64-
this.pushEntity(ctx, EntityContextType.VIEW_CREATE, {
65-
attrNameList: [AttrName.comment],
66-
endContextList: [CreateViewContext.name],
67-
});
81+
this.pushEntity(ctx, EntityContextType.VIEW_CREATE, [
82+
{
83+
attrName: AttrName.comment,
84+
endContextList: [CreateViewContext.name],
85+
},
86+
]);
6887
}
6988

7089
exitViewNamePath(ctx: ViewNamePathContext) {
@@ -76,10 +95,12 @@ export class ImpalaEntityCollector extends EntityCollector implements ImpalaSqlP
7695
}
7796

7897
exitDatabaseNameCreate(ctx: DatabaseNameCreateContext) {
79-
this.pushEntity(ctx, EntityContextType.DATABASE_CREATE, {
80-
attrNameList: [AttrName.comment],
81-
endContextList: [CreateSchemaContext.name],
82-
});
98+
this.pushEntity(ctx, EntityContextType.DATABASE_CREATE, [
99+
{
100+
attrName: AttrName.comment,
101+
endContextList: [CreateSchemaContext.name],
102+
},
103+
]);
83104
}
84105

85106
exitFunctionNameCreate(ctx: FunctionNameCreateContext) {

src/parser/postgresql/postgreEntityCollector.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@ export class PostgreSqlEntityCollector extends EntityCollector implements Postgr
4444
ctx,
4545
EntityContextType.TABLE,
4646
needCollectAttr
47-
? {
48-
attrNameList: [AttrName.alias],
49-
endContextList: [Table_refContext.name],
50-
}
47+
? [
48+
{
49+
attrName: AttrName.alias,
50+
endContextList: [Table_refContext.name],
51+
},
52+
]
5153
: undefined
5254
);
5355
}
@@ -62,10 +64,12 @@ export class PostgreSqlEntityCollector extends EntityCollector implements Postgr
6264
ctx,
6365
EntityContextType.VIEW,
6466
needCollectAttr
65-
? {
66-
attrNameList: [AttrName.alias],
67-
endContextList: [Table_refContext.name],
68-
}
67+
? [
68+
{
69+
attrName: AttrName.alias,
70+
endContextList: [Table_refContext.name],
71+
},
72+
]
6973
: undefined
7074
);
7175
}
@@ -79,10 +83,16 @@ export class PostgreSqlEntityCollector extends EntityCollector implements Postgr
7983
}
8084

8185
exitColumnNameCreate(ctx: ColumnNameCreateContext) {
82-
this.pushEntity(ctx, EntityContextType.COLUMN_CREATE, {
83-
attrNameList: [AttrName.comment, AttrName.colType],
84-
endContextList: [Column_defContext.name],
85-
});
86+
this.pushEntity(ctx, EntityContextType.COLUMN_CREATE, [
87+
{
88+
attrName: AttrName.comment,
89+
endContextList: [Column_defContext.name],
90+
},
91+
{
92+
attrName: AttrName.colType,
93+
endContextList: [Column_defContext.name],
94+
},
95+
]);
8696
}
8797

8898
exitProcedureName(ctx: ProcedureNameContext) {

src/parser/spark/sparkEntityCollector.ts

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ export class SparkEntityCollector extends EntityCollector implements SparkSqlPar
3333
}
3434

3535
exitNamespaceNameCreate(ctx: NamespaceNameCreateContext) {
36-
this.pushEntity(ctx, EntityContextType.DATABASE_CREATE, {
37-
attrNameList: [AttrName.comment],
38-
endContextList: [CreateNamespaceContext.name],
39-
});
36+
this.pushEntity(ctx, EntityContextType.DATABASE_CREATE, [
37+
{
38+
attrName: AttrName.comment,
39+
endContextList: [CreateNamespaceContext.name],
40+
},
41+
]);
4042
}
4143

4244
exitTableName(ctx: TableNameContext) {
@@ -45,44 +47,56 @@ export class SparkEntityCollector extends EntityCollector implements SparkSqlPar
4547
ctx,
4648
EntityContextType.TABLE,
4749
needCollectAttr
48-
? {
49-
attrNameList: [AttrName.alias],
50-
endContextList: [RelationPrimaryContext.name],
51-
}
50+
? [
51+
{
52+
attrName: AttrName.alias,
53+
endContextList: [RelationPrimaryContext.name],
54+
},
55+
]
5256
: undefined
5357
);
5458
}
5559

5660
exitTableNameCreate(ctx: TableNameCreateContext) {
57-
this.pushEntity(ctx, EntityContextType.TABLE_CREATE, {
58-
attrNameList: [AttrName.comment],
59-
endContextList: [CreateTableContext.name],
60-
});
61+
this.pushEntity(ctx, EntityContextType.TABLE_CREATE, [
62+
{
63+
attrName: AttrName.comment,
64+
endContextList: [CreateTableContext.name],
65+
},
66+
]);
6167
}
6268

6369
exitViewName(ctx: ViewNameContext) {
6470
this.pushEntity(ctx, EntityContextType.VIEW);
6571
}
6672

6773
exitViewNameCreate(ctx: ViewNameCreateContext) {
68-
this.pushEntity(ctx, EntityContextType.VIEW_CREATE, {
69-
attrNameList: [AttrName.comment],
70-
endContextList: [CreateViewContext.name],
71-
});
74+
this.pushEntity(ctx, EntityContextType.VIEW_CREATE, [
75+
{
76+
attrName: AttrName.comment,
77+
endContextList: [CreateViewContext.name],
78+
},
79+
]);
7280
}
7381

7482
exitFunctionNameCreate(ctx: FunctionNameCreateContext) {
7583
this.pushEntity(ctx, EntityContextType.FUNCTION_CREATE);
7684
}
7785

7886
exitColumnNameCreate(ctx: ColumnNameCreateContext) {
79-
this.pushEntity(ctx, EntityContextType.COLUMN_CREATE, {
80-
attrNameList: [AttrName.comment, AttrName.colType],
81-
endContextList: [
82-
CreateOrReplaceTableColTypeContext.name,
83-
IdentifierCommentContext.name,
84-
],
85-
});
87+
this.pushEntity(ctx, EntityContextType.COLUMN_CREATE, [
88+
{
89+
attrName: AttrName.comment,
90+
endContextList: [
91+
CreateOrReplaceTableColTypeContext.name,
92+
IdentifierCommentContext.name,
93+
],
94+
},
95+
{
96+
attrName: AttrName.colType,
97+
endContextList: [CreateOrReplaceTableColTypeContext.name],
98+
},
99+
]);
86100
}
87101

88102
/** ===== Statement begin */

0 commit comments

Comments
 (0)