2016-09-15 01:11:57 +00:00
|
|
|
// Package sql provides SQL implementations of the storage interface.
|
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
2018-11-16 19:35:47 +00:00
|
|
|
"context"
|
2016-09-15 01:11:57 +00:00
|
|
|
"database/sql"
|
|
|
|
"regexp"
|
2016-12-16 19:03:36 +00:00
|
|
|
"time"
|
2016-09-15 01:11:57 +00:00
|
|
|
|
2018-11-15 18:17:42 +00:00
|
|
|
"github.com/lib/pq"
|
2017-07-25 20:45:17 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-09-15 01:11:57 +00:00
|
|
|
|
|
|
|
// import third party drivers
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
// flavor represents a specific SQL implementation, and is used to translate query strings
|
|
|
|
// between different drivers. Flavors shouldn't aim to translate all possible SQL statements,
|
|
|
|
// only the specific queries used by the SQL storages.
|
|
|
|
type flavor struct {
|
|
|
|
queryReplacers []replacer
|
|
|
|
|
2018-11-15 15:45:36 +00:00
|
|
|
// Optional function to create and finish a transaction.
|
2016-09-15 01:11:57 +00:00
|
|
|
executeTx func(db *sql.DB, fn func(*sql.Tx) error) error
|
2016-12-16 19:03:36 +00:00
|
|
|
|
|
|
|
// Does the flavor support timezones?
|
|
|
|
supportsTimezones bool
|
2016-09-15 01:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A regexp with a replacement string.
|
|
|
|
type replacer struct {
|
|
|
|
re *regexp.Regexp
|
|
|
|
with string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match a postgres query binds. E.g. "$1", "$12", etc.
|
|
|
|
var bindRegexp = regexp.MustCompile(`\$\d+`)
|
|
|
|
|
|
|
|
func matchLiteral(s string) *regexp.Regexp {
|
|
|
|
return regexp.MustCompile(`\b` + regexp.QuoteMeta(s) + `\b`)
|
|
|
|
}
|
|
|
|
|
2018-11-20 14:42:30 +00:00
|
|
|
// Detect a serialization failure, which should trigger retrying the
|
|
|
|
// transaction according to PostgreSQL docs:
|
|
|
|
//
|
|
|
|
// https://www.postgresql.org/docs/current/transaction-iso.html#XACT-SERIALIZABLE
|
|
|
|
//
|
|
|
|
// "applications using this level must be prepared to retry transactions due to
|
|
|
|
// serialization failures"
|
|
|
|
func isRetryableSerializationFailure(err error) bool {
|
|
|
|
if pqErr, ok := err.(*pq.Error); ok {
|
|
|
|
return pqErr.Code.Name() == "serialization_failure"
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-09-15 01:11:57 +00:00
|
|
|
var (
|
|
|
|
// The "github.com/lib/pq" driver is the default flavor. All others are
|
|
|
|
// translations of this.
|
2016-11-01 06:00:55 +00:00
|
|
|
flavorPostgres = flavor{
|
2018-11-19 16:34:45 +00:00
|
|
|
// The default behavior for Postgres transactions is consistent reads, not
|
|
|
|
// consistent writes. For each transaction opened, ensure it has the
|
|
|
|
// correct isolation level.
|
2016-11-01 06:00:55 +00:00
|
|
|
//
|
|
|
|
// See: https://www.postgresql.org/docs/9.3/static/sql-set-transaction.html
|
|
|
|
//
|
2018-11-19 16:34:45 +00:00
|
|
|
// Be careful not to wrap sql errors in the callback 'fn', otherwise
|
|
|
|
// serialization failures will not be detected and retried.
|
2016-11-01 06:00:55 +00:00
|
|
|
executeTx: func(db *sql.DB, fn func(sqlTx *sql.Tx) error) error {
|
2018-11-16 19:35:47 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.TODO())
|
|
|
|
defer cancel()
|
2016-11-01 06:00:55 +00:00
|
|
|
|
2018-11-16 19:35:47 +00:00
|
|
|
opts := &sql.TxOptions{
|
|
|
|
Isolation: sql.LevelSerializable,
|
|
|
|
}
|
2018-11-15 18:17:42 +00:00
|
|
|
|
2018-11-16 19:35:47 +00:00
|
|
|
for {
|
|
|
|
tx, err := db.BeginTx(ctx, opts)
|
|
|
|
if err != nil {
|
2018-11-15 18:17:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := fn(tx); err != nil {
|
2018-11-20 14:42:30 +00:00
|
|
|
if isRetryableSerializationFailure(err) {
|
2018-11-19 16:34:45 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-11-15 18:17:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
2018-11-20 14:42:30 +00:00
|
|
|
if isRetryableSerializationFailure(err) {
|
2018-11-15 18:17:42 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-11-01 06:00:55 +00:00
|
|
|
}
|
|
|
|
},
|
2016-12-16 19:03:36 +00:00
|
|
|
|
|
|
|
supportsTimezones: true,
|
2016-11-01 06:00:55 +00:00
|
|
|
}
|
2016-09-15 01:11:57 +00:00
|
|
|
|
|
|
|
flavorSQLite3 = flavor{
|
|
|
|
queryReplacers: []replacer{
|
|
|
|
{bindRegexp, "?"},
|
|
|
|
// Translate for booleans to integers.
|
|
|
|
{matchLiteral("true"), "1"},
|
|
|
|
{matchLiteral("false"), "0"},
|
|
|
|
{matchLiteral("boolean"), "integer"},
|
|
|
|
// Translate other types.
|
|
|
|
{matchLiteral("bytea"), "blob"},
|
2016-12-16 19:03:36 +00:00
|
|
|
{matchLiteral("timestamptz"), "timestamp"},
|
2016-09-15 01:11:57 +00:00
|
|
|
// SQLite doesn't have a "now()" method, replace with "date('now')"
|
|
|
|
{regexp.MustCompile(`\bnow\(\)`), "date('now')"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (f flavor) translate(query string) string {
|
|
|
|
// TODO(ericchiang): Heavy cashing.
|
|
|
|
for _, r := range f.queryReplacers {
|
|
|
|
query = r.re.ReplaceAllString(query, r.with)
|
|
|
|
}
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
2016-12-16 19:03:36 +00:00
|
|
|
// translateArgs translates query parameters that may be unique to
|
|
|
|
// a specific SQL flavor. For example, standardizing "time.Time"
|
|
|
|
// types to UTC for clients that don't provide timezone support.
|
|
|
|
func (c *conn) translateArgs(args []interface{}) []interface{} {
|
|
|
|
if c.flavor.supportsTimezones {
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, arg := range args {
|
|
|
|
if t, ok := arg.(time.Time); ok {
|
|
|
|
args[i] = t.UTC()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
2016-09-15 01:11:57 +00:00
|
|
|
// conn is the main database connection.
|
|
|
|
type conn struct {
|
2017-02-21 23:00:22 +00:00
|
|
|
db *sql.DB
|
|
|
|
flavor flavor
|
|
|
|
logger logrus.FieldLogger
|
|
|
|
alreadyExistsCheck func(err error) bool
|
2016-09-15 01:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *conn) Close() error {
|
|
|
|
return c.db.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// conn implements the same method signatures as encoding/sql.DB.
|
|
|
|
|
|
|
|
func (c *conn) Exec(query string, args ...interface{}) (sql.Result, error) {
|
|
|
|
query = c.flavor.translate(query)
|
2016-12-16 19:03:36 +00:00
|
|
|
return c.db.Exec(query, c.translateArgs(args)...)
|
2016-09-15 01:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *conn) Query(query string, args ...interface{}) (*sql.Rows, error) {
|
|
|
|
query = c.flavor.translate(query)
|
2016-12-16 19:03:36 +00:00
|
|
|
return c.db.Query(query, c.translateArgs(args)...)
|
2016-09-15 01:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *conn) QueryRow(query string, args ...interface{}) *sql.Row {
|
|
|
|
query = c.flavor.translate(query)
|
2016-12-16 19:03:36 +00:00
|
|
|
return c.db.QueryRow(query, c.translateArgs(args)...)
|
2016-09-15 01:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExecTx runs a method which operates on a transaction.
|
|
|
|
func (c *conn) ExecTx(fn func(tx *trans) error) error {
|
|
|
|
if c.flavor.executeTx != nil {
|
|
|
|
return c.flavor.executeTx(c.db, func(sqlTx *sql.Tx) error {
|
|
|
|
return fn(&trans{sqlTx, c})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlTx, err := c.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := fn(&trans{sqlTx, c}); err != nil {
|
|
|
|
sqlTx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return sqlTx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
type trans struct {
|
|
|
|
tx *sql.Tx
|
|
|
|
c *conn
|
|
|
|
}
|
|
|
|
|
|
|
|
// trans implements the same method signatures as encoding/sql.Tx.
|
|
|
|
|
|
|
|
func (t *trans) Exec(query string, args ...interface{}) (sql.Result, error) {
|
|
|
|
query = t.c.flavor.translate(query)
|
2016-12-16 19:03:36 +00:00
|
|
|
return t.tx.Exec(query, t.c.translateArgs(args)...)
|
2016-09-15 01:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *trans) Query(query string, args ...interface{}) (*sql.Rows, error) {
|
|
|
|
query = t.c.flavor.translate(query)
|
2016-12-16 19:03:36 +00:00
|
|
|
return t.tx.Query(query, t.c.translateArgs(args)...)
|
2016-09-15 01:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *trans) QueryRow(query string, args ...interface{}) *sql.Row {
|
|
|
|
query = t.c.flavor.translate(query)
|
2016-12-16 19:03:36 +00:00
|
|
|
return t.tx.QueryRow(query, t.c.translateArgs(args)...)
|
2016-09-15 01:11:57 +00:00
|
|
|
}
|