1
0
Fork 0

Merge pull request #74 from zmedico/direct-sqlite3-issue-73

Use sqlite3.SQLiteDriver directly, fix issue #73
master
Philip O'Toole 9 years ago
commit 78979780fc

@ -1,37 +1,16 @@
package db package db
import ( import (
"database/sql"
"database/sql/driver" "database/sql/driver"
"fmt" "fmt"
"io" "io"
"sync"
"time" "time"
"github.com/mattn/go-sqlite3" "github.com/mattn/go-sqlite3"
) )
// Hook into the SQLite3 connection.
//
// This connection, mutex, and init() allows each Open() call to get access
// to the driver's internal conn object. The mutex ensures only 1 Open() call
// can run concurrently, and when it has returned, sqlite3conn will be set to
// the connection. This connection can be used for backups.
var sqlite3conn *sqlite3.SQLiteConn
var openMu sync.Mutex
const bkDelay = 250 const bkDelay = 250
func init() {
sql.Register("sqlite3_with_hook",
&sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
sqlite3conn = conn
return nil
},
})
}
// Config represents the configuration of the SQLite database. // Config represents the configuration of the SQLite database.
type Config struct { type Config struct {
DSN string // Datasource name DSN string // Datasource name
@ -53,7 +32,6 @@ func (c *Config) FQDSN(path string) string {
// DB is the SQL database. // DB is the SQL database.
type DB struct { type DB struct {
conn *sql.DB // Abstract Connection to database.
sqlite3conn *sqlite3.SQLiteConn // Driver connection to database. sqlite3conn *sqlite3.SQLiteConn // Driver connection to database.
path string // Path to database file. path string // Path to database file.
} }
@ -77,65 +55,44 @@ type Rows struct {
// Open an existing database, creating it if it does not exist. // Open an existing database, creating it if it does not exist.
func Open(dbPath string) (*DB, error) { func Open(dbPath string) (*DB, error) {
openMu.Lock() d := sqlite3.SQLiteDriver{}
defer openMu.Unlock() dbc, err := d.Open(dbPath)
dbc, err := sql.Open("sqlite3_with_hook", dbPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Ensure a connection is established before going further. Ignore error
// because database may not exist yet.
_ = dbc.Ping()
return &DB{ return &DB{
conn: dbc, sqlite3conn: dbc.(*sqlite3.SQLiteConn),
sqlite3conn: sqlite3conn,
path: dbPath, path: dbPath,
}, nil }, nil
} }
// OpenWithConfiguration an existing database, creating it if it does not exist. // OpenWithConfiguration an existing database, creating it if it does not exist.
func OpenWithConfiguration(dbPath string, conf *Config) (*DB, error) { func OpenWithConfiguration(dbPath string, conf *Config) (*DB, error) {
openMu.Lock() return Open(conf.FQDSN(dbPath))
defer openMu.Unlock()
dbc, err := sql.Open("sqlite3_with_hook", conf.FQDSN(dbPath))
if err != nil {
return nil, err
}
// Ensure a connection is established before going further. Ignore error
// because database may not exist yet.
_ = dbc.Ping()
return &DB{
conn: dbc,
sqlite3conn: sqlite3conn,
path: conf.FQDSN(dbPath),
}, nil
} }
// Close closes the underlying database connection. // Close closes the underlying database connection.
func (db *DB) Close() error { func (db *DB) Close() error {
return db.conn.Close() return db.sqlite3conn.Close()
} }
// Execute executes queries that modify the database. // Execute executes queries that modify the database.
func (db *DB) Execute(queries []string, tx, xTime bool) ([]*Result, error) { func (db *DB) Execute(queries []string, tx, xTime bool) ([]*Result, error) {
type Execer interface { type Execer interface {
Exec(query string, args ...interface{}) (sql.Result, error) Exec(query string, args []driver.Value) (driver.Result, error)
} }
var allResults []*Result var allResults []*Result
err := func() error { err := func() (error) {
var execer Execer var execer Execer
var rollback bool var rollback bool
var t driver.Tx
var err error
// Check for the err, if set rollback. // Check for the err, if set rollback.
defer func() { defer func() {
if t, ok := execer.(*sql.Tx); ok { if t != nil {
if rollback { if rollback {
t.Rollback() t.Rollback()
return return
@ -156,12 +113,15 @@ func (db *DB) Execute(queries []string, tx, xTime bool) ([]*Result, error) {
return true return true
} }
execer = db.sqlite3conn
// Create the correct execution object, depending on whether a // Create the correct execution object, depending on whether a
// transaction was requested. // transaction was requested.
if tx { if tx {
execer, _ = db.conn.Begin() t, err = db.sqlite3conn.Begin()
} else { if err != nil {
execer = db.conn return err
}
} }
// Execute each query. // Execute each query.
@ -173,7 +133,7 @@ func (db *DB) Execute(queries []string, tx, xTime bool) ([]*Result, error) {
result := &Result{} result := &Result{}
start := time.Now() start := time.Now()
r, err := execer.Exec(q) r, err := execer.Exec(q, nil)
if err != nil { if err != nil {
if handleError(result, err) { if handleError(result, err) {
continue continue
@ -231,10 +191,6 @@ func (db *DB) Query(queries []string, tx, xTime bool) ([]*Rows, error) {
} }
}() }()
if db.sqlite3conn == nil {
// handle race for issue #72
db.sqlite3conn = sqlite3conn
}
queryer = db.sqlite3conn queryer = db.sqlite3conn
// Create the correct query object, depending on whether a // Create the correct query object, depending on whether a

Loading…
Cancel
Save