tran.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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,
  26. name string) (err error) {
  27. buf.BeginPacket(packTransMgrReq)
  28. writeAllHeaders(buf, headers)
  29. var rqtype uint16 = tmBeginXact
  30. err = binary.Write(buf, binary.LittleEndian, &rqtype)
  31. if err != nil {
  32. return
  33. }
  34. err = binary.Write(buf, binary.LittleEndian, &isolation)
  35. if err != nil {
  36. return
  37. }
  38. err = writeBVarChar(buf, name)
  39. if err != nil {
  40. return
  41. }
  42. return buf.FinishPacket()
  43. }
  44. const (
  45. fBeginXact = 1
  46. )
  47. func sendCommitXact(buf *tdsBuffer, headers []headerStruct, name string, flags uint8, isolation uint8, newname string) error {
  48. buf.BeginPacket(packTransMgrReq)
  49. writeAllHeaders(buf, headers)
  50. var rqtype uint16 = tmCommitXact
  51. err := binary.Write(buf, binary.LittleEndian, &rqtype)
  52. if err != nil {
  53. return err
  54. }
  55. err = writeBVarChar(buf, name)
  56. if err != nil {
  57. return err
  58. }
  59. err = binary.Write(buf, binary.LittleEndian, &flags)
  60. if err != nil {
  61. return err
  62. }
  63. if flags&fBeginXact != 0 {
  64. err = binary.Write(buf, binary.LittleEndian, &isolation)
  65. if err != nil {
  66. return err
  67. }
  68. err = writeBVarChar(buf, name)
  69. if err != nil {
  70. return err
  71. }
  72. }
  73. return buf.FinishPacket()
  74. }
  75. func sendRollbackXact(buf *tdsBuffer, headers []headerStruct, name string, flags uint8, isolation uint8, newname string) error {
  76. buf.BeginPacket(packTransMgrReq)
  77. writeAllHeaders(buf, headers)
  78. var rqtype uint16 = tmRollbackXact
  79. err := binary.Write(buf, binary.LittleEndian, &rqtype)
  80. if err != nil {
  81. return err
  82. }
  83. err = writeBVarChar(buf, name)
  84. if err != nil {
  85. return err
  86. }
  87. err = binary.Write(buf, binary.LittleEndian, &flags)
  88. if err != nil {
  89. return err
  90. }
  91. if flags&fBeginXact != 0 {
  92. err = binary.Write(buf, binary.LittleEndian, &isolation)
  93. if err != nil {
  94. return err
  95. }
  96. err = writeBVarChar(buf, name)
  97. if err != nil {
  98. return err
  99. }
  100. }
  101. return buf.FinishPacket()
  102. }