Skip to content
This repository was archived by the owner on May 24, 2023. It is now read-only.

Commit 5be9c38

Browse files
committed
Add notes about prep stmt support; fix #41
1 parent 36fa382 commit 5be9c38

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

retrieving.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,31 @@ Go 1.1, but not all drivers are smart enough to do that. Caveat Emptor.
121121
Naturally prepared statements and the management of prepared statements cost
122122
resources. You should take care to close statements when they are not used again.
123123

124+
Avoiding Prepared Statements
125+
============================
126+
127+
Go creates prepared statements for you under the covers. A simple
128+
`db.Query(sql, param1, param2)`, for example, works by preparing the sql, then
129+
executing it with the parameters and finally closing the statement.
130+
131+
Sometimes a prepared statement is not what you want, however. There might be
132+
several reasons for this:
133+
134+
1. The database doesn't support prepared statements. When using the MySQL
135+
driver, for example, you can connect to MemSQL and Sphinx, because they
136+
support the MySQL wire protocol. But they don't support the "binary" protocol
137+
that includes prepared statements, so they can fail in confusing ways.
138+
2. The statements aren't reused enough to make them worthwhile, and security
139+
issues are handled in other ways, so performance overhead is undesired. An
140+
example of this can be seen at the
141+
[VividCortex blog](https://vividcortex.com/blog/2014/11/19/analyzing-prepared-statement-performance-with-vividcortex/).
142+
143+
If you don't want to use a prepared statement, you need to use `fmt.Sprint()` or
144+
similar to assemble the SQL, and pass this as the only argument to `db.Query()`
145+
or `db.QueryRow()`. And your driver needs to support plaintext query execution,
146+
which is added in Go 1.1 via the `Execer` interface,
147+
[documented here](http://golang.org/pkg/database/sql/driver/#Execer).
148+
124149
Single-Row Queries
125150
==================
126151

0 commit comments

Comments
 (0)