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.

116 lines
2.3 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "github.com/mattn/go-sqlite3"
  8. )
  9. type githubRepo struct {
  10. ID int `json:"id"`
  11. FullName string `json:"full_name"`
  12. Description string `json:"description"`
  13. HTMLURL string `json:"html_url"`
  14. }
  15. type githubModule struct {
  16. }
  17. func (m *githubModule) Create(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) {
  18. err := c.DeclareVTab(fmt.Sprintf(`
  19. CREATE TABLE %s (
  20. id INT,
  21. full_name TEXT,
  22. description TEXT,
  23. html_url TEXT
  24. )`, args[0]))
  25. if err != nil {
  26. return nil, err
  27. }
  28. return &ghRepoTable{}, nil
  29. }
  30. func (m *githubModule) Connect(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) {
  31. return m.Create(c, args)
  32. }
  33. func (m *githubModule) DestroyModule() {}
  34. type ghRepoTable struct {
  35. repos []githubRepo
  36. }
  37. func (v *ghRepoTable) Open() (sqlite3.VTabCursor, error) {
  38. resp, err := http.Get("https://api.github.com/repositories")
  39. if err != nil {
  40. return nil, err
  41. }
  42. defer resp.Body.Close()
  43. body, err := ioutil.ReadAll(resp.Body)
  44. if err != nil {
  45. return nil, err
  46. }
  47. var repos []githubRepo
  48. if err := json.Unmarshal(body, &repos); err != nil {
  49. return nil, err
  50. }
  51. return &ghRepoCursor{0, repos}, nil
  52. }
  53. func (v *ghRepoTable) BestIndex(cst []sqlite3.InfoConstraint, ob []sqlite3.InfoOrderBy) (*sqlite3.IndexResult, error) {
  54. used := make([]bool, len(csts))
  55. return &sqlite3.IndexResult{
  56. IdxNum: 0,
  57. IdxStr: "default",
  58. Used: used,
  59. }, nil
  60. }
  61. func (v *ghRepoTable) Disconnect() error { return nil }
  62. func (v *ghRepoTable) Destroy() error { return nil }
  63. type ghRepoCursor struct {
  64. index int
  65. repos []githubRepo
  66. }
  67. func (vc *ghRepoCursor) Column(c *sqlite3.SQLiteContext, col int) error {
  68. switch col {
  69. case 0:
  70. c.ResultInt(vc.repos[vc.index].ID)
  71. case 1:
  72. c.ResultText(vc.repos[vc.index].FullName)
  73. case 2:
  74. c.ResultText(vc.repos[vc.index].Description)
  75. case 3:
  76. c.ResultText(vc.repos[vc.index].HTMLURL)
  77. }
  78. return nil
  79. }
  80. func (vc *ghRepoCursor) Filter(idxNum int, idxStr string, vals []interface{}) error {
  81. vc.index = 0
  82. return nil
  83. }
  84. func (vc *ghRepoCursor) Next() error {
  85. vc.index++
  86. return nil
  87. }
  88. func (vc *ghRepoCursor) EOF() bool {
  89. return vc.index >= len(vc.repos)
  90. }
  91. func (vc *ghRepoCursor) Rowid() (int64, error) {
  92. return int64(vc.index), nil
  93. }
  94. func (vc *ghRepoCursor) Close() error {
  95. return nil
  96. }