-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
168 lines (137 loc) · 3.13 KB
/
model.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
package model
import (
"fmt"
"git.ultraware.nl/NiseVoid/qb"
"git.ultraware.nl/NiseVoid/qb/qbdb"
"git.ultraware.nl/NiseVoid/qb/qf"
)
type Model interface {
GetTable() *qb.Table
Select(f ...any) *qb.SelectBuilder
Insert(f ...qb.Field) *qb.InsertBuilder
Update() *qb.UpdateBuilder
Delete(c1 qb.Condition, c2 ...qb.Condition) qb.Query
Field(f any) qb.Field
}
type model struct {
table qb.Table
fields map[string]qb.TableField
}
type Option = func(*model)
func New(table string, options ...Option) Model {
m := &model{
table: qb.Table{Name: table},
fields: make(map[string]qb.TableField),
}
for _, fn := range options {
fn(m)
}
return m
}
func Columns(s ...string) func(*model) {
return func(d *model) {
for _, n := range s {
d.fields[n] = qb.TableField{Parent: &d.table, Name: n}
}
}
}
func (d *model) GetTable() *qb.Table {
return &d.table
}
func (d *model) Select(a ...any) *qb.SelectBuilder {
var fields []qb.Field
for _, v := range a {
switch t := v.(type) {
case []string:
for _, s := range t {
fields = append(fields, d.Field(s))
}
default:
fields = append(fields, d.Field(v))
}
}
return d.table.Select(fields)
}
func (d *model) Insert(f ...qb.Field) *qb.InsertBuilder {
return d.table.Insert(f)
}
func (d *model) Update() *qb.UpdateBuilder {
return d.table.Update()
}
func (d *model) Delete(c1 qb.Condition, c2 ...qb.Condition) qb.Query {
return d.table.Delete(c1, c2...)
}
func (d *model) Field(v any) qb.Field {
switch f := v.(type) {
case qf.CalculatedField:
return f
case qb.Field:
return f
case string:
for k, v := range d.fields {
if k == f {
return &v
}
}
}
panic(fmt.Sprintf(`column %q does not exist on table %q`, v, d.table.Name))
}
func Scan(rows qbdb.Rows) []map[string]any {
cols, err := rows.Columns()
if err != nil {
panic(err)
}
cols = makeUnique(cols)
var result []map[string]any
for rows.Next() {
// Create a slice of any's to represent each column,
// and a second slice to contain pointers to each item in the columns slice.
columns := make([]any, len(cols))
columnPointers := make([]any, len(cols))
for i := range columns {
columnPointers[i] = &columns[i]
}
// Scan the result into the column pointers...
if err := rows.Scan(columnPointers...); err != nil {
panic(err)
}
// Create our map, and retrieve the value for each column from the pointers slice,
// storing it in the map with the name of the column as the key.
m := make(map[string]any)
for i, colName := range cols {
val := columnPointers[i].(*any)
m[colName] = *val
}
result = append(result, m)
}
return result
}
func makeUnique(columns []string) []string {
// this should be a driver responsibility...
for i, col := range columns {
if len(col) == 0 {
columns[i] = fmt.Sprintf(`Column%d`, i)
}
is := indexOf(col, columns)
if len(is) > 1 {
for k, v := range is {
if v == i {
if k == 0 {
continue
}
columns[i] = fmt.Sprintf(`%s_%d`, col, k)
}
}
}
}
return columns
}
func indexOf(s string, in []string) []int {
var is []int
for i, v := range in {
if s == v {
is = append(is, i)
}
}
return is
}