-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt.go
70 lines (62 loc) · 1.04 KB
/
stmt.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
package autoprepare
import (
"database/sql"
"sync"
)
type stmt struct {
cond sync.Cond
lock sync.Mutex
ps *sql.Stmt
psHandles uint32 // number of goroutines using ps
hit uint64
q string
}
func newStmt(sql string, hit uint64) *stmt {
s := &stmt{q: sql, hit: hit}
s.cond.L = &s.lock
return s
}
func (s *stmt) acquire() *sql.Stmt {
if s == nil {
return nil
}
s.lock.Lock()
ps := s.ps
if ps != nil {
s.psHandles += 1
}
s.lock.Unlock()
return ps
}
func (s *stmt) release() {
s.lock.Lock()
s.psHandles -= 1
if s.psHandles == 0 {
s.cond.Broadcast()
}
s.lock.Unlock()
}
func (s *stmt) close() {
s.lock.Lock()
for s.psHandles > 0 {
s.cond.Wait()
}
ps := s.ps
s.ps = nil
s.lock.Unlock()
ps.Close()
}
func (s *stmt) put(v *sql.Stmt) {
if v == nil {
panic("nil *sql.Stmt")
}
s.lock.Lock()
s.ps = v
s.lock.Unlock()
}
func (s *stmt) prepared() (prepared bool) {
s.lock.Lock()
prepared = s.ps != nil
s.lock.Unlock()
return
}