Julien Schmidt | b8ae1f2 | 2012-05-04 02:19:16 | [diff] [blame] | 1 | // 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/. |
| 9 | package mysql |
| 10 | |
| 11 | import ( |
| 12 | "database/sql/driver" |
Julien Schmidt | b8ae1f2 | 2012-05-04 02:19:16 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | type 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 | |
| 25 | type mysqlStmt struct { |
| 26 | *stmtContent |
| 27 | } |
| 28 | |
| 29 | func (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 | |
| 36 | func (stmt mysqlStmt) NumInput() int { |
| 37 | return stmt.paramCount |
| 38 | } |
| 39 | |
| 40 | func (stmt mysqlStmt) Exec(args []driver.Value) (driver.Result, error) { |
| 41 | stmt.mc.affectedRows = 0 |
| 42 | stmt.mc.insertId = 0 |
Julien Schmidt | 4d4b409 | 2012-05-04 13:13:50 | [diff] [blame] | 43 | |
Julien Schmidt | b8ae1f2 | 2012-05-04 02:19:16 | [diff] [blame] | 44 | // 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 | |
| 82 | func (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 Schmidt | b8ae1f2 | 2012-05-04 02:19:16 | [diff] [blame] | 116 | // 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 Schmidt | e4b1048 | 2012-05-04 18:07:12 | [diff] [blame^] | 120 | //func (stmt mysqlStmt) ColumnConverter(idx int) driver.ValueConverter { |
| 121 | // debug(fmt.Sprintf("ColumnConverter(%d)", idx)) |
| 122 | // return driver.DefaultParameterConverter |
| 123 | //} |