-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.go
74 lines (71 loc) · 1.43 KB
/
driver.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
package sql
import (
"database/sql"
"reflect"
"strconv"
)
const (
DriverPostgres = "postgres"
DriverMysql = "mysql"
DriverMssql = "mssql"
DriverOracle = "oracle"
DriverSqlite3 = "sqlite3"
DriverNotSupport = "no support"
)
func GetDriver(db *sql.DB) string {
if db == nil {
return DriverNotSupport
}
driver := reflect.TypeOf(db.Driver()).String()
switch driver {
case "*pq.Driver":
return DriverPostgres
case "*godror.drv":
return DriverOracle
case "*mysql.MySQLDriver":
return DriverMysql
case "*mssql.Driver":
return DriverMssql
case "*sqlite3.SQLiteDriver":
return DriverSqlite3
default:
return DriverNotSupport
}
}
func BuildParam(i int) string {
return "?"
}
func BuildOracleParam(i int) string {
return ":" + strconv.Itoa(i)
}
func BuildMsSqlParam(i int) string {
return "@p" + strconv.Itoa(i)
}
func BuildDollarParam(i int) string {
return "$" + strconv.Itoa(i)
}
func GetBuildByDriver(driver string) func(i int) string {
switch driver {
case DriverPostgres:
return BuildDollarParam
case DriverOracle:
return BuildOracleParam
case DriverMssql:
return BuildMsSqlParam
default:
return BuildParam
}
}
func GetBuild(db *sql.DB) func(i int) string {
driver := reflect.TypeOf(db.Driver()).String()
switch driver {
case "*pq.Driver":
return BuildDollarParam
case "*godror.drv":
return BuildOracleParam
case "*mssql.Driver":
return BuildMsSqlParam
default:
return BuildParam
}
}