Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 3B556B5F560 for ; Fri, 3 Jun 2011 19:43:01 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 95759-06 for ; Fri, 3 Jun 2011 22:42:54 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-pw0-f46.google.com (mail-pw0-f46.google.com [209.85.160.46]) by mail.postgresql.org (Postfix) with ESMTP id 04800B5D8BD for ; Fri, 3 Jun 2011 19:42:53 -0300 (ADT) Received: by pwi15 with SMTP id 15so1190131pwi.19 for ; Fri, 03 Jun 2011 15:42:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:content-type:mime-version:subject:from :in-reply-to:date:content-transfer-encoding:message-id:references:to :x-mailer; bh=Ok5ixIEDGoMkZYvFvjmHawT0puG6C7krX6RrDEMvQgk=; b=TmfUKWrjRDwpWmCCUqHiTGq6ozY9X4k4AtGoKR8AmZt2jQuSYLUghrm5k4K7KuF2T9 J3aQMMzFn759SXotW3Dx1nQg08R0bZw/xrrOQYL4PIBuujimIuq/QwzzPj8WnPuaOmmX vqaEqcZOdcWddCD4ffTf0uR27IesqUNluBV8g= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=content-type:mime-version:subject:from:in-reply-to:date :content-transfer-encoding:message-id:references:to:x-mailer; b=EbLvMuJF4uBC/5wo/06nb7oNwBycSUsok12wyXzIKAzCMZN/l75Nyuce6rU9SdkACR o5h/IoLzSkM2CpShUozzkV2hdgCbupC6+iK0LYWOEoHHWCjpfow49pKVLLYMXQ77khAj tz0v98YGqM3WxE8Z/329mPzQ3pScMvKDH5zis= Received: by 10.142.132.4 with SMTP id f4mr395880wfd.306.1307140973488; Fri, 03 Jun 2011 15:42:53 -0700 (PDT) Received: from sexy.pa.trumpet.io (c-76-21-5-154.hsd1.ca.comcast.net [76.21.5.154]) by mx.google.com with ESMTPS id o16sm1292599wff.13.2011.06.03.15.42.51 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 03 Jun 2011 15:42:52 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Apple Message framework v1084) Subject: Re: Accepting Object[] as an acceptable input to setObject with Types.ARRAY? From: Steven Schlansker In-Reply-To: Date: Fri, 3 Jun 2011 15:42:50 -0700 Content-Transfer-Encoding: quoted-printable Message-Id: References: <201106032259.28453.rsmogura@softperience.eu> To: pgsql-jdbc@postgresql.org X-Mailer: Apple Mail (2.1084) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.888 tagged_above=-10 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RFC_ABUSE_POST=0.001, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201106/10 X-Sequence-Number: 21934 On Jun 3, 2011, at 3:02 PM, Samuel Gendler wrote: > > If I go down this path, I end up having to write a special case for = each > > combination of database and pool supported, which is very painful to = maintain. >=20 > If you happen to be using Spring to manage the declaration of your = data source, they have the NativeJdbcExtractor interface, with an = implementation for the most popular connection pools. That will give = you access to the native Connection object of your driver. I keep my = code portable by declaring both my connection pool and an appropriate = NativeJdbcExtractor together in an applicationContext file and then I = just include the correct context file for the runtime environment I'm = working in. Since my code always interacts only with the = nativeJdbcExtractor, so long as the code it executes on that native = connection isn't actually database specific, the code remains fully = portable with the exception of the applicationContext file, which can be = fed into it at run time. >=20 > If you're not using spring, you can model a similar system of your = own. =20 Wow, that is very clever how they do it! Basically they call=20 connection.getMetaData().getConnection() which most pools do not = intercept, giving you access to the connection via only the public APIs. And you do not even need to = use Spring if you just do that. I can definitely do this for my uses, thank you. That said I still think it is an interesting feature to consider, as = ultimately this is still a hack (even if an elegant one) and I have whipped up a small proof of = concept: diff -r e313d386bcf0 org/postgresql/jdbc2/AbstractJdbc2Statement.java --- a/org/postgresql/jdbc2/AbstractJdbc2Statement.java Fri Jun 03 = 13:21:43 2011 -0700 +++ b/org/postgresql/jdbc2/AbstractJdbc2Statement.java Fri Jun 03 = 13:57:52 2011 -0700 @@ -1731,7 +1796,9 @@ throw new PSQLException(GT.tr("Cannot cast an = instance of {0} to type {1}", new = Object[]{in.getClass().getName(),"Types.CLOB"}), = PSQLState.INVALID_PARAMETER_TYPE); break; case Types.ARRAY: - if (in instanceof Array) + if (in instanceof Object[]) + setArray(parameterIndex, = getPGConnection().createArrayOf(determineSQLType((Object[]) in), = (Object[]) in)); + else if (in instanceof Array) setArray(parameterIndex, (Array)in); else throw new PSQLException(GT.tr("Cannot cast an = instance of {0} to type {1}", new = Object[]{in.getClass().getName(),"Types.ARRAY"}), = PSQLState.INVALID_PARAMETER_TYPE); @@ -1750,6 +1817,38 @@ } } =20 + private String determineSQLType(Object[] array) { + Class componentType =3D array.getClass().getComponentType(); + if (componentType.equals(byte.class) || = componentType.equals(Byte.class)) + return "tinyint"; + if (componentType.equals(char.class) || = componentType.equals(Character.class)) + return "varchar"; + if (componentType.equals(short.class) || = componentType.equals(Short.class)) + return "smallint"; + if (componentType.equals(int.class) || = componentType.equals(Integer.class)) + return "int"; + if (componentType.equals(long.class) || = componentType.equals(Long.class)) + return "bigint"; + if (componentType.equals(float.class) || = componentType.equals(Float.class)) + return "float"; + if (componentType.equals(double.class) || = componentType.equals(Double.class)) + return "double"; + if (componentType.equals(boolean.class) || = componentType.equals(Boolean.class)) + return "bit"; + if (componentType.equals(String.class)) + return "varchar"; + if (componentType.equals(byte[].class)) + return "varbinary"; + if (componentType.equals(Date.class)) + return "date"; + if (componentType.equals(Time.class)) + return "time"; + if (componentType.equals(Timestamp.class)) + return "timestamp"; + // Cross your fingers and hope the default works... + return "varchar"; + } +=