mini_sql icon indicating copy to clipboard operation
mini_sql copied to clipboard

encode param as pg array

Open ermolaev opened this issue 3 years ago • 5 comments

ermolaev avatar Jun 07 '22 20:06 ermolaev

Hmmm ... maybe we support:

conn.auto_encode_arrays!

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

SamSaffron avatar Jun 08 '22 00:06 SamSaffron

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

ermolaev avatar Jun 08 '22 21:06 ermolaev

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 avatar Jun 08 '22 21:06 SamSaffron

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?

SamSaffron avatar Aug 01 '23 01:08 SamSaffron

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 avatar Aug 11 '23 14:08 ermolaev

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 https://github.com/rails/rails/pull/49388

ermolaev avatar Aug 20 '24 13:08 ermolaev