-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathdatabase_connect_cgo.go
58 lines (47 loc) · 1.1 KB
/
database_connect_cgo.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
//go:build cgo
package trdsql
import (
"database/sql"
// MySQL driver.
_ "github.com/go-sql-driver/mysql"
// PostgreSQL driver.
_ "github.com/lib/pq"
// SQLite3 driver.
_ "github.com/mattn/go-sqlite3"
// SQlite3 extension library.
sqlite3_stdlib "github.com/multiprocessio/go-sqlite3-stdlib"
)
var DefaultDriver = "sqlite3"
func init() {
// Enable sqlite3 extensions.
// It can be used by setting the driver to "sqlite3_ext".
sqlite3_stdlib.Register("sqlite3_ext")
}
// Connect is connects to the database.
// Currently supported drivers are sqlite3, mysql, postgres.
// Set quote character and maxBulk depending on the driver type.
func Connect(driver, dsn string) (*DB, error) {
sqlDB, err := sql.Open(driver, dsn)
if err != nil {
return nil, err
}
db := &DB{
DB: sqlDB,
driver: driver,
dsn: dsn,
}
debug.Printf("driver: %s, dsn: %s", driver, dsn)
switch driver {
case "sqlite3", "sqlite3_ext", "sqlite":
db.quote = "`"
db.maxBulk = 1000
case "mysql":
db.quote = "`"
db.maxBulk = 1000
case "postgres":
db.quote = "\""
default:
db.quote = "\""
}
return db, nil
}