|
2 | 2 | // Use of this source code is governed by a MIT-style
|
3 | 3 | // license that can be found in the LICENSE file.
|
4 | 4 |
|
5 |
| -// Package gorp provides a simple way to marshal Go structs to and from |
6 |
| -// SQL databases. It uses the database/sql package, and should work with any |
7 |
| -// compliant database/sql driver. |
8 |
| -// |
9 |
| -// Source code and project home: |
10 |
| -// https://github.com/go-gorp/gorp |
11 |
| -// |
12 | 5 | package gorp
|
13 | 6 |
|
14 | 7 | import (
|
@@ -183,6 +176,19 @@ func extractDbMap(e SqlExecutor) *DbMap {
|
183 | 176 | return nil
|
184 | 177 | }
|
185 | 178 |
|
| 179 | +// executor exposes the sql.DB and sql.Tx functions so that it can be used |
| 180 | +// on internal functions that need to be agnostic to the underlying object. |
| 181 | +type executor interface { |
| 182 | + Exec(query string, args ...interface{}) (sql.Result, error) |
| 183 | + Prepare(query string) (*sql.Stmt, error) |
| 184 | + QueryRow(query string, args ...interface{}) *sql.Row |
| 185 | + Query(query string, args ...interface{}) (*sql.Rows, error) |
| 186 | + ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) |
| 187 | + PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) |
| 188 | + QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row |
| 189 | + QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) |
| 190 | +} |
| 191 | + |
186 | 192 | func extractExecutorAndContext(e SqlExecutor) (executor, context.Context) {
|
187 | 193 | switch m := e.(type) {
|
188 | 194 | case *DbMap:
|
@@ -616,3 +622,51 @@ func insert(m *DbMap, exec SqlExecutor, list ...interface{}) error {
|
616 | 622 | }
|
617 | 623 | return nil
|
618 | 624 | }
|
| 625 | + |
| 626 | +func exec(e SqlExecutor, query string, args ...interface{}) (sql.Result, error) { |
| 627 | + executor, ctx := extractExecutorAndContext(e) |
| 628 | + |
| 629 | + if ctx != nil { |
| 630 | + return executor.ExecContext(ctx, query, args...) |
| 631 | + } |
| 632 | + |
| 633 | + return executor.Exec(query, args...) |
| 634 | +} |
| 635 | + |
| 636 | +func prepare(e SqlExecutor, query string) (*sql.Stmt, error) { |
| 637 | + executor, ctx := extractExecutorAndContext(e) |
| 638 | + |
| 639 | + if ctx != nil { |
| 640 | + return executor.PrepareContext(ctx, query) |
| 641 | + } |
| 642 | + |
| 643 | + return executor.Prepare(query) |
| 644 | +} |
| 645 | + |
| 646 | +func queryRow(e SqlExecutor, query string, args ...interface{}) *sql.Row { |
| 647 | + executor, ctx := extractExecutorAndContext(e) |
| 648 | + |
| 649 | + if ctx != nil { |
| 650 | + return executor.QueryRowContext(ctx, query, args...) |
| 651 | + } |
| 652 | + |
| 653 | + return executor.QueryRow(query, args...) |
| 654 | +} |
| 655 | + |
| 656 | +func query(e SqlExecutor, query string, args ...interface{}) (*sql.Rows, error) { |
| 657 | + executor, ctx := extractExecutorAndContext(e) |
| 658 | + |
| 659 | + if ctx != nil { |
| 660 | + return executor.QueryContext(ctx, query, args...) |
| 661 | + } |
| 662 | + |
| 663 | + return executor.Query(query, args...) |
| 664 | +} |
| 665 | + |
| 666 | +func begin(m *DbMap) (*sql.Tx, error) { |
| 667 | + if m.ctx != nil { |
| 668 | + return m.Db.BeginTx(m.ctx, nil) |
| 669 | + } |
| 670 | + |
| 671 | + return m.Db.Begin() |
| 672 | +} |
0 commit comments