@@ -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 )))
@@ -1008,9 +1018,8 @@ func (rc *SQLiteRows) Columns() []string {
1008
1018
return rc .cols
1009
1019
}
1010
1020
1011
- // DeclTypes return column types.
1012
- func (rc * SQLiteRows ) DeclTypes () []string {
1013
- if rc .decltype == nil {
1021
+ func (rc * SQLiteRows ) declTypes () []string {
1022
+ if rc .s .s != nil && rc .decltype == nil {
1014
1023
rc .decltype = make ([]string , rc .nc )
1015
1024
for i := 0 ; i < rc .nc ; i ++ {
1016
1025
rc .decltype [i ] = strings .ToLower (C .GoString (C .sqlite3_column_decltype (rc .s .s , C .int (i ))))
@@ -1019,8 +1028,20 @@ func (rc *SQLiteRows) DeclTypes() []string {
1019
1028
return rc .decltype
1020
1029
}
1021
1030
1031
+ // DeclTypes return column types.
1032
+ func (rc * SQLiteRows ) DeclTypes () []string {
1033
+ rc .s .mu .Lock ()
1034
+ defer rc .s .mu .Unlock ()
1035
+ return rc .declTypes ()
1036
+ }
1037
+
1022
1038
// Next move cursor to next.
1023
1039
func (rc * SQLiteRows ) Next (dest []driver.Value ) error {
1040
+ if rc .s .closed {
1041
+ return io .EOF
1042
+ }
1043
+ rc .s .mu .Lock ()
1044
+ defer rc .s .mu .Unlock ()
1024
1045
rv := C .sqlite3_step (rc .s .s )
1025
1046
if rv == C .SQLITE_DONE {
1026
1047
return io .EOF
@@ -1033,7 +1054,7 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
1033
1054
return nil
1034
1055
}
1035
1056
1036
- rc .DeclTypes ()
1057
+ rc .declTypes ()
1037
1058
1038
1059
for i := range dest {
1039
1060
switch C .sqlite3_column_type (rc .s .s , C .int (i )) {
0 commit comments