Skip to content

Commit 79a7ad5

Browse files
committed
Extended postgres interface
1 parent c80ef0b commit 79a7ad5

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

pkg/database/postgres/interface.go

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ type DB interface {
2121
optionsAndArgs ...any,
2222
) (pgx.Rows, error)
2323
QueryRow(ctx context.Context, sql string, optionsAndArgs ...any) pgx.Row
24+
SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults
2425
Begin(ctx context.Context) (pgx.Tx, error)
26+
BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)
2527
Ping(ctx context.Context) error
2628
}

pkg/database/postgres/pgx_sync_tx.go

+33
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ func (tx *PgxSyncTx) Query(
106106
)
107107
}
108108

109+
// SendBatch is used to wrap [pgx.Tx.QueryRow] in a [database.SyncTx].
110+
func (tx *PgxSyncTx) SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults {
111+
return database.WrapInSyncTxNoError(
112+
ctx,
113+
tx.syncTx,
114+
func(ctx context.Context) pgx.BatchResults {
115+
return tx.syncTx.Tx.SendBatch(ctx, b)
116+
},
117+
)
118+
}
119+
109120
// Close doesn't do anything for [PgxSyncRows] as these are closed in [Query].
110121
func (rows *PgxSyncRows) Close() {
111122
}
@@ -208,6 +219,28 @@ func (tx *PgxSyncTx) Begin(ctx context.Context) (pgx.Tx, error) {
208219
)
209220
}
210221

222+
// BeginTx is used to wrap [pgx.Tx.BeginTx] in a [database.SyncTx].
223+
func (tx *PgxSyncTx) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error) {
224+
return database.WrapInSyncTx(
225+
ctx,
226+
tx.syncTx,
227+
func(ctx context.Context) (pgx.Tx, error) {
228+
return tx.syncTx.Tx.Conn().BeginTx(ctx, txOptions)
229+
},
230+
)
231+
}
232+
233+
// Commit is used to wrap [pgx.Tx.Commit] in a [database.SyncTx].
234+
func (tx *PgxSyncTx) Commit(ctx context.Context) error {
235+
return database.WrapInSyncTxNoError(
236+
ctx,
237+
tx.syncTx,
238+
func(ctx context.Context) error {
239+
return tx.syncTx.Tx.Commit(ctx)
240+
},
241+
)
242+
}
243+
211244
// Rollback is used to wrap [pgx.Tx.Rollback] in a [database.SyncTx].
212245
func (tx *PgxSyncTx) Rollback(ctx context.Context) error {
213246
return database.WrapInSyncTxNoError(

pkg/database/postgres/spandb.go

+23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package postgres
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/XDoubleU/essentia/pkg/database"
78
"github.com/jackc/pgx/v5"
@@ -54,12 +55,34 @@ func (db *SpanDB) QueryRow(
5455
optionsAndArgs...)
5556
}
5657

58+
// SendBatch is used to wrap SendBatch in a [sentry.Span].
59+
func (db *SpanDB) SendBatch(
60+
ctx context.Context,
61+
b *pgx.Batch,
62+
) pgx.BatchResults {
63+
sql := ""
64+
for i, query := range b.QueuedQueries {
65+
sql += fmt.Sprintf("query %d: %s\n", i, query.SQL)
66+
}
67+
68+
span := database.StartSpan(ctx, db.dbName, sql)
69+
defer span.Finish()
70+
71+
return db.DB.SendBatch(ctx, b)
72+
}
73+
5774
// Begin doesn't wrap Begin in a [sentry.Span] as
5875
// this makes little sense for starting a transaction.
5976
func (db *SpanDB) Begin(ctx context.Context) (pgx.Tx, error) {
6077
return db.DB.Begin(ctx)
6178
}
6279

80+
// BeginTx doesn't wrap BeginTx in a [sentry.Span] as
81+
// this makes little sense for starting a transaction.
82+
func (db *SpanDB) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error) {
83+
return db.DB.BeginTx(ctx, txOptions)
84+
}
85+
6386
// Ping doesn't wrap Ping in a [sentry.Span] as
6487
// this makes little sense for pinging the db.
6588
func (db *SpanDB) Ping(ctx context.Context) error {

pkg/database/span.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/getsentry/sentry-go"
77
)
88

9-
func startSpan(ctx context.Context, dbName string, sql string) *sentry.Span {
9+
func StartSpan(ctx context.Context, dbName string, sql string) *sentry.Span {
1010
span := sentry.StartSpan(ctx, "db.query", sentry.WithDescription(sql))
1111
span.SetData("db.system", dbName)
1212

@@ -20,7 +20,7 @@ func WrapWithSpan[T any](
2020
dbName string,
2121
queryFunc func(ctx context.Context, sql string, args ...any) (T, error),
2222
sql string, args ...any) (T, error) {
23-
span := startSpan(ctx, dbName, sql)
23+
span := StartSpan(ctx, dbName, sql)
2424
defer span.Finish()
2525

2626
return queryFunc(ctx, sql, args...)
@@ -34,7 +34,7 @@ func WrapWithSpanNoError[T any](
3434
dbName string,
3535
queryFunc func(ctx context.Context, sql string, args ...any) T,
3636
sql string, args ...any) T {
37-
span := startSpan(ctx, dbName, sql)
37+
span := StartSpan(ctx, dbName, sql)
3838
defer span.Finish()
3939

4040
return queryFunc(ctx, sql, args...)

0 commit comments

Comments
 (0)