Debian Codegraph @

_example/vtable/vtable.go

raw
1 package main
2
3 import (
4 "encoding/json"
5 "fmt"
6 "io/ioutil"
7 "net/http"
8 "strings"
9
10 "github.com/mattn/go-sqlite3"
11 )
12
13 type githubRepo struct {
14 ID int `json:"id"`
15 FullName string `json:"full_name"`
16 Description string `json:"description"`
17 HTMLURL string `json:"html_url"`
18 }
19
20 type githubModule struct {
21 }
22
23 func (m *githubModule) Create(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) {
24 // args holds the raw xCreate arguments: args[0] is the module name,
25 // args[1] the database name, args[2] the virtual table name, and any
26 // user arguments follow.
27 if len(args) < 3 {
28 return nil, fmt.Errorf("githubModule: unexpected arguments: %v", args)
29 }
30 tableName := `"` + strings.ReplaceAll(args[2], `"`, `""`) + `"`
31 err := c.DeclareVTab(fmt.Sprintf(`
32 CREATE TABLE %s (
33 id INT,
34 full_name TEXT,
35 description TEXT,
36 html_url TEXT
37 )`, tableName))
38 if err != nil {
39 return nil, err
40 }
41 return &ghRepoTable{}, nil
42 }
43
44 func (m *githubModule) Connect(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) {
45 return m.Create(c, args)
46 }
47
48 func (m *githubModule) DestroyModule() {}
49
50 type ghRepoTable struct {
51 repos []githubRepo
52 }
53
54 func (v *ghRepoTable) Open() (sqlite3.VTabCursor, error) {
55 resp, err := http.Get("https://api.github.com/repositories")
56 if err != nil {
57 return nil, err
58 }
59 defer resp.Body.Close()
60 if resp.StatusCode != http.StatusOK {
61 return nil, fmt.Errorf("fetching repositories: %s", resp.Status)
62 }
63
64 body, err := ioutil.ReadAll(resp.Body)
65 if err != nil {
66 return nil, err
67 }
68
69 var repos []githubRepo
70 if err := json.Unmarshal(body, &repos); err != nil {
71 return nil, err
72 }
73 return &ghRepoCursor{0, repos}, nil
74 }
75
76 func (v *ghRepoTable) BestIndex(csts []sqlite3.InfoConstraint, ob []sqlite3.InfoOrderBy) (*sqlite3.IndexResult, error) {
77 used := make([]bool, len(csts))
78 return &sqlite3.IndexResult{
79 IdxNum: 0,
80 IdxStr: "default",
81 Used: used,
82 }, nil
83 }
84
85 func (v *ghRepoTable) Disconnect() error { return nil }
86 func (v *ghRepoTable) Destroy() error { return nil }
87
88 type ghRepoCursor struct {
89 index int
90 repos []githubRepo
91 }
92
93 func (vc *ghRepoCursor) Column(c *sqlite3.SQLiteContext, col int) error {
94 switch col {
95 case 0:
96 c.ResultInt(vc.repos[vc.index].ID)
97 case 1:
98 c.ResultText(vc.repos[vc.index].FullName)
99 case 2:
100 c.ResultText(vc.repos[vc.index].Description)
101 case 3:
102 c.ResultText(vc.repos[vc.index].HTMLURL)
103 }
104 return nil
105 }
106
107 func (vc *ghRepoCursor) Filter(idxNum int, idxStr string, vals []any) error {
108 vc.index = 0
109 return nil
110 }
111
112 func (vc *ghRepoCursor) Next() error {
113 vc.index++
114 return nil
115 }
116
117 func (vc *ghRepoCursor) EOF() bool {
118 return vc.index >= len(vc.repos)
119 }
120
121 func (vc *ghRepoCursor) Rowid() (int64, error) {
122 return int64(vc.index), nil
123 }
124
125 func (vc *ghRepoCursor) Close() error {
126 return nil
127 }