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

encode param as pg array #49

Merged
merged 3 commits into from
Aug 20, 2024
Merged

encode param as pg array #49

merged 3 commits into from
Aug 20, 2024

Conversation

ermolaev
Copy link
Contributor

@ermolaev ermolaev commented Jun 7, 2022

No description provided.

@SamSaffron
Copy link
Member

Hmmm ... maybe we support:

conn.auto_encode_arrays!

Then you would not need .array calls if you are careful with your usage.

@ermolaev
Copy link
Contributor Author

ermolaev commented Jun 8, 2022

if we introduce conn.auto_encode_arrays! then it will be necessary to rewrite the old sql

with conn.array developers can create arrays only in the necessary places

conn.query("select * from products where supplier_id = ? and category_ids && ?", 127, conn.array([12, 24, 67]))

conn.array need not only in prepared connection

@SamSaffron
Copy link
Member

understood, my thinking is that conn.auto_encode_arrays! would be something you enable if you are just about to start a brand new project and saves you ceremony.

@SamSaffron
Copy link
Member

Is there any downside to doing this 100% automatically, if we see an Array in the param we just use encode array? What can break with a change like that?

@ermolaev
Copy link
Contributor Author

ANY/SOME (array) support only postgres

postgres on planning step, rewrites IN to = ANY

explain analyze select id from suppliers where id in (12, 24, 67);


Bitmap Heap Scan on suppliers  (cost=2.97..6.57 rows=3 width=4)
  ->  Bitmap Index Scan on suppliers_pkey  (cost=0.00..2.96 rows=3 width=0)
        Index Cond: (id = ANY ('{12,24,67}'::bigint[]))

i think on pg connection good idea automatically use encode array, but then, we won't be able to use IN, this is a breaking change

@ermolaev
Copy link
Contributor Author

Hello @SamSaffron, I added array_encoder option for pg connection, query code more readable and unified in project (no ambiguity IN or ANY)

Rails is doing similar work rails/rails#49388

@SamSaffron SamSaffron merged commit 7ee85bf into discourse:main Aug 20, 2024
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants