forked from go-gorp/gorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgorp_go18.go
81 lines (62 loc) · 2.23 KB
/
gorp_go18.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
// Copyright 2012 James Cooper. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// Package gorp provides a simple way to marshal Go structs to and from
// SQL databases. It uses the database/sql package, and should work with any
// compliant database/sql driver.
//
// Source code and project home:
// https://github.com/go-gorp/gorp
//
// +build go1.8
package gorp
import (
"context"
"database/sql"
)
// executor exposes the sql.DB and sql.Tx functions so that it can be used
// on internal functions that need to be agnostic to the underlying object.
type executor interface {
Exec(query string, args ...interface{}) (sql.Result, error)
Prepare(query string) (*sql.Stmt, error)
QueryRow(query string, args ...interface{}) *sql.Row
Query(query string, args ...interface{}) (*sql.Rows, error)
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
}
func exec(e SqlExecutor, query string, args ...interface{}) (sql.Result, error) {
executor, ctx := extractExecutorAndContext(e)
if ctx != nil {
return executor.ExecContext(ctx, query, args...)
}
return executor.Exec(query, args...)
}
func prepare(e SqlExecutor, query string) (*sql.Stmt, error) {
executor, ctx := extractExecutorAndContext(e)
if ctx != nil {
return executor.PrepareContext(ctx, query)
}
return executor.Prepare(query)
}
func queryRow(e SqlExecutor, query string, args ...interface{}) *sql.Row {
executor, ctx := extractExecutorAndContext(e)
if ctx != nil {
return executor.QueryRowContext(ctx, query, args...)
}
return executor.QueryRow(query, args...)
}
func query(e SqlExecutor, query string, args ...interface{}) (*sql.Rows, error) {
executor, ctx := extractExecutorAndContext(e)
if ctx != nil {
return executor.QueryContext(ctx, query, args...)
}
return executor.Query(query, args...)
}
func begin(m *DbMap) (*sql.Tx, error) {
if m.ctx != nil {
return m.Db.BeginTx(m.ctx, nil)
}
return m.Db.Begin()
}