query.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package sqlitex
  2. import (
  3. "errors"
  4. "github.com/go-llsqlite/crawshaw"
  5. )
  6. var ErrNoResults = errors.New("sqlite: statement has no results")
  7. var ErrMultipleResults = errors.New("sqlite: statement has multiple result rows")
  8. func resultSetup(stmt *sqlite.Stmt) error {
  9. hasRow, err := stmt.Step()
  10. if err != nil {
  11. stmt.Reset()
  12. return err
  13. }
  14. if !hasRow {
  15. stmt.Reset()
  16. return ErrNoResults
  17. }
  18. return nil
  19. }
  20. func resultTeardown(stmt *sqlite.Stmt) error {
  21. hasRow, err := stmt.Step()
  22. if err != nil {
  23. stmt.Reset()
  24. return err
  25. }
  26. if hasRow {
  27. stmt.Reset()
  28. return ErrMultipleResults
  29. }
  30. return stmt.Reset()
  31. }
  32. // ResultInt steps the Stmt once and returns the first column as an int.
  33. //
  34. // If there are no rows in the result set, ErrNoResults is returned.
  35. //
  36. // If there are multiple rows, ErrMultipleResults is returned with the first
  37. // result.
  38. //
  39. // The Stmt is always Reset, so repeated calls will always return the first
  40. // result.
  41. func ResultInt(stmt *sqlite.Stmt) (int, error) {
  42. res, err := ResultInt64(stmt)
  43. return int(res), err
  44. }
  45. // ResultInt64 steps the Stmt once and returns the first column as an int64.
  46. //
  47. // If there are no rows in the result set, ErrNoResults is returned.
  48. //
  49. // If there are multiple rows, ErrMultipleResults is returned with the first
  50. // result.
  51. //
  52. // The Stmt is always Reset, so repeated calls will always return the first
  53. // result.
  54. func ResultInt64(stmt *sqlite.Stmt) (int64, error) {
  55. if err := resultSetup(stmt); err != nil {
  56. return 0, err
  57. }
  58. return stmt.ColumnInt64(0), resultTeardown(stmt)
  59. }
  60. // ResultText steps the Stmt once and returns the first column as a string.
  61. //
  62. // If there are no rows in the result set, ErrNoResults is returned.
  63. //
  64. // If there are multiple rows, ErrMultipleResults is returned with the first
  65. // result.
  66. //
  67. // The Stmt is always Reset, so repeated calls will always return the first
  68. // result.
  69. func ResultText(stmt *sqlite.Stmt) (string, error) {
  70. if err := resultSetup(stmt); err != nil {
  71. return "", err
  72. }
  73. return stmt.ColumnText(0), resultTeardown(stmt)
  74. }
  75. // ResultFloat steps the Stmt once and returns the first column as a float64.
  76. //
  77. // If there are no rows in the result set, ErrNoResults is returned.
  78. //
  79. // If there are multiple rows, ErrMultipleResults is returned with the first
  80. // result.
  81. //
  82. // The Stmt is always Reset, so repeated calls will always return the first
  83. // result.
  84. func ResultFloat(stmt *sqlite.Stmt) (float64, error) {
  85. if err := resultSetup(stmt); err != nil {
  86. return 0, err
  87. }
  88. return stmt.ColumnFloat(0), resultTeardown(stmt)
  89. }