Skip to content

Commit 1494361

Browse files
committed
added db.QueryValue
1 parent f54d157 commit 1494361

File tree

5 files changed

+43
-12
lines changed

5 files changed

+43
-12
lines changed

db/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ var (
1414
)
1515

1616
var (
17-
conn = sqldb.ConnectionWithError(
17+
globalConn = sqldb.ConnectionWithError(
1818
context.Background(),
1919
errors.New("database connection not initialized"),
2020
)
21-
connCtxKey int
21+
globalConnCtxKey int
2222
serializedTransactionCtxKey int
2323
)

db/conn.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ func SetConn(c sqldb.Connection) {
1414
if c == nil {
1515
panic("must not set nil sqldb.Connection")
1616
}
17-
conn = c
17+
globalConn = c
1818
}
1919

2020
// Conn returns a non nil sqldb.Connection from ctx
2121
// or the global connection set with SetConn.
2222
// The returned connection will use the passed context.
2323
// See sqldb.Connection.WithContext
2424
func Conn(ctx context.Context) sqldb.Connection {
25-
return ConnDefault(ctx, conn)
25+
return ConnDefault(ctx, globalConn)
2626
}
2727

2828
// ConnDefault returns a non nil sqldb.Connection from ctx
2929
// or the passed defaultConn.
3030
// The returned connection will use the passed context.
3131
// See sqldb.Connection.WithContext
3232
func ConnDefault(ctx context.Context, defaultConn sqldb.Connection) sqldb.Connection {
33-
c, _ := ctx.Value(&connCtxKey).(sqldb.Connection)
33+
c, _ := ctx.Value(&globalConnCtxKey).(sqldb.Connection)
3434
if c == nil {
3535
c = defaultConn
3636
}
@@ -45,7 +45,7 @@ func ConnDefault(ctx context.Context, defaultConn sqldb.Connection) sqldb.Connec
4545
// Passing a nil connection causes Conn(ctx)
4646
// to return the global connection set with SetConn.
4747
func ContextWithConn(ctx context.Context, conn sqldb.Connection) context.Context {
48-
return context.WithValue(ctx, &connCtxKey, conn)
48+
return context.WithValue(ctx, &globalConnCtxKey, conn)
4949
}
5050

5151
// ContextWithoutCancel returns a new context that inherits

db/querystruct.go renamed to db/query.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,41 @@ import (
55
"errors"
66
"fmt"
77
"reflect"
8+
"time"
89

910
"github.com/domonda/go-sqldb"
1011
)
1112

13+
// Now returns the result of the SQL now()
14+
// function for the current connection.
15+
// Useful for getting the timestamp of a
16+
// SQL transaction for use in Go code.
17+
func Now(ctx context.Context) (time.Time, error) {
18+
return Conn(ctx).Now()
19+
}
20+
21+
// Exec executes a query with optional args.
22+
func Exec(ctx context.Context, query string, args ...any) error {
23+
return Conn(ctx).Exec(query, args...)
24+
}
25+
26+
// QueryRow queries a single row and returns a RowScanner for the results.
27+
func QueryRow(ctx context.Context, query string, args ...any) sqldb.RowScanner {
28+
return Conn(ctx).QueryRow(query, args...)
29+
}
30+
31+
// QueryRows queries multiple rows and returns a RowsScanner for the results.
32+
func QueryRows(ctx context.Context, query string, args ...any) sqldb.RowsScanner {
33+
return Conn(ctx).QueryRows(query, args...)
34+
}
35+
36+
// QueryValue queries a single value of type T.
37+
func QueryValue[T any](ctx context.Context, query string, args ...any) (T, error) {
38+
var val T
39+
err := Conn(ctx).QueryRow(query, args...).Scan(&val)
40+
return val, err
41+
}
42+
1243
// QueryStruct uses the passed pkValues to query a table row
1344
// and scan it into a struct of type S that must have tagged fields
1445
// with primary key flags to identify the primary key column names

db/transaction.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func DebugNoTransaction(ctx context.Context, nonTxFunc func(context.Context) err
5151
// Recovered panics are re-paniced and rollback errors after a panic are logged with ErrLogger.
5252
func IsolatedTransaction(ctx context.Context, txFunc func(context.Context) error) error {
5353
return sqldb.IsolatedTransaction(Conn(ctx), nil, func(tx sqldb.Connection) error {
54-
return txFunc(context.WithValue(ctx, &connCtxKey, tx))
54+
return txFunc(context.WithValue(ctx, &globalConnCtxKey, tx))
5555
})
5656
}
5757

@@ -64,7 +64,7 @@ func IsolatedTransaction(ctx context.Context, txFunc func(context.Context) error
6464
// Recovered panics are re-paniced and rollback errors after a panic are logged with sqldb.ErrLogger.
6565
func Transaction(ctx context.Context, txFunc func(context.Context) error) error {
6666
return sqldb.Transaction(Conn(ctx), nil, func(tx sqldb.Connection) error {
67-
return txFunc(context.WithValue(ctx, &connCtxKey, tx))
67+
return txFunc(context.WithValue(ctx, &globalConnCtxKey, tx))
6868
})
6969
}
7070

@@ -135,7 +135,7 @@ func SerializedTransaction(ctx context.Context, txFunc func(context.Context) err
135135
// Recovered panics are re-paniced and rollback errors after a panic are logged with sqldb.ErrLogger.
136136
func TransactionOpts(ctx context.Context, opts *sql.TxOptions, txFunc func(context.Context) error) error {
137137
return sqldb.Transaction(Conn(ctx), opts, func(tx sqldb.Connection) error {
138-
return txFunc(context.WithValue(ctx, &connCtxKey, tx))
138+
return txFunc(context.WithValue(ctx, &globalConnCtxKey, tx))
139139
})
140140
}
141141

@@ -149,7 +149,7 @@ func TransactionOpts(ctx context.Context, opts *sql.TxOptions, txFunc func(conte
149149
func TransactionReadOnly(ctx context.Context, txFunc func(context.Context) error) error {
150150
opts := sql.TxOptions{ReadOnly: true}
151151
return sqldb.Transaction(Conn(ctx), &opts, func(tx sqldb.Connection) error {
152-
return txFunc(context.WithValue(ctx, &connCtxKey, tx))
152+
return txFunc(context.WithValue(ctx, &globalConnCtxKey, tx))
153153
})
154154
}
155155

db/transaction_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func TestSerializedTransaction(t *testing.T) {
13-
conn = mockconn.New(context.Background(), os.Stdout, nil)
13+
globalConn = mockconn.New(context.Background(), os.Stdout, nil)
1414

1515
expectSerialized := func(ctx context.Context) error {
1616
if !Conn(ctx).IsTransaction() {
@@ -64,7 +64,7 @@ func TestSerializedTransaction(t *testing.T) {
6464
}
6565

6666
func TestTransaction(t *testing.T) {
67-
conn = mockconn.New(context.Background(), os.Stdout, nil)
67+
globalConn = mockconn.New(context.Background(), os.Stdout, nil)
6868

6969
expectNonSerialized := func(ctx context.Context) error {
7070
if !Conn(ctx).IsTransaction() {

0 commit comments

Comments
 (0)