forked from mirror/go-sqlite3
Update
* TestConnGetFilename * TestConnAutoCommit * TestHooksNil * TestLimit * TestResultColumnTypeInteger * TestResultColumnTypeText * TestResultColumnTypeBLOB * TestResultColumnTypeFloat * TestResultColumnTypeDateTime * TestResultColumnTypeNULL * TestPreparedStatementConn * Fix: ColumnTypeDatabaseTypeName: Upper / Lower case
This commit is contained in:
parent
ded182737b
commit
09cb77ebcc
|
@ -13,6 +13,7 @@ tools/upgrade/*.h
|
|||
# Exclude upgrade binary
|
||||
cmd/upgrade/upgrade
|
||||
|
||||
debug.test
|
||||
.DS_Store
|
||||
.DS_Store?
|
||||
._*
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
// 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.
|
||||
|
||||
// +build cgo
|
||||
|
||||
package sqlite3
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConnGetFileName(t *testing.T) {
|
||||
tempFilename := TempFilename(t)
|
||||
defer os.Remove(tempFilename)
|
||||
|
||||
driverName := fmt.Sprintf("sqlite3-%s", t.Name())
|
||||
|
||||
var driverConn *SQLiteConn
|
||||
sql.Register(driverName, &SQLiteDriver{
|
||||
ConnectHook: func(conn *SQLiteConn) error {
|
||||
driverConn = conn
|
||||
return nil
|
||||
},
|
||||
})
|
||||
|
||||
db, err := sql.Open(driverName, tempFilename)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Ping(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
abs, err := filepath.Abs(tempFilename)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fh := driverConn.GetFilename(""); fh != abs {
|
||||
t.Fatalf("GetFileName failed; expected: %s, got: %s", fh, abs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnAutoCommit(t *testing.T) {
|
||||
tempFilename := TempFilename(t)
|
||||
defer os.Remove(tempFilename)
|
||||
|
||||
driverName := fmt.Sprintf("sqlite3-%s", t.Name())
|
||||
|
||||
var driverConn *SQLiteConn
|
||||
sql.Register(driverName, &SQLiteDriver{
|
||||
ConnectHook: func(conn *SQLiteConn) error {
|
||||
driverConn = conn
|
||||
return nil
|
||||
},
|
||||
})
|
||||
|
||||
db, err := sql.Open(driverName, tempFilename)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
if err := db.Ping(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !driverConn.AutoCommit() {
|
||||
t.Fatal("Autocommit was expected to be true")
|
||||
}
|
||||
}
|
|
@ -2,3 +2,5 @@
|
|||
-tags=sqlite_userauth
|
||||
|
||||
-cover
|
||||
|
||||
-race
|
||||
|
|
|
@ -10,11 +10,12 @@ package sqlite3
|
|||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUpdateAndTransactionHooks(t *testing.T) {
|
||||
func TestHooksUpdateAndTransaction(t *testing.T) {
|
||||
var events []string
|
||||
var commitHookReturn = 0
|
||||
|
||||
|
@ -74,3 +75,31 @@ func TestUpdateAndTransactionHooks(t *testing.T) {
|
|||
t.Errorf("Expected notifications %v but got %v", expected, events)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHooksNil(t *testing.T) {
|
||||
tempFilename := TempFilename(t)
|
||||
defer os.Remove(tempFilename)
|
||||
|
||||
driverName := fmt.Sprintf("sqlite3-%s", t.Name())
|
||||
|
||||
var driverConn *SQLiteConn
|
||||
sql.Register(driverName, &SQLiteDriver{
|
||||
ConnectHook: func(conn *SQLiteConn) error {
|
||||
driverConn = conn
|
||||
return nil
|
||||
},
|
||||
})
|
||||
|
||||
db, err := sql.Open(driverName, tempFilename)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
if err := db.Ping(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
driverConn.RegisterCommitHook(nil)
|
||||
driverConn.RegisterRollbackHook(nil)
|
||||
driverConn.RegisterUpdateHook(nil)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
// 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.
|
||||
|
||||
// +build cgo
|
||||
|
||||
package sqlite3
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLimit(t *testing.T) {
|
||||
tempFilename := TempFilename(t)
|
||||
defer os.Remove(tempFilename)
|
||||
|
||||
driverName := fmt.Sprintf("sqlite3-%s", t.Name())
|
||||
|
||||
var driverConn *SQLiteConn
|
||||
sql.Register(driverName, &SQLiteDriver{
|
||||
ConnectHook: func(conn *SQLiteConn) error {
|
||||
driverConn = conn
|
||||
return nil
|
||||
},
|
||||
})
|
||||
|
||||
db, err := sql.Open(driverName, tempFilename)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
if err := db.Ping(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if rv := driverConn.SetLimit(SQLITE_LIMIT_TRIGGER_DEPTH, 5); rv != 1000 {
|
||||
t.Fatalf("Unable to set limit; %d", rv)
|
||||
}
|
||||
|
||||
if limit := driverConn.GetLimit(SQLITE_LIMIT_TRIGGER_DEPTH); limit != 5 {
|
||||
t.Fatal("Limit was not set correctly")
|
||||
}
|
||||
}
|
|
@ -222,7 +222,7 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
|
|||
|
||||
// ColumnTypeDatabaseTypeName implement RowsColumnTypeDatabaseTypeName.
|
||||
func (rc *SQLiteRows) ColumnTypeDatabaseTypeName(i int) string {
|
||||
return C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i)))
|
||||
return strings.ToUpper(C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))))
|
||||
}
|
||||
|
||||
// ColumnTypeNullable implement RowsColumnTypeNullable.
|
||||
|
@ -231,10 +231,12 @@ func (rc *SQLiteRows) ColumnTypeNullable(i int) (nullable, ok bool) {
|
|||
}
|
||||
|
||||
// ColumnTypeScanType implement RowsColumnTypeScanType.
|
||||
// This can only be successfull retrieved while within Next()
|
||||
// The underlying SQLite function depends upon sqlite_step to be called.
|
||||
func (rc *SQLiteRows) ColumnTypeScanType(i int) reflect.Type {
|
||||
switch C.sqlite3_column_type(rc.s.s, C.int(i)) {
|
||||
case C.SQLITE_INTEGER:
|
||||
switch C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))) {
|
||||
switch strings.ToLower(C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i)))) {
|
||||
case "timestamp", "datetime", "date":
|
||||
return reflect.TypeOf(time.Time{})
|
||||
case "boolean":
|
||||
|
|
|
@ -0,0 +1,131 @@
|
|||
// 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.
|
||||
|
||||
// +build cgo
|
||||
|
||||
package sqlite3
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestResultColumnTypeInteger(t *testing.T) {
|
||||
testColumnType(t, "INTEGER", 1, reflect.TypeOf(int64(0)))
|
||||
}
|
||||
|
||||
func TestResultColumnTypeText(t *testing.T) {
|
||||
testColumnType(t, "TEXT", "FooBar", reflect.TypeOf(""))
|
||||
}
|
||||
|
||||
func TestResultColumnTypeBLOB(t *testing.T) {
|
||||
testColumnType(t, "BLOB", []byte{'\x20'}, reflect.TypeOf([]byte{}))
|
||||
}
|
||||
|
||||
func TestResultColumnTypeFloat(t *testing.T) {
|
||||
testColumnType(t, "REAL", float64(1.3), reflect.TypeOf(float64(0)))
|
||||
}
|
||||
|
||||
func TestResultColumnTypeBoolean(t *testing.T) {
|
||||
testColumnType(t, "BOOLEAN", true, reflect.TypeOf(true))
|
||||
}
|
||||
|
||||
func TestResultColumnTypeDateTime(t *testing.T) {
|
||||
testColumnType(t, "DATETIME", time.Now().Unix(), reflect.TypeOf(time.Time{}))
|
||||
}
|
||||
|
||||
func TestResultColumnTypeNULL(t *testing.T) {
|
||||
tempFilename := TempFilename(t)
|
||||
defer os.Remove(tempFilename)
|
||||
|
||||
db, err := sql.Open("sqlite3", tempFilename)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
if _, err := db.Exec("CREATE TABLE type (t TEXT);"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ins, err := db.Prepare("INSERT INTO type (t) VALUES(?)")
|
||||
if err != nil {
|
||||
t.Fatalf("Prepare Failed: %v", err)
|
||||
}
|
||||
|
||||
if ins.Exec(nil); err != nil {
|
||||
t.Fatalf("Insert Failed: %v", err)
|
||||
}
|
||||
|
||||
rows, err := db.Query("SELECT t FROM type LIMIT 1;")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
types, err := rows.ColumnTypes()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if types[0].DatabaseTypeName() != "TEXT" {
|
||||
t.Fatalf("Invalid column type; expected: %s, got: %s", "TEXT", types[0].DatabaseTypeName())
|
||||
}
|
||||
|
||||
if v := types[0].ScanType(); v != reflect.TypeOf(nil) {
|
||||
t.Fatalf("Wrong scan type returned, expected: %v; got: %v", reflect.TypeOf(nil), v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testColumnType(t *testing.T, dtype string, def interface{}, rtype reflect.Type) {
|
||||
tempFilename := TempFilename(t)
|
||||
defer os.Remove(tempFilename)
|
||||
|
||||
db, err := sql.Open("sqlite3", tempFilename)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf("CREATE TABLE type (t %s NOT NULL);", dtype)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ins, err := db.Prepare("INSERT INTO type (t) VALUES(?)")
|
||||
if err != nil {
|
||||
t.Fatalf("Prepare Failed: %v", err)
|
||||
}
|
||||
|
||||
if ins.Exec(def); err != nil {
|
||||
t.Fatalf("Insert Failed: %v", err)
|
||||
}
|
||||
|
||||
rows, err := db.Query("SELECT t FROM type LIMIT 1;")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
types, err := rows.ColumnTypes()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if types[0].DatabaseTypeName() != dtype {
|
||||
t.Fatalf("Invalid column type; expected: %s, got: %s", dtype, types[0].DatabaseTypeName())
|
||||
}
|
||||
|
||||
if v := types[0].ScanType(); v != rtype {
|
||||
t.Fatalf("Wrong scan type returned, expected: %v; got: %v", rtype, v)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
// 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.
|
||||
|
||||
// +build cgo
|
||||
|
||||
package sqlite3
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPreparedStatementConn(t *testing.T) {
|
||||
tempFilename := TempFilename(t)
|
||||
defer os.Remove(tempFilename)
|
||||
|
||||
driverName := fmt.Sprintf("sqlite3-%s", t.Name())
|
||||
|
||||
var driverConn *SQLiteConn
|
||||
sql.Register(driverName, &SQLiteDriver{
|
||||
ConnectHook: func(conn *SQLiteConn) error {
|
||||
driverConn = conn
|
||||
return nil
|
||||
},
|
||||
})
|
||||
|
||||
db, err := sql.Open(driverName, tempFilename)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.Exec("CREATE TABLE t (count INT);"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ins, err := driverConn.Prepare("INSERT INTO t (count) VALUES (?)")
|
||||
if err != nil {
|
||||
t.Fatalf("prepare: %v", err)
|
||||
}
|
||||
|
||||
qry, err := driverConn.Prepare("SELECT * FROM t WHERE count = ?")
|
||||
if err != nil {
|
||||
t.Fatalf("select: %v", err)
|
||||
}
|
||||
|
||||
for n := 1; n <= 3; n++ {
|
||||
if _, err := ins.(*SQLiteStmt).Exec([]driver.Value{n}); err != nil {
|
||||
t.Fatalf("insert(%d) = %v", n, err)
|
||||
}
|
||||
|
||||
if _, err := qry.(*SQLiteStmt).Query([]driver.Value{n}); err != nil {
|
||||
t.Fatalf("query(%d) = %v", n, err)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue