-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsql.go
153 lines (142 loc) · 4.34 KB
/
sql.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package dads
import (
"database/sql"
"fmt"
"reflect"
"time"
_ "github.com/go-sql-driver/mysql" // User MySQL driver
"github.com/jmoiron/sqlx"
)
// ConnectAffiliationsDB - connect to affilaitions DB
func ConnectAffiliationsDB(ctx *Ctx) {
if !ctx.AffsDBConfigured() {
Fatalf("requested connection to affiliations DB while connection parameters are not set")
}
connStr := ctx.DBConn
if connStr == "" {
if ctx.DBName == "" {
Fatalf("requested connection to affiliations DB while DB name was not specified")
}
if ctx.DBUser == "" {
Fatalf("requested connection to affiliations DB while DB user was not specified")
}
hostPort := ctx.DBHost
if hostPort == "" {
hostPort = "127.0.0.1"
}
if ctx.DBPort != "" {
hostPort += ":" + ctx.DBPort
}
userPass := ctx.DBUser
if ctx.DBPass != "" {
userPass += ":" + ctx.DBPass
}
opts := ctx.DBOpts
if opts == "" {
opts = "charset=utf8&parseTime=true"
}
// user:pwd@tcp(127.0.0.1:3306)/db?charset=utf8&parseTime=true
connStr = fmt.Sprintf("%s@tcp(%s)/%s?%s", userPass, hostPort, ctx.DBName, opts)
}
if ctx.Debug > 0 {
Printf("affiliations DB connect string: %s\n", connStr)
}
d, err := sqlx.Connect("mysql", connStr)
FatalOnError(err)
d.SetMaxOpenConns(3)
ctx.DB = d
FatalOnError(SetDBSessionOrigin(ctx))
}
// SetDBSessionOrigin - Set Session DB variable @origin to 'dads' so we will know which tool performed the DB operation
func SetDBSessionOrigin(ctx *Ctx) (err error) {
_, err = ExecSQL(ctx, nil, "set @origin = ?", DADSOrigin)
return err
}
// QueryOut - display DB query
func QueryOut(ctx *Ctx, in bool, err error, query string, args ...interface{}) {
pref := "<<< "
if in {
pref = ">>> "
}
q := pref + query + "\n"
if (err != nil || ctx.DebugSQL > 1) && len(args) > 0 {
s := ""
for vi, vv := range args {
switch v := vv.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, complex64, complex128, string, bool, time.Time:
s += fmt.Sprintf("%d:%+v ", vi+1, v)
case *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64, *float32, *float64, *complex64, *complex128, *string, *bool, *time.Time:
s += fmt.Sprintf("%d:%+v ", vi+1, v)
case nil:
s += fmt.Sprintf("%d:(null) ", vi+1)
default:
s += fmt.Sprintf("%d:%+v ", vi+1, reflect.ValueOf(vv))
}
}
q += "[" + s + "]\n"
}
if err != nil || ctx.DebugSQL > 0 {
Printf("%s", q)
if err != nil {
Printf("Error: %+v\n", err)
}
}
}
// ExecDB - execute DB query without transaction
func ExecDB(ctx *Ctx, query string, args ...interface{}) (res sql.Result, err error) {
if err != nil || ctx.DebugSQL > 0 {
QueryOut(ctx, true, err, query, args...)
}
res, err = ctx.DB.Exec(query, args...)
if err != nil || ctx.DebugSQL > 0 {
QueryOut(ctx, false, err, query, args...)
}
return
}
// ExecTX - execute DB query with transaction
func ExecTX(ctx *Ctx, tx *sql.Tx, query string, args ...interface{}) (res sql.Result, err error) {
if err != nil || ctx.DebugSQL > 0 {
QueryOut(ctx, true, err, query, args...)
}
res, err = tx.Exec(query, args...)
if err != nil || ctx.DebugSQL > 0 {
QueryOut(ctx, false, err, query, args...)
}
return
}
// ExecSQL - execute db query with transaction if provided
func ExecSQL(ctx *Ctx, tx *sql.Tx, query string, args ...interface{}) (sql.Result, error) {
if tx == nil {
return ExecDB(ctx, query, args...)
}
return ExecTX(ctx, tx, query, args...)
}
// QueryDB - query database without transaction
func QueryDB(ctx *Ctx, query string, args ...interface{}) (rows *sql.Rows, err error) {
if err != nil || ctx.DebugSQL > 0 {
QueryOut(ctx, true, err, query, args...)
}
rows, err = ctx.DB.Query(query, args...)
if err != nil || ctx.DebugSQL > 0 {
QueryOut(ctx, false, err, query, args...)
}
return
}
// QueryTX - query database with transaction
func QueryTX(ctx *Ctx, tx *sql.Tx, query string, args ...interface{}) (rows *sql.Rows, err error) {
if err != nil || ctx.DebugSQL > 0 {
QueryOut(ctx, true, err, query, args...)
}
rows, err = tx.Query(query, args...)
if err != nil || ctx.DebugSQL > 0 {
QueryOut(ctx, false, err, query, args...)
}
return
}
// QuerySQL - query DB using transaction if provided
func QuerySQL(ctx *Ctx, tx *sql.Tx, query string, args ...interface{}) (*sql.Rows, error) {
if tx == nil {
return QueryDB(ctx, query, args...)
}
return QueryTX(ctx, tx, query, args...)
}