-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_builder.go
77 lines (66 loc) · 2.27 KB
/
insert_builder.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
package query
//InsertBuilder is a builder for INSERT statements
type InsertBuilder struct {
query string
}
//NewInsertBuilder returns a new *InsertBuilder
func NewInsertBuilder() *InsertBuilder {
return new(InsertBuilder)
}
//Insert adds an INSERT statement to the builders'squery
func (i *InsertBuilder) Insert(table string) *InsertBuilder {
i.query = "INSERT INTO " + table
return i
}
//Fields adds the fields to be inserted to the builder's query
func (i *InsertBuilder) Fields(fields ...string) *InsertBuilder {
i.query += addFields("", true, fields...)
return i
}
//ValuesFromMap adds multiple value groups derived from ixToValues to the builder'query
//Any value for a string colmun should be wrapped in single quotes.
//Usage example:
// ValuesFromMap(map[int]string{
// 0: "'Mrs'",
// 1: "'Susan'",
// 2: "'Jerome'",
// 3: "'+2319057573110'"
// })
//
// Note that string values in ixToValues beginning with '(' won't be quoted
// by this method,as they will be assumed to be subqueries.
func (i *InsertBuilder) ValuesFromMap(ixToValues map[int]interface{}) *InsertBuilder {
i.query += " VALUES(" + values(ixToValues) + ")"
return i
}
//Values adds a set of values for each corresponding column to the builder's query.
//Any value for a string colmun should be wrapped in single quotes.
func (i *InsertBuilder) Values(values ...string) *InsertBuilder {
i.query += addFields("VALUES", true, values...)
return i
}
// ValuesSet adds another value set without adding the VALUES keyword
//
// Note that string values in ixToValues beginning with '(' won't be quoted
// by this method, as they will be assumed to be subqueries.
func (i *InsertBuilder) ValuesSet(ixToValues map[int]interface{}) *InsertBuilder {
i.query += ",(" + values(ixToValues) + ")"
return i
}
//Returning selects fields from the temporary inserted table
func (i *InsertBuilder) Returning(fields ...string) *InsertBuilder {
i.query += " RETURNING" + addFields("", false, fields...)
return i
}
//ReturningAll selects all fields from the temporary inserted table
func (i *InsertBuilder) ReturningAll() *InsertBuilder {
i.query += " RETURNING *"
return i
}
//Clear erases the builder's query
func (i *InsertBuilder) Clear() {
i.query = ""
}
func (i *InsertBuilder) String() string {
return i.query + ";"
}