Parse parses input sql
(sql string, logger common.Logger)
| 1964 | |
| 1965 | // Parse parses input sql |
| 1966 | func Parse(sql string, logger common.Logger) (aql *queryCom.AQLQuery, err error) { |
| 1967 | defer func() { |
| 1968 | if r := recover(); r != nil { |
| 1969 | var ok bool |
| 1970 | err, ok = r.(error) |
| 1971 | if !ok { |
| 1972 | err = fmt.Errorf("unkonwn error, reason: %v", r) |
| 1973 | } |
| 1974 | } |
| 1975 | }() |
| 1976 | |
| 1977 | // Setup the input sql |
| 1978 | is := util.NewCaseChangingStream(antlr.NewInputStream(sql), true) |
| 1979 | |
| 1980 | // Create the Lexer |
| 1981 | lexer := antlrgen.NewSqlBaseLexer(is) |
| 1982 | stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel) |
| 1983 | |
| 1984 | // Create the Parser |
| 1985 | p := antlrgen.NewSqlBaseParser(stream) |
| 1986 | |
| 1987 | // Finally parse the sql |
| 1988 | p.GetInterpreter().SetPredictionMode(antlr.PredictionModeSLL) |
| 1989 | parseTree, ok := p.Query().(*antlrgen.QueryContext) |
| 1990 | if !ok { |
| 1991 | err = fmt.Errorf("not a query") |
| 1992 | return nil, err |
| 1993 | } |
| 1994 | |
| 1995 | // Construct ASTBuilder |
| 1996 | v := &ASTBuilder{ |
| 1997 | Logger: logger, |
| 1998 | IStream: stream, |
| 1999 | ParameterPosition: 0, |
| 2000 | SQL2AqlCtx: &SQL2AqlContext{ |
| 2001 | MapQueryIdentifier: make(map[int]string), |
| 2002 | MapMeasures: make(map[int][]queryCom.Measure), |
| 2003 | MapDimensions: make(map[int][]queryCom.Dimension), |
| 2004 | MapJoinTables: make(map[int][]queryCom.Join), |
| 2005 | MapRowFilters: make(map[int][]string), |
| 2006 | MapOrderBy: make(map[int][]queryCom.SortField), |
| 2007 | MapLimit: make(map[int]int), |
| 2008 | }, |
| 2009 | } |
| 2010 | node := v.VisitQuery(parseTree) |
| 2011 | if _, ok := node.(*tree.Query); ok { |
| 2012 | aql = v.GetAQL() |
| 2013 | aql.SQLQuery = sql |
| 2014 | aqlJSON, _ := json.Marshal(aql) |
| 2015 | logger.Infof("convert SQL:\n%v\nto AQL:\n%v", sql, string(aqlJSON)) |
| 2016 | } |
| 2017 | |
| 2018 | if len(aql.SupportingDimensions) > 0 || len(aql.SupportingMeasures) > 0 { |
| 2019 | err = fmt.Errorf("sub query not supported yet") |
| 2020 | return |
| 2021 | } |
| 2022 | |
| 2023 | // non agg query overwrite |
no test coverage detected