Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

See query with arguments in Go database/sql package

I'm trying to test the behavior of passing arguments to the sql.DB.Query method (using the database/sql package, and the PostgreSQL driver at github.com/lib/pq). Is there any way to get the raw query string after it's been processed to see how the arguments were inserted? I was thinking, for example, of writing a prepared query and then inspecting the resulting statement. Any ideas? Thanks!

like image 832
joshlf Avatar asked Jan 03 '14 22:01

joshlf


People also ask

How do I test SQL queries in Golang?

Then we need to make 3 files: repository.go under repository directory and mysql.go and mysql_test.go under repository/mysql directory. repository.go just contain an interface for the implementation and struct for the data model. Next, mysql.go will have the implementation of repository interface for CRUD function.

How do I display query results in SQL?

You have the option of displaying your query results on the Run SQL window, as opposed to Data Display windows. To do this, go to View > Data Grid (Ctrl+G). Once you have selected this option, a panel will appear at the bottom of the window - your query results will be displayed there.

How do you query in go?

You can query for multiple rows using Query or QueryContext , which return a Rows representing the query results. Your code iterates over the returned rows using Rows. Next . Each iteration calls Scan to copy column values into variables.


1 Answers

The "raw query string" isn't parsed by the client and interpolated; it's passed on intact to the server.

If the query has no parameters, the client sends it as a simple query; if the query has parameters, the client sends it as an extended query with the parameters separate from the query. The server then parses the query and parameterizes it.

Go's database/sql doesn't include any logging, so you'll either have to write your own, sniff the traffic with Wireshark, or enable logging on the server side.

like image 92
Coda Hale Avatar answered Nov 15 '22 05:11

Coda Hale