-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinsert_operation.go
More file actions
46 lines (37 loc) · 971 Bytes
/
insert_operation.go
File metadata and controls
46 lines (37 loc) · 971 Bytes
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
package dbunit
import (
"fmt"
"log"
"strings"
)
type InsertOperation struct {
dbFactory DatabaseFactory
dataSet DataSet
}
func (d *InsertOperation) Execute(fixtureName string) {
d.ExecuteWithFilter(extractFileName(fixtureName), fixtureName, NoFilter())
}
func (d *InsertOperation) ExecuteWith(tableName string, fixtureName string) {
d.ExecuteWithFilter(tableName, fixtureName, NoFilter())
}
func (d *InsertOperation) ExecuteWithFilter(tableName string, fixtureName string, filter Filter) {
records, err := d.dataSet.Load(fixtureName)
if err != nil {
log.Fatal(err)
}
commands := []Command{}
for _, r := range records {
if !filter(r) {
sql := fmt.Sprintf(
"INSERT INTO %s (%s) VALUES (%s)",
tableName,
strings.Join(r.Columns(), ","),
strings.Join(r.ColumnsByAlias(), ","),
)
commands = append(commands, Command{record: r, sql: sql, tableName: tableName})
}
}
if len(commands) > 0 {
d.dbFactory.Exec(commands)
}
}