go-sqlite3/driver/driver.go

76 lines
1.6 KiB
Go
Raw Normal View History

2018-06-15 18:53:32 +03:00
// Copyright (C) 2018 The Go-SQLite3 Authors.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
2018-06-19 18:53:53 +03:00
// +build cgo
2018-06-15 18:53:32 +03:00
package sqlite3
2018-06-19 18:53:53 +03:00
/*
#cgo CFLAGS: -std=gnu99
#cgo CFLAGS: -DSQLITE_ENABLE_RTREE
#cgo CFLAGS: -DSQLITE_THREADSAFE=1
#cgo CFLAGS: -DHAVE_USLEEP=1
#cgo CFLAGS: -DSQLITE_ENABLE_FTS3
#cgo CFLAGS: -DSQLITE_ENABLE_FTS3_PARENTHESIS
#cgo CFLAGS: -DSQLITE_ENABLE_FTS4_UNICODE61
#cgo CFLAGS: -DSQLITE_TRACE_SIZE_LIMIT=15
#cgo CFLAGS: -DSQLITE_OMIT_DEPRECATED
#cgo CFLAGS: -DSQLITE_DISABLE_INTRINSIC
#cgo CFLAGS: -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1
#cgo CFLAGS: -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT
#cgo CFLAGS: -Wno-deprecated-declarations
#cgo linux,!android CFLAGS: -DHAVE_PREAD64=1 -DHAVE_PWRITE64=1
#ifndef USE_LIBSQLITE3
#include <sqlite3-binding.h>
#else
#include <sqlite3.h>
#endif
*/
import "C"
import (
"database/sql"
"database/sql/driver"
2018-07-03 14:49:21 +03:00
"sync"
2018-06-19 18:53:53 +03:00
)
var (
_ driver.Driver = (*SQLiteDriver)(nil)
)
func init() {
sql.Register("sqlite3", &SQLiteDriver{})
}
// SQLiteDriver implement sql.Driver.
type SQLiteDriver struct {
2018-07-03 14:49:21 +03:00
mu sync.Mutex
Config *Config
2018-06-19 18:53:53 +03:00
Extensions []string
ConnectHook func(*SQLiteConn) error
}
// Open database and return a new connection.
func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
2018-07-03 14:49:21 +03:00
d.mu.Lock()
defer d.mu.Unlock()
cfg, err := ParseDSN(dsn)
if err != nil {
return nil, err
}
// Configure Extensions
cfg.Extensions = d.Extensions
// Configure ConnectHook
cfg.ConnectHook = d.ConnectHook
// Set Configuration
d.Config = cfg
return cfg.createConnection()
2018-06-19 18:53:53 +03:00
}