statement.go 2.5 KB

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