You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by "mischa.z" <mi...@gmail.com> on 2007/01/10 19:10:09 UTC

postgres ARRAY type

Hey,

I can't seem to find the correct mapping for the postgres ARRAY type in 
the ibatis docs (do I have to write my own handler?). Can anyone give me 
a hint. Thanks.
-Mischa

Re: postgres ARRAY type

Posted by Brandon Goodin <br...@gmail.com>.
Looks fine to me.

Brandon

On 1/11/07, mischa.z <mi...@gmail.com> wrote:
>
> mischa.z wrote:
> > Hey,
> >
> > I can't seem to find the correct mapping for the postgres ARRAY type in
> > the ibatis docs (do I have to write my own handler?). Can anyone give me
> > a hint. Thanks.
> > -Mischa
> >
>
> I guess I found a solution (although I'm not sure if this is the best
> way to do it).
> For dealing with postgres' boolean[] type I wrote the following handler
> for mapping it to a Java type boolean[]:
>
>
> public class PGBooleanArrayTypeMapper
>    implements TypeHandlerCallback
> {
>      public void setParameter(
>          ParameterSetter setter,
>          Object parameter)
>        throws SQLException
>      {
>        throw new UnsupportedOperationException("Not implemented yet");
>      }
>
>      public Object getResult(
>          ResultGetter getter)
>        throws SQLException
>      {
>        ResultSet dbResult = getter.getArray().getResultSet();
>        boolean[] result = new boolean[255]; // expecting fix num of
> elements
>        while (dbResult.next())
>        {
>          String element = dbResult.getString(2);
>          result[dbResult.getInt(1)-1] = element.equals("YES") ? true :
> false;
>        }
>
>        return result;
>      }
>
>      public Object valueOf(
>          String s)
>      {
>        throw new UnsupportedOperationException("Not implemented yet");
>      }
> }
>
>
> The mapping for the sqlmap looks like:
>
> <resultMap id="get-codes-result" class="test.Codes">
>         <result property="codes" column="code" jdbcType="ARRAY"
> javaType="java.sql.Array" typeHandler="test.PGBooleanArrayTypeMapper"/>
> </resultMap>
>
> -Mischa
>

Re: postgres ARRAY type

Posted by "mischa.z" <mi...@gmail.com>.
mischa.z wrote:
> Hey,
> 
> I can't seem to find the correct mapping for the postgres ARRAY type in 
> the ibatis docs (do I have to write my own handler?). Can anyone give me 
> a hint. Thanks.
> -Mischa
> 

I guess I found a solution (although I'm not sure if this is the best 
way to do it).
For dealing with postgres' boolean[] type I wrote the following handler 
for mapping it to a Java type boolean[]:


public class PGBooleanArrayTypeMapper
   implements TypeHandlerCallback
{
     public void setParameter(
         ParameterSetter setter,
         Object parameter)
       throws SQLException
     {
       throw new UnsupportedOperationException("Not implemented yet");
     }

     public Object getResult(
         ResultGetter getter)
       throws SQLException
     {
       ResultSet dbResult = getter.getArray().getResultSet();
       boolean[] result = new boolean[255]; // expecting fix num of elements
       while (dbResult.next())
       {
         String element = dbResult.getString(2);
         result[dbResult.getInt(1)-1] = element.equals("YES") ? true : 
false;
       }

       return result;
     }

     public Object valueOf(
         String s)
     {
       throw new UnsupportedOperationException("Not implemented yet");
     }
}


The mapping for the sqlmap looks like:

<resultMap id="get-codes-result" class="test.Codes">
	<result property="codes" column="code" jdbcType="ARRAY"
javaType="java.sql.Array" typeHandler="test.PGBooleanArrayTypeMapper"/>
</resultMap>

-Mischa