forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.go
97 lines (85 loc) · 2.6 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
package db
import sq "github.com/Masterminds/squirrel"
// Delete returns a new query builder configured to delete rows from the table.
//
func (tbl *Table) Delete(
pred interface{},
args ...interface{},
) *DeleteBuilder {
return &DeleteBuilder{
Table: tbl,
sql: sq.Delete(tbl.Name).Where(pred, args...),
}
}
// Get returns a new query builder configured to select into the provided
// `dest`.
//
// Get behaves the same was as Select, but automatically limits the query
// generated to a single value and only populates a single struct.
func (tbl *Table) Get(
dest interface{},
pred interface{},
args ...interface{},
) *GetBuilder {
cols := columnsForStruct(dest)
sql := sq.Select(cols...).From(tbl.Name).Where(pred, args...).Limit(1)
return &GetBuilder{
Table: tbl,
dest: dest,
sql: sql,
}
}
// Insert returns a new query builder configured to insert structs into the
// table.
//
// Insert takes one or more struct (or pointer to struct) values, each of which
// represents a single row to be created in the table. The first value provided
// in a call to this function will operate as the template for the insert and
// will determine what columns are populated in the query. For this reason, it
// is highly recommmended that you always use the same struct type for any
// single call this function.
//
// An InsertBuilder uses the "db" struct tag to determine the column names that
// a given struct should be mapped to, and by default the unmofdified name of
// the field will be used. Similar to other struct tags, the value "-" will
// cause the field to be skipped.
//
// NOTE: using the omitempty option, such as used with json struct tags, is not
// supported.
func (tbl *Table) Insert(rows ...interface{}) *InsertBuilder {
return &InsertBuilder{
Table: tbl,
sql: sq.Insert(tbl.Name),
rows: rows,
}
}
// Select returns a new query builder configured to select into the provided
// `dest`.
func (tbl *Table) Select(
dest interface{},
pred interface{},
args ...interface{},
) *SelectBuilder {
cols := columnsForStruct(dest)
sql := sq.Select(cols...).From(tbl.Name).Where(pred, args...)
return &SelectBuilder{
Table: tbl,
dest: dest,
sql: sql,
}
}
// Update returns a new query builder configured to update rows that match the
// predicate with the values of the provided source struct. See docs for
// `UpdateBuildeExec` for more documentation.
func (tbl *Table) Update(
source interface{},
pred interface{},
args ...interface{},
) *UpdateBuilder {
sql := sq.Update(tbl.Name).Where(pred, args...)
return &UpdateBuilder{
Table: tbl,
source: source,
sql: sql,
}
}