-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpostgres.go
105 lines (85 loc) · 2.33 KB
/
postgres.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
98
99
100
101
102
103
104
105
package dbunit
import (
"database/sql"
"fmt"
"log"
"strings"
"github.com/jmoiron/sqlx"
"github.com/vulcand/predicate"
)
type PostgresDatabaseFactory struct {
db *sqlx.DB
opts options
}
type dbPredicate func(tableName string, column string, db *PostgresDatabaseFactory) int64
func nextSequenceValue(val int) dbPredicate {
return func(tableName string, column string, db *PostgresDatabaseFactory) int64 {
for i := 0; i <= val; i++ {
db.nextSequenceVal(tableName, column)
}
return int64(val)
}
}
func (p *PostgresDatabaseFactory) Exec(cmds []Command) {
for _, c := range cmds {
values := map[string]interface{}{}
for column, val := range c.record.Values() {
if expression, ok := val.(string); ok {
if strings.HasPrefix(expression, "FUNC=") {
values[column] = p.parseValue(c.tableName, column, expression)
} else {
values[column] = val
}
} else {
values[column] = val
}
}
if p.opts.sql {
fmt.Println("[DBUnit] Debug SQL")
fmt.Println("---")
fmt.Println(c.sql)
if len(values) != 0 {
args := []string{}
for k, v := range values {
value := strings.ReplaceAll(fmt.Sprintf("%v", v), "RAW=", "")
args = append(args, fmt.Sprintf("%v=%v", k, value))
}
fmt.Println(strings.Join(args, ", "))
}
fmt.Println("---")
fmt.Println()
}
if _, err := p.db.NamedExec(c.sql, values); err != nil {
log.Println("file:", c.record.fileName, " error:", err, " sql:", c.sql, " values:", c.record.values)
}
}
}
func (p *PostgresDatabaseFactory) Close() error {
return p.db.Close()
}
func (p *PostgresDatabaseFactory) DB() *sql.DB {
return p.db.DB
}
func (p *PostgresDatabaseFactory) nextSequenceVal(tableName, column string) {
_, err := p.db.Exec(fmt.Sprintf("select nextval(pg_get_serial_sequence('%s', '%s'))", tableName, column))
if err != nil {
log.Println(err)
}
}
func (p *PostgresDatabaseFactory) parseValue(tableName, column, expression string) interface{} {
parser, err := predicate.NewParser(predicate.Def{
Functions: map[string]interface{}{
"NextValue": nextSequenceValue,
},
})
if err != nil {
log.Println(err)
return nil
}
pr, err := parser.Parse(strings.TrimPrefix(expression, "FUNC="))
if err != nil {
log.Println(fmt.Sprintf("Could not parse %s, %v", expression, err))
return nil
}
return pr.(dbPredicate)(tableName, column, p)
}