dolthub / go-mysql-server

A MySQL-compatible relational database with a storage agnostic query engine. Implemented in pure Go.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Extracting update query columns

abbnaseri opened this issue · comments

Could you tell me how i can extract which columns the update query wants to update and what are their values? By the way, huge thanks for your awesome project.

Hey @abbnaseri, can you provide a little more detail? Are you trying to implement a sql.RowUpdater?

Hey @zachmu, Thanks for your response. In our project we want to parse the sql queries that we are receiving and extract its components and then use those components to build a request and send it to another service for database updates. there is a legacy service which we are trying to detach it from real database. for example if this is the query we are receiving:

UPDATE users SET email = "example@example.com" WHERE id = 1;

we want to know that this is an 'update' query and it tries to update 'email' column based on id = 1 condition. I read the BACKEND.md but i didn't get which interface i should implement. I'll be grateful if you help me with that.

I think what you really want is the Vitess. This is our fork;

https://github.com/dolthub/vitess

That handles query parsing in go-mysql-server.

Yeah I came across this project earlier but i didn't understand how it can solve my problem. What we want is all MySQL functionality with this exception that for queries we need all their components for building the requests and for response we must return valid MySQL responses/errors so that the legacy project doesn't feel it isn't a real MySQL.

Based on this, you probably don't want to implement a custom backend. You probably want just want the parser capability.

You need to:

  1. Call sqlparser.Parse() to parse the statement from the client
  2. Determine if it's an UpdateStatement
  3. If so, dig into the AST produced by Parse to see if the conditions you want to trigger on obtain to that query
  4. Trigger your alternate business logic if so

Here's an example of walking the AST produced by the Parse function, should get you started:

https://github.com/dolthub/go-mysql-server/blob/main/sql/parse/parse.go#L185

Going to cvlose this as I think we answered your question.