tran.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package mssql
  2. // Transaction Manager requests
  3. // http://msdn.microsoft.com/en-us/library/dd339887.aspx
  4. import (
  5. "encoding/binary"
  6. )
  7. const (
  8. tmGetDtcAddr = 0
  9. tmPropagateXact = 1
  10. tmBeginXact = 5
  11. tmPromoteXact = 6
  12. tmCommitXact = 7
  13. tmRollbackXact = 8
  14. tmSaveXact = 9
  15. )
  16. type isoLevel uint8
  17. const (
  18. isolationUseCurrent isoLevel = 0
  19. isolationReadUncommited = 1
  20. isolationReadCommited = 2
  21. isolationRepeatableRead = 3
  22. isolationSerializable = 4
  23. isolationSnapshot = 5
  24. )
  25. func sendBeginXact(buf *tdsBuffer, headers []headerStruct, isolation isoLevel, name string, resetSession bool) (err error) {
  26. buf.BeginPacket(packTransMgrReq, resetSession)
  27. writeAllHeaders(buf, headers)
  28. var rqtype uint16 = tmBeginXact
  29. err = binary.Write(buf, binary.LittleEndian, &rqtype)
  30. if err != nil {
  31. return
  32. }
  33. err = binary.Write(buf, binary.LittleEndian, &isolation)
  34. if err != nil {
  35. return
  36. }
  37. err = writeBVarChar(buf, name)
  38. if err != nil {
  39. return
  40. }
  41. return buf.FinishPacket()
  42. }
  43. const (
  44. fBeginXact = 1
  45. )
  46. func sendCommitXact(buf *tdsBuffer, headers []headerStruct, name string, flags uint8, isolation uint8, newname string, resetSession bool) error {
  47. buf.BeginPacket(packTransMgrReq, resetSession)
  48. writeAllHeaders(buf, headers)
  49. var rqtype uint16 = tmCommitXact
  50. err := binary.Write(buf, binary.LittleEndian, &rqtype)
  51. if err != nil {
  52. return err
  53. }
  54. err = writeBVarChar(buf, name)
  55. if err != nil {
  56. return err
  57. }
  58. err = binary.Write(buf, binary.LittleEndian, &flags)
  59. if err != nil {
  60. return err
  61. }
  62. if flags&fBeginXact != 0 {
  63. err = binary.Write(buf, binary.LittleEndian, &isolation)
  64. if err != nil {
  65. return err
  66. }
  67. err = writeBVarChar(buf, name)
  68. if err != nil {
  69. return err
  70. }
  71. }
  72. return buf.FinishPacket()
  73. }
  74. func sendRollbackXact(buf *tdsBuffer, headers []headerStruct, name string, flags uint8, isolation uint8, newname string, resetSession bool) error {
  75. buf.BeginPacket(packTransMgrReq, resetSession)
  76. writeAllHeaders(buf, headers)
  77. var rqtype uint16 = tmRollbackXact
  78. err := binary.Write(buf, binary.LittleEndian, &rqtype)
  79. if err != nil {
  80. return err
  81. }
  82. err = writeBVarChar(buf, name)
  83. if err != nil {
  84. return err
  85. }
  86. err = binary.Write(buf, binary.LittleEndian, &flags)
  87. if err != nil {
  88. return err
  89. }
  90. if flags&fBeginXact != 0 {
  91. err = binary.Write(buf, binary.LittleEndian, &isolation)
  92. if err != nil {
  93. return err
  94. }
  95. err = writeBVarChar(buf, name)
  96. if err != nil {
  97. return err
  98. }
  99. }
  100. return buf.FinishPacket()
  101. }