UUID's within MYSQL

1,413 views
Skip to first unread message

Roger Thomas

unread,
Jun 12, 2013, 11:49:25 AM6/12/13
to jooq...@googlegroups.com
Hi, I'm currently looking at an issue which requires UUID support in a MYSQL world.

As standard MYSQL does not have a UUID type, instead the 2 normal solutions are to store the value in a varchar(36) or a BYTE(16), with the BYTE solution being recommended due to the reduced database storage requirements and better index performance.

What support in jOOQ is there for such mapping, or do I have to go down the route of Custom data types and type conversion ?


Thanks

Lukas Eder

unread,
Jun 18, 2013, 3:46:46 AM6/18/13
to jooq...@googlegroups.com, Roger Thomas
Hi Roger,

I'm sorry, this mail was lost in my inbox.

java.util.UUID types are supported since jOOQ 3.0:

If your database doesn't natively support UUIDs (e.g. MySQL), you can let the jOOQ code generator force UUID types upon your column:

        <forcedType>
            <name>UUID</name>
            <expressions>(?i:(.*?.)?T_EXOTIC_TYPES.UU)</expressions>
        </forcedType>

A similar usage of forcedType can be seen here:

Using VARCHAR(36) is certainly going to work. I have never tried using BYTE(16) yet. Should you try the latter, feel free to provide feedback on how this works out.

Cheers
Lukas


2013/6/12 Roger Thomas <rithom...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Roger Thomas

unread,
Jun 18, 2013, 7:17:04 PM6/18/13
to jooq...@googlegroups.com, Roger Thomas
Thanks for the reply, I'm currently planning to go with VARCHAR(36) as I don't have a space problem and anyway it will be easier to decode/view the results:)

If I get some R&D time, I'll try BYTE(16) and let you know.

Lukas Eder

unread,
Jun 20, 2013, 2:39:19 AM6/20/13
to jooq...@googlegroups.com
OK. I will add integration tests to ensure that BINARY(16) or similar types work well with jOOQ's mapping them to UUID, in all databases:
 
Cheers
Lukas


2013/6/19 Roger Thomas <rithom...@gmail.com>
Thanks for the reply, I'm currently planning to go with VARCHAR(36) as I don't have a space problem and anyway it will be easier to decode/view the results:)

If I get some R&D time, I'll try BYTE(16) and let you know.

Lukas Eder

unread,
Jun 22, 2013, 6:29:54 AM6/22/13
to jooq...@googlegroups.com
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:

/**
* Copyright (c) 2009-2013, Lukas Eder, lukas...@gmail.com
* 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;
    }
}


2013/6/20 Lukas Eder <lukas...@gmail.com>
Reply all
Reply to author
Forward
0 new messages