Hi there,
I've just added integration testing for BINARY(16) with UUID data types in MySQL. Below, you will find a Converter<byte[], UUID> implementation that will do the job for you. Note that currently, it is not possible to support both string-based and byte[]-based UUID conversion in jOOQ, as jOOQ currently allows only one Converter<U, T> for any U type.
Hope this helps
Lukas
The sample code generation configuration file:
The sample Converter implementation:
/**
* All rights reserved.
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOQ" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.jooq.test._.converters;
import java.util.UUID;
import org.jooq.Converter;
public class UUIDBinaryConverter implements Converter<byte[], UUID> {
/**
* Generated UID
*/
private static final long serialVersionUID = -4543995777404574519L;
@Override
public UUID from(byte[] data) {
if (data == null)
return null;
long msb = 0;
long lsb = 0;
for (int i = 0; i < 8; i++)
msb = (msb << 8) | (data[i] & 0xff);
for (int i = 8; i < 16; i++)
lsb = (lsb << 8) | (data[i] & 0xff);
return new UUID(msb, lsb);
}
@Override
public byte[] to(UUID data) {
if (data == null)
return null;
byte[] result = new byte[16];
long msb = data.getMostSignificantBits();
long lsb = data.getLeastSignificantBits();
for (int i = 7; i >= 0; i--) {
result[i] = (byte) (msb & 0xFF);
msb >>= 8;
}
for (int i = 15; i >= 8; i--) {
result[i] = (byte) (lsb & 0xFF);
lsb >>= 8;
}
return result;
}
@Override
public Class<byte[]> fromType() {
return byte[].class;
}
@Override
public Class<UUID> toType() {
return UUID.class;
}
}