hugsql

2025-02-27T02:40:04.987069Z

Is there a way to use an identifier to pass a schema with a constant table name?

igrishaev 2025-02-27T12:34:13.239029Z

That's exactly what I was asking about two posts earlier. I have long tables named like this: {{ realm }}_foo_bar_baz where realm might be prod , test , training and so on. And I'ld like to pass only the realm part. But it looks like it's not possible in Hug

2025-02-27T14:25:11.525759Z

that's really unfortunate

igrishaev 2025-02-27T14:29:41.608829Z

Alas, indeed. Sharing this for the context:

👍 1
igrishaev 2025-02-27T14:32:36.526559Z

Apparently, I ended up with a primitive templating system:

(defn render [template context]
  (reduce-kv
   (fn [result field value]
     (let [pattern
           (re-pattern
            (str "\\{\\{\\s*" (name field) "\\s*\\}\\}"))]
       (str/replace result pattern (str value))))
   template
   context))

(render "I've got {{ amount }} apples" {:amount 3})
I've got 3 apples
and I use it to render vast SQL files

igrishaev 2025-02-27T14:33:35.650559Z

Usually my SQL files have everything hardcoded except schemas, because they rely on the current environment.

2025-02-27T14:34:07.729839Z

yeah, this is the exact case that I wanted to cover

igrishaev 2025-02-27T14:34:29.667609Z

That's of course dangerous in terms of injections

2025-02-27T14:34:41.415889Z

I mean identifiers in hug already are

2025-02-27T14:34:57.247629Z

just can't have untrusted input going in there

igrishaev 2025-02-27T14:35:18.569819Z

They get qouted with or '' depending on the backend option

igrishaev 2025-02-27T14:35:35.751089Z

yeah, it's the same after all

igrishaev 2025-02-27T14:37:04.764689Z

But if you find a way to do it in Hug, could you please let me know?

2025-02-27T14:37:36.304319Z

Absolutely will!

2025-02-27T02:41:07.829519Z

I tried in mysql, but this appears to break because of the quoting

select * from `:i:schema`.tableName