-
Notifications
You must be signed in to change notification settings - Fork 3
/
insert.go
88 lines (72 loc) · 2.16 KB
/
insert.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
package sqlbuilder
import "strings"
// Insert returns a new INSERT statement with the default dialect.
func Insert() InsertStatement {
return InsertStatement{dialect: DefaultDialect}
}
type insertSet struct {
col string
arg interface{}
raw bool
}
type insertRet struct {
sql string
dest interface{}
}
// InsertStatement represents an INSERT statement.
type InsertStatement struct {
dialect Dialect
table string
sets []insertSet
rets []insertRet
}
// Dialect returns a new statement with dialect set to 'dialect'.
func (s InsertStatement) Dialect(dialect Dialect) InsertStatement {
s.dialect = dialect
return s
}
// Into returns a new statement with the table to insert into set to 'table'.
func (s InsertStatement) Into(table string) InsertStatement {
s.table = table
return s
}
// Set returns a new statement with column 'col' set to value 'val'.
func (s InsertStatement) Set(col string, val interface{}) InsertStatement {
s.sets = append(s.sets, insertSet{col, val, false})
return s
}
// SetSQL returns a new statement with column 'col' set to the raw SQL expression 'sql'.
func (s InsertStatement) SetSQL(col, sql string) InsertStatement {
s.sets = append(s.sets, insertSet{col, sql, true})
return s
}
// Return returns a new statement with a RETURNING clause.
func (s InsertStatement) Return(col string, dest interface{}) InsertStatement {
s.rets = append(s.rets, insertRet{sql: col, dest: dest})
return s
}
// Build builds the SQL query. It returns the SQL query and the argument slice.
func (s InsertStatement) Build() (query string, args []interface{}, dest []interface{}) {
var cols, vals []string
idx := 0
for _, set := range s.sets {
cols = append(cols, set.col)
if set.raw {
vals = append(vals, set.arg.(string))
} else {
args = append(args, set.arg)
vals = append(vals, s.dialect.Placeholder(idx))
idx++
}
}
query = "INSERT INTO " + s.table + " (" + strings.Join(cols, ", ") + ") VALUES (" + strings.Join(vals, ", ") + ")"
if len(s.rets) > 0 {
var args []string
for _, ret := range s.rets {
args = append(args, ret.sql)
dest = append(dest, ret.dest)
}
query += " RETURNING " + strings.Join(args, ", ")
}
return
}