Comments (4)
@create-renegr Hi! In order to take advantage of the sql-injection protecting parameter-binding features of HugSQL's value parameters, you will need to take a look at extending the underlying clojure.java.jdbc
protocols to support a Clojure data type (either a built-in type or a type/record of your own) to be treated as a jsonb object.
Have a look at the following blog post for examples of extending these protocols to support postgresql's json data type. The jsonb would likely be similar.
http://hiim.tv/clojure/2014/05/15/clojure-postgres-json/
from hugsql.
Hi,
thanks for your quick response.
The postgres error message I receive when using just :val
instead of :sql:val
is this:
ERROR: function jsonb_set(jsonb, unknown, character varying) does not exist Hint: No function matches the given name and argument types. You might need to add explicit type casts. Position: 65
Does this mean, that I need to make sure, that instead of "charactery varying" I get something like String?
I don't understand why I need to make a protocol to support Clojure Strings. Because the :val
is just a plain String, which is already supported by HugSQL.
I can't see (yet), why your proposed solution would solve my actual problem.
Kind regards,
RenΓ©
from hugsql.
@create-renegr Hey, sorry for the slow reply on this. I missed your response the other day.
You are right: for jsonb_set usage you do not need to implement the protocols I suggested as long as you do the following:
- Provide explicit type casts to Postgresql for your json fields (this is hinted at in the error you are receiving).
- Pass in valid json (for json string, it needs the double quote) when calling your Clojure function.
Here's an example:
(deftest q52
(testing "pg jsonb_set"
(let [db (:postgresql dbs)]
(hugsql/db-run db "drop table if exists t1" {} :!)
(hugsql/db-run db "create table t1 (id serial primary key, props jsonb)" {} :!)
(hugsql/db-run db "insert into t1 (props) values ('{\"attr\": \"value-1\"}')" {} :!)
(hugsql/db-run db
"update t1 set props = jsonb_set(props, '{\"attr\"}', :val::jsonb) where id = :id"
{:id 1 :val "\"value-2\""} :!)
(hugsql/db-run db "drop table t1" {} :!))))
from hugsql.
That did the job!!
Thank you very much for the explanation. Now that makes sense. Wasn't sure which layer had to provide the explicit typecast.
Closing the question, since you provided a clear answer.
from hugsql.
Related Issues (20)
- Taking array as an input
- Not able to update multiple column values
- Optional null parameters HOT 1
- Way to get generated SQL without executing? HOT 1
- Add support for next.jdbc.sql/insert-multi! HOT 1
- Avoid calling apply in 'hugsql.adapter.clojure-java-jdbc' HOT 5
- Incorrect Arities Shown in Docs for def-fb-fns HOT 1
- HugSql fails during AOT compilation with depstar HOT 4
- Switch to newer groupId of next.jdbc dependency in hugsql-adapter-next-jdbc HOT 1
- How to use a function from the database? HOT 2
- Migration fails if a comment is present on line with semicolon HOT 2
- Babashka support HOT 2
- No comments in doc on mobile? HOT 1
- Using classpath resource sql functions breaks cider HOT 3
- Hugsql 0.5.3 still requires SQL files on uberjar compilation (with leiningen) HOT 1
- strange key with jsonb HOT 3
- Should hugsql switch to next.jdbc by default ? HOT 2
- Cannot look up by UUID HOT 2
- Bulk update? HOT 1
- Inline parameters
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hugsql.