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

pg array constructor (:'ARRAY) gets quoted when :quoted is true #434

Closed
liwp opened this issue Sep 23, 2022 · 3 comments
Closed

pg array constructor (:'ARRAY) gets quoted when :quoted is true #434

liwp opened this issue Sep 23, 2022 · 3 comments
Assignees
Labels
bug It's broken, I'll fix it!

Comments

@liwp
Copy link

liwp commented Sep 23, 2022

I'm trying to use the pg "array constructor" as described here, eg:

user=> (sql/format {:select [[[:'ARRAY {:select :oid :from :pg_proc :where [:like :proname [:inline "bytea%"]]}]]]})
["SELECT ARRAY (SELECT oid FROM pg_proc WHERE proname LIKE 'bytea%')"]

But when I pass :quoted true to sql/format the ARRAY gets quoted as well:

user=> (sql/format {:select [[[:'ARRAY {:select :oid :from :pg_proc :where [:like :proname [:inline "bytea%"]]}]]]} :quoted true)
["SELECT \"ARRAY\" (SELECT \"oid\" FROM \"pg_proc\" WHERE \"proname\" LIKE 'bytea%')"]

And pg doesn't like it (second example):

postgres=# SELECT ARRAY (SELECT oid FROM pg_proc WHERE proname LIKE 'bytea%');
                                        array
--------------------------------------------------------------------------------------
 {3331,3544,3543,2011,1954,1948,1952,1951,1244,1950,2005,1949,1953,2006,31,2412,2413}
(1 row)

postgres=# SELECT "ARRAY" (SELECT "oid" FROM "pg_proc" WHERE "proname" LIKE 'bytea%');
ERROR:  syntax error at or near "SELECT"
LINE 1: SELECT "ARRAY" (SELECT "oid" FROM "pg_proc" WHERE "proname" ...

My workaround is to format the subquery first with :quoted true, then write the ARRAY function call as raw SQL, and finally format the complete query again with :quoted true:

(let [sub-query (-> {:select :oid :from :pg_proc :where [:like :proname [:inline "bytea%"]]}
                    (sql/format :quoted true)
                    first)]
  (sql/format {:select [[[:raw (str "ARRAY(" sub-query ")")]]]} :quoted true))
["SELECT ARRAY(SELECT \"oid\" FROM \"pg_proc\" WHERE \"proname\" LIKE 'bytea%')"]

I'm happy to be told I'm doing it wrong 🙂

@seancorfield
Copy link
Owner

Yeah, I recently extended the :' "escape" syntax to another context and noticed this difference so I'm giving it some hammock time to try to decide what's the right thing to do here...

@seancorfield seancorfield self-assigned this Oct 1, 2022
@seancorfield seancorfield added the bug It's broken, I'll fix it! label Oct 1, 2022
@seancorfield
Copy link
Owner

I consider this a bug but have not yet decided how to fix it.

@liwp
Copy link
Author

liwp commented Oct 10, 2022

Thanks for the update! As I wrote, we have a workaround so it's not urgent for us.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It's broken, I'll fix it!
Projects
None yet
Development

No branches or pull requests

2 participants