Skip to content

Commit fa2e709

Browse files
committed
add db help package to contain some help function
1 parent 609aefe commit fa2e709

3 files changed

Lines changed: 115 additions & 1 deletion

File tree

core.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ type Queryable interface {
3838
NamedQuery(string, interface{}) (*Rows, error)
3939
InGet(any, string, ...any) error
4040
InSelect(any, string, ...any) error
41-
InExec(query string, args ...any) (sql.Result, error)
41+
InExec(string, ...any) (sql.Result, error)
4242
MustInExec(string, ...any) sql.Result
4343
}

db/db.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package db
2+
3+
import (
4+
"context"
5+
6+
"github.com/bitbus/sqlx"
7+
)
8+
9+
// StructScan[T] all rows from an sql.Rows or an sqlx.Rows into the dest slice.
10+
// StructScan[T] will scan in the entire rows result, so if you do not want to
11+
// allocate structs for the entire result, use Queryx and see sqlx.Rows.StructScan.
12+
// If rows is sqlx.Rows, it will use its mapper, otherwise it will use the default.
13+
func StructScan[T any](rows *sqlx.Rows) (dest *T, err error) {
14+
dest = new(T)
15+
err = sqlx.StructScan(rows, dest)
16+
return
17+
}
18+
19+
// Get[T] using the prepared statement.
20+
// Any placeholder parameters are replaced with supplied args.
21+
// An error is returned if the result set is empty.
22+
func Get[T any](stmt *sqlx.Stmt, args ...any) (dest *T, err error) {
23+
dest = new(T)
24+
err = stmt.Get(dest, args...)
25+
return
26+
}
27+
28+
// GetContext[T] using the prepared statement.
29+
// Any placeholder parameters are replaced with supplied args.
30+
// An error is returned if the result set is empty.
31+
func GetContext[T any](ctx context.Context, stmt *sqlx.Stmt, args ...any) (dest *T, err error) {
32+
dest = new(T)
33+
err = stmt.GetContext(ctx, dest, args...)
34+
return
35+
}
36+
37+
// Select[T] using the prepared statement.
38+
// Any placeholder parameters are replaced with supplied args.
39+
func Select[T any](stmt *sqlx.Stmt, args ...any) (dest *T, err error) {
40+
dest = new(T)
41+
err = stmt.Select(dest, args...)
42+
return
43+
}
44+
45+
// SelectContext[T] using the prepared statement.
46+
// Any placeholder parameters are replaced with supplied args.
47+
func SelectContext[T any](ctx context.Context, stmt *sqlx.Stmt, args ...any) (dest *T, err error) {
48+
dest = new(T)
49+
err = stmt.SelectContext(ctx, dest, args...)
50+
return
51+
}
52+
53+
// NamedGet[T] using this NamedStmt
54+
// Any named placeholder parameters are replaced with fields from arg.
55+
func NamedGet[T any](stmt *sqlx.NamedStmt, arg any) (dest *T, err error) {
56+
dest = new(T)
57+
err = stmt.Get(dest, arg)
58+
return
59+
}
60+
61+
// NamedGetContext using this NamedStmt
62+
// Any named placeholder parameters are replaced with fields from arg.
63+
func NamedGetContext[T any](ctx context.Context, stmt *sqlx.NamedStmt, arg any) (dest *T, err error) {
64+
dest = new(T)
65+
err = stmt.GetContext(ctx, dest, arg)
66+
return
67+
}
68+
69+
// NamedSelect using this NamedStmt
70+
// Any named placeholder parameters are replaced with fields from arg.
71+
func NamedSelect[T any](stmt *sqlx.NamedStmt, arg any) (dest *T, err error) {
72+
dest = new(T)
73+
err = stmt.Select(dest, arg)
74+
return
75+
}
76+
77+
// NamedSelectContext using this NamedStmt
78+
// Any named placeholder parameters are replaced with fields from arg.
79+
func NamedSelectContext[T any](ctx context.Context, stmt *sqlx.NamedStmt, arg any) (dest *T, err error) {
80+
dest = new(T)
81+
err = stmt.SelectContext(ctx, dest, arg)
82+
return
83+
}
84+
85+
// InGet[T] for in scene does a QueryRow using the provided Queryer, and scans the resulting row
86+
// to dest. If dest is scannable, the result must only have one column. Otherwise,
87+
// StructScan is used. Get will return sql.ErrNoRows like row.Scan would.
88+
// Any placeholder parameters are replaced with supplied args.
89+
// An error is returned if the result set is empty.
90+
func InGet[T any](q sqlx.Queryable, query string, args ...any) (dest *T, err error) {
91+
dest = new(T)
92+
err = q.InGet(*dest, query, args...)
93+
return
94+
}
95+
96+
// InSelect[T] for in scene executes a query using the provided Queryer, and StructScans each row
97+
// into dest, which must be a slice. If the slice elements are scannable, then
98+
// the result set must have only one column. Otherwise, StructScan is used.
99+
// The *sql.Rows are closed automatically.
100+
// Any placeholder parameters are replaced with supplied args.
101+
func InSelect[T any](q sqlx.Queryable, query string, args ...any) (dest *T, err error) {
102+
dest = new(T)
103+
err = q.InSelect(dest, query, args...)
104+
return
105+
}

db/de_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package db
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestUsage(t *testing.T) {
8+
// TODO: add usage test case here.
9+
}

0 commit comments

Comments
 (0)