2

In my Go app I want to be able to analyze a SQL query before to execute it. I want to get: type (update, insert, delete etc). This is easy, but next steps not. table to be affected, columns to be updated (on insert/update) most important - condition, list of columns and values.

Is there any go library for this?

Something to pass a sql query and get back some structure with info about this query

1 Answer 1

2

Yes, you have sqlparser for golang.

Note that the sqlparser is been pulled out from the database clustering system vitess

You can use the sql parser like,

reader := strings.NewReader("INSERT INTO table1 VALUES (1, 'a');")

tokens := sqlparser.NewTokenizer(reader)
for {
    stmt, err := sqlparser.ParseNext(tokens)
    if err == io.EOF {
        break
    }
    // Do your logics with the statements.
}
Sign up to request clarification or add additional context in comments.

1 Comment

I would suggest importing the parser from vitess.io/vitess/go/vt/sqlparser directly as the above mentioned one has not been maintained for over 4 years now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.