11/* eslint-disable no-underscore-dangle */
22import R from 'ramda' ;
3- import { ErrorListener , CommonTokenStream , CharStream , RuleNode } from 'antlr4' ;
3+ import { ErrorListener , CommonTokenStream , CharStream , RuleNode , ParseTree } from 'antlr4' ;
44
55import GenericSqlLexer from './GenericSqlLexer' ;
66import GenericSqlParser , {
@@ -92,11 +92,8 @@ export class SqlParser {
9292 protected parse ( ) {
9393 const { sql } = this ;
9494
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 ) => {
10097 if ( stop >= chars . size ) {
10198 stop = chars . size - 1 ;
10299 }
@@ -166,8 +163,8 @@ export class SqlParser {
166163 this . ast . accept ( nodeVisitor ( {
167164 visitNode ( ctx ) {
168165 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 ( ) === '*' ) {
171168 result = true ;
172169 }
173170 }
@@ -191,38 +188,39 @@ export class SqlParser {
191188 const whereBuildingVisitor = nodeVisitor ( {
192189 visitNode ( ctx ) {
193190 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 ;
205203 } 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 ;
208206 }
209207 }
210208 }
211209 } ) ;
212210
213211 this . ast . accept ( nodeVisitor ( {
214212 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 ;
218216 if ( lastNode instanceof IdPathContext ) {
219- originalAlias = lastNode . getChild ( lastNode . childCount - 1 ) . text ;
217+ originalAlias = lastNode . children ? lastNode . children [ lastNode . children . length - 1 ] . getText ( ) : '' ;
220218 } else {
221- originalAlias = lastNode . text ;
219+ originalAlias = lastNode ? lastNode . getText ( ) : '' ;
222220 }
223221
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 ;
226224 ctx . _where . accept ( whereBuildingVisitor ) ;
227225 }
228226 }
@@ -239,9 +237,9 @@ export class SqlParser {
239237
240238 this . ast . accept ( nodeVisitor ( {
241239 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 ;
245243 }
246244 }
247245 } ) ) ;
0 commit comments