-
Notifications
You must be signed in to change notification settings - Fork 335
/
table.go
167 lines (131 loc) · 3.65 KB
/
table.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
package drivers
import (
"fmt"
"github.com/cortezaproject/corteza/server/pkg/dal"
"github.com/doug-martin/goqu/v9/exp"
)
type (
// TableCodec is an RDBMS representation of data.Model structure and its arguments
TableCodec interface {
Columns() []Column
Ident() exp.IdentifierExpression
MakeScanBuffer() []any
Encode(r dal.ValueGetter) (_ []any, err error)
Decode(buf []any, r dal.ValueSetter) (err error)
AttributeExpression(string) (exp.Expression, error)
}
// GenericTableCodec is a generic implementation of TableCodec
GenericTableCodec struct {
// table identifier (name)
ident exp.IdentifierExpression
// all columns we're selecting from when
// we're selecting from all columns
columns []Column
model *dal.Model
dialect Dialect
}
)
var (
_ TableCodec = &GenericTableCodec{}
)
func NewTableCodec(m *dal.Model, d Dialect) *GenericTableCodec {
gtc := &GenericTableCodec{
dialect: d,
model: m,
ident: exp.NewIdentifierExpression("", m.Ident, ""),
}
var (
colIdent string
attr *dal.Attribute
done = make(map[string]bool)
cols = make([]Column, 0, len(m.Attributes))
)
for a := range m.Attributes {
attr = m.Attributes[a]
colIdent = attr.StoreIdent()
switch attr.Store.(type) {
case *dal.CodecRecordValueSetJSON:
if done[colIdent] {
continue
}
// when dealing with encoded types there is probably
// a different column that can handle the encoded payload
cols = append(cols, &SimpleJsonDocColumn{
name: colIdent,
attributes: collectStdRecordValueJSONColumns(colIdent, m.Attributes...),
})
default:
cols = append(cols, NewSingleValueColumn(d, attr))
}
done[colIdent] = true
}
gtc.columns = cols
return gtc
}
func (t *GenericTableCodec) Ident() exp.IdentifierExpression {
return t.ident
}
func (t *GenericTableCodec) Columns() []Column {
return t.columns
}
func (t *GenericTableCodec) MakeScanBuffer() []any {
out := make([]any, len(t.columns))
for c := range t.columns {
out[c] = t.columns[c].Type().MakeScanBuffer()
}
return out
}
func (t *GenericTableCodec) Encode(r dal.ValueGetter) (_ []any, err error) {
enc := make([]any, len(t.columns))
for c := range t.columns {
enc[c], err = t.columns[c].Encode(r)
if err != nil {
return
}
}
return enc, nil
}
func (t *GenericTableCodec) Decode(buf []any, r dal.ValueSetter) (err error) {
if len(buf) != len(t.columns) {
return fmt.Errorf("columns incompatilbe with scan buffer")
}
for i, c := range t.columns {
if err = c.Decode(buf[i], r); err != nil {
return err
}
}
return
}
func (t *GenericTableCodec) AttributeExpression(ident string) (exp.Expression, error) {
attr := t.model.Attributes.FindByIdent(ident)
if attr == nil {
return nil, fmt.Errorf("unknown attribute %q", ident)
}
switch s := attr.Store.(type) {
case *dal.CodecAlias:
// using column directly
return exp.NewLiteralExpression("?", exp.NewIdentifierExpression("", t.model.Ident, s.Ident)), nil
case *dal.CodecRecordValueSetJSON:
// using JSON to handle embedded values
lit, err := t.dialect.JsonExtractUnquote(exp.NewIdentifierExpression("", t.model.Ident, s.Ident), attr.Ident, 0)
if err != nil {
return nil, err
}
return t.dialect.AttributeCast(attr, lit)
}
return exp.NewLiteralExpression("?", exp.NewIdentifierExpression("", t.model.Ident, ident)), nil
}
func collectStdRecordValueJSONColumns(ident string, aa ...*dal.Attribute) []*dal.Attribute {
filtered := make([]*dal.Attribute, 0)
for _, a := range aa {
storeType, is := a.Store.(*dal.CodecRecordValueSetJSON)
if !is {
continue
}
if ident != storeType.Ident {
continue
}
filtered = append(filtered, a)
}
return filtered
}