Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why not use sql.DB instead of adapter.Tx #148

Open
dosco opened this issue Dec 26, 2022 · 8 comments
Open

Why not use sql.DB instead of adapter.Tx #148

dosco opened this issue Dec 26, 2022 · 8 comments
Labels

Comments

@dosco
Copy link

dosco commented Dec 26, 2022

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

@vgarvardt
Copy link
Owner

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.

@dosco
Copy link
Author

dosco commented Dec 27, 2022

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)
}

@vgarvardt
Copy link
Owner

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.

@owenthereal
Copy link

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
Copy link

owenthereal commented Oct 25, 2023

@vgarvardt
Copy link
Owner

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

@owenthereal
Copy link

owenthereal commented Oct 28, 2023

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

@vgarvardt
Copy link
Owner

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants