You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Marek Šabo <ms...@buk.cvut.cz> on 2011/02/07 20:41:18 UTC

Mapping (My)SQL function

Hi,

I would like to ask for advice, what would be a good way to get outcome 
of (my)sql functions in cayenne. Is there any other way than calling it 
from template queries? E.g. would it be possible to map somehow 
INET_NTOA(ipAddrColumn) to a virtual column which would be accessible as 
string representation in the object model?

Thanks,

-- 
Marek Šabo



Re: Mapping (My)SQL function

Posted by Andrus Adamchik <an...@objectstyle.org>.
Don't remember what was the thinking around the time we wrote about the Modeler support here. The way things are going now (with DI and stuff), our direction is to move extensions and runtime configs from the modeler and into the custom DI code as much as possible. So I will probably remove this line from the docs.

Andrus 


On Feb 12, 2011, at 11:04 PM, Marek Šabo wrote:
> Hi,
> 
> I just realized that while I wrote this I tried to find some info on extended types (http://cayenne.apache.org/doc30/extended-types.html). Maybe it would be good idea to include a short example how to implement one or at least a put note pointing user to existing types for inspiration in wiki.
> 
> Also there is note that modeller support will be added in future so I would like to ask what's the current status.
> 
> Regards,
> 
> -- 
> Marek Šabo
> 
> 
> 


Re: Mapping (My)SQL function

Posted by Marek Šabo <ms...@buk.cvut.cz>.
Hi,

I just realized that while I wrote this I tried to find some info on 
extended types (http://cayenne.apache.org/doc30/extended-types.html). 
Maybe it would be good idea to include a short example how to implement 
one or at least a put note pointing user to existing types for 
inspiration in wiki.

Also there is note that modeller support will be added in future so I 
would like to ask what's the current status.

Regards,

-- 
Marek Šabo



Re: Mapping (My)SQL function

Posted by Marek Šabo <ms...@buk.cvut.cz>.
Hi,

here's the relevant code:

public class Inet4AddressType implements ExtendedType {

     @Override
     public String getClassName() {
         return Inet4Address.class.getName();
     }

     @Override
     public void setJdbcObject(PreparedStatement statement, Object 
value, int pos, int type, int scale) throws Exception {
         if (value == null) {
             statement.setNull(pos, type);
         } else {
             if (type == Types.BIGINT) {
                 byte[] ip = ((Inet4Address) value).getAddress();
                 long ipNum = 0;
                 ipNum += (long) (ip[0] & 0xFF) << 24; //TODO this needs 
to be long-cast or it will overflow
                 ipNum += (ip[1] & 0xFF) << 16;
                 ipNum += (ip[2] & 0xFF) << 8;
                 ipNum += (ip[3] & 0xFF);
                 statement.setLong(pos, ipNum);
             } else {
                 throw new IllegalArgumentException("Only BIGINT can be 
mapped as '" + getClassName() + "', got " + 
TypesMapping.getSqlNameByType(type));
             }
         }
     }

     @Override
     public Object materializeObject(ResultSet rs, int index, int type) 
throws Exception {
         if (type == Types.BIGINT) {
             long l = rs.getLong(index);
             return InetAddress.getByAddress(new byte[]{(byte) ((l >> 
24) & 0xFF),
                         (byte) ((l >> 16) & 0xFF),
                         (byte) ((l >> 8) & 0xFF),
                         (byte) (l)});
         } else {
             throw new IllegalArgumentException(getClassName() + "' can 
only be constructed from BIGINT, got " + 
TypesMapping.getSqlNameByType(type));
         }
     }
}


Re: Mapping (My)SQL function

Posted by Michael Gentry <mg...@masslight.net>.
Hi Marek,

If you don't mind sharing, I'm sure many would like to see it.

Thanks,

mrg


On Thu, Feb 10, 2011 at 5:08 PM, Marek Šabo <ms...@buk.cvut.cz> wrote:
> Hi Michael,
>
> I fooled around a little bit and settled down with extended type
> implementation for Inet4Address. Should anyone want to see the code I can
> post it.
>
> Regards,
>
> marek
>
> On 02/09/2011 03:30 PM, Michael Gentry wrote:
>>
>> Hi Marek,
>>
>> I'd probably just wrap it in utility methods, but I tend to take the
>> simple way out.  Like Andrus, I'd be curious to know if you find a
>> different way.
>>
>> Thanks,
>>
>> mrg
>>
>>
>> On Mon, Feb 7, 2011 at 2:41 PM, Marek Šabo<ms...@buk.cvut.cz>  wrote:
>>>
>>> Hi,
>>>
>>> I would like to ask for advice, what would be a good way to get outcome
>>> of
>>> (my)sql functions in cayenne. Is there any other way than calling it from
>>> template queries? E.g. would it be possible to map somehow
>>> INET_NTOA(ipAddrColumn) to a virtual column which would be accessible as
>>> string representation in the object model?
>>>
>>> Thanks,
>>>
>>> --
>>> Marek Šabo
>>>
>>>
>>>
>
>
> --
> Marek Šabo
> Server Manager
> Club SU CVUT Buben
> Bubenečská Kolej (421)
> XMPP: zeratul021@gmail.com
>
>

Re: Mapping (My)SQL function

Posted by Marek Šabo <ms...@buk.cvut.cz>.
Hi Michael,

I fooled around a little bit and settled down with extended type 
implementation for Inet4Address. Should anyone want to see the code I 
can post it.

Regards,

marek

On 02/09/2011 03:30 PM, Michael Gentry wrote:
> Hi Marek,
>
> I'd probably just wrap it in utility methods, but I tend to take the
> simple way out.  Like Andrus, I'd be curious to know if you find a
> different way.
>
> Thanks,
>
> mrg
>
>
> On Mon, Feb 7, 2011 at 2:41 PM, Marek Šabo<ms...@buk.cvut.cz>  wrote:
>> Hi,
>>
>> I would like to ask for advice, what would be a good way to get outcome of
>> (my)sql functions in cayenne. Is there any other way than calling it from
>> template queries? E.g. would it be possible to map somehow
>> INET_NTOA(ipAddrColumn) to a virtual column which would be accessible as
>> string representation in the object model?
>>
>> Thanks,
>>
>> --
>> Marek Šabo
>>
>>
>>


-- 
Marek Šabo
Server Manager
Club SU CVUT Buben
Bubenečská Kolej (421)
XMPP: zeratul021@gmail.com


Re: Mapping (My)SQL function

Posted by Michael Gentry <mg...@masslight.net>.
Hi Marek,

I'd probably just wrap it in utility methods, but I tend to take the
simple way out.  Like Andrus, I'd be curious to know if you find a
different way.

Thanks,

mrg


On Mon, Feb 7, 2011 at 2:41 PM, Marek Šabo <ms...@buk.cvut.cz> wrote:
> Hi,
>
> I would like to ask for advice, what would be a good way to get outcome of
> (my)sql functions in cayenne. Is there any other way than calling it from
> template queries? E.g. would it be possible to map somehow
> INET_NTOA(ipAddrColumn) to a virtual column which would be accessible as
> string representation in the object model?
>
> Thanks,
>
> --
> Marek Šabo
>
>
>

Re: Mapping (My)SQL function

Posted by Andrus Adamchik <an...@objectstyle.org>.
I can't predict the outcome, but certainly worth trying and reporting back the result.

Andrus

On Feb 7, 2011, at 2:41 PM, Marek Šabo wrote:

> Hi,
> 
> I would like to ask for advice, what would be a good way to get outcome of (my)sql functions in cayenne. Is there any other way than calling it from template queries? E.g. would it be possible to map somehow INET_NTOA(ipAddrColumn) to a virtual column which would be accessible as string representation in the object model?
> 
> Thanks,
> 
> -- 
> Marek Šabo
> 
> 
>