crawshaw.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //go:build !zombiezen_sqlite
  2. package sqlitex
  3. import (
  4. "context"
  5. "github.com/go-llsqlite/crawshaw/sqlitex"
  6. sqlite "github.com/go-llsqlite/adapter"
  7. )
  8. var (
  9. // In zombiezen this can be done with ExecOption parameter to Execute. In crawshaw this is a
  10. // noop for now. Here is how it was done for zombiezen:
  11. // https://github.com/zombiezen/go-sqlite/commit/754b7de62e83f3bc7fd226d0e9e1ab2bbe0f6916.
  12. Transaction = Save
  13. )
  14. type ExecOptions = sqlitex.ExecOptions
  15. func Execute(conn *sqlite.Conn, query string, opts *ExecOptions) error {
  16. return sqlitex.Execute(conn.Conn, query, opts)
  17. }
  18. func ExecuteScript(conn *sqlite.Conn, queries string, opts *ExecOptions) (err error) {
  19. return sqlitex.ExecuteScript(conn.Conn, queries, opts)
  20. }
  21. // TODO: Actually implement checked for crawshaw.
  22. func ExecChecked(conn *sqlite.Conn, query string, resultFn func(stmt *sqlite.Stmt) error, args ...interface{}) error {
  23. return sqlitex.Execute(conn.Conn, query, &ExecOptions{
  24. Args: args,
  25. ResultFunc: resultFn,
  26. })
  27. }
  28. func ExecScript(conn *sqlite.Conn, queries string) error {
  29. return sqlitex.ExecScript(conn.Conn, queries)
  30. }
  31. func Save(conn *sqlite.Conn) (releaseFn func(*error)) {
  32. return sqlitex.Save(conn.Conn)
  33. }
  34. func Exec(conn *sqlite.Conn, query string, resultFn func(stmt *sqlite.Stmt) error, args ...interface{}) error {
  35. return sqlitex.Exec(conn.Conn, query, resultFn, args...)
  36. }
  37. func ExecTransient(conn *sqlite.Conn, query string, resultFn func(stmt *sqlite.Stmt) error, args ...interface{}) (err error) {
  38. return sqlitex.ExecTransient(conn.Conn, query, resultFn, args...)
  39. }
  40. type Pool struct {
  41. *sqlitex.Pool
  42. }
  43. func Open(uri string, flags sqlite.OpenFlags, poolSize int) (*Pool, error) {
  44. crawshawPool, err := sqlitex.Open(uri, flags, poolSize)
  45. return &Pool{crawshawPool}, err
  46. }
  47. func (me Pool) Get(ctx context.Context) *sqlite.Conn {
  48. conn := me.Pool.Get(ctx)
  49. if conn == nil {
  50. return nil
  51. }
  52. return &sqlite.Conn{conn}
  53. }
  54. func (me Pool) Put(conn *sqlite.Conn) {
  55. me.Pool.Put(conn.Conn)
  56. }