Usage improvement of ArrayTypeHandler?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Usage improvement of ArrayTypeHandler?

Dirk Olmes
Hi,

I'm getting my feet wet with mybatis for a project at work where we look for a simple, lightweight way to abstract from plain JDBC. That's mybatis 3.5.1 BTW.

I'm using a Postgresql database with a table that holds multiple values for a column. This is modeled using the java.sql.Array datatype. I can read Arrays from the database just fine using the org.apache.ibatis.type.ArrayTypeHandler but inserting is a bit more difficult. I had to create a custom TypeHandler to map between the Object[] from my mapped objects and the java.sql.Array that the ArrayTypeHandler expects.

Since the ArrayTypeHandler unwraps the object from the java.sql.Array in the extractArray method I'm wondering if it shouldn't do so on the writing side, too. I.e. if an Object[] is passed in as parameter object in setNonNullParameter, shouldn't an Array instance be transparently created by the type mapper and parameter object be passed in?

I thought I'd check for feedback before I go and invest time in a test/extension ...

-dirk

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/aacae16e-133a-4595-a11d-82ab4c35e866%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Usage improvement of ArrayTypeHandler?

Iwao AVE!

Hello Dirk,

Thank you for bringing that up!
I have been thinking about the same thing!
Could you open a new issue on the tracker or send a pull request, please?

A few vague ideas about the new implementation:

  • Use Connection#createArrayOf().
  • Use jdbcType as the first argument if supplied and use the default mapping (i.e. Appendix B of the JDBC spec) as a fallback?
  • If the parameter is an instance of java.sql.Array, just call setArray() to keep backward compatibility?
  • Should/can it support an array of user defined type?

Regards,
Iwao


On Mon, May 20, 2019 at 2:51 PM 'Dirk Olmes' via mybatis-user <[hidden email]> wrote:
Hi,

I'm getting my feet wet with mybatis for a project at work where we look for a simple, lightweight way to abstract from plain JDBC. That's mybatis 3.5.1 BTW.

I'm using a Postgresql database with a table that holds multiple values for a column. This is modeled using the java.sql.Array datatype. I can read Arrays from the database just fine using the org.apache.ibatis.type.ArrayTypeHandler but inserting is a bit more difficult. I had to create a custom TypeHandler to map between the Object[] from my mapped objects and the java.sql.Array that the ArrayTypeHandler expects.

Since the ArrayTypeHandler unwraps the object from the java.sql.Array in the extractArray method I'm wondering if it shouldn't do so on the writing side, too. I.e. if an Object[] is passed in as parameter object in setNonNullParameter, shouldn't an Array instance be transparently created by the type mapper and parameter object be passed in?

I thought I'd check for feedback before I go and invest time in a test/extension ...

-dirk

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/aacae16e-133a-4595-a11d-82ab4c35e866%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CA%2Buep2SEazF6rtnFidBYG%3DhXytggeYb94fYWu%2Bi%3DZsOL0po4-A%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Usage improvement of ArrayTypeHandler?

Dirk Olmes
On 5/20/19 3:28 PM, Iwao AVE! wrote:
> Hello Dirk,

Hi Iwao,

Thanks for your feedback.

> Thank you for bringing that up!
> I have been thinking about the same thing!
> Could you open a new issue on the tracker or send a pull request, please?

I'm motivated to work on a PR but it'll take a while to make progress in
parallel to the day job.

> A few vague ideas about the new implementation:
>
>   * Use |Connection#createArrayOf()|.

That's how my current implementation does it :-)

>   * Use |jdbcType| as the first argument if supplied and use the default
>     mapping (i.e. Appendix B of the JDBC spec) as a fallback?

This will be a tricky one. Using the jdbcType for creating the array
worked in my simple tests so looks like a good approach. Regarding the
Appendix B mappings - is there something already available in the
mybatis sources somewhere?

I've had trouble with ARRAYs vs jdbcType when attempting to insert NULL
values. If I specify jdbcType=ARRAY on the insert statement's parameter
mapping I lose the type info reqiured for creating the proper Array
type. If I specify jdbcType=BIGINT (or whatever type the array elements
consist of), the mybatis code does the wrong thing. I can debug through
the source again to bring up the concrete places - writing this off the
top of my head right now.

Any ideas how to approach NULL values?

>   * If the parameter is an instance of |java.sql.Array|, just call
>     |setArray()| to keep backward compatibility?

Definitely.

>   * Should/can it support an array of user defined type?

Not my primary goal right now but I might take a look into that later.

-dirk

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/2ccd2290-bfb1-5bd5-e1b5-586270a437ad%40googlemail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Usage improvement of ArrayTypeHandler?

Dirk Olmes


On Tuesday, May 21, 2019 at 1:05:29 PM UTC+2, Dirk Olmes wrote:
On 5/20/19 3:28 PM, Iwao AVE! wrote:
> Hello Dirk,

Hi Iwao,

Thanks for your feedback.

> Thank you for bringing that up!
> I have been thinking about the same thing!
> Could you open a new issue on the tracker or send a pull request, please?

I'm motivated to work on a PR but it'll take a while to make progress in
parallel to the day job.


Meanwhile I managed to work on this a bit more. This is my first version of the improvement:  https://github.com/mybatis/mybatis-3/pull/1548

-dirk

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/d03d7ec7-617f-4d96-b2b2-74a0163c64da%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Usage improvement of ArrayTypeHandler?

Iwao AVE!
Thanks a lot, Dirk !
I'll review as soon as possible.

Regards,
Iwao



On Wed, May 22, 2019 at 4:56 PM 'Dirk Olmes' via mybatis-user <[hidden email]> wrote:


On Tuesday, May 21, 2019 at 1:05:29 PM UTC+2, Dirk Olmes wrote:
On 5/20/19 3:28 PM, Iwao AVE! wrote:
> Hello Dirk,

Hi Iwao,

Thanks for your feedback.

> Thank you for bringing that up!
> I have been thinking about the same thing!
> Could you open a new issue on the tracker or send a pull request, please?

I'm motivated to work on a PR but it'll take a while to make progress in
parallel to the day job.


Meanwhile I managed to work on this a bit more. This is my first version of the improvement:  https://github.com/mybatis/mybatis-3/pull/1548

-dirk

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/d03d7ec7-617f-4d96-b2b2-74a0163c64da%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CA%2Buep2R8zBTSxzfaroepfUMN5j%3D3Kshk6Re4gvmLMAwUEfYaMA%40mail.gmail.com.