@@ -1017,25 +1017,37 @@ func (c *SQLiteConn) query(ctx context.Context, query string, args []driver.Name
10171017 if err != nil {
10181018 return nil , err
10191019 }
1020- s .(* SQLiteStmt ).cls = true
1020+ ss := s .(* SQLiteStmt )
1021+ ss .cls = true
1022+ // sqlite3_prepare_v2 returns SQLITE_OK with a NULL statement handle
1023+ // when the input is empty or contains only whitespace/comments.
1024+ if ss .s == nil {
1025+ tail := ss .t
1026+ ss .Close ()
1027+ if tail == "" {
1028+ return & SQLiteRows {cls : true , ctx : ctx }, nil
1029+ }
1030+ query = tail
1031+ continue
1032+ }
10211033 na := s .NumInput ()
10221034 if len (args )- start < na {
1023- s .Close ()
1035+ ss .Close ()
10241036 return nil , fmt .Errorf ("not enough args to execute query: want %d got %d" , na , len (args )- start )
10251037 }
10261038 stmtArgs := stmtArgs (args , start , na )
1027- rows , err := s .( * SQLiteStmt ) .query (ctx , stmtArgs )
1039+ rows , err := ss .query (ctx , stmtArgs )
10281040 if err != nil && err != driver .ErrSkip {
1029- s .Close ()
1041+ ss .Close ()
10301042 return rows , err
10311043 }
10321044 start += na
1033- tail := s .( * SQLiteStmt ) .t
1045+ tail := ss .t
10341046 if tail == "" {
10351047 return rows , nil
10361048 }
10371049 rows .Close ()
1038- s .Close ()
1050+ ss .Close ()
10391051 query = tail
10401052 }
10411053}
@@ -2441,6 +2453,9 @@ func (rc *SQLiteRows) Close() error {
24412453
24422454// Columns return column names.
24432455func (rc * SQLiteRows ) Columns () []string {
2456+ if rc .s == nil {
2457+ return rc .cols
2458+ }
24442459 rc .s .mu .Lock ()
24452460 defer rc .s .mu .Unlock ()
24462461 if rc .s .s != nil && int (rc .nc ) != len (rc .cols ) {
@@ -2464,13 +2479,19 @@ func (rc *SQLiteRows) declTypes() []string {
24642479
24652480// DeclTypes return column types.
24662481func (rc * SQLiteRows ) DeclTypes () []string {
2482+ if rc .s == nil {
2483+ return rc .decltype
2484+ }
24672485 rc .s .mu .Lock ()
24682486 defer rc .s .mu .Unlock ()
24692487 return rc .declTypes ()
24702488}
24712489
24722490// Next move cursor to next. Attempts to honor context timeout from QueryContext call.
24732491func (rc * SQLiteRows ) Next (dest []driver.Value ) error {
2492+ if rc .s == nil {
2493+ return io .EOF
2494+ }
24742495 rc .s .mu .Lock ()
24752496 defer rc .s .mu .Unlock ()
24762497
0 commit comments