-
Notifications
You must be signed in to change notification settings - Fork 23
/
db.go
68 lines (57 loc) · 1.4 KB
/
db.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
package jet
import (
"database/sql"
)
// LogFunc can be set on the Db instance to allow query logging.
type LogFunc func(queryId, query string, args ...interface{})
type Db struct {
*sql.DB
// LogFunc is the log function to use for query logging.
// Defaults to nil.
LogFunc LogFunc
// The column converter to use.
// Defaults to SnakeCaseConverter.
ColumnConverter ColumnConverter
driver string
source string
lru *lru
}
// Open opens a new database connection.
func Open(driverName, dataSourceName string) (*Db, error) {
db, err := sql.Open(driverName, dataSourceName)
if err != nil {
return nil, err
}
j := &Db{
ColumnConverter: SnakeCaseConverter, // default
driver: driverName,
source: dataSourceName,
lru: newLru(),
}
j.DB = db
return j, nil
}
// SetMaxCachedStatements sets the max number of statements to cache in the LRU. The default is 500.
func (db *Db) SetMaxCachedStatements(n int) {
db.lru.maxItems = n
}
// Begin starts a new transaction
func (db *Db) Begin() (*Tx, error) {
qid := newQueryId()
if db.LogFunc != nil {
db.LogFunc(qid, "BEGIN")
}
tx, err := db.DB.Begin()
if err != nil {
return nil, err
}
return &Tx{
db: db,
tx: tx,
qid: qid,
}, nil
}
// Query creates a prepared query that can be run with Rows or Run.
func (db *Db) Query(query string, args ...interface{}) Runnable {
return newQuery(db, db, query, args...)
}