@@ -182,6 +182,7 @@ type SQLiteTx struct {
182
182
183
183
// SQLiteStmt implement sql.Stmt.
184
184
type SQLiteStmt struct {
185
+ mu sync.Mutex
185
186
c * SQLiteConn
186
187
s * C.sqlite3_stmt
187
188
t string
@@ -803,6 +804,8 @@ func (c *SQLiteConn) prepare(ctx context.Context, query string) (driver.Stmt, er
803
804
804
805
// Close the statement.
805
806
func (s * SQLiteStmt ) Close () error {
807
+ s .mu .Lock ()
808
+ defer s .mu .Unlock ()
806
809
if s .closed {
807
810
return nil
808
811
}
@@ -980,26 +983,33 @@ func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result
980
983
981
984
// Close the rows.
982
985
func (rc * SQLiteRows ) Close () error {
986
+ rc .s .mu .Lock ()
983
987
if rc .s .closed || rc .closed {
988
+ rc .s .mu .Unlock ()
984
989
return nil
985
990
}
986
991
rc .closed = true
987
992
if rc .done != nil {
988
993
close (rc .done )
989
994
}
990
995
if rc .cls {
996
+ rc .s .mu .Unlock ()
991
997
return rc .s .Close ()
992
998
}
993
999
rv := C .sqlite3_reset (rc .s .s )
994
1000
if rv != C .SQLITE_OK {
1001
+ rc .s .mu .Unlock ()
995
1002
return rc .s .c .lastError ()
996
1003
}
1004
+ rc .s .mu .Unlock ()
997
1005
return nil
998
1006
}
999
1007
1000
1008
// Columns return column names.
1001
1009
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 ) {
1003
1013
rc .cols = make ([]string , rc .nc )
1004
1014
for i := 0 ; i < rc .nc ; i ++ {
1005
1015
rc .cols [i ] = C .GoString (C .sqlite3_column_name (rc .s .s , C .int (i )))
@@ -1010,7 +1020,9 @@ func (rc *SQLiteRows) Columns() []string {
1010
1020
1011
1021
// DeclTypes return column types.
1012
1022
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 {
1014
1026
rc .decltype = make ([]string , rc .nc )
1015
1027
for i := 0 ; i < rc .nc ; i ++ {
1016
1028
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 {
1021
1033
1022
1034
// Next move cursor to next.
1023
1035
func (rc * SQLiteRows ) Next (dest []driver.Value ) error {
1036
+ if rc .s .closed {
1037
+ return io .EOF
1038
+ }
1039
+ rc .s .mu .Lock ()
1024
1040
rv := C .sqlite3_step (rc .s .s )
1025
1041
if rv == C .SQLITE_DONE {
1042
+ rc .s .mu .Unlock ()
1026
1043
return io .EOF
1027
1044
}
1028
1045
if rv != C .SQLITE_ROW {
1046
+ defer rc .s .mu .Unlock ()
1029
1047
rv = C .sqlite3_reset (rc .s .s )
1030
1048
if rv != C .SQLITE_OK {
1031
1049
return rc .s .c .lastError ()
1032
1050
}
1051
+ rc .s .mu .Unlock ()
1033
1052
return nil
1034
1053
}
1035
1054
1036
1055
rc .DeclTypes ()
1037
1056
1057
+ rc .s .mu .Lock ()
1058
+ defer rc .s .mu .Unlock ()
1059
+
1038
1060
for i := range dest {
1039
1061
switch C .sqlite3_column_type (rc .s .s , C .int (i )) {
1040
1062
case C .SQLITE_INTEGER :
0 commit comments