Register special-syntax function #343
-
|
Hi! I'm registering trim function with special syntax. My implementation now is: I don't feel comfortable with that i have to check second argument on Is there another approach? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
I would expect the original I'd probably write it like this: (hsql/register-fn! :trim
(fn [_ [text position character]]
(let [[sql-t & params-t] (hsql/format-expr text {:nested true})
[sql-p & params-p] (when position (hsql/format-expr position {:nested true}))
[sql-c & params-c] (when character (hsql/format-expr [:inline character] {:nested true}))]
(cond
character
(-> [(format "TRIM(%s %s FROM %s)" sql-p sql-c sql-t)]
(into params-p)
(into params-c)
(into params-t))
position
(-> [(format "TRIM(%s FROM %s)" sql-p sql-t)]
(into params-p)
(into params-t))
:else
(-> [(format "TRIM(%s)" sql-t)])))))If |
Beta Was this translation helpful? Give feedback.
I would expect the original
positionto come in asnilif it is omitted -- ascharacterdoes -- but you are shadowing that with yourpositionbased onsql-a. That doesn't look like a good way to handle that. I would also recommend against using:inlinehere since you're opening yourself up the SQL injection attacks.I'd probably write it like this: