-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_instruction.go
122 lines (99 loc) · 3.29 KB
/
multi_instruction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package nansql
import (
"context"
"database/sql"
"github.com/jmoiron/sqlx"
)
// Transaction is an interface that defines the methods for managing transactions.
type Transaction interface {
Begin(ctx context.Context) error
Commit(ctx context.Context) error
Rollback(ctx context.Context) error
}
// MultiInstruction is a struct that provides methods for executing multiple SQL instructions within a transaction.
type MultiInstruction struct {
db *sqlx.DB
tx *sqlx.Tx
}
// NewMultiInstruction creates a new instance of MultiInstruction.
func NewMultiInstruction(db *sqlx.DB) *MultiInstruction {
return &MultiInstruction{
db: db,
tx: nil,
}
}
// Begin starts a new transaction.
func (t *MultiInstruction) Begin(ctx context.Context) error {
tx, err := t.db.BeginTxx(ctx, nil)
if err != nil {
return err
}
t.tx = tx
return nil
}
// Commit commits the transaction.
func (t *MultiInstruction) Commit(ctx context.Context) error {
err := t.tx.Commit()
if err != nil {
return err
}
return nil
}
// Rollback rolls back the transaction.
func (t *MultiInstruction) Rollback(ctx context.Context) error {
err := t.tx.Rollback()
if err != nil {
return err
}
return nil
}
// Query executes a query that returns multiple rows.
func (t *MultiInstruction) Query(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error) {
return t.tx.QueryxContext(ctx, query, args...)
}
// QueryRow executes a query that returns a single row.
func (t *MultiInstruction) QueryRow(ctx context.Context, query string, args ...interface{}) *sqlx.Row {
return t.tx.QueryRowxContext(ctx, query, args...)
}
// Exec executes a query that does not return any rows.
func (t *MultiInstruction) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
return t.tx.ExecContext(ctx, query, args...)
}
// Prepare prepares a statement for execution.
func (t *MultiInstruction) Prepare(ctx context.Context, query string) (*sqlx.Stmt, error) {
return t.tx.PreparexContext(ctx, query)
}
// Select executes a query that selects multiple rows and stores the result in the provided slice.
func (t *MultiInstruction) Select(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
return t.tx.SelectContext(ctx, dest, query, args...)
}
// Get executes a query that selects a single row and stores the result in the provided struct.
func (t *MultiInstruction) Get(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
return t.tx.GetContext(ctx, dest, query, args...)
}
// CommitAndClose commits the transaction and closes it.
func (t *MultiInstruction) CommitAndClose(ctx context.Context) error {
err := t.tx.Commit()
if err != nil {
return err
}
t.tx = nil
return nil
}
// RollbackAndClose rolls back the transaction and closes it.
func (t *MultiInstruction) RollbackAndClose(ctx context.Context) error {
err := t.tx.Rollback()
if err != nil {
return err
}
t.tx = nil
return nil
}
// Rebind replaces placeholders in the query with the appropriate dialect-specific sequence.
func (t *MultiInstruction) Rebind(query string) string {
return t.tx.Rebind(query)
}
// NamedExec executes a named query.
func (t *MultiInstruction) NamedExec(ctx context.Context, query string, arg interface{}) (sql.Result, error) {
return t.tx.NamedExecContext(ctx, query, arg)
}