diff --git a/sqlite3.go b/sqlite3.go index ff29d79..58bdab1 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -173,6 +173,12 @@ var SQLiteTimestampFormats = []string{ "2006-01-02", } +const ( + columnDate string = "date" + columnDatetime string = "datetime" + columnTimestamp string = "timestamp" +) + func init() { sql.Register("sqlite3", &SQLiteDriver{}) } @@ -392,7 +398,7 @@ func (c *SQLiteConn) RegisterCommitHook(callback func() int) { if callback == nil { C.sqlite3_commit_hook(c.db, nil, nil) } else { - C.sqlite3_commit_hook(c.db, (*[0]byte)(unsafe.Pointer(C.commitHookTrampoline)), unsafe.Pointer(newHandle(c, callback))) + C.sqlite3_commit_hook(c.db, (*[0]byte)(C.commitHookTrampoline), unsafe.Pointer(newHandle(c, callback))) } } @@ -405,7 +411,7 @@ func (c *SQLiteConn) RegisterRollbackHook(callback func()) { if callback == nil { C.sqlite3_rollback_hook(c.db, nil, nil) } else { - C.sqlite3_rollback_hook(c.db, (*[0]byte)(unsafe.Pointer(C.rollbackHookTrampoline)), unsafe.Pointer(newHandle(c, callback))) + C.sqlite3_rollback_hook(c.db, (*[0]byte)(C.rollbackHookTrampoline), unsafe.Pointer(newHandle(c, callback))) } } @@ -422,7 +428,7 @@ func (c *SQLiteConn) RegisterUpdateHook(callback func(int, string, string, int64 if callback == nil { C.sqlite3_update_hook(c.db, nil, nil) } else { - C.sqlite3_update_hook(c.db, (*[0]byte)(unsafe.Pointer(C.updateHookTrampoline)), unsafe.Pointer(newHandle(c, callback))) + C.sqlite3_update_hook(c.db, (*[0]byte)(C.updateHookTrampoline), unsafe.Pointer(newHandle(c, callback))) } } @@ -504,7 +510,7 @@ func (c *SQLiteConn) RegisterFunc(name string, impl interface{}, pure bool) erro } func sqlite3CreateFunction(db *C.sqlite3, zFunctionName *C.char, nArg C.int, eTextRep C.int, pApp uintptr, xFunc unsafe.Pointer, xStep unsafe.Pointer, xFinal unsafe.Pointer) C.int { - return C._sqlite3_create_function(db, zFunctionName, nArg, eTextRep, C.uintptr_t(pApp), (*[0]byte)(unsafe.Pointer(xFunc)), (*[0]byte)(unsafe.Pointer(xStep)), (*[0]byte)(unsafe.Pointer(xFinal))) + return C._sqlite3_create_function(db, zFunctionName, nArg, eTextRep, C.uintptr_t(pApp), (*[0]byte)(xFunc), (*[0]byte)(xStep), (*[0]byte)(xFinal)) } // RegisterAggregator makes a Go type available as a SQLite aggregation function. @@ -1073,7 +1079,7 @@ func (s *SQLiteStmt) bind(args []namedValue) error { case int64: rv = C.sqlite3_bind_int64(s.s, n, C.sqlite3_int64(v)) case bool: - if bool(v) { + if v { rv = C.sqlite3_bind_int(s.s, n, 1) } else { rv = C.sqlite3_bind_int(s.s, n, 0) @@ -1279,7 +1285,7 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error { case C.SQLITE_INTEGER: val := int64(C.sqlite3_column_int64(rc.s.s, C.int(i))) switch rc.decltype[i] { - case "timestamp", "datetime", "date": + case columnTimestamp, columnDatetime, columnDate: var t time.Time // Assume a millisecond unix timestamp if it's 13 digits -- too // large to be a reasonable timestamp in seconds. @@ -1310,10 +1316,10 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error { n := int(C.sqlite3_column_bytes(rc.s.s, C.int(i))) switch dest[i].(type) { case sql.RawBytes: - dest[i] = (*[1 << 30]byte)(unsafe.Pointer(p))[0:n] + dest[i] = (*[1 << 30]byte)(p)[0:n] default: slice := make([]byte, n) - copy(slice[:], (*[1 << 30]byte)(unsafe.Pointer(p))[0:n]) + copy(slice[:], (*[1 << 30]byte)(p)[0:n]) dest[i] = slice } case C.SQLITE_NULL: @@ -1326,7 +1332,7 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error { s := C.GoStringN((*C.char)(unsafe.Pointer(C.sqlite3_column_text(rc.s.s, C.int(i)))), C.int(n)) switch rc.decltype[i] { - case "timestamp", "datetime", "date": + case columnTimestamp, columnDatetime, columnDate: var t time.Time s = strings.TrimSuffix(s, "Z") for _, format := range SQLiteTimestampFormats { diff --git a/sqlite3_test.go b/sqlite3_test.go index 84ecb5a..782f57c 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -563,7 +563,7 @@ func TestBoolean(t *testing.T) { t.Fatalf("Expected 1 row but %v", counter) } - if id != 1 && fbool != true { + if id != 1 && !fbool { t.Fatalf("Value for id 1 should be %v, not %v", bool1, fbool) } @@ -585,7 +585,7 @@ func TestBoolean(t *testing.T) { t.Fatalf("Expected 1 row but %v", counter) } - if id != 2 && fbool != false { + if id != 2 && fbool { t.Fatalf("Value for id 2 should be %v, not %v", bool2, fbool) } @@ -1591,10 +1591,7 @@ func BenchmarkCustomFunctions(b *testing.B) { sql.Register("sqlite3_BenchmarkCustomFunctions", &SQLiteDriver{ ConnectHook: func(conn *SQLiteConn) error { // Impure function to force sqlite to reexecute it each time. - if err := conn.RegisterFunc("custom_add", customAdd, false); err != nil { - return err - } - return nil + return conn.RegisterFunc("custom_add", customAdd, false) }, }) }) @@ -1701,7 +1698,7 @@ func (db *TestDB) tearDown() { // q replaces ? parameters if needed func (db *TestDB) q(sql string) string { switch db.dialect { - case POSTGRESQL: // repace with $1, $2, .. + case POSTGRESQL: // replace with $1, $2, .. qrx := regexp.MustCompile(`\?`) n := 0 return qrx.ReplaceAllStringFunc(sql, func(string) string {