blob: f2bee5d55409791e4286189f71937558e3491a0e [file] [log] [blame]
Julien Schmidtb8ae1f22012-05-04 02:19:161// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2//
3// Copyright 2012 Julien Schmidt. All rights reserved.
4// http://www.julienschmidt.com
5//
6// This Source Code Form is subject to the terms of the Mozilla Public
7// License, v. 2.0. If a copy of the MPL was not distributed with this file,
8// You can obtain one at http://mozilla.org/MPL/2.0/.
9package mysql
10
11import (
12 "database/sql/driver"
Julien Schmidtb8ae1f22012-05-04 02:19:1613)
14
15type stmtContent struct {
16 mc *mysqlConn
17 id uint32
18 query string
19 paramCount int
20 params []*mysqlField
21 args *[]driver.Value
22 newParamsBound bool
23}
24
25type mysqlStmt struct {
26 *stmtContent
27}
28
29func (stmt mysqlStmt) Close() error {
30 e := stmt.mc.writeCommandPacket(COM_STMT_CLOSE, stmt.id)
31 stmt.params = nil
32 stmt.mc = nil
33 return e
34}
35
36func (stmt mysqlStmt) NumInput() int {
37 return stmt.paramCount
38}
39
40func (stmt mysqlStmt) Exec(args []driver.Value) (driver.Result, error) {
41 stmt.mc.affectedRows = 0
42 stmt.mc.insertId = 0
Julien Schmidt4d4b4092012-05-04 13:13:5043
Julien Schmidtb8ae1f22012-05-04 02:19:1644 // Send command
45 e := stmt.buildExecutePacket(&args)
46 if e != nil {
47 return nil, e
48 }
49
50 // Read Result
51 var resLen int
52 resLen, e = stmt.mc.readResultSetHeaderPacket()
53 if e != nil {
54 return nil, e
55 }
56
57 if resLen > 0 {
58 _, e = stmt.mc.readUntilEOF()
59 if e != nil {
60 return nil, e
61 }
62
63 stmt.mc.affectedRows, e = stmt.mc.readUntilEOF()
64 if e != nil {
65 return nil, e
66 }
67 }
68 if e != nil {
69 return nil, e
70 }
71
72 if stmt.mc.affectedRows == 0 {
73 return driver.ResultNoRows, nil
74 }
75
76 return &mysqlResult{
77 affectedRows: int64(stmt.mc.affectedRows),
78 insertId: int64(stmt.mc.insertId)},
79 nil
80}
81
82func (stmt mysqlStmt) Query(args []driver.Value) (dr driver.Rows, e error) {
83 // Send command
84 e = stmt.buildExecutePacket(&args)
85 if e != nil {
86 return nil, e
87 }
88
89 // Get Result
90 var resLen int
91 rows := new(mysqlRows)
92 rows.content = new(rowsContent)
93 resLen, e = stmt.mc.readResultSetHeaderPacket()
94 if e != nil {
95 return nil, e
96 }
97
98 if resLen > 0 {
99 // Columns
100 rows.content.columns, e = stmt.mc.readColumns(resLen)
101 if e != nil {
102 return
103 }
104
105 // Rows
106 e = stmt.mc.readBinaryRows(rows.content)
107 if e != nil {
108 return
109 }
110 }
111
112 dr = rows
113 return
114}
115
Julien Schmidtb8ae1f22012-05-04 02:19:16116// ColumnConverter returns a ValueConverter for the provided
117// column index. If the type of a specific column isn't known
118// or shouldn't be handled specially, DefaultValueConverter
119// can be returned.
Julien Schmidte4b10482012-05-04 18:07:12120//func (stmt mysqlStmt) ColumnConverter(idx int) driver.ValueConverter {
121// debug(fmt.Sprintf("ColumnConverter(%d)", idx))
122// return driver.DefaultParameterConverter
123//}