@@ -22,6 +22,7 @@ type Tx struct {
2222 ctx context.Context
2323}
2424
25+ // BeginTx begin a transaction with option
2526func (db * DB ) BeginTx (ctx context.Context , opts * sql.TxOptions ) (* Tx , error ) {
2627 hookCtx := contexts .NewContextHook (ctx , "BEGIN TRANSACTION" , nil )
2728 ctx , err := db .beforeProcess (hookCtx )
@@ -36,10 +37,12 @@ func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
3637 return & Tx {tx , db , ctx }, nil
3738}
3839
40+ // Begin begins a transaction
3941func (db * DB ) Begin () (* Tx , error ) {
4042 return db .BeginTx (context .Background (), nil )
4143}
4244
45+ // Commit submit the transaction
4346func (tx * Tx ) Commit () error {
4447 hookCtx := contexts .NewContextHook (tx .ctx , "COMMIT" , nil )
4548 ctx , err := tx .db .beforeProcess (hookCtx )
@@ -54,6 +57,7 @@ func (tx *Tx) Commit() error {
5457 return nil
5558}
5659
60+ // Rollback rollback the transaction
5761func (tx * Tx ) Rollback () error {
5862 hookCtx := contexts .NewContextHook (tx .ctx , "ROLLBACK" , nil )
5963 ctx , err := tx .db .beforeProcess (hookCtx )
@@ -68,6 +72,7 @@ func (tx *Tx) Rollback() error {
6872 return nil
6973}
7074
75+ // PrepareContext prepare the query
7176func (tx * Tx ) PrepareContext (ctx context.Context , query string ) (* Stmt , error ) {
7277 names := make (map [string ]int )
7378 var i int
@@ -89,19 +94,23 @@ func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error) {
8994 return & Stmt {stmt , tx .db , names , query }, nil
9095}
9196
97+ // Prepare prepare the query
9298func (tx * Tx ) Prepare (query string ) (* Stmt , error ) {
9399 return tx .PrepareContext (context .Background (), query )
94100}
95101
102+ // StmtContext creates Stmt with context
96103func (tx * Tx ) StmtContext (ctx context.Context , stmt * Stmt ) * Stmt {
97104 stmt .Stmt = tx .Tx .StmtContext (ctx , stmt .Stmt )
98105 return stmt
99106}
100107
108+ // Stmt creates Stmt
101109func (tx * Tx ) Stmt (stmt * Stmt ) * Stmt {
102110 return tx .StmtContext (context .Background (), stmt )
103111}
104112
113+ // ExecMapContext executes query with args in a map
105114func (tx * Tx ) ExecMapContext (ctx context.Context , query string , mp interface {}) (sql.Result , error ) {
106115 query , args , err := MapToSlice (query , mp )
107116 if err != nil {
@@ -110,10 +119,12 @@ func (tx *Tx) ExecMapContext(ctx context.Context, query string, mp interface{})
110119 return tx .ExecContext (ctx , query , args ... )
111120}
112121
122+ // ExecMap executes query with args in a map
113123func (tx * Tx ) ExecMap (query string , mp interface {}) (sql.Result , error ) {
114124 return tx .ExecMapContext (context .Background (), query , mp )
115125}
116126
127+ // ExecStructContext executes query with args in a struct
117128func (tx * Tx ) ExecStructContext (ctx context.Context , query string , st interface {}) (sql.Result , error ) {
118129 query , args , err := StructToSlice (query , st )
119130 if err != nil {
@@ -122,6 +133,7 @@ func (tx *Tx) ExecStructContext(ctx context.Context, query string, st interface{
122133 return tx .ExecContext (ctx , query , args ... )
123134}
124135
136+ // ExecContext executes a query with args
125137func (tx * Tx ) ExecContext (ctx context.Context , query string , args ... interface {}) (sql.Result , error ) {
126138 hookCtx := contexts .NewContextHook (ctx , query , args )
127139 ctx , err := tx .db .beforeProcess (hookCtx )
@@ -136,10 +148,12 @@ func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{}
136148 return res , err
137149}
138150
151+ // ExecStruct executes query with args in a struct
139152func (tx * Tx ) ExecStruct (query string , st interface {}) (sql.Result , error ) {
140153 return tx .ExecStructContext (context .Background (), query , st )
141154}
142155
156+ // QueryContext query with args
143157func (tx * Tx ) QueryContext (ctx context.Context , query string , args ... interface {}) (* Rows , error ) {
144158 hookCtx := contexts .NewContextHook (ctx , query , args )
145159 ctx , err := tx .db .beforeProcess (hookCtx )
@@ -157,10 +171,12 @@ func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{
157171 return & Rows {rows , tx .db }, nil
158172}
159173
174+ // Query query with args
160175func (tx * Tx ) Query (query string , args ... interface {}) (* Rows , error ) {
161176 return tx .QueryContext (context .Background (), query , args ... )
162177}
163178
179+ // QueryMapContext query with args in a map
164180func (tx * Tx ) QueryMapContext (ctx context.Context , query string , mp interface {}) (* Rows , error ) {
165181 query , args , err := MapToSlice (query , mp )
166182 if err != nil {
@@ -169,10 +185,12 @@ func (tx *Tx) QueryMapContext(ctx context.Context, query string, mp interface{})
169185 return tx .QueryContext (ctx , query , args ... )
170186}
171187
188+ // QueryMap query with args in a map
172189func (tx * Tx ) QueryMap (query string , mp interface {}) (* Rows , error ) {
173190 return tx .QueryMapContext (context .Background (), query , mp )
174191}
175192
193+ // QueryStructContext query with args in struct
176194func (tx * Tx ) QueryStructContext (ctx context.Context , query string , st interface {}) (* Rows , error ) {
177195 query , args , err := StructToSlice (query , st )
178196 if err != nil {
@@ -181,19 +199,23 @@ func (tx *Tx) QueryStructContext(ctx context.Context, query string, st interface
181199 return tx .QueryContext (ctx , query , args ... )
182200}
183201
202+ // QueryStruct query with args in struct
184203func (tx * Tx ) QueryStruct (query string , st interface {}) (* Rows , error ) {
185204 return tx .QueryStructContext (context .Background (), query , st )
186205}
187206
207+ // QueryRowContext query one row with args
188208func (tx * Tx ) QueryRowContext (ctx context.Context , query string , args ... interface {}) * Row {
189209 rows , err := tx .QueryContext (ctx , query , args ... )
190210 return & Row {rows , err }
191211}
192212
213+ // QueryRow query one row with args
193214func (tx * Tx ) QueryRow (query string , args ... interface {}) * Row {
194215 return tx .QueryRowContext (context .Background (), query , args ... )
195216}
196217
218+ // QueryRowMapContext query one row with args in a map
197219func (tx * Tx ) QueryRowMapContext (ctx context.Context , query string , mp interface {}) * Row {
198220 query , args , err := MapToSlice (query , mp )
199221 if err != nil {
@@ -202,10 +224,12 @@ func (tx *Tx) QueryRowMapContext(ctx context.Context, query string, mp interface
202224 return tx .QueryRowContext (ctx , query , args ... )
203225}
204226
227+ // QueryRowMap query one row with args in a map
205228func (tx * Tx ) QueryRowMap (query string , mp interface {}) * Row {
206229 return tx .QueryRowMapContext (context .Background (), query , mp )
207230}
208231
232+ // QueryRowStructContext query one row with args in struct
209233func (tx * Tx ) QueryRowStructContext (ctx context.Context , query string , st interface {}) * Row {
210234 query , args , err := StructToSlice (query , st )
211235 if err != nil {
@@ -214,6 +238,7 @@ func (tx *Tx) QueryRowStructContext(ctx context.Context, query string, st interf
214238 return tx .QueryRowContext (ctx , query , args ... )
215239}
216240
241+ // QueryRowStruct query one row with args in struct
217242func (tx * Tx ) QueryRowStruct (query string , st interface {}) * Row {
218243 return tx .QueryRowStructContext (context .Background (), query , st )
219244}
0 commit comments