forked from slicebit/qb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert.go
63 lines (56 loc) · 1.75 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
package qb
import (
"fmt"
"strings"
)
// Insert generates an insert statement and returns it
// Insert(usersTable).Values(map[string]interface{}{"id": 1})
func Insert(table TableElem) InsertStmt {
return InsertStmt{
table: table,
values: map[string]interface{}{},
returning: []ColumnElem{},
}
}
// InsertStmt is the base struct for any insert statements
type InsertStmt struct {
table TableElem
values map[string]interface{}
returning []ColumnElem
}
// Values accepts map[string]interface{} and forms the values map of insert statement
func (s InsertStmt) Values(vals map[string]interface{}) InsertStmt {
for k, v := range vals {
s.values[k] = v
}
return s
}
// Returning accepts the column names as strings and forms the returning array of insert statement
// NOTE: Please use it in only postgres dialect, otherwise it'll crash
func (s InsertStmt) Returning(cols ...string) InsertStmt {
for _, c := range cols {
s.returning = append(s.returning, s.table.C(c))
}
return s
}
// Build generates a statement out of InsertStmt object
func (s InsertStmt) Build(dialect Dialect) *Stmt {
statement := Statement()
colNames := []string{}
values := []string{}
for k, v := range s.values {
colNames = append(colNames, dialect.Escape(k))
statement.AddBinding(v)
values = append(values, dialect.Placeholder())
}
statement.AddClause(fmt.Sprintf("INSERT INTO %s(%s)", dialect.Escape(s.table.Name), strings.Join(colNames, ", ")))
statement.AddClause(fmt.Sprintf("VALUES(%s)", strings.Join(values, ", ")))
returning := []string{}
for _, r := range s.returning {
returning = append(returning, dialect.Escape(r.Name))
}
if len(s.returning) > 0 {
statement.AddClause(fmt.Sprintf("RETURNING %s", strings.Join(returning, ", ")))
}
return statement
}