Fork github.com/mattn/go-sqlite3 with adjustment for go1.16.2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
2.4 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/mattn/go-sqlite3"
  5. )
  6. type seriesModule struct{}
  7. func (m *seriesModule) EponymousOnlyModule() {}
  8. func (m *seriesModule) Create(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) {
  9. err := c.DeclareVTab(fmt.Sprintf(`
  10. CREATE TABLE %s (
  11. value INT,
  12. start HIDDEN,
  13. stop HIDDEN,
  14. step HIDDEN
  15. )`, args[0]))
  16. if err != nil {
  17. return nil, err
  18. }
  19. return &seriesTable{0, 0, 1}, nil
  20. }
  21. func (m *seriesModule) Connect(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) {
  22. return m.Create(c, args)
  23. }
  24. func (m *seriesModule) DestroyModule() {}
  25. type seriesTable struct {
  26. start int64
  27. stop int64
  28. step int64
  29. }
  30. func (v *seriesTable) Open() (sqlite3.VTabCursor, error) {
  31. return &seriesCursor{v, 0}, nil
  32. }
  33. func (v *seriesTable) BestIndex(csts []sqlite3.InfoConstraint, ob []sqlite3.InfoOrderBy) (*sqlite3.IndexResult, error) {
  34. used := make([]bool, len(csts))
  35. for c, cst := range csts {
  36. if cst.Usable && cst.Op == sqlite3.OpEQ {
  37. used[c] = true
  38. }
  39. }
  40. return &sqlite3.IndexResult{
  41. IdxNum: 0,
  42. IdxStr: "default",
  43. Used: used,
  44. }, nil
  45. }
  46. func (v *seriesTable) Disconnect() error { return nil }
  47. func (v *seriesTable) Destroy() error { return nil }
  48. type seriesCursor struct {
  49. *seriesTable
  50. value int64
  51. }
  52. func (vc *seriesCursor) Column(c *sqlite3.SQLiteContext, col int) error {
  53. switch col {
  54. case 0:
  55. c.ResultInt64(vc.value)
  56. case 1:
  57. c.ResultInt64(vc.seriesTable.start)
  58. case 2:
  59. c.ResultInt64(vc.seriesTable.stop)
  60. case 3:
  61. c.ResultInt64(vc.seriesTable.step)
  62. }
  63. return nil
  64. }
  65. func (vc *seriesCursor) Filter(idxNum int, idxStr string, vals []interface{}) error {
  66. switch {
  67. case len(vals) < 1:
  68. vc.seriesTable.start = 0
  69. vc.seriesTable.stop = 1000
  70. vc.value = vc.seriesTable.start
  71. case len(vals) < 2:
  72. vc.seriesTable.start = vals[0].(int64)
  73. vc.seriesTable.stop = 1000
  74. vc.value = vc.seriesTable.start
  75. case len(vals) < 3:
  76. vc.seriesTable.start = vals[0].(int64)
  77. vc.seriesTable.stop = vals[1].(int64)
  78. vc.value = vc.seriesTable.start
  79. case len(vals) < 4:
  80. vc.seriesTable.start = vals[0].(int64)
  81. vc.seriesTable.stop = vals[1].(int64)
  82. vc.seriesTable.step = vals[2].(int64)
  83. }
  84. return nil
  85. }
  86. func (vc *seriesCursor) Next() error {
  87. vc.value += vc.step
  88. return nil
  89. }
  90. func (vc *seriesCursor) EOF() bool {
  91. return vc.value > vc.stop
  92. }
  93. func (vc *seriesCursor) Rowid() (int64, error) {
  94. return int64(vc.value), nil
  95. }
  96. func (vc *seriesCursor) Close() error {
  97. return nil
  98. }