Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y2ltc-0000Pr-PV for pgsql-jdbc@arkaria.postgresql.org; Sun, 21 Dec 2014 19:10:28 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Y2ltc-000244-2y for pgsql-jdbc@arkaria.postgresql.org; Sun, 21 Dec 2014 19:10:28 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y2ltb-00023x-1e for pgsql-jdbc@postgresql.org; Sun, 21 Dec 2014 19:10:27 +0000 Received: from mail5.spectralogic.com ([192.30.190.19]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y2ltV-0002R1-FB for pgsql-jdbc@postgresql.org; Sun, 21 Dec 2014 19:10:25 +0000 From: Hans Klett To: Christopher BROWN CC: List Subject: Re: Postgresql 9.4 / JSONB / JDBC Thread-Topic: [JDBC] Postgresql 9.4 / JSONB / JDBC Thread-Index: AQHQHDm6yb9SyAjHfE6Uwlbo9MT88pyY212AgAAgB4CAAWeAgA== Date: Sun, 21 Dec 2014 18:41:04 +0000 Message-ID: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: user-agent: Microsoft-MacOutlook/14.3.5.130515 x-originating-ip: [192.168.254.6] Content-Type: multipart/alternative; boundary="_000_D0BC61C61412hanskspectralogiccom_" MIME-Version: 1.0 X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org --_000_D0BC61C61412hanskspectralogiccom_ Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable Hello Christopher, If you=92re already using Jackson, can=92t you just serialize the json data= structure and bind it as a single SQL parameter? Jackson can handle writin= g out correct JSON, and prepared statements can handle escaping. Am I missing something? Thanks, - Hans From: Christopher BROWN > Date: Saturday, December 20, 2014 at 7:14 AM To: Dave Cramer > Cc: List > Subject: Re: [JDBC] Postgresql 9.4 / JSONB / JDBC Dave, I've only just started looking at how JSONB works in Postgresql, and am mai= nly basing ideas on what I can see here: http://www.postgresql.org/docs/9.4/static/datatype-json.html I suppose the basic requirement would be to be able to do stuff like this i= n JDBC: PreparedStatement stmt =3D connection.prepareStatement("UPDATE MYTABLE SET = MYJSONCOL =3D ? WHERE MYID =3D ?"); stmt.setObject(1, map); stmt.setInt(2, 123); ...as opposed to something like this (with a potentially large and variable= number of "?" markers): prepareStatement("UPDATE MYTABLE SET MYJSONCOL =3D '{\"?\":?, \"?\": {\"?\"= :?}}' WHERE MYID =3D ?"); ...or just creating a normal statement and rolling my own escaping to preve= nt SQL injection. It would make sense to only allow keys of strings, and values of either Map= , Collection, Iterable, Stream, array, string, number, boolean, or null (al= though I'm a bit confused about the latter after consulting table 8-23 from= the linked documentation above). That would be the first requirement. The second would be an optimisation to avoid having to convert existing dat= a structures but might need to be "a capability that you can add" rather th= an "built in". For example, I use the Jackson JSON API (there are others o= f course) and I wouldn't expect it to be bundled into the driver, however i= t'd be useful to pass a JSONNode object into "stmt.setObject" and have a wa= y of telling the driver how to serialize it (I don't know if the getObject = method of a resultset can be forced to use Map or something else). Jackson= has useful serialization methods of course, so I could just write a string= suffixed with "::json", but I'm guessing maybe the driver provides some so= rt of optimized binary representation which could be used for better perfor= mance. Thanks, Christopher On 20 December 2014 at 13:19, Dave Cramer > wrote: Christopher, No you have not missed anything, there has been nothing done with jsonb and= the driver. Since you are the first, perhaps you can give me your wish lis= t ? What would you like to see happen ? Dave Cramer dave.cramer(at)credativ(dot)ca http://www.credativ.ca On 20 December 2014 at 04:44, Christopher BROWN > wrote: Hello, I'm new to this list, and have tried searching both the mailing list archiv= es, and internet in general, for information on how to use the new JSONB da= ta type with JDBC. Found almost nothing in the mailing list archives, and = internet searches "helpfully" assume I've misspelled "json" and give me res= ults that are unrelated or refer to the basic "JSON" type from older Postgr= esql versions. How can this type be used, with simple "flat" key-value collections (maps),= with tree-like maps-of-maps, and (ideally) with JSON APIs such as Jackson = JSON API? Is it possible? Is it already described somewhere that I missed? Thanks, Christopher --_000_D0BC61C61412hanskspectralogiccom_ Content-Type: text/html; charset="Windows-1252" Content-ID: <29583C1F7D2D6E4C9E778FBB89C78FC5@spectralogic.com> Content-Transfer-Encoding: quoted-printable
Hello Christopher,

If you=92re already using Jackson, can=92t you just serialize the json= data structure and bind it as a single SQL parameter? Jackson can handle w= riting out correct JSON, and prepared statements can handle escaping.

Am I missing something?

Thanks,
- Hans

From: Christopher BROWN <brown@reflexe.fr>
Date: Saturday, December 20, 2014 a= t 7:14 AM
To: Dave Cramer <pg@fastcrypt.com>
Cc: List <pgsql-jdbc@postgresql.org>
Subject: Re: [JDBC] Postgresql 9.4 = / JSONB / JDBC

Dave,

I've only just started looking at how JSONB works in Postgresql, and a= m mainly basing ideas on what I can see here:

I suppose the basic requirement would be to be able to do stuff like t= his in JDBC:

PreparedStatement stmt =3D connection.prepareStatement("UPDATE MY= TABLE SET MYJSONCOL =3D ? WHERE MYID =3D ?");
stmt.setObject(1, map);
stmt.setInt(2, 123);

...as opposed to something like this (with a potentially large and var= iable number of "?" markers):

prepareStatement("UPDATE MYTABLE SET MYJSONCOL =3D '{\"?\&qu= ot;:?, \"?\": {\"?\":?}}' WHERE MYID =3D ?");=

...or just creating a normal statement and rolling my own escaping to = prevent SQL injection.

It would make sense to only allow keys of strings, and values of eithe= r Map, Collection, Iterable, Stream, array, string, number, boolean, or nul= l (although I'm a bit confused about the latter after consulting table 8-23= from the linked documentation above).

That would be the first requirement.

The second would be an optimisation to avoid having to convert existin= g data structures but might need to be "a capability that you can add&= quot; rather than "built in".  For example, I use the Jackso= n JSON API (there are others of course) and I wouldn't expect it to be bundled into the driver, however it'd be useful to pass a JSONNod= e object into "stmt.setObject" and have a way of telling the driv= er how to serialize it (I don't know if the getObject method of a resultset= can be forced to use Map or something else).  Jackson has useful serialization methods of course, so I could just write = a string suffixed with "::json", but I'm guessing maybe the drive= r provides some sort of optimized binary representation which could be used= for better performance.

Thanks,
--_000_D0BC61C61412hanskspectralogiccom_--