-
Notifications
You must be signed in to change notification settings - Fork 863
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
sqlx bridge #253
Comments
I don't have the experience with sqlx to know the best way to bridge the gap between these libraries. However, if I understand correctly part of the problem is incompatibilities between database/sql types expecting text protocol values, and pgx expecting binary protocol values. This should be mitigated in a future |
@jackc - What I've read so far about pgx really made me very much interested in using it. I have two requirements that I couldn't figure out if pgx supports it already:
|
directly scanner rows into a struct should not be handle in the driver imo. |
Same problem with types. I need to use slices, jsonb, nulls. sqlx has few basic null types, that's all. So I'm forced to use naked pgx. But I would like to use sqlx because of things like StructScan. If pgx had StructScan, it could postpone problem. |
I don't think that struct mapping belongs in the driver directly -- though maybe it could fit as a sub-package. I don't have any plans for doing it myself though. And if I did do something it would probably run more along code generation for struct mapping than reflection. Regarding sqlx and more advanced types, the |
@jackc Agree with you. It is not a problem driver should solve. But! PGX is considered to be used directly, without any sql builder, mapper. But anyway, it does not mean that it should handle mapping at all. I gonna write some easy sql builder and mapper on the top of pgx, but as separated project. The bad this is, we already have many and many libraries around: sqlx, sql, gorm, pop, upper.io/db, etc. But they are compatible only on basic things. Like, sqlx is a great tool, but can't handle some fields when pgx is used. |
@jackc I love this option I am actually considering it as an alternative to sqlx. Do you have any pointers or libs that can do code generation like I would do in Lisp or Elixir ? |
@jeromer oddly enough I'm working on a code generation for sql, genieql, might fit what your looking for, though i havent directly implemented pgx for it yet its on my list. I'd love for you to take a look and provide feedback if nothing else. its still in flux but its mostly stable. right now most of the work I'm putting into it is sensible defaults to ease usage and corner case clean up of some experimental stuff I was trying. there is another one - xo found it around the same time I was well into genieql. |
I experimented with with a code generation tool of my own: https://github.com/jackc/pgxdata. I finished it enough to use it with https://github.com/jackc/tpr, but I'm not actively developing it at the moment. |
https://github.com/jackc/pgxdata is good for me , Usage like this: pgxdataa tool to generate a jackc/pgx database go package tailored to exists postgresql database schema. UsageSTEP 1 first install pgxdata
STEP 2 add GOPATH/bin to PATH , make sure pgxdata runing anywhere STEP 3 then create a postgresql schema like this
STEP 4 run pgxdata once to create a new go package name "dbi" within $GOPATH/src
this command will create a directory name "dbi" and two file inside "dbi"
cat the ./dbi/config.toml like this
STEP 5 edit the config file dbi/config.toml
with this look like
STEP 6 goto dbi directory and run generate
Final check $GOPATH/src/dbi to see all generated go package for postgres database pgxdata
|
I just pushed https://github.com/jeromer/sqrible (caution: fresh paint !). I'll use it on 2 projects. |
(Related issue : jmoiron/sqlx#193)
(Related commit : https://github.com/jeromer/pgx/commit/5097a8100cb350853e1aa8bcf05787ff41c69216)
When using certain postgres type, like int arrays sqlx fails to scan what
postgres returns probably because it does not seem to call pgx's scanner but
database/sql
's instead.The problem is that when you want to use certain postgres types there is
actually no way to use sqlx and pgx at the same type. Either you use pgx and
all the scanning works fine but you can not use
Struct
tags. Or you usesqlx
withStruct
tags but limit yourself to whatsqlx
(and eventuallydatabase/sql
) can scan.I tried to modify
sqlx
so that it callspgx
's scanner but it failsmiserably. So I tried to modify
pgx
instead by providing aStructScan
method which can be used like this:
StructScan
only acts as a proxy topgx.Scan
so no Scan methods weremodified.
There is a test file in
cmd/main.go
just modifiy postgres credential to matchyour test environnent and run
go run main.go
.@jackc and @jmoiron I'd love to get your feedback on this.
Please note that I dit not touch Go for around 3 years and working back with Go
after 3 years of Erlang is quite a cultural change so my modifications may be
completely horrible or not idiomatic go.
sqlx and pgx are both great libs. I am sure providing some kind of bridge
between those libs would be beneficial to a lot of people.
Thanks for reading
:)
The text was updated successfully, but these errors were encountered: