-
Notifications
You must be signed in to change notification settings - Fork 405
/
dbhelper.go
43 lines (38 loc) · 1.31 KB
/
dbhelper.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
// Package dbhelper wraps SQL and specific capabilities of an SQL db
package dbhelper
import (
"fmt"
"github.com/jmoiron/sqlx"
"github.com/sirupsen/logrus"
"net/url"
)
var sqlHelpers []Helper
// Register registers a new SQL helper
func Register(helper Helper) {
logrus.Infof("Registering sql helper '%s'", helper)
sqlHelpers = append(sqlHelpers, helper)
}
// Helper provides DB-specific SQL capabilities
type Helper interface {
fmt.Stringer
// Supports indicates if this helper supports this driver name
Supports(driverName string) bool
// PreConnect calculates the connect URL for the db from a canonical URL used in Fn config
PreConnect(url *url.URL) (string, error)
// PostCreate Apply any configuration to the DB prior to use
PostCreate(db *sqlx.DB) (*sqlx.DB, error)
// CheckTableExists checks if a table exists in the DB
CheckTableExists(tx *sqlx.Tx, table string) (bool, error)
// IsDuplicateKeyError determines if an error indicates if the prior error was caused by a duplicate key insert
IsDuplicateKeyError(err error) bool
}
// GetHelper returns a helper for a specific driver
func GetHelper(driverName string) (Helper, bool) {
for _, helper := range sqlHelpers {
if helper.Supports(driverName) {
return helper, true
}
logrus.Debugf("%s does not support %s", helper, driverName)
}
return nil, false
}