statement.go 2.5 KB

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