2
0

Always use bound parameters

PostgreSQL has two string syntaxes, one that allows backslash escapes and one
that does not (SQL standard conforming strings). By default PostgreSQL uses
standard conforming strings. QuoteString was only designed for use with
standard conforming strings. If PostgreSQL was configured with certain
combinations of the standard_conforming_strings and backslash_quote settings,
QuoteString may not correctly sanitize strings. QuoteString was only used in
unprepared queries, bound parameters are used for prepared queries.

This commit alters pgx to use always use bound parameters.

As a consequence of never doing string interpolation there is no need to have
separate Text and Binary encoders. There is now only the Encoder interface.

This change had a negative effect on the performance of simple unprepared
queries, but prepared statements should already be used for performance.

fixes #26

https://github.com/jackc/pgx/issues/26
This commit is contained in:
Jack Christensen
2014-07-18 14:44:34 -05:00
parent d57e4902a1
commit 61bf7d841a
9 changed files with 166 additions and 339 deletions
+11 -31
View File
@@ -317,42 +317,22 @@ func (c *Conn) Query(sql string, args ...interface{}) (*Rows, error) {
c.rows = Rows{conn: c}
rows := &c.rows
if ps, present := c.preparedStatements[sql]; present {
rows.fields = ps.FieldDescriptions
err := c.sendPreparedQuery(ps, args...)
ps, ok := c.preparedStatements[sql]
if !ok {
var err error
ps, err = c.Prepare("", sql)
if err != nil {
rows.abort(err)
}
return rows, rows.err
}
err := c.sendSimpleQuery(sql, args...)
if err != nil {
rows.abort(err)
return rows, rows.err
}
// Simple queries don't know the field descriptions of the result.
// Read until that is known before returning
for {
t, r, err := c.rxMsg()
if err != nil {
rows.Fatal(err)
return rows, rows.err
}
switch t {
case rowDescription:
rows.fields = rows.conn.rxRowDescription(r)
return rows, nil
default:
err = rows.conn.processContextFreeMsg(t, r)
if err != nil {
rows.Fatal(err)
return rows, rows.err
}
}
}
rows.fields = ps.FieldDescriptions
err := c.sendPreparedQuery(ps, args...)
if err != nil {
rows.abort(err)
}
return rows, rows.err
}
// QueryRow is a convenience wrapper over Query. Any error that occurs while