result.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public
  6. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  7. // You can obtain one at http://mozilla.org/MPL/2.0/.
  8. package mysql
  9. import "database/sql/driver"
  10. // Result exposes data not available through *connection.Result.
  11. //
  12. // This is accessible by executing statements using sql.Conn.Raw() and
  13. // downcasting the returned result:
  14. //
  15. // res, err := rawConn.Exec(...)
  16. // res.(mysql.Result).AllRowsAffected()
  17. type Result interface {
  18. driver.Result
  19. // AllRowsAffected returns a slice containing the affected rows for each
  20. // executed statement.
  21. AllRowsAffected() []int64
  22. // AllLastInsertIds returns a slice containing the last inserted ID for each
  23. // executed statement.
  24. AllLastInsertIds() []int64
  25. }
  26. type mysqlResult struct {
  27. // One entry in both slices is created for every executed statement result.
  28. affectedRows []int64
  29. insertIds []int64
  30. }
  31. func (res *mysqlResult) LastInsertId() (int64, error) {
  32. return res.insertIds[len(res.insertIds)-1], nil
  33. }
  34. func (res *mysqlResult) RowsAffected() (int64, error) {
  35. return res.affectedRows[len(res.affectedRows)-1], nil
  36. }
  37. func (res *mysqlResult) AllLastInsertIds() []int64 {
  38. return append([]int64{}, res.insertIds...) // defensive copy
  39. }
  40. func (res *mysqlResult) AllRowsAffected() []int64 {
  41. return append([]int64{}, res.affectedRows...) // defensive copy
  42. }