pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
createArrayOf("bytea", Array<byte[]>) problem
3+ messages / 2 participants
[nested] [flat]

* createArrayOf("bytea", Array<byte[]>) problem
@ 2014-02-14 11:14 Daniel Selinger <d.selinger@focusmr.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Daniel Selinger @ 2014-02-14 11:14 UTC (permalink / raw)
  To: pgsql-jdbc

Hi all,

I have a problem with submitting arrays of bytea to the database via 
native jdbc arrays.

i'm trying to do the following:

List<byte[]> hashes = new ArrayList<>();
//add some SHA256 in form of byte[]
srcPst = srcConnection.prepareStatement("select array_dims(?)");
srcPst.setArray(1, srcConnection.createArrayOf("bytea", hashes.toArray()));
srcRs = srcPst.executeQuery();
srcRs.next();
srcRs.getString(1);

--> "[1:5][1:32]"


if i do the same thing on the database:

select array_dims(array_agg(hashcolumn)) from hashtable; (hashcolumn is 
of type bytea(32))

--> "[1:5]"


now im struggling how to use the bound data from jdbc on the database, 
because when i use the "[1:5][1:32]" array on the database for something 
like

select * from hashtable where hashcolumn = ANY(?), i get no results, 
although i retrieved the hashes from a resultset on exaclty this table 
with getBytes("hashcolumn")


any suggestions or hints on what i might doing wrong?







-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: createArrayOf("bytea", Array<byte[]>) problem
@ 2014-02-19 10:39 armin walland <armin@qwuh.com>
  2014-02-19 10:46 ` Re: createArrayOf("bytea", Array<byte[]>) problem armin walland <armin@qwuh.com>
  0 siblings, 1 reply; 3+ messages in thread

From: armin walland @ 2014-02-19 10:39 UTC (permalink / raw)
  To: pgsql-jdbc

hi list!

i think the reason is the string representation of the 
java.sql.Array being created/sent which is (as far
as i understood it) being created in

AbstractJdbc4Connection.appendArray(StringBuffer sb, Object elements, char delim).


this function recusrively calls itself if 
o.getClass().isArray() is true, and appends 
all array values as a subarray.

for normal multidimensional arrays i think this is correct, 
but in case of a bytea[] i'm not sure. my best guess is, 
that if the Oid of the parameter is BYTEA or BYTEA_ARRAY,
the value should be hex-encoded and not treated as a nested array.

what is being sent to the server looks like this:

String representation of array: 
{{"118","-110","-61","-83","53","64","-69","-128","60","2","11","58","-18","102","-51","-120","-121","18","50","52","-22","12","110","113","67","-64","-83","-41","63","-12","49","-19"},{"63","-60","-52","-2","116","88","112","-30","-64","-39","-97","113","-13","15","-16","101","108","-115","-19","-44","28","-63","-41","-45","-45","118","-80","-37","-26","-123","-30","-13"},{"-117","91","-99","-80","-63","61","-78","66","86","-56","41","-86","54","74","-87","12","109","46","-70","49","-117","-110","50","-92","-85","-109","19","-71","84","-45","85","95"}}

but according to johto in the #postgresql irc channel, 
postgres expects an array of bytea to look like so:

String representation of array: {"\x7692c3ad3540bb803c020b3aee66cd8887123234ea0c6e7143c0add73ff431ed","\x3fc4ccfe745870e2c0d99f71f30ff0656c8dedd41cc1d7d3d376b0dbe685e2f3","\x8b5b9db0c13db24256c829aa364aa90c6d2eba318b9232a4ab9313b954d3555f"}


i have attached a small program demonstrating the problem and 
also showing, that passing an array of encoded strings works as 
expected.

my question is:

* should the jdbc driver hex-encode the bytea values inside a bytea[]
before sending them to the server
OR
* should the server "know" that the multidimensional array being 
passed is actually a bytea[] and not a byte[][].

-- 
rgds, armin walland

-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc

Attachments:

  pgByteaBug.tgz (application/x-compressed-tar, 7.8K) download

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: createArrayOf("bytea", Array<byte[]>) problem
  2014-02-19 10:39 Re: createArrayOf("bytea", Array<byte[]>) problem armin walland <armin@qwuh.com>
@ 2014-02-19 10:46 ` armin walland <armin@qwuh.com>
  0 siblings, 0 replies; 3+ messages in thread

From: armin walland @ 2014-02-19 10:46 UTC (permalink / raw)
  To: pgsql-jdbc

please disregard the erroneous comment "// this either yields 0 rows " in 
the example. 

--
rgds, armin walland


-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2014-02-19 10:46 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-02-14 11:14 createArrayOf("bytea", Array<byte[]>) problem Daniel Selinger <d.selinger@focusmr.com>
2014-02-19 10:39 Re: createArrayOf("bytea", Array<byte[]>) problem armin walland <armin@qwuh.com>
2014-02-19 10:46 ` armin walland <armin@qwuh.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox