forked from asg017/sqlite-html
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta.go
196 lines (175 loc) · 4.96 KB
/
meta.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package meta
import (
"fmt"
"io"
"runtime"
"strings"
"github.com/asg017/sqlite-html/attrs"
"github.com/asg017/sqlite-html/elements"
"github.com/asg017/sqlite-html/internal"
"github.com/asg017/sqlite-html/query"
"github.com/asg017/sqlite-html/utils"
"github.com/augmentable-dev/vtab"
"go.riyazali.net/sqlite"
)
var allFuncs = [][]internal.Function{
attrs.Funcs,
elements.Funcs,
query.Funcs,
utils.Funcs,
}
var allModules = [][]internal.Module{
attrs.Modules,
elements.Modules,
query.Modules,
// Can't document itself yet :(
//Modules,
}
// html_version()
// return the version string of the current sqlite-html module.
// passed in from top-level Makefile and build args
type HtmlVersionFunc struct {
version string
}
func (*HtmlVersionFunc) Deterministic() bool { return true }
func (*HtmlVersionFunc) Args() int { return 0 }
func (f *HtmlVersionFunc) Apply(c *sqlite.Context, values ...sqlite.Value) {
c.ResultText(f.version)
}
// html_debug()
// Returns more information for the current html module, including build date + comment hash.
type HtmlDebugFunc struct {
version string
commit string
date string
}
func (*HtmlDebugFunc) Deterministic() bool { return true }
func (*HtmlDebugFunc) Args() int { return 0 }
func (f *HtmlDebugFunc) Apply(c *sqlite.Context, values ...sqlite.Value) {
c.ResultText(fmt.Sprintf("Version: %s\nCommit: %s\nRuntime: %s %s/%s\nDate: %s\n",
f.version,
f.commit,
runtime.Version(),
runtime.GOOS,
runtime.GOARCH,
f.date,
))
}
// bro idk if this works
var HtmlDocsColumns = []vtab.Column{
{Name: "_name", Type: sqlite.SQLITE_TEXT.String(), NotNull: true, Hidden: true, Filters: []*vtab.ColumnFilter{{Op: sqlite.INDEX_CONSTRAINT_EQ, Required: false, OmitCheck: true}}},
{Name: "_type", Type: sqlite.SQLITE_TEXT.String(), NotNull: true, Hidden: true, Filters: []*vtab.ColumnFilter{{Op: sqlite.INDEX_CONSTRAINT_EQ, Required: false, OmitCheck: true}}},
{Name: "source", Type: sqlite.SQLITE_TEXT.String()},
{Name: "type", Type: sqlite.SQLITE_TEXT.String()},
{Name: "name", Type: sqlite.SQLITE_TEXT.String()},
{Name: "column", Type: sqlite.SQLITE_TEXT.String()},
{Name: "tag", Type: sqlite.SQLITE_TEXT.String()},
{Name: "value", Type: sqlite.SQLITE_TEXT.String()},
}
type HtmlDocsRow struct {
rType string
name string
column string
tag string
value string
}
// html_docs(name, [type])
type HtmlDocsCursor struct {
current int
rows []*HtmlDocsRow
version string
}
func (cur *HtmlDocsCursor) Column(ctx *sqlite.Context, c int) error {
col := HtmlDocsColumns[c].Name
switch col {
case "source":
ctx.ResultText(fmt.Sprintf("sqlite-html http_docs() %s", cur.version))
case "type":
ctx.ResultText(cur.rows[cur.current].rType)
case "name":
ctx.ResultText(cur.rows[cur.current].name)
case "column":
ctx.ResultText(cur.rows[cur.current].column)
case "tag":
ctx.ResultText(cur.rows[cur.current].tag)
case "value":
ctx.ResultText(cur.rows[cur.current].value)
}
return nil
}
func (cur *HtmlDocsCursor) Next() (vtab.Row, error) {
cur.current += 1
if cur.current >= len(cur.rows) {
return nil, io.EOF
}
return cur, nil
}
func HtmlDocsBuildIterator(params RegisterParams) func(constraints []*vtab.Constraint, order []*sqlite.OrderBy) (vtab.Iterator, error) {
return func(constraints []*vtab.Constraint, order []*sqlite.OrderBy) (vtab.Iterator, error) {
argName := ""
argType := "table"
for _, constraint := range constraints {
if constraint.Op == sqlite.INDEX_CONSTRAINT_EQ {
colName := HtmlDocsColumns[constraint.ColIndex].Name
switch colName {
case "_name":
argName = constraint.Value.Text()
case "_type":
argType = constraint.Value.Text()
}
}
}
rows := []*HtmlDocsRow{}
if argType == "" || strings.ToLower(argType) == "table" {
for _, modules := range allModules {
for _, m := range modules {
if argName == "" || m.Match(argName) {
for tag, value := range m.Documentation {
rows = append(rows, &HtmlDocsRow{
rType: "function",
name: m.Name,
column: "",
tag: tag,
value: value,
})
}
for column, docs := range m.ColumnDocumentation {
for tag, value := range docs {
rows = append(rows, &HtmlDocsRow{
rType: "table",
name: m.Name,
column: column,
tag: tag,
value: value,
})
}
}
}
}
}
} else if argType == "" || strings.ToLower(argType) == "function" {
for _, funcs := range allFuncs {
for _, f := range funcs {
if argName == "" || f.Match(argName) {
for tag, value := range f.Documentation {
rows = append(rows, &HtmlDocsRow{
rType: "function",
name: f.Name,
column: "",
tag: tag,
value: value,
})
}
}
}
}
} else {
return nil, (fmt.Errorf("not a valid type parameter: '%s'", argType))
}
return &HtmlDocsCursor{
current: -1,
rows: rows,
version: params.Version,
}, nil
}
}