forked from go-sql-driver/mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement.go
93 lines (78 loc) · 1.8 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
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
//
// Copyright 2012 Julien Schmidt. All rights reserved.
// http://www.julienschmidt.com
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
package mysql
import (
"database/sql/driver"
)
type mysqlStmt struct {
mc *mysqlConn
id uint32
paramCount int
params []mysqlField
}
func (stmt *mysqlStmt) Close() (err error) {
err = stmt.mc.writeCommandPacketUint32(comStmtClose, stmt.id)
stmt.mc = nil
return
}
func (stmt *mysqlStmt) NumInput() int {
return stmt.paramCount
}
func (stmt *mysqlStmt) Exec(args []driver.Value) (driver.Result, error) {
stmt.mc.affectedRows = 0
stmt.mc.insertId = 0
// Send command
err := stmt.writeExecutePacket(args)
if err != nil {
return nil, err
}
// Read Result
var resLen int
resLen, err = stmt.mc.readResultSetHeaderPacket()
if err == nil {
if resLen > 0 {
// Columns
err = stmt.mc.readUntilEOF()
if err != nil {
return nil, err
}
// Rows
err = stmt.mc.readUntilEOF()
}
if err == nil {
return &mysqlResult{
affectedRows: int64(stmt.mc.affectedRows),
insertId: int64(stmt.mc.insertId),
}, nil
}
}
return nil, err
}
func (stmt *mysqlStmt) Query(args []driver.Value) (driver.Rows, error) {
// Send command
err := stmt.writeExecutePacket(args)
if err != nil {
return nil, err
}
// Read Result
var resLen int
resLen, err = stmt.mc.readResultSetHeaderPacket()
if err != nil {
return nil, err
}
rows := &mysqlRows{stmt.mc, true, nil, false}
if resLen > 0 {
// Columns
rows.columns, err = stmt.mc.readColumns(resLen)
if err != nil {
return nil, err
}
}
return rows, err
}