2016-12-11 20:49:48 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2017-03-05 14:48:17 +03:00
|
|
|
|
|
|
|
"github.com/mattn/go-sqlite3"
|
2016-12-11 20:49:48 +03:00
|
|
|
)
|
|
|
|
|
2017-03-05 14:52:55 +03:00
|
|
|
type githubRepo struct {
|
2016-12-11 20:49:48 +03:00
|
|
|
ID int `json:"id"`
|
|
|
|
FullName string `json:"full_name"`
|
|
|
|
Description string `json:"description"`
|
2017-03-05 14:52:55 +03:00
|
|
|
HTMLURL string `json:"html_url"`
|
2016-12-11 20:49:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type githubModule struct {
|
|
|
|
}
|
|
|
|
|
2017-03-05 14:49:45 +03:00
|
|
|
func (m *githubModule) Create(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) {
|
2016-12-11 20:49:48 +03:00
|
|
|
err := c.DeclareVTab(fmt.Sprintf(`
|
|
|
|
CREATE TABLE %s (
|
|
|
|
id INT,
|
|
|
|
full_name TEXT,
|
|
|
|
description TEXT,
|
|
|
|
html_url TEXT
|
|
|
|
)`, args[0]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ghRepoTable{}, nil
|
|
|
|
}
|
|
|
|
|
2017-03-05 14:49:45 +03:00
|
|
|
func (m *githubModule) Connect(c *sqlite3.SQLiteConn, args []string) (sqlite3.VTab, error) {
|
2016-12-11 20:49:48 +03:00
|
|
|
return m.Create(c, args)
|
|
|
|
}
|
|
|
|
|
2017-03-05 14:49:45 +03:00
|
|
|
func (m *githubModule) DestroyModule() {}
|
2016-12-11 20:49:48 +03:00
|
|
|
|
|
|
|
type ghRepoTable struct {
|
2017-03-05 14:52:55 +03:00
|
|
|
repos []githubRepo
|
2016-12-11 20:49:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *ghRepoTable) Open() (sqlite3.VTabCursor, error) {
|
|
|
|
resp, err := http.Get("https://api.github.com/repositories")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-05 14:52:55 +03:00
|
|
|
var repos []githubRepo
|
2016-12-11 20:49:48 +03:00
|
|
|
if err := json.Unmarshal(body, &repos); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ghRepoCursor{0, repos}, nil
|
|
|
|
}
|
|
|
|
|
2023-04-06 05:46:26 +03:00
|
|
|
func (v *ghRepoTable) BestIndex(csts []sqlite3.InfoConstraint, ob []sqlite3.InfoOrderBy) (*sqlite3.IndexResult, error) {
|
2022-10-19 02:04:52 +03:00
|
|
|
used := make([]bool, len(csts))
|
|
|
|
return &sqlite3.IndexResult{
|
|
|
|
IdxNum: 0,
|
|
|
|
IdxStr: "default",
|
|
|
|
Used: used,
|
|
|
|
}, nil
|
2016-12-11 20:49:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *ghRepoTable) Disconnect() error { return nil }
|
|
|
|
func (v *ghRepoTable) Destroy() error { return nil }
|
|
|
|
|
|
|
|
type ghRepoCursor struct {
|
|
|
|
index int
|
2017-03-05 14:52:55 +03:00
|
|
|
repos []githubRepo
|
2016-12-11 20:49:48 +03:00
|
|
|
}
|
|
|
|
|
2017-03-05 02:15:00 +03:00
|
|
|
func (vc *ghRepoCursor) Column(c *sqlite3.SQLiteContext, col int) error {
|
2016-12-11 20:49:48 +03:00
|
|
|
switch col {
|
|
|
|
case 0:
|
|
|
|
c.ResultInt(vc.repos[vc.index].ID)
|
|
|
|
case 1:
|
|
|
|
c.ResultText(vc.repos[vc.index].FullName)
|
|
|
|
case 2:
|
|
|
|
c.ResultText(vc.repos[vc.index].Description)
|
|
|
|
case 3:
|
2017-03-05 14:52:55 +03:00
|
|
|
c.ResultText(vc.repos[vc.index].HTMLURL)
|
2016-12-11 20:49:48 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vc *ghRepoCursor) Filter(idxNum int, idxStr string, vals []interface{}) error {
|
|
|
|
vc.index = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vc *ghRepoCursor) Next() error {
|
|
|
|
vc.index++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vc *ghRepoCursor) EOF() bool {
|
|
|
|
return vc.index >= len(vc.repos)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vc *ghRepoCursor) Rowid() (int64, error) {
|
|
|
|
return int64(vc.index), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vc *ghRepoCursor) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|