go-sqlbuilder icon indicating copy to clipboard operation
go-sqlbuilder copied to clipboard

Language request: Google SQL

Open raphaelfff opened this issue 1 year ago • 1 comments

https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax

Also known as Google Standard SQL, in use in BigQuery

raphaelfff avatar Sep 27 '24 18:09 raphaelfff

It seems that Google Standard SQL can be considered as an alias of MySQL. Do you find any difference in SQL syntax?

huandu avatar Sep 29 '24 08:09 huandu

@raphaelfff I'm still looking forward to your feedback. Per my investigation, we can build Google SQL with MySQL flavor. And we can use SQL() method to build non-standard SQL syntax. If you find any difficulty to build SQL using MySQL, feel free to reopen this issue and let me know.

huandu avatar Nov 06 '24 07:11 huandu

Sorry, completly missed that One difference i can notice is the way params are handled, in MySQL ? is used, whereas in bigquery all params are named, and refered to as @paramname in the SQL query

raphaelfff avatar Mar 03 '25 09:03 raphaelfff

@raphaelfff Do you use go-sql-spanner as the SQL driver?

If so, the @paramname is a named parameter created by sql.Named(). SQL builder supports it. See Use sql.Named in a builder for more details.

huandu avatar Mar 04 '25 02:03 huandu

the trick is that i really want consumer to not have to worry about named params, but rather have the interface behave as unnamed params, but have the name generated magically @param1 etc

raphaelfff avatar Mar 04 '25 09:03 raphaelfff

I'm not sure why you need to worry about the named params. Maybe you can show me some code and then I can understand your need better.

Here are some samples showing how to use this packages with github.com/googleapis/go-sql-spanner.

The method QueryContext/ExecContext in github.com/googleapis/go-sql-spanner supports ? as placeholder, you can build SQL like following.

sb := sqlbuilder.Select("id", "text").From("tweets")
sb.Where(sb.GreaterThan("likes", 500))
sql, args := sb.Build()

// db.QueryContext(ctx, "SELECT id, text FROM tweets WHERE likes > ?", 500)
db.QueryContext(ctx, sql, ...args)

Or, you can use @paramname with sql.Named() like following.

builder := sqlbuilder.DeleteFrom("tweets")
builder.Where(builder.Equal("id", sql.Named("id", 14544498215374)))
sql, args := builder.Build()

// db.ExecContext(ctx, "DELETE FROM tweets WHERE id = @id", sql.Named("id", 14544498215374))
db.ExecContext(ctx, sql, ...args)

Hope these samples can help you.

huandu avatar Mar 05 '25 03:03 huandu