-
-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathhelper.go
More file actions
108 lines (88 loc) · 2.43 KB
/
Copy pathhelper.go
File metadata and controls
108 lines (88 loc) · 2.43 KB
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
106
107
108
package testfixtures
import (
"cmp"
"database/sql"
"fmt"
"strings"
"github.com/go-testfixtures/testfixtures/v3/shared"
)
type ParamType string
func (p ParamType) String() string {
return string(p)
}
func (p ParamType) Valid() error {
switch p {
case ParamTypeDollar, ParamTypeQuestion, ParamTypeAtSign:
return nil
default:
return fmt.Errorf("testfixtures: param type %s is not supported", p)
}
}
const (
ParamTypeDollar ParamType = "$"
ParamTypeQuestion ParamType = "?"
ParamTypeAtSign ParamType = "@"
)
type loadFunction func(tx *sql.Tx) error
type helper interface {
init(*sql.DB) error
disableReferentialIntegrity(*sql.DB, loadFunction) error
paramType() ParamType
getDefaultParamType() ParamType
setCustomParamType(ParamType)
databaseName(shared.Queryable) (string, error)
tableNames(shared.Queryable) ([]string, error)
isTableModified(shared.Queryable, string) (bool, error)
computeTablesChecksum(shared.Queryable) error
quoteKeyword(string) string
whileInsertOnTable(*sql.Tx, string, func() error) error
cleanTableQuery(string) string
buildInsertSQL(q shared.Queryable, tableName string, columns, values []string) (string, error)
}
var (
_ helper = &clickhouse{}
_ helper = &spanner{}
_ helper = &mySQL{}
_ helper = &postgreSQL{}
_ helper = &sqlite{}
_ helper = &sqlserver{}
)
type baseHelper struct {
customParamType ParamType
}
func (b *baseHelper) setCustomParamType(paramType ParamType) {
b.customParamType = paramType
}
func (b *baseHelper) paramType() ParamType {
return cmp.Or(b.customParamType, b.getDefaultParamType())
}
func (b *baseHelper) getDefaultParamType() ParamType {
return ParamTypeDollar
}
// shared methods
func (baseHelper) init(_ *sql.DB) error {
return nil
}
func (baseHelper) quoteKeyword(str string) string {
return fmt.Sprintf(`"%s"`, str)
}
func (baseHelper) whileInsertOnTable(_ *sql.Tx, _ string, fn func() error) error {
return fn()
}
func (baseHelper) isTableModified(_ shared.Queryable, _ string) (bool, error) {
return true, nil
}
func (baseHelper) computeTablesChecksum(_ shared.Queryable) error {
return nil
}
func (baseHelper) cleanTableQuery(tableName string) string {
return fmt.Sprintf("DELETE FROM %s", tableName)
}
func (h baseHelper) buildInsertSQL(_ shared.Queryable, tableName string, columns, values []string) (string, error) {
return fmt.Sprintf(
"INSERT INTO %s (%s) VALUES (%s)",
tableName,
strings.Join(columns, ", "),
strings.Join(values, ", "),
), nil
}