-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilter_backing.go
More file actions
77 lines (61 loc) · 1.62 KB
/
filter_backing.go
File metadata and controls
77 lines (61 loc) · 1.62 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
package duckql
import (
"github.com/rqlite/sql"
"reflect"
)
type SliceFilter struct {
exec *QueryExecutor
s *SQLizer
data []any
}
func (f *SliceFilter) Rows() ResultRows {
return f.exec.Rows()
}
func (f *SliceFilter) FillIntermediate(table *IntermediateTable) {
if table.Source == nil {
panic("cannot fill intermediate without a table")
}
for _, column := range table.Source.Columns {
table.Columns = append(table.Columns, column)
}
for _, d := range f.data {
if reflect.TypeOf(d).Kind() == reflect.Slice {
v := reflect.ValueOf(d)
if v.Len() == 0 {
continue
}
if f.s.TableForData(v.Index(0).Interface()) != table.Source {
continue
}
for i := 0; i < v.Len(); i++ {
var result ResultRow
for _, column := range table.Source.Columns {
result = append(result, ResultValue{Name: column, Value: v.Index(i).Elem().FieldByName(table.Source.ColumnMappings[column].GoField)})
}
table.Rows = append(table.Rows, result)
}
} else {
v := reflect.ValueOf(d)
var result ResultRow
for _, column := range table.Source.Columns {
result = append(result, ResultValue{Name: column, Value: v.Elem().FieldByName(table.Source.ColumnMappings[column].GoField)})
}
table.Rows = append(table.Rows, result)
}
}
}
func (f *SliceFilter) Visit(n sql.Node) (sql.Visitor, sql.Node, error) {
if f.exec == nil {
f.exec = NewQueryExecutor(f.s, f.FillIntermediate)
}
return f.exec.Visit(n)
}
func (f *SliceFilter) VisitEnd(n sql.Node) (sql.Node, error) {
return n, nil
}
func NewSliceFilter(s *SQLizer, data []any) *SliceFilter {
return &SliceFilter{
s: s,
data: data,
}
}