xugu_result.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package xugu
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. )
  6. type xuguResult struct {
  7. xgConn *xuguConn
  8. // Returns the number of rows affected
  9. // by update, delete and other related operations
  10. affectedRows int64
  11. // Returns the GUID number of
  12. // the insert operation (not supported)
  13. insertId int64
  14. }
  15. // LastInsertId returns the integer generated by the database
  16. // in response to a command. Typically this will be from an
  17. // "auto increment" column when inserting a new row. Not all
  18. // databases support this feature, and the syntax of such
  19. // statements varies.
  20. func (result *xuguResult) LastInsertId() (int64, error) {
  21. sockSendPutStatement(result.xgConn, []byte("select last_insert_id();"), nil, 0)
  22. sockSendExecute(result.xgConn)
  23. //接收结果
  24. //recv msg
  25. aR, err := xuguSockRecvMsg(result.xgConn)
  26. if err != nil {
  27. return 0, err
  28. }
  29. switch aR.rt {
  30. case selectResult:
  31. return int64(binary.BigEndian.Uint64(aR.s.Values[0][0].Col_Data)), nil
  32. }
  33. return 0, fmt.Errorf("last insert id error")
  34. }
  35. // RowsAffected returns the number of rows affected by an
  36. // update, insert, or delete. Not every database or database
  37. // driver may support this.
  38. func (result *xuguResult) RowsAffected() (int64, error) {
  39. return result.affectedRows, nil
  40. }