remove ccirrelo/supervisor, update

everything seems to work even though sirupsen is upper case?

:cyfap:
This commit is contained in:
Reed Allman
2017-09-05 11:36:47 -07:00
parent 78ba35fb23
commit 27e43c5d94
397 changed files with 13691 additions and 8828 deletions

View File

@@ -65,7 +65,7 @@ FAQ
* Want to get time.Time with current locale
Use `loc=auto` in SQLite3 filename schema like `file:foo.db?loc=auto`.
Use `_loc=auto` in SQLite3 filename schema like `file:foo.db?_loc=auto`.
* Can I use this in multiple routines concurrently?

View File

@@ -53,6 +53,12 @@ func doneTrampoline(ctx *C.sqlite3_context) {
ai.Done(ctx)
}
//export compareTrampoline
func compareTrampoline(handlePtr uintptr, la C.int, a *C.char, lb C.int, b *C.char) C.int {
cmp := lookupHandle(handlePtr).(func(string, string) int)
return C.int(cmp(C.GoStringN(a, la), C.GoStringN(b, lb)))
}
//export commitHookTrampoline
func commitHookTrampoline(handle uintptr) int {
callback := lookupHandle(handle).(func() int)

View File

@@ -100,6 +100,8 @@ int _sqlite3_create_function(
}
void callbackTrampoline(sqlite3_context*, int, sqlite3_value**);
int compareTrampoline(void*, int, char*, int, char*);
int commitHookTrampoline(void*);
void rollbackHookTrampoline(void*);
void updateHookTrampoline(void*, int, char*, char*, sqlite3_int64);
@@ -182,6 +184,7 @@ type SQLiteTx struct {
// SQLiteStmt implement sql.Stmt.
type SQLiteStmt struct {
mu sync.Mutex
c *SQLiteConn
s *C.sqlite3_stmt
t string
@@ -325,6 +328,29 @@ func (tx *SQLiteTx) Rollback() error {
return err
}
// RegisterCollation makes a Go function available as a collation.
//
// cmp receives two UTF-8 strings, a and b. The result should be 0 if
// a==b, -1 if a < b, and +1 if a > b.
//
// cmp must always return the same result given the same
// inputs. Additionally, it must have the following properties for all
// strings A, B and C: if A==B then B==A; if A==B and B==C then A==C;
// if A<B then B>A; if A<B and B<C then A<C.
//
// If cmp does not obey these constraints, sqlite3's behavior is
// undefined when the collation is used.
func (c *SQLiteConn) RegisterCollation(name string, cmp func(string, string) int) error {
handle := newHandle(c, cmp)
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
rv := C.sqlite3_create_collation(c.db, cname, C.SQLITE_UTF8, unsafe.Pointer(handle), (*[0]byte)(unsafe.Pointer(C.compareTrampoline)))
if rv != C.SQLITE_OK {
return c.lastError()
}
return nil
}
// RegisterCommitHook sets the commit hook for a connection.
//
// If the callback returns non-zero the transaction will become a rollback.
@@ -803,6 +829,8 @@ func (c *SQLiteConn) prepare(ctx context.Context, query string) (driver.Stmt, er
// Close the statement.
func (s *SQLiteStmt) Close() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return nil
}
@@ -811,6 +839,7 @@ func (s *SQLiteStmt) Close() error {
return errors.New("sqlite statement with already closed database connection")
}
rv := C.sqlite3_finalize(s.s)
s.s = nil
if rv != C.SQLITE_OK {
return s.c.lastError()
}
@@ -867,10 +896,11 @@ func (s *SQLiteStmt) bind(args []namedValue) error {
case float64:
rv = C.sqlite3_bind_double(s.s, n, C.double(v))
case []byte:
if len(v) == 0 {
ln := len(v)
if ln == 0 {
v = placeHolder
}
rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(&v[0]), C.int(len(v)))
rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(&v[0]), C.int(ln))
case time.Time:
b := []byte(v.Format(SQLiteTimestampFormats[0]))
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
@@ -978,7 +1008,9 @@ func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result
// Close the rows.
func (rc *SQLiteRows) Close() error {
rc.s.mu.Lock()
if rc.s.closed || rc.closed {
rc.s.mu.Unlock()
return nil
}
rc.closed = true
@@ -986,18 +1018,23 @@ func (rc *SQLiteRows) Close() error {
close(rc.done)
}
if rc.cls {
rc.s.mu.Unlock()
return rc.s.Close()
}
rv := C.sqlite3_reset(rc.s.s)
if rv != C.SQLITE_OK {
rc.s.mu.Unlock()
return rc.s.c.lastError()
}
rc.s.mu.Unlock()
return nil
}
// Columns return column names.
func (rc *SQLiteRows) Columns() []string {
if rc.nc != len(rc.cols) {
rc.s.mu.Lock()
defer rc.s.mu.Unlock()
if rc.s.s != nil && 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)))
@@ -1006,9 +1043,8 @@ func (rc *SQLiteRows) Columns() []string {
return rc.cols
}
// DeclTypes return column types.
func (rc *SQLiteRows) DeclTypes() []string {
if rc.decltype == nil {
func (rc *SQLiteRows) declTypes() []string {
if rc.s.s != nil && 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))))
@@ -1017,8 +1053,20 @@ func (rc *SQLiteRows) DeclTypes() []string {
return rc.decltype
}
// DeclTypes return column types.
func (rc *SQLiteRows) DeclTypes() []string {
rc.s.mu.Lock()
defer rc.s.mu.Unlock()
return rc.declTypes()
}
// Next move cursor to next.
func (rc *SQLiteRows) Next(dest []driver.Value) error {
if rc.s.closed {
return io.EOF
}
rc.s.mu.Lock()
defer rc.s.mu.Unlock()
rv := C.sqlite3_step(rc.s.s)
if rv == C.SQLITE_DONE {
return io.EOF
@@ -1031,7 +1079,7 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
return nil
}
rc.DeclTypes()
rc.declTypes()
for i := range dest {
switch C.sqlite3_column_type(rc.s.s, C.int(i)) {

View File

@@ -8,9 +8,13 @@
package sqlite3
import (
"context"
"database/sql"
"fmt"
"math/rand"
"os"
"testing"
"time"
)
func TestNamedParams(t *testing.T) {
@@ -48,3 +52,91 @@ func TestNamedParams(t *testing.T) {
t.Error("Failed to db.QueryRow: not matched results")
}
}
var (
testTableStatements = []string{
`DROP TABLE IF EXISTS test_table`,
`
CREATE TABLE IF NOT EXISTS test_table (
key1 VARCHAR(64) PRIMARY KEY,
key_id VARCHAR(64) NOT NULL,
key2 VARCHAR(64) NOT NULL,
key3 VARCHAR(64) NOT NULL,
key4 VARCHAR(64) NOT NULL,
key5 VARCHAR(64) NOT NULL,
key6 VARCHAR(64) NOT NULL,
data BLOB NOT NULL
);`,
}
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
)
func randStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}
func initDatabase(t *testing.T, db *sql.DB, rowCount int64) {
t.Logf("Executing db initializing statements")
for _, query := range testTableStatements {
_, err := db.Exec(query)
if err != nil {
t.Fatal(err)
}
}
for i := int64(0); i < rowCount; i++ {
query := `INSERT INTO test_table
(key1, key_id, key2, key3, key4, key5, key6, data)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?);`
args := []interface{}{
randStringBytes(50),
fmt.Sprint(i),
randStringBytes(50),
randStringBytes(50),
randStringBytes(50),
randStringBytes(50),
randStringBytes(50),
randStringBytes(50),
randStringBytes(2048),
}
_, err := db.Exec(query, args...)
if err != nil {
t.Fatal(err)
}
}
}
func TestShortTimeout(t *testing.T) {
db, err := sql.Open("sqlite3", "file::memory:?mode=memory&cache=shared")
if err != nil {
t.Fatal(err)
}
defer db.Close()
initDatabase(t, db, 10000)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Microsecond)
defer cancel()
query := `SELECT key1, key_id, key2, key3, key4, key5, key6, data
FROM test_table
ORDER BY key2 ASC`
rows, err := db.QueryContext(ctx, query)
if err != nil {
t.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var key1, keyid, key2, key3, key4, key5, key6 string
var data []byte
err = rows.Scan(&key1, &keyid, &key2, &key3, &key4, &key5, &key6, &data)
if err != nil {
break
}
}
if context.DeadlineExceeded != ctx.Err() {
t.Fatal(ctx.Err())
}
}

View File

@@ -10,5 +10,6 @@ package sqlite3
#cgo CFLAGS: -DUSE_LIBSQLITE3
#cgo linux LDFLAGS: -lsqlite3
#cgo darwin LDFLAGS: -L/usr/local/opt/sqlite/lib -lsqlite3
#cgo solaris LDFLAGS: -lsqlite3
*/
import "C"

View File

@@ -9,5 +9,6 @@ package sqlite3
/*
#cgo CFLAGS: -I.
#cgo linux LDFLAGS: -ldl
#cgo solaris LDFLAGS: -lc
*/
import "C"

View File

@@ -6,6 +6,7 @@
package sqlite3
import (
"bytes"
"database/sql"
"database/sql/driver"
"errors"
@@ -1231,6 +1232,127 @@ func TestFunctionRegistration(t *testing.T) {
}
}
func rot13(r rune) rune {
switch {
case r >= 'A' && r <= 'Z':
return 'A' + (r-'A'+13)%26
case r >= 'a' && r <= 'z':
return 'a' + (r-'a'+13)%26
}
return r
}
func TestCollationRegistration(t *testing.T) {
collateRot13 := func(a, b string) int {
ra, rb := strings.Map(rot13, a), strings.Map(rot13, b)
return strings.Compare(ra, rb)
}
collateRot13Reverse := func(a, b string) int {
return collateRot13(b, a)
}
sql.Register("sqlite3_CollationRegistration", &SQLiteDriver{
ConnectHook: func(conn *SQLiteConn) error {
if err := conn.RegisterCollation("rot13", collateRot13); err != nil {
return err
}
if err := conn.RegisterCollation("rot13reverse", collateRot13Reverse); err != nil {
return err
}
return nil
},
})
db, err := sql.Open("sqlite3_CollationRegistration", ":memory:")
if err != nil {
t.Fatal("Failed to open database:", err)
}
defer db.Close()
populate := []string{
`CREATE TABLE test (s TEXT)`,
`INSERT INTO test VALUES ("aaaa")`,
`INSERT INTO test VALUES ("ffff")`,
`INSERT INTO test VALUES ("qqqq")`,
`INSERT INTO test VALUES ("tttt")`,
`INSERT INTO test VALUES ("zzzz")`,
}
for _, stmt := range populate {
if _, err := db.Exec(stmt); err != nil {
t.Fatal("Failed to populate test DB:", err)
}
}
ops := []struct {
query string
want []string
}{
{
"SELECT * FROM test ORDER BY s COLLATE rot13 ASC",
[]string{
"qqqq",
"tttt",
"zzzz",
"aaaa",
"ffff",
},
},
{
"SELECT * FROM test ORDER BY s COLLATE rot13 DESC",
[]string{
"ffff",
"aaaa",
"zzzz",
"tttt",
"qqqq",
},
},
{
"SELECT * FROM test ORDER BY s COLLATE rot13reverse ASC",
[]string{
"ffff",
"aaaa",
"zzzz",
"tttt",
"qqqq",
},
},
{
"SELECT * FROM test ORDER BY s COLLATE rot13reverse DESC",
[]string{
"qqqq",
"tttt",
"zzzz",
"aaaa",
"ffff",
},
},
}
for _, op := range ops {
rows, err := db.Query(op.query)
if err != nil {
t.Fatalf("Query %q failed: %s", op.query, err)
}
got := []string{}
defer rows.Close()
for rows.Next() {
var s string
if err = rows.Scan(&s); err != nil {
t.Fatalf("Reading row for %q: %s", op.query, err)
}
got = append(got, s)
}
if err = rows.Err(); err != nil {
t.Fatalf("Reading rows for %q: %s", op.query, err)
}
if !reflect.DeepEqual(got, op.want) {
t.Fatalf("Unexpected output from %q\ngot:\n%s\n\nwant:\n%s", op.query, strings.Join(got, "\n"), strings.Join(op.want, "\n"))
}
}
}
func TestDeclTypes(t *testing.T) {
d := SQLiteDriver{}
@@ -1343,6 +1465,61 @@ func TestUpdateAndTransactionHooks(t *testing.T) {
}
}
func TestNilAndEmptyBytes(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
actualNil := []byte("use this to use an actual nil not a reference to nil")
emptyBytes := []byte{}
for tsti, tst := range []struct {
name string
columnType string
insertBytes []byte
expectedBytes []byte
}{
{"actual nil blob", "blob", actualNil, nil},
{"referenced nil blob", "blob", nil, nil},
{"empty blob", "blob", emptyBytes, emptyBytes},
{"actual nil text", "text", actualNil, nil},
{"referenced nil text", "text", nil, nil},
{"empty text", "text", emptyBytes, emptyBytes},
} {
if _, err = db.Exec(fmt.Sprintf("create table tbl%d (txt %s)", tsti, tst.columnType)); err != nil {
t.Fatal(tst.name, err)
}
if bytes.Equal(tst.insertBytes, actualNil) {
if _, err = db.Exec(fmt.Sprintf("insert into tbl%d (txt) values (?)", tsti), nil); err != nil {
t.Fatal(tst.name, err)
}
} else {
if _, err = db.Exec(fmt.Sprintf("insert into tbl%d (txt) values (?)", tsti), &tst.insertBytes); err != nil {
t.Fatal(tst.name, err)
}
}
rows, err := db.Query(fmt.Sprintf("select txt from tbl%d", tsti))
if err != nil {
t.Fatal(tst.name, err)
}
if !rows.Next() {
t.Fatal(tst.name, "no rows")
}
var scanBytes []byte
if err = rows.Scan(&scanBytes); err != nil {
t.Fatal(tst.name, err)
}
if err = rows.Err(); err != nil {
t.Fatal(tst.name, err)
}
if tst.expectedBytes == nil && scanBytes != nil {
t.Errorf("%s: %#v != %#v", tst.name, scanBytes, tst.expectedBytes)
} else if !bytes.Equal(scanBytes, tst.expectedBytes) {
t.Errorf("%s: %#v != %#v", tst.name, scanBytes, tst.expectedBytes)
}
}
}
var customFunctionOnce sync.Once
func BenchmarkCustomFunctions(b *testing.B) {