sqlproxy

package module
v0.0.0-...-ba72445 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2019 License: Apache-2.0 Imports: 7 Imported by: 0

README

sqlproxy

GoDoc

This is a driver for the standard library's database/sql package that passes SQL statements through to another driver, but adds hooks to extend the standard library's API.

Example

Augment your existing database driver with statement logging:

import "github.com/majewsky/sqlproxy"

...

sql.Register("postgres-with-logging", &sqlproxy.Driver {
    ProxiedDriverName: "postgresql",
    BeforeQueryHook: func(query string, args[]interface{}) {
        log.Printf("SQL: %s %#v", query, args)
    },
})

As always, sql.Register() may only be called once per driver name, so put this in func init() or a sync.Once.

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.

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

func TraceQuery(printer func(string)) func(string, []interface{})

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.

func (*Driver) Open

func (d *Driver) Open(dataSource string) (driver.Conn, error)

Open implements the Driver interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL