Adding value to the map in sql statement dynamically

2020-12-20 Thread Ganesh Neelekani
Hello Team, I am new to clojure and I wanted to write a function where input from map keys are passed as parameter as input to the sql query. (def query-body1 " select first_name, last_name from ${table_name} where person_id=${person_id} order by ${id} ") (def query-body2 " select first_nam

Re: Adding value to the map in sql statement dynamically

2020-12-20 Thread alpeware llc
Welcome to Clojure! You could just define a function taking a map as an argument and return the query as a string using destructering [0] (defn people [{:keys [table person id]}] (str "SELECT * FROM " table " WHERE person_id = " person " ORDER BY " id)) You may want to have a look at Sean Corn

Re: Adding value to the map in sql statement dynamically

2020-12-20 Thread Ganesh Neelekani
Hello Alpaware, Thanks for the reply, In the above, I just gave an example, Bt this will be dynamic, Any number of keys can come, How to segregate and assigned to particular variable? Thanking you. -- With regards. Ganesh N Neel

Re: Adding value to the map in sql statement dynamically

2020-12-20 Thread alpeware llc
The most straight forward approach is to simply define a different function for each use case you have using the same approach. At some point you will have to decide which function to call with what input. In more general terms, you want to think about taking an input (a map), applying some trans

Re: Adding value to the map in sql statement dynamically

2020-12-20 Thread Brandon R
Hey Ganesh, Just to add to the above information, you may want to check out next.jdbc as well. If you specifically want to create functions that return complete SQL, then maybe this isn't what you want, but I just wanted to point out parameterization in next.jdbc since you mentioned you're new to

Re: Adding value to the map in sql statement dynamically

2020-12-20 Thread Erik Assum
Hi Ganesh, Please don’t do it this way, as you’re opening yourself to sql-injection. When doing parameterized queries in sql, please reach for prepared statements. As others have mentioned, next.jdbc with a sprinkle of honeysql on top should be exact what you’re looking for. Erik. -- i fa