Skip to content

Commit d40d490

Browse files
committed
fixes #458
1 parent a97e7bb commit d40d490

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

sqlite3.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ func (s *SQLiteStmt) Close() error {
811811
return errors.New("sqlite statement with already closed database connection")
812812
}
813813
rv := C.sqlite3_finalize(s.s)
814+
s.s = nil
814815
if rv != C.SQLITE_OK {
815816
return s.c.lastError()
816817
}

sqlite3_go18_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
package sqlite3
99

1010
import (
11+
"context"
1112
"database/sql"
13+
"fmt"
14+
"math/rand"
1215
"os"
1316
"testing"
17+
"time"
1418
)
1519

1620
func TestNamedParams(t *testing.T) {
@@ -48,3 +52,91 @@ func TestNamedParams(t *testing.T) {
4852
t.Error("Failed to db.QueryRow: not matched results")
4953
}
5054
}
55+
56+
var (
57+
testTableStatements = []string{
58+
`DROP TABLE IF EXISTS test_table`,
59+
`
60+
CREATE TABLE IF NOT EXISTS test_table (
61+
key1 VARCHAR(64) PRIMARY KEY,
62+
key_id VARCHAR(64) NOT NULL,
63+
key2 VARCHAR(64) NOT NULL,
64+
key3 VARCHAR(64) NOT NULL,
65+
key4 VARCHAR(64) NOT NULL,
66+
key5 VARCHAR(64) NOT NULL,
67+
key6 VARCHAR(64) NOT NULL,
68+
data BLOB NOT NULL
69+
);`,
70+
}
71+
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
72+
)
73+
74+
func randStringBytes(n int) string {
75+
b := make([]byte, n)
76+
for i := range b {
77+
b[i] = letterBytes[rand.Intn(len(letterBytes))]
78+
}
79+
return string(b)
80+
}
81+
82+
func initDatabase(t *testing.T, db *sql.DB, rowCount int64) {
83+
t.Logf("Executing db initializing statements")
84+
for _, query := range testTableStatements {
85+
_, err := db.Exec(query)
86+
if err != nil {
87+
t.Fatal(err)
88+
}
89+
}
90+
for i := int64(0); i < rowCount; i++ {
91+
query := `INSERT INTO test_table
92+
(key1, key_id, key2, key3, key4, key5, key6, data)
93+
VALUES
94+
(?, ?, ?, ?, ?, ?, ?, ?);`
95+
args := []interface{}{
96+
randStringBytes(50),
97+
fmt.Sprint(i),
98+
randStringBytes(50),
99+
randStringBytes(50),
100+
randStringBytes(50),
101+
randStringBytes(50),
102+
randStringBytes(50),
103+
randStringBytes(50),
104+
randStringBytes(2048),
105+
}
106+
_, err := db.Exec(query, args...)
107+
if err != nil {
108+
t.Fatal(err)
109+
}
110+
}
111+
}
112+
113+
func TestShortTimeout(t *testing.T) {
114+
db, err := sql.Open("sqlite3", "file::memory:?mode=memory&cache=shared")
115+
if err != nil {
116+
t.Fatal(err)
117+
}
118+
defer db.Close()
119+
initDatabase(t, db, 10000)
120+
121+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Microsecond)
122+
defer cancel()
123+
query := `SELECT key1, key_id, key2, key3, key4, key5, key6, data
124+
FROM test_table
125+
ORDER BY key2 ASC`
126+
rows, err := db.QueryContext(ctx, query)
127+
if err != nil {
128+
t.Fatal(err)
129+
}
130+
defer rows.Close()
131+
for rows.Next() {
132+
var key1, keyid, key2, key3, key4, key5, key6 string
133+
var data []byte
134+
err = rows.Scan(&key1, &keyid, &key2, &key3, &key4, &key5, &key6, &data)
135+
if err != nil {
136+
break
137+
}
138+
}
139+
if context.DeadlineExceeded != ctx.Err() {
140+
t.Fatal(ctx.Err())
141+
}
142+
}

0 commit comments

Comments
 (0)