This repository has been archived by the owner on Mar 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ql.go
86 lines (74 loc) · 2.14 KB
/
ql.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
package ql
import (
"database/sql"
"time"
"github.com/mibk/ql/dialect"
"github.com/mibk/ql/query"
)
// Connection is a connection to the database with an EventReceiver to send events,
// errors, and timings to.
type Connection struct {
DB *sql.DB
EventReceiver
}
// NewConnection instantiates a Connection for a given database/sql connection
// and event receiver.
func NewConnection(db *sql.DB, log EventReceiver) *Connection {
if log == nil {
log = nullReceiver
}
return &Connection{DB: db, EventReceiver: log}
}
// Open opens a database by calling sql.Open. It returns new Connection with
// nil EventReceiver.
func Open(driverName, dataSourceName string) (*Connection, error) {
switch driverName {
case "mysql":
default:
panic("unsupported driver")
}
db, err := sql.Open(driverName, dataSourceName)
if err != nil {
return nil, err
}
return NewConnection(db, nil), nil
}
// MustOpen is like Open but panics on error.
func MustOpen(driverName, dataSourceName string) *Connection {
conn, err := Open(driverName, dataSourceName)
if err != nil {
panic(err)
}
return conn
}
// MustOpenAndVerify is like MustOpen but it verifies the connection and panics
// on error.
func MustOpenAndVerify(driverName, dataSourceName string) *Connection {
conn := MustOpen(driverName, dataSourceName)
if err := conn.Ping(); err != nil {
panic(err)
}
return conn
}
// Close closes the database, releasing any open resources.
func (c *Connection) Close() error {
return c.DB.Close()
}
// Ping verifies a connection to the database is still alive, establishing a connection if necessary.
func (c *Connection) Ping() error {
return c.DB.Ping()
}
type runner interface {
Exec(query string, args ...interface{}) (sql.Result, error)
Query(query string, args ...interface{}) (*sql.Rows, error)
}
var D Dialect = dialect.Mysql{}
// Dialect is an interface that wraps the diverse properties of individual
// SQL drivers.
type Dialect interface {
EscapeIdent(w query.Writer, ident string)
EscapeBool(w query.Writer, b bool)
EscapeString(w query.Writer, s string)
EscapeTime(w query.Writer, t time.Time)
ApplyLimitAndOffset(w query.Writer, limit, offset uint64)
}