Fork me on GitHub
#honeysql
<
2022-11-03
>
quan xing10:11:53

how can I covert string vector to honeysql like this:

(defn sql-vec ["or",
                ["and", ["=","col_a","a"],["=","col_b",1]]
                ,
                ["and", ["=","col_d","a"],["=","col_e",1]]])

(what-fn sql-vec)
output :
 [:or
     [:and [:= :col_a "a"],[:= :col_b 1]
     [:and [:= :col_c "a"],[:= :col_d 1]]]
Is there a function that already exists

seancorfield15:11:11

How would you tell the difference between a column or function name and an actual string value? How/why are you using strings in the first place? HoneySQL accepts symbols as well as keywords, if that's any help and lets you use lists instead of vectors in most places: '(or (and (= col_a "a") (= col_b 1)) (and (= col_d "a") (= col_e 1)))

quan xing16:11:20

thank you. but I want to use a json string to define sql where condition, frontend post json where to backend. I wrote it for a long time, and it basically worked. but it felt ugly. the recursive processing of the data confused me.

(defn first-second->key
    [[f1 f2 f3]]
    [(keyword f1) (keyword f2) f3])

  (defn reslove-exp-v
    [v]
    (if (vector? v)
      (if (every? #(not (vector? %)) v)
        (first-second->key v)
        (mapv reslove-exp-v v))
      (keyword v)))

  (defn reslove-exp
    [exp]
    (if (every? #(not (vector? %)) exp)
      (first-second->key exp)
      (mapv reslove-exp-v exp)))

  
  (reslove-exp ["=" "id" "1234"])
  ;; => [:= :id "1234"]

  (reslove-exp ["or",
                ["and", ["=","col_a","a"],["=","dff",1]]
                ,
                ["and", ["=","col_b","bb"],["=","abc",1]]])
  ;; => [:or [:and [:= :col_a "a"] [:= :dff 1]] [:and [:= :col_b "bb"] [:= :abc 1]]]

seancorfield16:11:40

Bear in mind that [:= "a" :col_x] is a perfectly valid condition too...

quan xing00:11:33

ok, thank you!

orestis18:11:33

Having seen systems that send “where” queries to the backend, I would strongly suggest you find a different approach. Eg find your own mini-language or data structure to encode this information.

orestis18:11:54

Your approach and your function can easily be circumvented to bypass all your where clauses.