Documentation ¶
Overview ¶
Package sqlproxy provides a database/sql driver that adds hooks to an existing SQL driver. For example, to augment a PostgreSQL driver with statement logging:
//this assumes that a "postgresql" driver is already registered sql.Register("postgres-with-logging", &sqlproxy.Driver { ProxiedDriverName: "postgresql", BeforeQueryHook: func(query string, args[]interface{}) { log.Printf("SQL: %s %#v", query, args) }, })
There's also a BeforePrepareHook that can be used to reject or edit query strings.
Caveats ¶
Do not use this code on production databases. This package is intended for development purposes only, and access to it should remain behind a debugging switch. It only implements the bare necessities of the database/sql driver interface and hides optimizations and advanced features of the proxied SQL driver.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func TraceQuery ¶
TraceQuery produces a function that can be given to a sqlproxy.Driver as a BeforeQueryHook. It prints all executed SQL statements (including values bound to the statement) onto the given printer. The printer will be called exactly once per statement, and its argument will not contain any line breaks. For example:
sql.Register("postgres-with-logging", &sqlproxy.Driver { ProxiedDriverName: "postgres", BeforeQueryHook: sqlproxy.TraceQuery(func(msg string) { log.Println(msg) }), })
Types ¶
type Driver ¶
type Driver struct { //ProxiedDriverName identifies the SQL driver which will be used to actually //perform SQL queries. ProxiedDriverName string //BeforePrepareHook (optional) runs just before a query is prepared (both for //explicit Prepare() calls and one-off queries). The return value will be //substituted for the original query string, allowing the hook to rewrite //queries arbitrarily. If an error is returned, it will be propagated to the //caller of db.Prepare() or tx.Prepare() etc. BeforePrepareHook func(query string) (string, error) //BeforeQueryHook (optional) runs just before a query is executed, e.g. by //the Exec(), Query() or QueryRows() methods of sql.DB, sql.Tx and sql.Stmt. BeforeQueryHook func(query string, args []interface{}) }
Driver implements sql.Driver. See package documentation for details.