forked from proullon/ramsql
-
Notifications
You must be signed in to change notification settings - Fork 3
/
conn.go
65 lines (50 loc) · 1.41 KB
/
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
package ramsql
import (
"database/sql/driver"
"sync"
"github.com/mlhoyt/ramsql/engine/log"
"github.com/mlhoyt/ramsql/engine/protocol"
)
// Conn implements sql/driver Conn interface
type Conn struct {
// Mutex is locked when a Statement is created
// then released on Statement.Exec or Statement.Query
mutex sync.Mutex
// Socket is the network connection to RamSQL engine
conn protocol.DriverConn
// socket net.Conn
// This conn belongs to this server
parent *Server
}
func newConn(conn protocol.DriverConn, parent *Server) driver.Conn {
parent.openingConn()
return &Conn{conn: conn, parent: parent}
}
// Prepare returns a prepared statement, bound to this connection.
func (c *Conn) Prepare(query string) (driver.Stmt, error) {
stmt := prepareStatement(c, query)
return stmt, nil
}
// Close invalidates and potentially stops any current
// prepared statements and transactions, marking this
// connection as no longer in use.
//
// Because the sql package maintains a free pool of
// connections and only calls Close when there's a surplus of
// idle connections, it shouldn't be necessary for drivers to
// do their own connection caching.
func (c *Conn) Close() error {
log.Debug("Conn.Close")
c.conn.Close()
if c.parent != nil {
c.parent.closingConn()
}
return nil
}
// Begin starts and returns a new transaction.
func (c *Conn) Begin() (driver.Tx, error) {
tx := Tx{
conn: c,
}
return &tx, nil
}