/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.facebook.presto.raptor.util;

import org.skife.jdbi.v2.ResultSetMapperFactory;
import org.skife.jdbi.v2.StatementContext;
import org.skife.jdbi.v2.tweak.Argument;
import org.skife.jdbi.v2.tweak.ArgumentFactory;
import org.skife.jdbi.v2.tweak.ResultSetMapper;
import org.skife.jdbi.v2.util.TypedMapper;

import java.nio.ByteBuffer;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;

import static com.google.common.base.Preconditions.checkNotNull;

public final class UuidUtil
{
    private UuidUtil() {}

    public static final class UuidArgumentFactory
            implements ArgumentFactory<UUID>
    {
        @Override
        public boolean accepts(Class<?> expectedType, Object value, StatementContext ctx)
        {
            return value instanceof UUID;
        }

        @Override
        public Argument build(Class<?> expectedType, UUID value, StatementContext ctx)
        {
            return new UuidArgument(value);
        }
    }

    public static final class UuidArgument
            implements Argument
    {
        private final byte[] value;

        public UuidArgument(UUID value)
        {
            this.value = uuidToBytes(checkNotNull(value, "value is null"));
        }

        @Override
        public void apply(int position, PreparedStatement statement, StatementContext ctx)
                throws SQLException
        {
            statement.setBytes(position, value);
        }
    }

    @SuppressWarnings("rawtypes")
    public static class UuidMapperFactory
            implements ResultSetMapperFactory
    {
        @Override
        public boolean accepts(Class type, StatementContext ctx)
        {
            return type == UUID.class;
        }

        @Override
        public ResultSetMapper mapperFor(Class type, StatementContext ctx)
        {
            return new UuidMapper();
        }
    }

    public static final class UuidMapper
            extends TypedMapper<UUID>
    {
        @Override
        protected UUID extractByName(ResultSet r, String name)
                throws SQLException
        {
            return uuidFromBytes(r.getBytes(name));
        }

        @Override
        protected UUID extractByIndex(ResultSet r, int index)
                throws SQLException
        {
            return uuidFromBytes(r.getBytes(index));
        }
    }

    public static UUID uuidFromBytes(byte[] bytes)
    {
        ByteBuffer buffer = ByteBuffer.wrap(bytes);
        long msb = buffer.getLong();
        long lsb = buffer.getLong();
        return new UUID(msb, lsb);
    }

    public static byte[] uuidToBytes(UUID uuid)
    {
        return ByteBuffer.allocate(16)
                .putLong(uuid.getMostSignificantBits())
                .putLong(uuid.getLeastSignificantBits())
                .array();
    }
}

@@ -1006,83 +1006,40 @@
 ava.
-util.UUID;%0A%0Aimport static com.google.common.base.Preconditions.checkNotNull
+sql.Types;%0Aimport java.util.UUID
 ;%0A%0Ap
@@ -1649,20 +1649,17 @@
 nal 
-byte%5B%5D value
+UUID uuid
 ;%0A%0A 
@@ -1690,21 +1690,20 @@
 nt(UUID 
-value
+uuid
 )%0A      
@@ -1727,65 +1727,19 @@
 his.
-value
+uuid
  = uuid
-ToBytes(checkNotNull(value, %22value is null%22))
 ;%0A  
@@ -1918,42 +1918,312 @@
     
-statement.setBytes(position, value
+if (uuid == null) %7B%0A                statement.setNull(position, Types.VARBINARY);%0A            %7D%0A            else %7B%0A                statement.setBytes(position, uuidToBytes(uuid));%0A            %7D%0A        %7D%0A%0A        @Override%0A        public String toString()%0A        %7B%0A            return String.valueOf(uuid
 );%0A 

Handle null and toString in UuidArgument