Skip to content

Commit 7383a4a

Browse files
committed
sqlitex: add a simple ExecuteTransient function for backward-compatibility.
1 parent 8d9ffff commit 7383a4a

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

sqlitex/sqlitex.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package sqlitex
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/agentio/sqlite/sqliteh"
7+
"github.com/agentio/sqlite/sqlitepool"
8+
)
9+
10+
type ExecOptions struct {
11+
Args []any
12+
ResultFunc func(stmt sqliteh.Stmt) error
13+
}
14+
15+
func ExecuteTransient(conn *sqlitepool.Rx, query string, options *ExecOptions) error {
16+
stmt := conn.Prepare(query)
17+
for i, a := range options.Args {
18+
switch v := a.(type) {
19+
case nil:
20+
stmt.BindNull(i + 1)
21+
case int64:
22+
stmt.BindInt64(i+1, v)
23+
case float64:
24+
stmt.BindDouble(i+1, v)
25+
case string:
26+
stmt.BindText64(i+1, v)
27+
case []byte:
28+
stmt.BindBlob64(i, v)
29+
default:
30+
return fmt.Errorf("unhandled type %T (fixme)", a)
31+
}
32+
}
33+
running := true
34+
for running {
35+
if row, err := stmt.Step(nil); err != nil {
36+
return err
37+
} else if !row {
38+
running = false
39+
} else if options.ResultFunc != nil {
40+
err = options.ResultFunc(stmt)
41+
if err != nil {
42+
return err
43+
}
44+
}
45+
}
46+
return stmt.Reset()
47+
}

0 commit comments

Comments
 (0)