-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
105 lines (90 loc) · 2.99 KB
/
types.go
File metadata and controls
105 lines (90 loc) · 2.99 KB
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
package d1http
type ResponseInfo struct {
Code int `json:"code"`
Message string `json:"message"`
DocumentationURL string `json:"documentation_url,omitempty"`
Source *ResponseSource `json:"source,omitempty"`
}
type ResponseSource struct {
Pointer string `json:"pointer,omitempty"`
}
type ResultInfo struct {
Count int `json:"count,omitempty"`
Page int `json:"page,omitempty"`
PerPage int `json:"per_page,omitempty"`
TotalCount int `json:"total_count,omitempty"`
}
type envelope[T any] struct {
Success bool `json:"success"`
Errors []ResponseInfo `json:"errors"`
Messages []ResponseInfo `json:"messages"`
Result T `json:"result"`
ResultInfo *ResultInfo `json:"result_info,omitempty"`
}
type Page[T any] struct {
Items T
ResultInfo *ResultInfo
Messages []ResponseInfo
}
type QueryMeta struct {
ChangedDB bool `json:"changed_db,omitempty"`
Changes float64 `json:"changes,omitempty"`
Duration float64 `json:"duration,omitempty"`
LastRowID float64 `json:"last_row_id,omitempty"`
RowsRead float64 `json:"rows_read,omitempty"`
RowsWritten float64 `json:"rows_written,omitempty"`
ServedBy string `json:"served_by,omitempty"`
ServedByColo string `json:"served_by_colo,omitempty"`
ServedByPrimary bool `json:"served_by_primary,omitempty"`
ServedByRegion string `json:"served_by_region,omitempty"`
SizeAfter float64 `json:"size_after,omitempty"`
Timings QueryTimings `json:"timings,omitempty"`
}
type QueryTimings struct {
SQLDurationMS float64 `json:"sql_duration_ms,omitempty"`
}
func (m QueryMeta) RowsReadInt64() int64 { return int64(m.RowsRead) }
func (m QueryMeta) RowsWrittenInt64() int64 { return int64(m.RowsWritten) }
func (m QueryMeta) ChangesInt64() int64 { return int64(m.Changes) }
func (m QueryMeta) LastRowIDInt64() int64 { return int64(m.LastRowID) }
type Statement struct {
SQL string `json:"sql"`
Params []any `json:"params,omitempty"`
}
type rawRequest struct {
SQL string `json:"sql"`
Params []any `json:"params,omitempty"`
}
type RawResult struct {
Meta QueryMeta `json:"meta"`
Results RawRows `json:"results"`
Success bool `json:"success"`
}
type RawRows struct {
Columns []string `json:"columns"`
Rows [][]any `json:"rows"`
}
func (r RawResult) First() []any {
if len(r.Results.Rows) == 0 {
return nil
}
return r.Results.Rows[0]
}
func (r RawResult) Maps() []map[string]any {
out := make([]map[string]any, 0, len(r.Results.Rows))
for _, row := range r.Results.Rows {
m := make(map[string]any, len(r.Results.Columns))
for i, col := range r.Results.Columns {
if i < len(row) {
m[col] = row[i]
}
}
out = append(out, m)
}
return out
}
type QueryResult struct {
Meta QueryMeta `json:"meta"`
Results []map[string]any `json:"results"`
Success bool `json:"success"`
}