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

formatf for positional parameter convenience? #495

Closed
seancorfield opened this issue Jun 23, 2023 · 3 comments
Closed

formatf for positional parameter convenience? #495

seancorfield opened this issue Jun 23, 2023 · 3 comments
Assignees
Labels
documentation I need to write something up! enhancement

Comments

@seancorfield
Copy link
Owner

seancorfield commented Jun 23, 2023

If you're using the quoted symbol style queries, it is a nuisance to have to break out of that whenever you want a non-literal value in the query.

It would be nice if there was a quoted symbol style version of something like this:

(sql/format {:select :* :from :users :where [:= :name user-name]})

Proposal:

(sql/formatf '{select * from users where (= name ?1)} user-name) ; was $1 but ? introduces params

It would be purely a convenience wrapper to allow quoted symbol style queries with parameters.

Behind the scenes, it would run (zipmap (map (comp keyword str inc) (range)) args) to create the :params option and then invoke sql/format.

Open question:

  • How could options be passed?
@seancorfield seancorfield added enhancement needs analysis I need to think about this! labels Jun 23, 2023
@seancorfield seancorfield self-assigned this Jun 23, 2023
seancorfield added a commit that referenced this issue Jun 23, 2023
@seancorfield seancorfield added documentation I need to write something up! and removed needs analysis I need to think about this! labels Aug 12, 2023
@seancorfield
Copy link
Owner Author

Since $1 is a valid numbered parameter, I implemented ?1 as a "named" parameter so you get {:params {:1 <value>}}.

seancorfield added a commit that referenced this issue Jan 7, 2024
@seancorfield
Copy link
Owner Author

seancorfield commented Jan 15, 2024

The "two more possible solutions" use clojure.template and are macros (so no .cljs equivalent right now, since cljs.template does not exist): format& which assumes all local bindings could be variables in a quoted DSL to be substituted and formatv which lets you declare variables to be substituted (which can be local or non-local).

Although the downside is that they are .clj-only, the upside is that they accept all the options, since they are non-variadic wrappers around format, unlike formatf.

@seancorfield
Copy link
Owner Author

I'm beginning to settle on formatv as the best option here, even tho' it is .clj-only and a macro. I have removed format& since it duplicates formatv without any of the control. I'll probably remove formatf at this point as well.

I need to add tests and document formatv more extensively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation I need to write something up! enhancement
Projects
None yet
Development

No branches or pull requests

1 participant