-
Notifications
You must be signed in to change notification settings - Fork 4
/
stmt.go
125 lines (100 loc) · 3.85 KB
/
stmt.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
123
124
125
// Copyright 2022 lesismal. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package sqlw
import (
"context"
"database/sql"
"fmt"
"reflect"
)
type Stmt struct {
*DB
*sql.Stmt
query string
}
func (stmt *Stmt) Sql(ctx context.Context, dst interface{}, args ...interface{}) string {
return stmt.query
}
func (stmt *Stmt) ExecContext(ctx context.Context, args ...interface{}) (Result, error) {
result, err := stmt.Stmt.ExecContext(ctx, args...)
return newResult(result, stmt.query, args), err
}
func (stmt *Stmt) Exec(args ...interface{}) (Result, error) {
return stmt.ExecContext(stmt.ctx, args...)
}
func (stmt *Stmt) QueryRowContext(ctx context.Context, dst interface{}, args ...interface{}) (Result, error) {
if dst == nil {
return nil, fmt.Errorf("[sqlw %v] invalid dest value nil: %v", opTypSelect, reflect.TypeOf(dst))
}
rows, err := stmt.Stmt.QueryContext(ctx, args...)
if err != nil {
return nil, err
}
defer rows.Close()
err = rowsToStruct(rows, dst, stmt.parseFieldName, stmt.mapping, sqlMappingKey(opTypSelect, stmt.query, reflect.TypeOf(dst)), stmt.rawScan)
return newResult(nil, stmt.query, args), err
}
func (stmt *Stmt) QueryRow(dst interface{}, args ...interface{}) (Result, error) {
return stmt.QueryRowContext(stmt.ctx, dst, args...)
}
func (stmt *Stmt) QueryContext(ctx context.Context, dst interface{}, args ...interface{}) (Result, error) {
rows, err := stmt.Stmt.QueryContext(ctx, args...)
if err != nil {
return nil, err
}
defer rows.Close()
if isStructPtr(reflect.TypeOf(dst)) {
err = rowsToStruct(rows, dst, stmt.parseFieldName, stmt.mapping, sqlMappingKey(opTypSelect, stmt.query, reflect.TypeOf(dst)), stmt.rawScan)
return newResult(nil, stmt.query, args), err
}
err = rowsToSlice(rows, dst, stmt.parseFieldName, stmt.mapping, sqlMappingKey(opTypSelect, stmt.query, reflect.TypeOf(dst)), stmt.rawScan)
return newResult(nil, stmt.query, args), err
}
func (stmt *Stmt) Query(dst interface{}, args ...interface{}) (Result, error) {
return stmt.QueryContext(stmt.ctx, dst, args...)
}
func (stmt *Stmt) SelectContext(ctx context.Context, dst interface{}, args ...interface{}) (Result, error) {
return stmt.QueryContext(ctx, dst, args...)
}
func (stmt *Stmt) Select(dst interface{}, args ...interface{}) (Result, error) {
return stmt.QueryContext(stmt.ctx, dst, args...)
}
// deprecated.
// func (stmt *Stmt) SelectOneContext(ctx context.Context, dst interface{}, args ...interface{}) (Result, error) {
// typ := reflect.TypeOf(dst)
// if !isStructPtr(typ) {
// return newResult(nil, stmt.query, args), fmt.Errorf("[sqlw %v] invalid dest type: %v", opTypSelect, typ)
// }
// return stmt.SelectContext(ctx, dst, args...)
// }
// deprecated.
// func (stmt *Stmt) SelectOne(dst interface{}, args ...interface{}) (Result, error) {
// return stmt.SelectOneContext(stmt.ctx , dst, args...)
// }
func (stmt *Stmt) InsertContext(ctx context.Context, args ...interface{}) (Result, error) {
return insertContext(ctx, nil, stmt, stmt.query, stmt.DB, args...)
}
func (stmt *Stmt) Insert(args ...interface{}) (Result, error) {
return stmt.InsertContext(stmt.ctx, args...)
}
func (stmt *Stmt) UpdateContext(ctx context.Context, args ...interface{}) (Result, error) {
return updateByExecContext(ctx, nil, stmt.DB, stmt, stmt.query, args...)
}
func (stmt *Stmt) Update(args ...interface{}) (Result, error) {
return stmt.UpdateContext(stmt.ctx, args...)
}
func (stmt *Stmt) DeleteContext(ctx context.Context, args ...interface{}) (Result, error) {
result, err := stmt.Stmt.ExecContext(ctx, args...)
return newResult(result, stmt.query, args), err
}
func (stmt *Stmt) Delete(args ...interface{}) (Result, error) {
return stmt.DeleteContext(stmt.ctx, args...)
}
func NewStmt(db *DB, stmt *sql.Stmt, query string) *Stmt {
return &Stmt{
DB: db,
Stmt: stmt,
query: query,
}
}