-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmtexecute.go
74 lines (58 loc) · 2.1 KB
/
stmtexecute.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
package msg
/*
Byte banging mysql's X Protocol StmtExecute protobuf.
func FindByName(buf []byte, name string) Msg {
s := NewStmtExecute(buf, "SELECT id, name, email FROM users WHERE name = ?")
s.AppendArgString(name)
return s
}
s := FindByName(buf, "Bob")
_, err := s.WriteTo(conn) // Run
*/
import (
"database/sql/driver"
"encoding/binary"
"io"
"github.com/renthraysk/mysqlx/proto"
"github.com/renthraysk/mysqlx/protobuf/mysqlx"
)
const xnamespace = "mysqlx"
const (
tagStmtExecuteStmt = 1
tagStmtExecuteArgs = 2
tagStmtExecuteNamespace = 3
tagStmtExecuteCompactMetadata = 4
)
// StmtExecute is a builder and sender of StmtExecute proto message
type StmtExecute []byte
// NewStmtExecute creates a new StmtExecute which attempts to use the unused capacity of buf.
func NewStmtExecute(buf []byte, stmt string, args []driver.Value) (StmtExecute, error) {
var err error
b := append(buf[len(buf):], 0, 0, 0, 0, byte(mysqlx.ClientMessages_SQL_STMT_EXECUTE))
b = proto.AppendWireString(b, tagStmtExecuteStmt, stmt)
b, err = appendAnyValues(b, tagStmtExecuteArgs, args)
return StmtExecute(b), err
}
// NewStmtExecute creates a new StmtExecute which attempts to use the unused capacity of buf.
func NewStmtExecuteNamed(buf []byte, stmt string, args []driver.NamedValue) (StmtExecute, error) {
var err error
b := append(buf[len(buf):], 0, 0, 0, 0, byte(mysqlx.ClientMessages_SQL_STMT_EXECUTE))
b = proto.AppendWireString(b, tagStmtExecuteStmt, stmt)
b, err = appendAnyNamedValues(b, tagStmtExecuteArgs, args)
return StmtExecute(b), err
}
// WriteTo writes protobuf marshalled data to w, implementation of Msg interface
func (s StmtExecute) WriteTo(w io.Writer) (int64, error) {
binary.LittleEndian.PutUint32(s, uint32(len(s)-4))
n, err := w.Write(s)
return int64(n), err
}
// SetNamespace serialises the Namespace field of the StmtExecute protobuf.
func (s *StmtExecute) SetNamespace(namespace string) {
*s = proto.AppendWireString(*s, tagStmtExecuteNamespace, namespace)
}
func Ping(buf []byte) Msg {
p, _ := NewStmtExecute(buf, "ping", nil)
p.SetNamespace(xnamespace)
return p
}