I’m using ANTL in my go program.
I have a query
INSERT INTO "C##MYADMIN"."EMPLOYEE" ("ID", "NAME_TARGET", "SALARY")
VALUES (1999998, 'Alice Wonderland', (SELECT AVG("SALARY") FROM "C##MYADMIN"."EMPLOYEE"));
which contains the subquery SELECT AVG("SALARY") FROM "C##MYADMIN"."EMPLOYEE"
.
I tried to extract the subquery with
func (s *mockListener) EnterInsert_statement(ctx *Insert_statementContext) {
singTblInsert := ctx.Single_table_insert()
insertInto := singTblInsert.Insert_into_clause()
colList := insertInto.Paren_column_list().Column_list()
ValList := singTblInsert.Values_clause().Expressions().AllExpression()
colNames := colList.AllColumn_name()
for i, col := range colNames {
colName := col.GetText()
valStr := ValList[i].GetText()
s.kv[colName] = valStr
}
}
query := `INSERT INTO "C##MYADMIN"."EMPLOYEE" ("ID", "NAME_TARGET", "SALARY") VALUES (1999998, 'Alice Wonderland', (SELECT AVG("SALARY") FROM "C##MYADMIN"."EMPLOYEE"));`
lexer := NewPlSqlLexer(antlr.NewInputStream(query))
stream := antlr.NewCommonTokenStream(lexer, 0)
p := NewPlSqlParser(stream)
tree := p.Sql_script()
l := &mockListener{
kv: make(map[string]interface{}),
}
antlr.ParseTreeWalkerDefault.Walk(l, tree)
and I found ValList[i].GetText()
gives me SELECTAVG("SALARY")FROM"C##MYADMIN"."EMPLOYEE"
, which means the spaces of the subquery are all incorrectly removed.
1