forked from minus5/gofreetds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mssql_conn.go
69 lines (57 loc) · 1.92 KB
/
mssql_conn.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
package freetds
import (
"database/sql"
"database/sql/driver"
)
//register driver for use with database/sql package
func init() {
sql.Register("mssql", &MssqlDriver{})
}
//implements Driver interface from http://golang.org/src/pkg/database/sql/driver/driver.go
type MssqlDriver struct{}
//implements Open for Driver interface from http://golang.org/src/pkg/database/sql/driver/driver.go
func (d *MssqlDriver) Open(dsn string) (driver.Conn, error) {
conn, err := NewConn(dsn)
if err != nil {
return nil, err
}
return &MssqlConn{conn: conn}, nil
}
//implements Conn interface from http://golang.org/src/pkg/database/sql/driver/driver.go
type MssqlConn struct {
conn *Conn
}
//implements Prepare for Conn interface from http://golang.org/src/pkg/database/sql/driver/driver.go
func (c *MssqlConn) Prepare(query string) (driver.Stmt, error) {
_, numInput := query2Statement(query)
s := &MssqlStmt{query: query, numInput: numInput, conn: c.conn}
return s, nil
}
//implements Close for Conn interface from http://golang.org/src/pkg/database/sql/driver/driver.go
func (c *MssqlConn) Close() error {
c.conn.Close()
return nil
}
//implements Begin for Conn interface from http://golang.org/src/pkg/database/sql/driver/driver.go
func (c *MssqlConn) Begin() (driver.Tx, error) {
t := &MssqlConnTx{conn: c.conn}
return t, t.begin()
}
//implements Tx interface from http://golang.org/src/pkg/database/sql/driver/driver.go
type MssqlConnTx struct {
conn *Conn
}
func (t *MssqlConnTx) begin() error {
_, err := t.conn.Exec("begin transaction")
return err
}
//implements Commit for Tx interface from http://golang.org/src/pkg/database/sql/driver/driver.go
func (t *MssqlConnTx) Commit() error {
_, err := t.conn.Exec("commit transaction")
return err
}
//implements Rollback for Tx interface from http://golang.org/src/pkg/database/sql/driver/driver.go
func (t *MssqlConnTx) Rollback() error {
_, err := t.conn.Exec("rollback transaction")
return err
}