/
ha_sqlite_stmt.go
105 lines (95 loc) · 3.29 KB
/
ha_sqlite_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
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
package driver
import (
"context"
"database/sql/driver"
"fmt"
"github.com/uglyer/ha-sqlite/proto"
)
type HaSqliteStmt struct {
driver.Stmt
query string
txToken string
dbId int64
client proto.DBClient
}
// NewHaSqliteStmt TODO 实现真实的预编译动作
func NewHaSqliteStmt(ctx context.Context, client proto.DBClient, dbId int64, txToken string, query string) (*HaSqliteStmt, error) {
return &HaSqliteStmt{
query: query,
dbId: dbId,
txToken: txToken,
client: client,
}, nil
}
// Close closes the statement.
//
// As of Go 1.1, a Stmt will not be closed if it's in use
// by any queries.
//
// Drivers must ensure all network calls made by Close
// do not block indefinitely (e.g. apply a timeout).
func (s *HaSqliteStmt) Close() error {
return nil
}
// NumInput returns the number of placeholder parameters.
//
// If NumInput returns >= 0, the sql package will sanity check
// argument counts from callers and return errors to the caller
// before the statement's Exec or Query methods are called.
//
// NumInput may also return -1, if the driver doesn't know
// its number of placeholders. In that case, the sql package
// will not sanity check Exec or Query argument counts.
func (s *HaSqliteStmt) NumInput() int {
return -1
}
// Exec executes a query that doesn't return rows, such
// as an INSERT or UPDATE.
//
// Deprecated: Drivers should implement StmtExecContext instead (or additionally).
func (s *HaSqliteStmt) Exec(args []driver.Value) (driver.Result, error) {
return s.ExecContext(context.Background(), proto.ValuesToNamedValues(args))
}
// ExecContext is an optional interface that may be implemented by a Conn.
func (s *HaSqliteStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
if len(args) > MaxTupleParams {
return nil, fmt.Errorf("too many parameters (%d) max = %d", len(args), MaxTupleParams)
}
parameters, err := proto.DriverNamedValueToParameters(args)
if err != nil {
return nil, fmt.Errorf("convert named value to parameters error %v", err)
}
statements := []*proto.Statement{{Sql: s.query, Parameters: parameters}}
req := &proto.ExecRequest{
Request: &proto.Request{
TxToken: s.txToken,
DbId: s.dbId,
Statements: statements,
},
}
return proto.DBClientExecCheckResult(s.client, ctx, req)
}
// Query executes a query that may return rows, such as a
// SELECT.
//
// Deprecated: Drivers should implement StmtQueryContext instead (or additionally).
func (s *HaSqliteStmt) Query(args []driver.Value) (driver.Rows, error) {
return s.QueryContext(context.Background(), proto.ValuesToNamedValues(args))
}
// QueryContext is an optional interface that may be implemented by a Conn.
func (s *HaSqliteStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
if len(args) > MaxTupleParams {
return nil, fmt.Errorf("too many parameters (%d) max = %d", len(args), MaxTupleParams)
}
parameters, err := proto.DriverNamedValueToParameters(args)
if err != nil {
return nil, fmt.Errorf("convert named value to parameters error %v", err)
}
statements := []*proto.Statement{{Sql: s.query, Parameters: parameters}}
req := &proto.QueryRequest{Request: &proto.Request{
TxToken: s.txToken,
DbId: s.dbId,
Statements: statements,
}}
return proto.DBClientQueryCheckResult(s.client, ctx, req)
}