Skip site navigation (1) Skip section navigation (2)

Re: [PATCHES] [PATCH] Ability to PreparedStatement.setObject(#, int[])

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Doug Fields <dfields-postgres(at)pexicom(dot)com>
Cc: pgsql-patches(at)postgresql(dot)org, pgsql-jdbc(at)postgresql(dot)org
Subject: Re: [PATCHES] [PATCH] Ability to PreparedStatement.setObject(#, int[])
Date: 2002-02-22 05:48:28
Message-ID: 200202220548.g1M5mS727092@candle.pha.pa.us (view raw or flat)
Thread:
Lists: pgsql-jdbcpgsql-patches
This has been saved for the 7.3 release:

	http://candle.pha.pa.us/cgi-bin/pgpatches2

---------------------------------------------------------------------------

Doug Fields wrote:
> Hello,
> 
> (Find Unified Context Diff below against 7.2 shipping sources)
> 
> Currently there is no way to set an int[] object to an INTEGER[] in 
> Postgres with JDBC, unless you create a string and do it. However, reading 
> p266-267 of JDBC API Tutorial and Reference, 2nd Ed by White, Fisher, 
> Cattell, Hamilton, Hapner, indicates that we should be able to setObject it 
> and have it work.
> 
> The current 7.2 drivers do not support this functionality, that of calling 
> setObject() with an array. Instead, the driver tries to use object 
> serialization to store it.
> 
> In theory, any base type or String array should be converted to the 
> appropriate array format and stored in an array type in the database. I 
> have only patched it to recognize an int[] array to demonstrate what should 
> be done. If people wish, I can extend my patch to all base array types 
> other than byte (which is already handled).
> 
> Cheers,
> 
> Doug
> 
> 
> diff -ru5 org-orig/postgresql/jdbc2/PreparedStatement.java 
> org/postgresql/jdbc2/PreparedStatement.java
> --- org-orig/postgresql/jdbc2/PreparedStatement.java    Tue Jan 15 02:37:33 
> 2002
> +++ org/postgresql/jdbc2/PreparedStatement.java Wed Feb 20 20:34:32 2002
> @@ -754,11 +754,29 @@
>                          setTimestamp(parameterIndex, (Timestamp)x);
>                  else if (x instanceof Boolean)
>                          setBoolean(parameterIndex, 
> ((Boolean)x).booleanValue());
>                  else if (x instanceof PGobject)
>                          setString(parameterIndex, ((PGobject)x).getValue());
> -               else
> +               else if (x instanceof int[]) {
> +                       /* Turn into a string and use setString instead.
> +                        * This should be used for other array types as well.
> +                        * See p266-267 of JDBC API Tutorial and Reference,
> +                        * 2nd Ed by White, Fisher, Cattell, Hamilton, Hapner
> +                        * for why this method should be done this way.
> +                        * Doug Fields <dfields-pg-jdbc(at)pexicom(dot)com>
> +                        * Feb 20, 2002 */
> +                       StringBuffer sb = new StringBuffer();
> +                       int[] y = (int[])x; // Ease of use
> +                       sb.append('{');
> +                       for (int i = 0; i < y.length; i++) {
> +                               if (i > 0)
> +                                       sb.append(',');
> +                               sb.append(y[i]);
> +                       }
> +                       sb.append('}');
> +                       setString(parameterIndex, sb.toString());
> +               } else
>                          // Try to store java object in database
>                          setSerialize(parameterIndex, 
> connection.storeObject(x), x.getClass().getName() );
>          }
> 
>          /*
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
> 

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman(at)candle(dot)pha(dot)pa(dot)us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

In response to

pgsql-patches by date

Next:From: Bruce MomjianDate: 2002-02-22 05:53:37
Subject: Re: JDBC CStmt (inline this time)
Previous:From: Bruce MomjianDate: 2002-02-22 05:45:12
Subject: Re: simple code cleanups

pgsql-jdbc by date

Next:From: Bruce MomjianDate: 2002-02-22 05:53:37
Subject: Re: JDBC CStmt (inline this time)
Previous:From: Bruce MomjianDate: 2002-02-22 05:48:15
Subject: Re: [PATCH] Empty arrays cause SQLExceptions when using Array.getArray

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group