Skip to content

Commit 911f1c4

Browse files
committed
fix lock
1 parent ee72024 commit 911f1c4

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

sqlite3.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ type SQLiteTx struct {
182182

183183
// SQLiteStmt implement sql.Stmt.
184184
type SQLiteStmt struct {
185+
mu sync.Mutex
185186
c *SQLiteConn
186187
s *C.sqlite3_stmt
187188
t string
@@ -803,6 +804,8 @@ func (c *SQLiteConn) prepare(ctx context.Context, query string) (driver.Stmt, er
803804

804805
// Close the statement.
805806
func (s *SQLiteStmt) Close() error {
807+
s.mu.Lock()
808+
defer s.mu.Unlock()
806809
if s.closed {
807810
return nil
808811
}
@@ -980,26 +983,33 @@ func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result
980983

981984
// Close the rows.
982985
func (rc *SQLiteRows) Close() error {
986+
rc.s.mu.Lock()
983987
if rc.s.closed || rc.closed {
988+
rc.s.mu.Unlock()
984989
return nil
985990
}
986991
rc.closed = true
987992
if rc.done != nil {
988993
close(rc.done)
989994
}
990995
if rc.cls {
996+
rc.s.mu.Unlock()
991997
return rc.s.Close()
992998
}
993999
rv := C.sqlite3_reset(rc.s.s)
9941000
if rv != C.SQLITE_OK {
1001+
rc.s.mu.Unlock()
9951002
return rc.s.c.lastError()
9961003
}
1004+
rc.s.mu.Unlock()
9971005
return nil
9981006
}
9991007

10001008
// Columns return column names.
10011009
func (rc *SQLiteRows) Columns() []string {
1002-
if rc.nc != len(rc.cols) {
1010+
rc.s.mu.Lock()
1011+
defer rc.s.mu.Unlock()
1012+
if rc.s.s != nil && rc.nc != len(rc.cols) {
10031013
rc.cols = make([]string, rc.nc)
10041014
for i := 0; i < rc.nc; i++ {
10051015
rc.cols[i] = C.GoString(C.sqlite3_column_name(rc.s.s, C.int(i)))
@@ -1010,7 +1020,9 @@ func (rc *SQLiteRows) Columns() []string {
10101020

10111021
// DeclTypes return column types.
10121022
func (rc *SQLiteRows) DeclTypes() []string {
1013-
if rc.decltype == nil {
1023+
rc.s.mu.Lock()
1024+
defer rc.s.mu.Unlock()
1025+
if rc.s.s != nil && rc.decltype == nil {
10141026
rc.decltype = make([]string, rc.nc)
10151027
for i := 0; i < rc.nc; i++ {
10161028
rc.decltype[i] = strings.ToLower(C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))))
@@ -1021,20 +1033,30 @@ func (rc *SQLiteRows) DeclTypes() []string {
10211033

10221034
// Next move cursor to next.
10231035
func (rc *SQLiteRows) Next(dest []driver.Value) error {
1036+
if rc.s.closed {
1037+
return io.EOF
1038+
}
1039+
rc.s.mu.Lock()
10241040
rv := C.sqlite3_step(rc.s.s)
10251041
if rv == C.SQLITE_DONE {
1042+
rc.s.mu.Unlock()
10261043
return io.EOF
10271044
}
10281045
if rv != C.SQLITE_ROW {
1046+
defer rc.s.mu.Unlock()
10291047
rv = C.sqlite3_reset(rc.s.s)
10301048
if rv != C.SQLITE_OK {
10311049
return rc.s.c.lastError()
10321050
}
1051+
rc.s.mu.Unlock()
10331052
return nil
10341053
}
10351054

10361055
rc.DeclTypes()
10371056

1057+
rc.s.mu.Lock()
1058+
defer rc.s.mu.Unlock()
1059+
10381060
for i := range dest {
10391061
switch C.sqlite3_column_type(rc.s.s, C.int(i)) {
10401062
case C.SQLITE_INTEGER:

0 commit comments

Comments
 (0)