-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatement.go
112 lines (100 loc) · 2.92 KB
/
statement.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package unixodbc
import (
"context"
"database/sql/driver"
"github.com/ninthclowd/unixodbc/internal/odbc"
"runtime/trace"
)
var (
_ driver.StmtExecContext = (*PreparedStatement)(nil)
_ driver.StmtQueryContext = (*PreparedStatement)(nil)
_ driver.Stmt = (*PreparedStatement)(nil)
)
type PreparedStatement struct {
odbcStatement odbc.Statement
query string
conn *Connection
numInput int
}
func (s *PreparedStatement) closeWithError(err error) error {
if s.odbcStatement != nil {
closeErr := s.odbcStatement.Close()
s.odbcStatement = nil
if closeErr != nil {
return closeErr
}
}
return err
}
// Close implements driver.Stmt
func (s *PreparedStatement) Close() error {
delete(s.conn.uncachedStatements, s)
if s.odbcStatement == nil {
return nil
}
//move the statement to the LRU, closing the statement if no room in cache
return s.conn.cachedStatements.Put(s.query, s)
}
// NumInput implements driver.Stmt
func (s *PreparedStatement) NumInput() int {
return s.numInput
}
// Exec will never be called because driver.StmtExecContext is implemented
func (s *PreparedStatement) Exec(args []driver.Value) (driver.Result, error) {
panic("unexpected call to Exec() from driver")
}
// ExecContext implements driver.StmtExecContext
func (s *PreparedStatement) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
if s.odbcStatement == nil {
return nil, odbc.ErrInvalidHandle
}
ctx, trc := trace.NewTask(ctx, "statement::ExecContext")
defer trc.End()
var err error
trace.WithRegion(ctx, "BindParams", func() {
err = s.odbcStatement.BindParams(toValues(args)...)
})
if err != nil {
return nil, s.closeWithError(err)
}
trace.WithRegion(ctx, "Execute", func() {
err = s.odbcStatement.Execute(ctx)
})
if err != nil {
return nil, s.closeWithError(err)
}
return &result{lastInsertId: 0, rowsAffected: 0}, nil //TODO stats for exec
}
// Query will never be called because driver.StmtQueryContext is implemented
func (s *PreparedStatement) Query(args []driver.Value) (driver.Rows, error) {
panic("unexpected call to Query() from driver")
}
// QueryContext implements driver.StmtQueryContext
func (s *PreparedStatement) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
if s.odbcStatement == nil {
return nil, odbc.ErrInvalidHandle
}
ctx, trc := trace.NewTask(ctx, "statement::QueryContext")
defer trc.End()
var err error
trace.WithRegion(ctx, "BindParams", func() {
err = s.odbcStatement.BindParams(toValues(args)...)
})
if err != nil {
return nil, s.closeWithError(err)
}
trace.WithRegion(ctx, "Execute", func() {
err = s.odbcStatement.Execute(ctx)
})
if err != nil {
return nil, s.closeWithError(err)
}
var rs odbc.RecordSet
trace.WithRegion(ctx, "RecordSet", func() {
rs, err = s.odbcStatement.RecordSet()
})
if err != nil {
return nil, s.closeWithError(err)
}
return &Rows{odbcRecordset: rs, ctx: ctx}, nil
}