pganalyze / pg_query_go

Go library to parse and normalize SQL queries using the PostgreSQL query parser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot read type information for nodes objects

vbansal22 opened this issue · comments

I am trying to parse a SQL query go object, but cannot switch on any of its types. For example

`func TestSQLParseAliasQueryAsObject(t *testing.T) {

var aliasQuery = SELECT s.sl_name, s.sl_avail + shoelace_arrive.arr_quant, current_user, current_timestamp FROM shoelace_arrive shoelace_arrive, shoelace_data shoelace_data, shoelace_data s WHERE s.sl_name = shoelace_arrive.arr_name AND shoelace_data.sl_name = s.sl_name AND s.sl_avail + shoelace_arrive.arr_quant <> s.sl_avail

fmt.Print("TestSQLParseTree --->")
tree, err := pg_query.Parse(aliasQuery)
nodes := tree.Statements

fmt.Println(len(nodes))
for key, value := range nodes {
	fmt.Printf("The key is %v", key)
	switch valueType := value.(type) {
	case nodes.RawStmt: //nodes.RawStmt undefined (type []"github.com/lfittl/pg_query_go/nodes".Node has no field or method RawStmt)
	default:
		fmt.Printf("default type is %v", valueType)

	}
	//fmt.Print(reflect.TypeOf(value))
}

if err != nil {
	panic(err)
}
fmt.Printf("%s\n", tree)

}`

Please look at the above switch statement. My code needs to make a decision on whether the type is of nodes.RawStmt. But I get the error shown in comments when I do that.

I'm suspecting the issue here is that you have nodes twice (once as a variable, once as an import)

Could you try renaming the variable in nodes := tree.Statements to something else?

Tried giving variable another name (node1), but still getting the same error

nodes1 := tree.Statements

nodes1.RawStmt undefined (type []"github.com/lfittl/pg_query_go/nodes".Node has no field or method RawStmt)

Also I cannot step into the code using the debugger in vscode or atom. However on command line I can. I am using delve

dbservice_test.txt

change extension to .go