-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathproxy.go
131 lines (105 loc) · 2.77 KB
/
proxy.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
package y
import (
"database/sql"
"reflect"
sq "github.com/Masterminds/squirrel"
)
// Proxy contains a schema of a type
type Proxy struct {
v value
schema
}
// Put creates a new object
func (p *Proxy) Put(db sq.BaseRunner) (int64, error) {
return Put(db, p)
}
// Query returns Finder with a custom query
func (p *Proxy) Query(b sq.SelectBuilder) *Finder {
return makeFinder(p, b)
}
// Fetch returns a collection of objects
func (p *Proxy) Fetch(db sq.BaseRunner) (*Collection, error) {
return p.Find().Fetch(db)
}
// FindBy returns Finder with qualified query
func (p *Proxy) FindBy(q Qualifier) *Finder {
return p.Find().Qualify(q)
}
// Find returns Finder with qualified query
func (p *Proxy) Find() *Finder {
return p.Query(builder{p.schema}.forFinder())
}
// Collection creates a collection of proxy values
func (p *Proxy) Collection() *Collection {
c := p.blankCollection()
p.v.addTo(c)
return c
}
// Join adds related collection to self
func (p *Proxy) Join(db sq.BaseRunner, in *Collection) (*Collection, error) {
fk := p.schema.fk(in.schema)
idx := in.lookidx(fk.target)
c, err := p.findByEq(sq.Eq{fk.from: idx.keys}).Fetch(db)
if err != nil {
return nil, err
}
if !c.Empty() {
in.Join(c)
}
return c, nil
}
// Load fetches an object from db by primary key
func (p *Proxy) Load(db sq.BaseRunner) error {
return p.loadBy(db, p.primary())
}
// MustLoad fetches an object and panic if an error occurred
func (p *Proxy) MustLoad(db sq.BaseRunner) *Proxy {
err := p.Load(db)
if err != nil {
panic(err.Error())
}
return p
}
// Field returns reflected field by name
func (p *Proxy) Field(name string) reflect.Value {
return p.schema.fval(p.v, name)
}
// Map returns a simple map of struct values
func (p *Proxy) Map() Values {
return p.schema.mapped(p.v)
}
// Update saves changes of the object
func (p *Proxy) Update(db sq.BaseRunner, v Values) error {
return Update(db, p, v)
}
// Truncate erases all data
func (p *Proxy) Truncate(db sq.BaseRunner) error {
return Truncate(db, p)
}
// Delete removes a proxy by primary
func (p *Proxy) Delete(db sq.BaseRunner) (int64, error) {
return p.DeleteBy(db, p.primary())
}
// DeleteBy removes a proxy by values
func (p *Proxy) DeleteBy(db sq.BaseRunner, by Values) (int64, error) {
return DeleteBy(db, p, by)
}
func (p *Proxy) blankCollection() *Collection {
return makeCollection(p)
}
func (p *Proxy) findByEq(eq sq.Eq) *Finder {
return p.FindBy(ByEq(eq))
}
func (p *Proxy) primary() Values {
return p.schema.pk(p.v)
}
func (p *Proxy) version() *sql.NullInt64 {
return p.Field(_version).Addr().Interface().(*sql.NullInt64)
}
func (p *Proxy) loadBy(db sq.BaseRunner, eq Values) error {
return p.findByEq(sq.Eq(eq)).Load(db)
}
func proxyOf(v reflect.Value) *Proxy {
val := valueOf(v)
return &Proxy{val, schemaOf(val.typo())}
}