2014-08-18 11:56:31 +04:00
|
|
|
// Copyright (C) 2014 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-08-18 12:00:59 +04:00
|
|
|
|
2013-08-13 17:10:30 +04:00
|
|
|
package sqlite3
|
2011-11-11 16:36:22 +04:00
|
|
|
|
|
|
|
/*
|
2014-12-19 01:16:42 +03:00
|
|
|
#cgo CFLAGS: -std=gnu99
|
2015-02-24 05:27:09 +03:00
|
|
|
#cgo CFLAGS: -DSQLITE_ENABLE_RTREE -DSQLITE_THREADSAFE
|
|
|
|
#cgo CFLAGS: -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS
|
2015-03-11 22:18:10 +03:00
|
|
|
#include <sqlite3-binding.h>
|
2011-11-11 16:36:22 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-10-02 07:50:50 +04:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
# include <errno.h>
|
|
|
|
#endif
|
|
|
|
|
2013-04-06 18:06:48 +04:00
|
|
|
#ifndef SQLITE_OPEN_READWRITE
|
|
|
|
# define SQLITE_OPEN_READWRITE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SQLITE_OPEN_FULLMUTEX
|
|
|
|
# define SQLITE_OPEN_FULLMUTEX 0
|
|
|
|
#endif
|
|
|
|
|
2013-02-03 18:25:30 +04:00
|
|
|
static int
|
|
|
|
_sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs) {
|
|
|
|
#ifdef SQLITE_OPEN_URI
|
|
|
|
return sqlite3_open_v2(filename, ppDb, flags | SQLITE_OPEN_URI, zVfs);
|
|
|
|
#else
|
|
|
|
return sqlite3_open_v2(filename, ppDb, flags, zVfs);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-11-11 16:36:22 +04:00
|
|
|
static int
|
|
|
|
_sqlite3_bind_text(sqlite3_stmt *stmt, int n, char *p, int np) {
|
|
|
|
return sqlite3_bind_text(stmt, n, p, np, SQLITE_TRANSIENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_sqlite3_bind_blob(sqlite3_stmt *stmt, int n, void *p, int np) {
|
|
|
|
return sqlite3_bind_blob(stmt, n, p, np, SQLITE_TRANSIENT);
|
|
|
|
}
|
2012-12-07 07:58:08 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2015-03-24 00:17:00 +03:00
|
|
|
static int
|
|
|
|
_sqlite3_exec(sqlite3* db, const char* pcmd, long* rowid, long* changes)
|
|
|
|
{
|
|
|
|
int rv = sqlite3_exec(db, pcmd, 0, 0, 0);
|
|
|
|
*rowid = (long) sqlite3_last_insert_rowid(db);
|
|
|
|
*changes = (long) sqlite3_changes(db);
|
|
|
|
return rv;
|
2012-12-07 07:58:08 +04:00
|
|
|
}
|
|
|
|
|
2015-03-24 00:17:00 +03:00
|
|
|
static int
|
|
|
|
_sqlite3_step(sqlite3_stmt* stmt, long* rowid, long* changes)
|
|
|
|
{
|
|
|
|
int rv = sqlite3_step(stmt);
|
|
|
|
sqlite3* db = sqlite3_db_handle(stmt);
|
|
|
|
*rowid = (long) sqlite3_last_insert_rowid(db);
|
|
|
|
*changes = (long) sqlite3_changes(db);
|
|
|
|
return rv;
|
2012-12-07 07:58:08 +04:00
|
|
|
}
|
|
|
|
|
2011-11-11 16:36:22 +04:00
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
import (
|
2012-01-20 20:44:24 +04:00
|
|
|
"database/sql"
|
|
|
|
"database/sql/driver"
|
2012-03-01 20:52:03 +04:00
|
|
|
"errors"
|
2014-11-30 01:08:02 +03:00
|
|
|
"fmt"
|
2012-03-01 20:52:03 +04:00
|
|
|
"io"
|
2015-03-04 16:49:17 +03:00
|
|
|
"net/url"
|
2014-11-14 11:13:35 +03:00
|
|
|
"runtime"
|
2015-01-02 09:31:46 +03:00
|
|
|
"strconv"
|
2012-04-07 08:17:54 +04:00
|
|
|
"strings"
|
|
|
|
"time"
|
2011-11-11 16:36:22 +04:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2012-12-30 04:36:29 +04:00
|
|
|
// Timestamp formats understood by both this module and SQLite.
|
|
|
|
// The first format in the slice will be used when saving time values
|
|
|
|
// into the database. When parsing a string from a timestamp or
|
|
|
|
// datetime column, the formats are tried in order.
|
|
|
|
var SQLiteTimestampFormats = []string{
|
|
|
|
"2006-01-02 15:04:05.999999999",
|
2012-12-30 04:51:15 +04:00
|
|
|
"2006-01-02T15:04:05.999999999",
|
2012-12-30 04:36:29 +04:00
|
|
|
"2006-01-02 15:04:05",
|
2012-12-30 04:51:15 +04:00
|
|
|
"2006-01-02T15:04:05",
|
|
|
|
"2006-01-02 15:04",
|
|
|
|
"2006-01-02T15:04",
|
2012-12-30 04:36:29 +04:00
|
|
|
"2006-01-02",
|
2014-07-08 13:56:44 +04:00
|
|
|
"2006-01-02 15:04:05-07:00",
|
2012-12-30 04:36:29 +04:00
|
|
|
}
|
2012-04-07 08:17:54 +04:00
|
|
|
|
2011-11-11 16:36:22 +04:00
|
|
|
func init() {
|
2013-08-25 07:36:35 +04:00
|
|
|
sql.Register("sqlite3", &SQLiteDriver{})
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
|
|
|
|
2014-10-13 06:05:49 +04:00
|
|
|
// Return SQLite library Version information.
|
|
|
|
func Version() (libVersion string, libVersionNumber int, sourceId string) {
|
|
|
|
libVersion = C.GoString(C.sqlite3_libversion())
|
|
|
|
libVersionNumber = int(C.sqlite3_libversion_number())
|
|
|
|
sourceId = C.GoString(C.sqlite3_sourceid())
|
|
|
|
return libVersion, libVersionNumber, sourceId
|
|
|
|
}
|
|
|
|
|
2013-01-31 11:48:30 +04:00
|
|
|
// Driver struct.
|
2011-11-11 16:36:22 +04:00
|
|
|
type SQLiteDriver struct {
|
2013-08-25 07:36:35 +04:00
|
|
|
Extensions []string
|
|
|
|
ConnectHook func(*SQLiteConn) error
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
|
|
|
|
2013-01-31 11:48:30 +04:00
|
|
|
// Conn struct.
|
2011-11-11 16:36:22 +04:00
|
|
|
type SQLiteConn struct {
|
2015-03-04 16:49:17 +03:00
|
|
|
db *C.sqlite3
|
|
|
|
loc *time.Location
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
|
|
|
|
2013-01-31 11:48:30 +04:00
|
|
|
// Tx struct.
|
2011-11-11 16:36:22 +04:00
|
|
|
type SQLiteTx struct {
|
|
|
|
c *SQLiteConn
|
|
|
|
}
|
|
|
|
|
2013-01-31 11:48:30 +04:00
|
|
|
// Stmt struct.
|
|
|
|
type SQLiteStmt struct {
|
|
|
|
c *SQLiteConn
|
|
|
|
s *C.sqlite3_stmt
|
|
|
|
t string
|
|
|
|
closed bool
|
2014-07-15 20:11:07 +04:00
|
|
|
cls bool
|
2013-01-31 11:48:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Result struct.
|
|
|
|
type SQLiteResult struct {
|
2013-05-11 16:45:48 +04:00
|
|
|
id int64
|
2013-05-11 16:43:31 +04:00
|
|
|
changes int64
|
2013-01-31 11:48:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rows struct.
|
|
|
|
type SQLiteRows struct {
|
|
|
|
s *SQLiteStmt
|
|
|
|
nc int
|
|
|
|
cols []string
|
|
|
|
decltype []string
|
2014-07-15 20:11:07 +04:00
|
|
|
cls bool
|
2013-01-31 11:48:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Commit transaction.
|
2011-11-11 16:36:22 +04:00
|
|
|
func (tx *SQLiteTx) Commit() error {
|
2014-06-25 06:41:58 +04:00
|
|
|
_, err := tx.c.exec("COMMIT")
|
|
|
|
return err
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
|
|
|
|
2013-01-31 11:48:30 +04:00
|
|
|
// Rollback transaction.
|
2011-11-11 16:36:22 +04:00
|
|
|
func (tx *SQLiteTx) Rollback() error {
|
2014-06-25 06:41:58 +04:00
|
|
|
_, err := tx.c.exec("ROLLBACK")
|
|
|
|
return err
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
|
|
|
|
2013-09-09 05:44:44 +04:00
|
|
|
// AutoCommit return which currently auto commit or not.
|
2013-08-23 09:11:15 +04:00
|
|
|
func (c *SQLiteConn) AutoCommit() bool {
|
2013-08-23 09:26:33 +04:00
|
|
|
return int(C.sqlite3_get_autocommit(c.db)) != 0
|
2013-08-23 09:11:15 +04:00
|
|
|
}
|
|
|
|
|
2013-11-19 13:13:19 +04:00
|
|
|
func (c *SQLiteConn) lastError() Error {
|
2014-04-01 16:01:19 +04:00
|
|
|
return Error{
|
|
|
|
Code: ErrNo(C.sqlite3_errcode(c.db)),
|
|
|
|
ExtendedCode: ErrNoExtended(C.sqlite3_extended_errcode(c.db)),
|
|
|
|
err: C.GoString(C.sqlite3_errmsg(c.db)),
|
2013-11-19 13:13:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-25 06:41:58 +04:00
|
|
|
// Implements Execer
|
|
|
|
func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) {
|
2014-06-25 22:54:09 +04:00
|
|
|
if len(args) == 0 {
|
|
|
|
return c.exec(query)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
s, err := c.Prepare(query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var res driver.Result
|
|
|
|
if s.(*SQLiteStmt).s != nil {
|
|
|
|
na := s.NumInput()
|
2014-08-18 13:48:48 +04:00
|
|
|
if len(args) < na {
|
2014-11-30 01:08:02 +03:00
|
|
|
return nil, fmt.Errorf("Not enough args to execute query. Expected %d, got %d.", na, len(args))
|
2014-08-18 13:23:58 +04:00
|
|
|
}
|
2014-06-25 22:54:09 +04:00
|
|
|
res, err = s.Exec(args[:na])
|
|
|
|
if err != nil && err != driver.ErrSkip {
|
|
|
|
s.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
args = args[na:]
|
|
|
|
}
|
|
|
|
tail := s.(*SQLiteStmt).t
|
2014-07-15 20:11:07 +04:00
|
|
|
s.Close()
|
2014-06-25 22:54:09 +04:00
|
|
|
if tail == "" {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
query = tail
|
|
|
|
}
|
2014-06-25 06:41:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Queryer
|
|
|
|
func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) {
|
2014-06-25 22:54:09 +04:00
|
|
|
for {
|
|
|
|
s, err := c.Prepare(query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-07-15 20:11:07 +04:00
|
|
|
s.(*SQLiteStmt).cls = true
|
2014-06-25 22:54:09 +04:00
|
|
|
na := s.NumInput()
|
2014-11-30 01:08:02 +03:00
|
|
|
if len(args) < na {
|
|
|
|
return nil, fmt.Errorf("Not enough args to execute query. Expected %d, got %d.", na, len(args))
|
|
|
|
}
|
2014-06-25 22:54:09 +04:00
|
|
|
rows, err := s.Query(args[:na])
|
|
|
|
if err != nil && err != driver.ErrSkip {
|
|
|
|
s.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
args = args[na:]
|
|
|
|
tail := s.(*SQLiteStmt).t
|
|
|
|
if tail == "" {
|
|
|
|
return rows, nil
|
|
|
|
}
|
2014-11-13 20:21:49 +03:00
|
|
|
rows.Close()
|
2014-06-25 22:54:09 +04:00
|
|
|
s.Close()
|
|
|
|
query = tail
|
|
|
|
}
|
2014-06-25 06:41:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SQLiteConn) exec(cmd string) (driver.Result, error) {
|
2011-11-11 16:36:22 +04:00
|
|
|
pcmd := C.CString(cmd)
|
|
|
|
defer C.free(unsafe.Pointer(pcmd))
|
2015-03-24 00:17:00 +03:00
|
|
|
|
|
|
|
var rowid, changes C.long
|
|
|
|
rv := C._sqlite3_exec(c.db, pcmd, &rowid, &changes)
|
2011-11-11 16:36:22 +04:00
|
|
|
if rv != C.SQLITE_OK {
|
2014-06-25 06:41:58 +04:00
|
|
|
return nil, c.lastError()
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
2015-03-24 00:17:00 +03:00
|
|
|
return &SQLiteResult{int64(rowid), int64(changes)}, nil
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
|
|
|
|
2013-01-31 11:48:30 +04:00
|
|
|
// Begin transaction.
|
2011-11-11 16:36:22 +04:00
|
|
|
func (c *SQLiteConn) Begin() (driver.Tx, error) {
|
2014-06-25 06:41:58 +04:00
|
|
|
if _, err := c.exec("BEGIN"); err != nil {
|
2011-11-11 16:36:22 +04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &SQLiteTx{c}, nil
|
|
|
|
}
|
|
|
|
|
2013-11-19 13:13:19 +04:00
|
|
|
func errorString(err Error) string {
|
|
|
|
return C.GoString(C.sqlite3_errstr(C.int(err.Code)))
|
2013-06-21 00:52:38 +04:00
|
|
|
}
|
|
|
|
|
2013-01-31 11:48:30 +04:00
|
|
|
// Open database and return a new connection.
|
|
|
|
// You can specify DSN string with URI filename.
|
|
|
|
// test.db
|
|
|
|
// file:test.db?cache=shared&mode=memory
|
|
|
|
// :memory:
|
|
|
|
// file::memory:
|
2015-03-04 16:49:17 +03:00
|
|
|
// go-sqlite handle especially query parameters.
|
|
|
|
// loc=XXX
|
|
|
|
// Specify location of time format. It's possible to specify "auto".
|
2011-11-11 16:36:22 +04:00
|
|
|
func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
|
|
|
|
if C.sqlite3_threadsafe() == 0 {
|
|
|
|
return nil, errors.New("sqlite library was not compiled for thread-safe operation")
|
|
|
|
}
|
|
|
|
|
2015-03-04 16:49:17 +03:00
|
|
|
var loc *time.Location
|
2015-03-05 05:05:58 +03:00
|
|
|
pos := strings.IndexRune(dsn, '?')
|
|
|
|
if pos >= 1 {
|
|
|
|
params, err := url.ParseQuery(dsn[pos+1:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// loc
|
|
|
|
if val := params.Get("loc"); val != "" {
|
|
|
|
if val == "auto" {
|
|
|
|
loc = time.Local
|
|
|
|
} else {
|
|
|
|
loc, err = time.LoadLocation(val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Invalid loc: %v: %v", val, err)
|
2015-03-04 16:49:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-05 05:05:58 +03:00
|
|
|
|
2015-03-05 20:23:57 +03:00
|
|
|
if !strings.HasPrefix(dsn, "file:") {
|
2015-03-05 20:00:09 +03:00
|
|
|
dsn = dsn[:pos]
|
|
|
|
}
|
2015-03-04 16:49:17 +03:00
|
|
|
}
|
|
|
|
|
2011-11-11 16:36:22 +04:00
|
|
|
var db *C.sqlite3
|
|
|
|
name := C.CString(dsn)
|
|
|
|
defer C.free(unsafe.Pointer(name))
|
2013-02-03 18:25:30 +04:00
|
|
|
rv := C._sqlite3_open_v2(name, &db,
|
2011-11-11 16:36:22 +04:00
|
|
|
C.SQLITE_OPEN_FULLMUTEX|
|
|
|
|
C.SQLITE_OPEN_READWRITE|
|
2013-02-03 18:25:30 +04:00
|
|
|
C.SQLITE_OPEN_CREATE,
|
2011-11-11 16:36:22 +04:00
|
|
|
nil)
|
|
|
|
if rv != 0 {
|
2013-11-19 13:13:19 +04:00
|
|
|
return nil, Error{Code: ErrNo(rv)}
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
|
|
|
if db == nil {
|
|
|
|
return nil, errors.New("sqlite succeeded without returning a database")
|
|
|
|
}
|
2012-03-12 09:20:55 +04:00
|
|
|
|
2012-04-07 08:24:39 +04:00
|
|
|
rv = C.sqlite3_busy_timeout(db, 5000)
|
2012-03-12 09:20:55 +04:00
|
|
|
if rv != C.SQLITE_OK {
|
2013-11-19 13:13:19 +04:00
|
|
|
return nil, Error{Code: ErrNo(rv)}
|
2012-03-12 09:20:55 +04:00
|
|
|
}
|
|
|
|
|
2015-03-04 16:49:17 +03:00
|
|
|
conn := &SQLiteConn{db: db, loc: loc}
|
2013-08-23 08:58:54 +04:00
|
|
|
|
2013-08-25 07:36:35 +04:00
|
|
|
if len(d.Extensions) > 0 {
|
|
|
|
rv = C.sqlite3_enable_load_extension(db, 1)
|
|
|
|
if rv != C.SQLITE_OK {
|
|
|
|
return nil, errors.New(C.GoString(C.sqlite3_errmsg(db)))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, extension := range d.Extensions {
|
2015-01-26 18:50:50 +03:00
|
|
|
cext := C.CString(extension)
|
|
|
|
defer C.free(unsafe.Pointer(cext))
|
|
|
|
rv = C.sqlite3_load_extension(db, cext, nil, nil)
|
|
|
|
if rv != C.SQLITE_OK {
|
|
|
|
return nil, errors.New(C.GoString(C.sqlite3_errmsg(db)))
|
2013-08-25 07:36:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = C.sqlite3_enable_load_extension(db, 0)
|
|
|
|
if rv != C.SQLITE_OK {
|
|
|
|
return nil, errors.New(C.GoString(C.sqlite3_errmsg(db)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-23 08:58:54 +04:00
|
|
|
if d.ConnectHook != nil {
|
2013-08-25 07:04:51 +04:00
|
|
|
if err := d.ConnectHook(conn); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-08-23 08:58:54 +04:00
|
|
|
}
|
2014-11-14 11:13:35 +03:00
|
|
|
runtime.SetFinalizer(conn, (*SQLiteConn).Close)
|
2013-08-23 08:58:54 +04:00
|
|
|
return conn, nil
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
|
|
|
|
2013-01-31 11:48:30 +04:00
|
|
|
// Close the connection.
|
2011-11-11 16:36:22 +04:00
|
|
|
func (c *SQLiteConn) Close() error {
|
2013-10-24 17:21:37 +04:00
|
|
|
rv := C.sqlite3_close_v2(c.db)
|
2011-11-11 16:36:22 +04:00
|
|
|
if rv != C.SQLITE_OK {
|
2013-11-19 13:13:19 +04:00
|
|
|
return c.lastError()
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
|
|
|
c.db = nil
|
2014-11-14 11:13:35 +03:00
|
|
|
runtime.SetFinalizer(c, nil)
|
2011-11-11 16:36:22 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-01-31 11:48:30 +04:00
|
|
|
// Prepare query string. Return a new statement.
|
2011-11-11 16:36:22 +04:00
|
|
|
func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error) {
|
|
|
|
pquery := C.CString(query)
|
|
|
|
defer C.free(unsafe.Pointer(pquery))
|
|
|
|
var s *C.sqlite3_stmt
|
2013-09-09 05:44:44 +04:00
|
|
|
var tail *C.char
|
|
|
|
rv := C.sqlite3_prepare_v2(c.db, pquery, -1, &s, &tail)
|
2011-11-11 16:36:22 +04:00
|
|
|
if rv != C.SQLITE_OK {
|
2013-11-19 13:13:19 +04:00
|
|
|
return nil, c.lastError()
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
|
|
|
var t string
|
2015-03-24 00:18:23 +03:00
|
|
|
if tail != nil && *tail != '\000' {
|
2013-09-09 05:44:44 +04:00
|
|
|
t = strings.TrimSpace(C.GoString(tail))
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
2014-11-14 11:13:35 +03:00
|
|
|
ss := &SQLiteStmt{c: c, s: s, t: t}
|
|
|
|
runtime.SetFinalizer(ss, (*SQLiteStmt).Close)
|
|
|
|
return ss, nil
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
|
|
|
|
2013-01-31 11:48:30 +04:00
|
|
|
// Close the statement.
|
2011-11-11 16:36:22 +04:00
|
|
|
func (s *SQLiteStmt) Close() error {
|
2012-02-20 11:14:49 +04:00
|
|
|
if s.closed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
s.closed = true
|
2013-02-13 05:32:40 +04:00
|
|
|
if s.c == nil || s.c.db == nil {
|
|
|
|
return errors.New("sqlite statement with already closed database connection")
|
|
|
|
}
|
2011-11-11 16:38:53 +04:00
|
|
|
rv := C.sqlite3_finalize(s.s)
|
2011-11-11 16:36:22 +04:00
|
|
|
if rv != C.SQLITE_OK {
|
2013-11-19 13:13:19 +04:00
|
|
|
return s.c.lastError()
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
2014-11-14 11:13:35 +03:00
|
|
|
runtime.SetFinalizer(s, nil)
|
2011-11-11 16:36:22 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-01-31 11:48:30 +04:00
|
|
|
// Return a number of parameters.
|
2011-11-11 16:36:22 +04:00
|
|
|
func (s *SQLiteStmt) NumInput() int {
|
|
|
|
return int(C.sqlite3_bind_parameter_count(s.s))
|
|
|
|
}
|
|
|
|
|
2012-02-20 11:14:49 +04:00
|
|
|
func (s *SQLiteStmt) bind(args []driver.Value) error {
|
2011-11-11 16:36:22 +04:00
|
|
|
rv := C.sqlite3_reset(s.s)
|
2011-11-11 16:38:53 +04:00
|
|
|
if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
|
2013-11-19 13:13:19 +04:00
|
|
|
return s.c.lastError()
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, v := range args {
|
2011-11-11 16:38:53 +04:00
|
|
|
n := C.int(i + 1)
|
2011-11-11 16:36:22 +04:00
|
|
|
switch v := v.(type) {
|
|
|
|
case nil:
|
|
|
|
rv = C.sqlite3_bind_null(s.s, n)
|
|
|
|
case string:
|
2011-12-03 02:32:38 +04:00
|
|
|
if len(v) == 0 {
|
|
|
|
b := []byte{0}
|
|
|
|
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(0))
|
|
|
|
} else {
|
|
|
|
b := []byte(v)
|
|
|
|
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
|
|
|
|
}
|
2011-11-11 16:36:22 +04:00
|
|
|
case int64:
|
|
|
|
rv = C.sqlite3_bind_int64(s.s, n, C.sqlite3_int64(v))
|
|
|
|
case bool:
|
|
|
|
if bool(v) {
|
2012-03-12 09:20:55 +04:00
|
|
|
rv = C.sqlite3_bind_int(s.s, n, 1)
|
2011-11-11 16:36:22 +04:00
|
|
|
} else {
|
|
|
|
rv = C.sqlite3_bind_int(s.s, n, 0)
|
|
|
|
}
|
|
|
|
case float64:
|
|
|
|
rv = C.sqlite3_bind_double(s.s, n, C.double(v))
|
|
|
|
case []byte:
|
|
|
|
var p *byte
|
|
|
|
if len(v) > 0 {
|
|
|
|
p = &v[0]
|
|
|
|
}
|
|
|
|
rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(p), C.int(len(v)))
|
2012-04-07 08:17:54 +04:00
|
|
|
case time.Time:
|
2015-03-04 19:17:38 +03:00
|
|
|
b := []byte(v.UTC().Format(SQLiteTimestampFormats[0]))
|
|
|
|
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
|
|
|
if rv != C.SQLITE_OK {
|
2013-11-19 13:13:19 +04:00
|
|
|
return s.c.lastError()
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-18 04:06:30 +04:00
|
|
|
// Query the statement with arguments. Return records.
|
2012-02-20 11:14:49 +04:00
|
|
|
func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error) {
|
2011-11-11 16:36:22 +04:00
|
|
|
if err := s.bind(args); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-07-15 20:11:07 +04:00
|
|
|
return &SQLiteRows{s, int(C.sqlite3_column_count(s.s)), nil, nil, s.cls}, nil
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
|
|
|
|
2013-01-31 11:48:30 +04:00
|
|
|
// Return last inserted ID.
|
2011-11-14 17:10:13 +04:00
|
|
|
func (r *SQLiteResult) LastInsertId() (int64, error) {
|
2013-05-11 16:43:31 +04:00
|
|
|
return r.id, nil
|
2011-11-14 17:10:13 +04:00
|
|
|
}
|
|
|
|
|
2013-01-31 11:48:30 +04:00
|
|
|
// Return how many rows affected.
|
2011-11-14 17:10:13 +04:00
|
|
|
func (r *SQLiteResult) RowsAffected() (int64, error) {
|
2013-05-11 16:43:31 +04:00
|
|
|
return r.changes, nil
|
2011-11-14 17:10:13 +04:00
|
|
|
}
|
|
|
|
|
2013-01-31 11:48:30 +04:00
|
|
|
// Execute the statement with arguments. Return result object.
|
2012-02-20 11:14:49 +04:00
|
|
|
func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) {
|
2011-11-11 16:36:22 +04:00
|
|
|
if err := s.bind(args); err != nil {
|
2014-11-16 17:51:46 +03:00
|
|
|
C.sqlite3_reset(s.s)
|
2011-11-11 16:36:22 +04:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-24 00:17:00 +03:00
|
|
|
var rowid, changes C.long
|
|
|
|
rv := C._sqlite3_step(s.s, &rowid, &changes)
|
2011-11-11 16:38:53 +04:00
|
|
|
if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
|
2015-01-26 11:40:18 +03:00
|
|
|
err := s.c.lastError()
|
2014-11-16 17:51:46 +03:00
|
|
|
C.sqlite3_reset(s.s)
|
2015-01-26 11:40:18 +03:00
|
|
|
return nil, err
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
2015-03-24 00:17:00 +03:00
|
|
|
return &SQLiteResult{int64(rowid), int64(changes)}, nil
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
|
|
|
|
2013-01-31 11:48:30 +04:00
|
|
|
// Close the rows.
|
2011-11-11 16:36:22 +04:00
|
|
|
func (rc *SQLiteRows) Close() error {
|
2013-09-09 07:28:34 +04:00
|
|
|
if rc.s.closed {
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-15 20:11:07 +04:00
|
|
|
if rc.cls {
|
|
|
|
return rc.s.Close()
|
|
|
|
}
|
2012-03-12 09:20:55 +04:00
|
|
|
rv := C.sqlite3_reset(rc.s.s)
|
|
|
|
if rv != C.SQLITE_OK {
|
2013-11-19 13:13:19 +04:00
|
|
|
return rc.s.c.lastError()
|
2012-03-12 09:20:55 +04:00
|
|
|
}
|
|
|
|
return nil
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
|
|
|
|
2013-01-31 11:48:30 +04:00
|
|
|
// Return column names.
|
2011-11-11 16:36:22 +04:00
|
|
|
func (rc *SQLiteRows) Columns() []string {
|
|
|
|
if rc.nc != len(rc.cols) {
|
|
|
|
rc.cols = make([]string, rc.nc)
|
|
|
|
for i := 0; i < rc.nc; i++ {
|
|
|
|
rc.cols[i] = C.GoString(C.sqlite3_column_name(rc.s.s, C.int(i)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc.cols
|
|
|
|
}
|
|
|
|
|
2013-01-31 11:48:30 +04:00
|
|
|
// Move cursor to next.
|
2012-02-20 11:14:49 +04:00
|
|
|
func (rc *SQLiteRows) Next(dest []driver.Value) error {
|
2011-11-11 16:36:22 +04:00
|
|
|
rv := C.sqlite3_step(rc.s.s)
|
2012-03-01 20:52:03 +04:00
|
|
|
if rv == C.SQLITE_DONE {
|
|
|
|
return io.EOF
|
|
|
|
}
|
2011-11-11 16:36:22 +04:00
|
|
|
if rv != C.SQLITE_ROW {
|
2013-09-09 08:44:24 +04:00
|
|
|
rv = C.sqlite3_reset(rc.s.s)
|
|
|
|
if rv != C.SQLITE_OK {
|
2013-11-19 13:13:19 +04:00
|
|
|
return rc.s.c.lastError()
|
2013-09-09 08:44:24 +04:00
|
|
|
}
|
|
|
|
return nil
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
2012-04-07 08:17:54 +04:00
|
|
|
|
|
|
|
if rc.decltype == nil {
|
|
|
|
rc.decltype = make([]string, rc.nc)
|
|
|
|
for i := 0; i < rc.nc; i++ {
|
|
|
|
rc.decltype[i] = strings.ToLower(C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-11 16:36:22 +04:00
|
|
|
for i := range dest {
|
2011-11-11 16:38:53 +04:00
|
|
|
switch C.sqlite3_column_type(rc.s.s, C.int(i)) {
|
|
|
|
case C.SQLITE_INTEGER:
|
2012-04-07 08:17:54 +04:00
|
|
|
val := int64(C.sqlite3_column_int64(rc.s.s, C.int(i)))
|
2012-09-11 17:17:09 +04:00
|
|
|
switch rc.decltype[i] {
|
2014-09-02 19:36:34 +04:00
|
|
|
case "timestamp", "datetime", "date":
|
2015-01-02 09:31:46 +03:00
|
|
|
unixTimestamp := strconv.FormatInt(val, 10)
|
2015-03-05 05:05:58 +03:00
|
|
|
var t time.Time
|
2015-01-02 09:31:46 +03:00
|
|
|
if len(unixTimestamp) == 13 {
|
|
|
|
duration, err := time.ParseDuration(unixTimestamp + "ms")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error parsing %s value %d, %s", rc.decltype[i], val, err)
|
|
|
|
}
|
|
|
|
epoch := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
|
2015-03-05 05:05:58 +03:00
|
|
|
t = epoch.Add(duration)
|
2015-01-02 09:31:46 +03:00
|
|
|
} else {
|
2015-03-05 05:05:58 +03:00
|
|
|
t = time.Unix(val, 0)
|
|
|
|
}
|
|
|
|
if rc.s.c.loc != nil {
|
|
|
|
t = t.In(rc.s.c.loc)
|
2015-01-02 09:31:46 +03:00
|
|
|
}
|
2015-03-05 05:05:58 +03:00
|
|
|
dest[i] = t
|
2012-05-25 13:01:03 +04:00
|
|
|
case "boolean":
|
2012-09-11 17:17:09 +04:00
|
|
|
dest[i] = val > 0
|
2012-05-25 13:01:03 +04:00
|
|
|
default:
|
2012-04-07 08:17:54 +04:00
|
|
|
dest[i] = val
|
|
|
|
}
|
2011-11-11 16:38:53 +04:00
|
|
|
case C.SQLITE_FLOAT:
|
|
|
|
dest[i] = float64(C.sqlite3_column_double(rc.s.s, C.int(i)))
|
|
|
|
case C.SQLITE_BLOB:
|
|
|
|
p := C.sqlite3_column_blob(rc.s.s, C.int(i))
|
2013-10-24 17:25:07 +04:00
|
|
|
if p == nil {
|
|
|
|
dest[i] = nil
|
|
|
|
continue
|
|
|
|
}
|
2013-08-02 08:41:09 +04:00
|
|
|
n := int(C.sqlite3_column_bytes(rc.s.s, C.int(i)))
|
2012-08-20 19:20:58 +04:00
|
|
|
switch dest[i].(type) {
|
|
|
|
case sql.RawBytes:
|
|
|
|
dest[i] = (*[1 << 30]byte)(unsafe.Pointer(p))[0:n]
|
|
|
|
default:
|
|
|
|
slice := make([]byte, n)
|
|
|
|
copy(slice[:], (*[1 << 30]byte)(unsafe.Pointer(p))[0:n])
|
|
|
|
dest[i] = slice
|
|
|
|
}
|
2011-11-11 16:38:53 +04:00
|
|
|
case C.SQLITE_NULL:
|
|
|
|
dest[i] = nil
|
|
|
|
case C.SQLITE_TEXT:
|
2012-04-07 08:17:54 +04:00
|
|
|
var err error
|
2014-10-23 21:12:32 +04:00
|
|
|
var timeVal time.Time
|
2012-04-07 08:17:54 +04:00
|
|
|
s := C.GoString((*C.char)(unsafe.Pointer(C.sqlite3_column_text(rc.s.s, C.int(i)))))
|
2012-12-26 05:01:39 +04:00
|
|
|
|
2012-12-30 02:20:27 +04:00
|
|
|
switch rc.decltype[i] {
|
2014-08-11 14:03:27 +04:00
|
|
|
case "timestamp", "datetime", "date":
|
2015-03-05 05:05:58 +03:00
|
|
|
var t time.Time
|
2015-03-04 16:58:32 +03:00
|
|
|
for _, format := range SQLiteTimestampFormats {
|
2015-03-04 19:17:38 +03:00
|
|
|
if timeVal, err = time.ParseInLocation(format, s, time.UTC); err == nil {
|
2015-03-05 05:05:58 +03:00
|
|
|
t = timeVal
|
2015-03-04 16:58:32 +03:00
|
|
|
break
|
2012-11-04 04:45:58 +04:00
|
|
|
}
|
2012-12-30 04:36:29 +04:00
|
|
|
}
|
|
|
|
if err != nil {
|
2012-12-30 02:20:27 +04:00
|
|
|
// The column is a time value, so return the zero time on parse failure.
|
2015-03-05 05:05:58 +03:00
|
|
|
t = time.Time{}
|
|
|
|
}
|
|
|
|
if rc.s.c.loc != nil {
|
|
|
|
t = t.In(rc.s.c.loc)
|
2012-04-07 08:17:54 +04:00
|
|
|
}
|
2015-03-05 05:05:58 +03:00
|
|
|
dest[i] = t
|
2012-12-26 05:01:39 +04:00
|
|
|
default:
|
2013-12-05 19:58:28 +04:00
|
|
|
dest[i] = []byte(s)
|
2012-04-07 08:17:54 +04:00
|
|
|
}
|
2012-12-26 05:01:39 +04:00
|
|
|
|
2011-11-11 16:36:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|