gue icon indicating copy to clipboard operation
gue copied to clipboard

Why not use sql.DB instead of adapter.Tx

Open dosco opened this issue 2 years ago • 8 comments

Using this adapter.Tx makes it hard to write portable functions it would be best to just use sql.DB instead of adapter.Tx

dosco avatar Dec 26 '22 03:12 dosco

Adapter generalises db interface because only github.com/lib/pq supports database/sql interface, while github.com/jackc/pgx/v5 and github.com/jackc/pgx/v4 drivers have own interfaces.

vgarvardt avatar Dec 26 '22 12:12 vgarvardt

That makes sense I endedup writing an adapter for sql.DB so I could use sql.DB with generic functions.

myConnPool := NewConnPool(dbSQL)
tx, err := myConnPool.Begin(c)
if err != nil {
log.Fatal(err)
}
defer tx.Rollback(c)
... use tx in my function

However github.com/jackc/pgx/v5 and github.com/jackc/pgx/v4 does work with sql.DB you just have to include them as a driver in your package.

_ "github.com/jackc/pgx/v5/stdlib"

db, err := sql.Open("pgx", "postgres://postgres:@localhost:5432/example_db")
if err != nil {
	log.Fatal(err)
}

dosco avatar Dec 27 '22 09:12 dosco

Originally library worked only with pgx so using native driver is more preferred than stdlib wrapper, other drivers were added later, some of them are already gone.

vgarvardt avatar Dec 31 '22 12:12 vgarvardt

I wonder whether we could have an adapter for sql.DB. This would free me from initialling an extra pgxpool because I already have the sql.DB by importing github.com/jackc/pgx/v5/stdlib for other usage.

owenthereal avatar Oct 25 '23 16:10 owenthereal

I could see the sql.DB adapter will be mostly the same as https://github.com/vgarvardt/gue/pull/139/files#diff-9f1fabd30174ef9b36c0d73790c48b5604600215ccc00167a7896ca086132600

owenthereal avatar Oct 25 '23 16:10 owenthereal

@owenthereal probably you're looking for https://github.com/vgarvardt/gue#libpq

vgarvardt avatar Oct 25 '23 18:10 vgarvardt

@vgarvardt Ah missed that. Thanks! I was confused by the package name. I would suggest rename the package to stdlib or something

owenthereal avatar Oct 28 '23 13:10 owenthereal

@owenthereal sdlib sql is an interface and several drivers implement it - github.com/lib/pq, github.com/go-sql-driver/mysql, github.com/mattn/go-sqlite3 and some more do, but only the first one works here, so naming the package stdlib or driversql would be even more misleading. In the PR that you referenced I tried to add mysql support, but it simply does not work.

vgarvardt avatar Oct 28 '23 13:10 vgarvardt