You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been mocking up some changes to the generated interfaces to make using transactions easier, but I wanted to get opinions before I spend the time implementing it. The below snippets are using pgx, but it should translate similarly to database/sql as well.
// Shared interface between DB and Tx. This is unchanged from the current implementation.interfaceDBTX {
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
QueryRow(context.Context, string, ...interface{}) pgx.Row
}
// DB is roughly equivalent to the pgx.Conn, but Begin is pgx.Conn's BeginEx, and// I based BeginFunc on BoltDB's transactions because it provides a fairly ergonomic// API once you get used to it.// https://github.com/etcd-io/bbolt#transactionsinterfaceDB {
DBTX// Begin starts a transaction - be careful with this. You must either Commit or// Rollback the transaction for the sake of your database.Begin(ctxcontext.Context) (Tx, error)
// Begin starts a transaction and executes f. If f does not return an err, the transaction will be// committed. If it does, then the transaction will be rolled back. BeginFunc returns the inner// returned error.BeginFunc(ctxcontext.Context, f (context.Context, Tx), error) error
}
interfaceTx {
DBTXCommit(ctx context.Context) errorRollback(ctx context.Context) error
}
Here is the main problem I've been running into - before I started switching to sqlc, I attached all the generated methods to the DBTX, but because of the possibility of using prepared queries with database/sql, this is not possible - in sqlc there is a concrete Queries type which all of them are attached to. There could be some unexported generated types, but that seems less than ideal. Do you have any ideas to solve that? Is this even worth pursuing? Are there better ways/APIs to make transactions easier to use?
This discussion was converted from issue #1223 on October 09, 2021 17:53.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I've been mocking up some changes to the generated interfaces to make using transactions easier, but I wanted to get opinions before I spend the time implementing it. The below snippets are using pgx, but it should translate similarly to database/sql as well.
Here is the main problem I've been running into - before I started switching to sqlc, I attached all the generated methods to the DBTX, but because of the possibility of using prepared queries with database/sql, this is not possible - in sqlc there is a concrete Queries type which all of them are attached to. There could be some unexported generated types, but that seems less than ideal. Do you have any ideas to solve that? Is this even worth pursuing? Are there better ways/APIs to make transactions easier to use?
Beta Was this translation helpful? Give feedback.
All reactions