Giter Club home page Giter Club logo

Comments (4)

csummers avatar csummers commented on July 24, 2024

@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.

create-renegr avatar create-renegr commented on July 24, 2024

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.

csummers avatar csummers commented on July 24, 2024

@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:

  1. Provide explicit type casts to Postgresql for your json fields (this is hinted at in the error you are receiving).
  2. 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.

create-renegr avatar create-renegr commented on July 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.