1
1
/* eslint-disable no-underscore-dangle */
2
2
import R from 'ramda' ;
3
- import { ErrorListener , CommonTokenStream , CharStream , RuleNode } from 'antlr4' ;
3
+ import { ErrorListener , CommonTokenStream , CharStream , RuleNode , ParseTree } from 'antlr4' ;
4
4
5
5
import GenericSqlLexer from './GenericSqlLexer' ;
6
6
import GenericSqlParser , {
@@ -92,11 +92,8 @@ export class SqlParser {
92
92
protected parse ( ) {
93
93
const { sql } = this ;
94
94
95
- const chars = CharStreams . fromString ( SqlParser . sqlUpperCase ( sql ) ) ;
96
- chars . getText = ( interval ) => {
97
- const start = interval . a ;
98
- let stop = interval . b ;
99
-
95
+ const chars = new CharStream ( SqlParser . sqlUpperCase ( sql ) ) ;
96
+ chars . getText = ( start , stop ) => {
100
97
if ( stop >= chars . size ) {
101
98
stop = chars . size - 1 ;
102
99
}
@@ -166,8 +163,8 @@ export class SqlParser {
166
163
this . ast . accept ( nodeVisitor ( {
167
164
visitNode ( ctx ) {
168
165
if ( ctx instanceof QueryContext ) {
169
- const selectItems = ctx . tryGetRuleContext ( 0 , SelectFieldsContext ) ;
170
- if ( selectItems && selectItems . text === '*' ) {
166
+ const selectItems = ctx . getTypedRuleContext ( SelectFieldsContext , 0 ) ;
167
+ if ( selectItems && selectItems . getText ( ) === '*' ) {
171
168
result = true ;
172
169
}
173
170
}
@@ -191,38 +188,39 @@ export class SqlParser {
191
188
const whereBuildingVisitor = nodeVisitor ( {
192
189
visitNode ( ctx ) {
193
190
if ( ctx instanceof IdPathContext ) {
194
- result += sql . substring ( cursor , ctx . start . startIndex ) ;
195
- cursor = ctx . start . startIndex ;
196
-
197
- const child = ctx . getChild ( 0 ) ;
198
- if ( child && child . text === originalAlias ) {
199
- const withoutFirst = R . drop ( 1 , < ParseTree [ ] > ctx . children ) ;
200
- result += [ tableAlias ] . concat ( withoutFirst . map ( c => c . text ) ) . join ( '' ) ;
201
- cursor = < number > ctx . stop ?. stopIndex + 1 ;
202
- } else if ( ctx . childCount === 1 ) {
203
- result += [ tableAlias , '.' ] . concat ( ctx . children ?. map ( c => c . text ) ) . join ( '' ) ;
204
- cursor = < number > ctx . stop ?. stopIndex + 1 ;
191
+ result += sql . substring ( cursor , ctx . start . start ) ;
192
+ cursor = ctx . start . start ;
193
+
194
+ const { children } = ctx as any ;
195
+ const child = children ? children [ 0 ] : null ;
196
+ if ( child && child . getText ( ) === originalAlias ) {
197
+ const withoutFirst = R . drop ( 1 , < ParseTree [ ] > ctx . children || [ ] ) ;
198
+ result += [ tableAlias ] . concat ( withoutFirst . map ( ( c : ParseTree ) => c . getText ( ) ) ) . join ( '' ) ;
199
+ cursor = ( ctx . stop ?. stop || 0 ) + 1 ;
200
+ } else if ( children && children . length === 1 ) {
201
+ result += [ tableAlias , '.' ] . concat ( children ?. map ( ( c : ParseTree ) => c . getText ( ) ) ) . join ( '' ) ;
202
+ cursor = ( ctx . stop ?. stop || 0 ) + 1 ;
205
203
} else {
206
- result += sql . substring ( cursor , ctx . stop ?. stopIndex ) ;
207
- cursor = < number > ctx . stop ?. stopIndex ;
204
+ result += sql . substring ( cursor , ctx . stop ?. stop ) ;
205
+ cursor = < number > ctx . stop ?. stop ;
208
206
}
209
207
}
210
208
}
211
209
} ) ;
212
210
213
211
this . ast . accept ( nodeVisitor ( {
214
212
visitNode ( ctx ) {
215
- if ( ctx instanceof QueryContext && ctx . _from && ctx . _where ) {
216
- const aliasField = ctx . _from . getRuleContext ( 0 , AliasFieldContext ) ;
217
- const lastNode = aliasField . getChild ( aliasField . childCount - 1 ) ;
213
+ if ( ctx instanceof QueryContext && ctx . _from_ && ctx . _where ) {
214
+ const aliasField = ctx . _from_ . getTypedRuleContext ( AliasFieldContext , 0 ) ;
215
+ const lastNode : any = ( aliasField as any ) . children ? ( aliasField as any ) . children [ ( aliasField as any ) . children . length - 1 ] : null ;
218
216
if ( lastNode instanceof IdPathContext ) {
219
- originalAlias = lastNode . getChild ( lastNode . childCount - 1 ) . text ;
217
+ originalAlias = lastNode . children ? lastNode . children [ lastNode . children . length - 1 ] . getText ( ) : '' ;
220
218
} else {
221
- originalAlias = lastNode . text ;
219
+ originalAlias = lastNode ? lastNode . getText ( ) : '' ;
222
220
}
223
221
224
- cursor = ctx . _where . start . startIndex ;
225
- end = < number > ctx . _where . stop ?. stopIndex + 1 ;
222
+ cursor = ctx . _where . start . start ;
223
+ end = < number > ( ctx . _where . stop ?. stop || 0 ) + 1 ;
226
224
ctx . _where . accept ( whereBuildingVisitor ) ;
227
225
}
228
226
}
@@ -239,9 +237,9 @@ export class SqlParser {
239
237
240
238
this . ast . accept ( nodeVisitor ( {
241
239
visitNode ( ctx ) {
242
- if ( ctx instanceof QueryContext && ctx . _from ) {
243
- const aliasField = ctx . _from . getRuleContext ( 0 , AliasFieldContext ) ;
244
- result = aliasField . getChild ( 0 ) . text ;
240
+ if ( ctx instanceof QueryContext && ctx . _from_ ) {
241
+ const aliasField = ctx . _from_ . getTypedRuleContext ( AliasFieldContext , 0 ) ;
242
+ result = ( aliasField as any ) . children ? ( aliasField as any ) . children [ 0 ] . getText ( ) : null ;
245
243
}
246
244
}
247
245
} ) ) ;
0 commit comments