This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
stmt.go
66 lines (54 loc) · 1.4 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
package grpcsql
import (
"database/sql/driver"
"github.com/CanonicalLtd/go-grpc-sql/internal/protocol"
)
// Stmt is a prepared statement. It is bound to a Conn and not
// used by multiple goroutines concurrently.
type Stmt struct {
conn *Conn
id int64
numInput int
}
// Close closes the statement.
func (s *Stmt) Close() error {
_, err := s.conn.exec(protocol.NewRequestStmtClose(s.id))
return err
}
// NumInput returns the number of placeholder parameters.
func (s *Stmt) NumInput() int {
return s.numInput
}
// Exec executes a query that doesn't return rows, such
func (s *Stmt) Exec(args []driver.Value) (driver.Result, error) {
values, err := protocol.FromDriverValues(args)
if err != nil {
return nil, err
}
response, err := s.conn.exec(protocol.NewRequestExec(s.id, values))
if err != nil {
return nil, err
}
result := &Result{
lastInsertID: response.Exec().LastInsertId,
rowsAffected: response.Exec().RowsAffected,
}
return result, nil
}
// Query executes a query that may return rows, such as a
func (s *Stmt) Query(args []driver.Value) (driver.Rows, error) {
values, err := protocol.FromDriverValues(args)
if err != nil {
return nil, err
}
response, err := s.conn.exec(protocol.NewRequestQuery(s.id, values))
if err != nil {
return nil, err
}
rows := &Rows{
conn: s.conn,
id: response.Query().Id,
columns: response.Query().Columns,
}
return rows, nil
}