-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdataquery.go
More file actions
133 lines (111 loc) · 2.75 KB
/
dataquery.go
File metadata and controls
133 lines (111 loc) · 2.75 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
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
package goquery
import (
"errors"
"fmt"
"reflect"
)
const selectkey = "select"
//const updatekey = "update"
//const insertkey = "insert"
type RowFunction func(r Rows) error
type Rows interface {
Columns() ([]string, error)
ColumnTypes() ([]reflect.Type, error)
Next() bool
Scan(dest ...interface{}) error
ScanStruct(dest interface{}) error
ToMap() (map[string]any, error)
Close() error
}
// converts the current Rows position to a map
func RowToMap(r Rows) (map[string]any, error) {
cols, err := r.Columns()
if err != nil {
return nil, err
}
colTypes, err := r.ColumnTypes()
if err != nil {
return nil, err
}
vals := make([]any, len(cols))
for i := range vals {
pval := reflect.New(colTypes[i])
ival := pval.Interface() //call Elem to dereference the pointer created by reflect.New
vals[i] = ival
}
err = r.Scan(vals...)
valmap := make(map[string]any)
//this is pretty gross, but it is significantly faster than reflection which is the fallback
for i, col := range cols {
val := vals[i]
var concreteVal any
switch v := val.(type) {
case *string:
concreteVal = *v
case *int64:
concreteVal = *v
case *int32:
concreteVal = *v
case *float64:
concreteVal = *v
case *float32:
concreteVal = *v
case *bool:
concreteVal = *v
default:
// Fallback to reflection ONLY for unknown types
concreteVal = reflect.Indirect(reflect.ValueOf(val)).Interface()
}
valmap[col] = concreteVal
}
return valmap, nil
}
type DataSet interface {
Entity() string
FieldSlice() interface{} //@depricated. Will be removed in the next version...maybe
Fields() interface{} //@depricated. Will be removed in the next version...maybe
Commands() map[string]string
PutCommand(key string, stmt string)
}
type Statements map[string]string
func (s Statements) Get(key string) (string, error) {
if val, ok := s[key]; ok {
return val, nil
}
return "", errors.New("Invalid statement")
}
func (s Statements) GetOrPanic(key string) string {
if val, ok := s[key]; ok {
return val
}
panic(errors.New("Invalid statement"))
}
type TableDataSet struct {
Name string
Schema string //optional
Statements Statements
TableFields any
}
func (t *TableDataSet) FieldSlice() interface{} {
typ := reflect.TypeOf(t.TableFields)
slice := reflect.New(reflect.SliceOf(typ))
return slice.Interface()
}
func (t *TableDataSet) Fields() interface{} {
return t.TableFields
}
func (t *TableDataSet) Entity() string {
if t.Schema != "" {
return fmt.Sprintf("%s.%s", t.Schema, t.Name)
}
return t.Name
}
func (t *TableDataSet) Commands() map[string]string {
return t.Statements
}
func (t *TableDataSet) PutCommand(key string, stmt string) {
if t.Statements == nil {
t.Statements = make(map[string]string)
}
t.Statements[key] = stmt
}