-
Notifications
You must be signed in to change notification settings - Fork 14
/
pool.go
53 lines (44 loc) · 785 Bytes
/
pool.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
package sqlf
import (
"sync"
"github.com/valyala/bytebufferpool"
)
var (
stmtPool = sync.Pool{New: newStmt}
)
func newStmt() interface{} {
return &Stmt{
chunks: make(stmtChunks, 0, 8),
}
}
func getStmt(d *Dialect) *Stmt {
stmt := stmtPool.Get().(*Stmt)
stmt.dialect = d
stmt.buf = getBuffer()
return stmt
}
func reuseStmt(q *Stmt) {
q.chunks = q.chunks[:0]
if len(q.args) > 0 {
for n := range q.args {
q.args[n] = nil
}
q.args = q.args[:0]
}
if len(q.dest) > 0 {
for n := range q.dest {
q.dest[n] = nil
}
q.dest = q.dest[:0]
}
putBuffer(q.buf)
q.buf = nil
q.sql = ""
stmtPool.Put(q)
}
func getBuffer() *bytebufferpool.ByteBuffer {
return bytebufferpool.Get()
}
func putBuffer(buf *bytebufferpool.ByteBuffer) {
bytebufferpool.Put(buf)
}