query := "SELECT name, age FROM users WHERE age > 18" parsed, err := gorp.Parse(query) if err != nil { panic(err) } selectQuery, ok := parsed.(*gorp.SelectQuery) if !ok { panic("Query not a SELECT statement") } fmt.Println(selectQuery.SelectColumns) // prints ["name", "age"] fmt.Println(selectQuery.FromTable) // prints "users" fmt.Println(selectQuery.WhereClause) // prints "age > 18"
query := "SELECT COUNT(*) FROM orders WHERE customer_id = ? AND date > ?" parsed, err := gorp.Parse(query) if err != nil { panic(err) } selectQuery, ok := parsed.(*gorp.SelectQuery) if !ok { panic("Query not a SELECT statement") } countExpr, ok := selectQuery.SelectColumns[0].(*gorp.FunctionExpression) if !ok { panic("No count function in query") } fmt.Println(countExpr.FunctionName) // prints "COUNT" fmt.Println(countExpr.Arguments[0]) // prints "*" whereExpr, ok := selectQuery.WhereClause.(*gorp.BinaryExpression) if !ok { panic("No binary expression in WHERE clause") } fmt.Println(whereExpr.LeftOperand) // prints "customer_id = ?" fmt.Println(whereExpr.RightOperand) // prints "date > ?"This example demonstrates how to extract information from more complex queries, such as those containing function calls and placeholders. In summary, SelectQuery is a parser for SQL select queries in the Gorp library for Go. It can be used to extract information from simple as well as complex queries.