本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
4.0KB

  1. // Copyright 2019 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package core
  5. import (
  6. "context"
  7. "database/sql"
  8. )
  9. type Tx struct {
  10. *sql.Tx
  11. db *DB
  12. }
  13. func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
  14. tx, err := db.DB.BeginTx(ctx, opts)
  15. if err != nil {
  16. return nil, err
  17. }
  18. return &Tx{tx, db}, nil
  19. }
  20. func (db *DB) Begin() (*Tx, error) {
  21. tx, err := db.DB.Begin()
  22. if err != nil {
  23. return nil, err
  24. }
  25. return &Tx{tx, db}, nil
  26. }
  27. func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error) {
  28. names := make(map[string]int)
  29. var i int
  30. query = re.ReplaceAllStringFunc(query, func(src string) string {
  31. names[src[1:]] = i
  32. i += 1
  33. return "?"
  34. })
  35. stmt, err := tx.Tx.PrepareContext(ctx, query)
  36. if err != nil {
  37. return nil, err
  38. }
  39. return &Stmt{stmt, tx.db, names}, nil
  40. }
  41. func (tx *Tx) Prepare(query string) (*Stmt, error) {
  42. return tx.PrepareContext(context.Background(), query)
  43. }
  44. func (tx *Tx) StmtContext(ctx context.Context, stmt *Stmt) *Stmt {
  45. stmt.Stmt = tx.Tx.StmtContext(ctx, stmt.Stmt)
  46. return stmt
  47. }
  48. func (tx *Tx) Stmt(stmt *Stmt) *Stmt {
  49. return tx.StmtContext(context.Background(), stmt)
  50. }
  51. func (tx *Tx) ExecMapContext(ctx context.Context, query string, mp interface{}) (sql.Result, error) {
  52. query, args, err := MapToSlice(query, mp)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return tx.Tx.ExecContext(ctx, query, args...)
  57. }
  58. func (tx *Tx) ExecMap(query string, mp interface{}) (sql.Result, error) {
  59. return tx.ExecMapContext(context.Background(), query, mp)
  60. }
  61. func (tx *Tx) ExecStructContext(ctx context.Context, query string, st interface{}) (sql.Result, error) {
  62. query, args, err := StructToSlice(query, st)
  63. if err != nil {
  64. return nil, err
  65. }
  66. return tx.Tx.ExecContext(ctx, query, args...)
  67. }
  68. func (tx *Tx) ExecStruct(query string, st interface{}) (sql.Result, error) {
  69. return tx.ExecStructContext(context.Background(), query, st)
  70. }
  71. func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) {
  72. rows, err := tx.Tx.QueryContext(ctx, query, args...)
  73. if err != nil {
  74. return nil, err
  75. }
  76. return &Rows{rows, tx.db}, nil
  77. }
  78. func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) {
  79. return tx.QueryContext(context.Background(), query, args...)
  80. }
  81. func (tx *Tx) QueryMapContext(ctx context.Context, query string, mp interface{}) (*Rows, error) {
  82. query, args, err := MapToSlice(query, mp)
  83. if err != nil {
  84. return nil, err
  85. }
  86. return tx.QueryContext(ctx, query, args...)
  87. }
  88. func (tx *Tx) QueryMap(query string, mp interface{}) (*Rows, error) {
  89. return tx.QueryMapContext(context.Background(), query, mp)
  90. }
  91. func (tx *Tx) QueryStructContext(ctx context.Context, query string, st interface{}) (*Rows, error) {
  92. query, args, err := StructToSlice(query, st)
  93. if err != nil {
  94. return nil, err
  95. }
  96. return tx.QueryContext(ctx, query, args...)
  97. }
  98. func (tx *Tx) QueryStruct(query string, st interface{}) (*Rows, error) {
  99. return tx.QueryStructContext(context.Background(), query, st)
  100. }
  101. func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row {
  102. rows, err := tx.QueryContext(ctx, query, args...)
  103. return &Row{rows, err}
  104. }
  105. func (tx *Tx) QueryRow(query string, args ...interface{}) *Row {
  106. return tx.QueryRowContext(context.Background(), query, args...)
  107. }
  108. func (tx *Tx) QueryRowMapContext(ctx context.Context, query string, mp interface{}) *Row {
  109. query, args, err := MapToSlice(query, mp)
  110. if err != nil {
  111. return &Row{nil, err}
  112. }
  113. return tx.QueryRowContext(ctx, query, args...)
  114. }
  115. func (tx *Tx) QueryRowMap(query string, mp interface{}) *Row {
  116. return tx.QueryRowMapContext(context.Background(), query, mp)
  117. }
  118. func (tx *Tx) QueryRowStructContext(ctx context.Context, query string, st interface{}) *Row {
  119. query, args, err := StructToSlice(query, st)
  120. if err != nil {
  121. return &Row{nil, err}
  122. }
  123. return tx.QueryRowContext(ctx, query, args...)
  124. }
  125. func (tx *Tx) QueryRowStruct(query string, st interface{}) *Row {
  126. return tx.QueryRowStructContext(context.Background(), query, st)
  127. }
上海开阖软件有限公司 沪ICP备12045867号-1