statement.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. import (
  11. "database/sql/driver"
  12. "fmt"
  13. )
  14. type stmtContent struct {
  15. mc *mysqlConn
  16. id uint32
  17. query string
  18. paramCount int
  19. params []*mysqlField
  20. args *[]driver.Value
  21. newParamsBound bool
  22. }
  23. type mysqlStmt struct {
  24. *stmtContent
  25. }
  26. func (stmt mysqlStmt) Close() error {
  27. e := stmt.mc.writeCommandPacket(COM_STMT_CLOSE, stmt.id)
  28. stmt.params = nil
  29. stmt.mc = nil
  30. return e
  31. }
  32. func (stmt mysqlStmt) NumInput() int {
  33. return stmt.paramCount
  34. }
  35. func (stmt mysqlStmt) Exec(args []driver.Value) (driver.Result, error) {
  36. stmt.mc.affectedRows = 0
  37. stmt.mc.insertId = 0
  38. // Send command
  39. e := stmt.buildExecutePacket(&args)
  40. if e != nil {
  41. return nil, e
  42. }
  43. // Read Result
  44. var resLen int
  45. resLen, e = stmt.mc.readResultSetHeaderPacket()
  46. if e != nil {
  47. return nil, e
  48. }
  49. if resLen > 0 {
  50. _, e = stmt.mc.readUntilEOF()
  51. if e != nil {
  52. return nil, e
  53. }
  54. stmt.mc.affectedRows, e = stmt.mc.readUntilEOF()
  55. if e != nil {
  56. return nil, e
  57. }
  58. }
  59. if e != nil {
  60. return nil, e
  61. }
  62. if stmt.mc.affectedRows == 0 {
  63. return driver.ResultNoRows, nil
  64. }
  65. return &mysqlResult{
  66. affectedRows: int64(stmt.mc.affectedRows),
  67. insertId: int64(stmt.mc.insertId)},
  68. nil
  69. }
  70. func (stmt mysqlStmt) Query(args []driver.Value) (dr driver.Rows, e error) {
  71. // Send command
  72. e = stmt.buildExecutePacket(&args)
  73. if e != nil {
  74. return nil, e
  75. }
  76. // Get Result
  77. var resLen int
  78. rows := new(mysqlRows)
  79. rows.content = new(rowsContent)
  80. resLen, e = stmt.mc.readResultSetHeaderPacket()
  81. if e != nil {
  82. return nil, e
  83. }
  84. if resLen > 0 {
  85. // Columns
  86. rows.content.columns, e = stmt.mc.readColumns(resLen)
  87. if e != nil {
  88. return
  89. }
  90. // Rows
  91. e = stmt.mc.readBinaryRows(rows.content)
  92. if e != nil {
  93. return
  94. }
  95. }
  96. dr = rows
  97. return
  98. }
  99. // ColumnConverter returns a ValueConverter for the provided
  100. // column index. If the type of a specific column isn't known
  101. // or shouldn't be handled specially, DefaultValueConverter
  102. // can be returned.
  103. func (stmt mysqlStmt) ColumnConverter(idx int) driver.ValueConverter {
  104. debug(fmt.Sprintf("ColumnConverter(%d)", idx))
  105. return driver.DefaultParameterConverter
  106. }