ByteStringColumnMapper.java

/*
 * Copyright 2018-2019 Providence Authors
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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 net.morimekta.proto.jdbi.v3;

import com.google.protobuf.ByteString;
import org.jdbi.v3.core.mapper.ColumnMapper;
import org.jdbi.v3.core.result.ResultSetException;
import org.jdbi.v3.core.result.UnableToProduceResultException;
import org.jdbi.v3.core.statement.StatementContext;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Base64;

/**
 * Map a byte string column to value.
 */
public class ByteStringColumnMapper implements ColumnMapper<ByteString> {
    public static final ByteStringColumnMapper INSTANCE = new ByteStringColumnMapper();

    @Override
    public ByteString map(ResultSet rs, int i, StatementContext ctx) throws SQLException {
        int columnType = rs.getMetaData().getColumnType(i);
        switch (columnType) {
            case Types.BINARY:
            case Types.VARBINARY:
            case Types.LONGVARBINARY:
                InputStream is = rs.getBinaryStream(i);
                if (is != null) {
                    try (is) {
                        return ByteString.readFrom(is);
                    } catch (IOException e) {
                        throw new UnableToProduceResultException(e.getMessage(), e, ctx);
                    }
                }
                return null;
            case Types.BLOB:
                Blob blob = rs.getBlob(i);
                if (blob != null) {
                    try (var bs = blob.getBinaryStream()) {
                        return ByteString.readFrom(bs, (int) blob.length());
                    } catch (IOException e) {
                        throw new UnableToProduceResultException(e.getMessage(), e, ctx);
                    }
                }
                return null;
            case Types.CHAR:
            case Types.VARCHAR:
            case Types.LONGVARCHAR:
            case Types.NCHAR:
            case Types.NVARCHAR:
            case Types.CLOB: {
                InputStream tmp = rs.getBinaryStream(i);
                if (tmp != null) {
                    try (var base64 = Base64.getDecoder().wrap(tmp)) {
                        return ByteString.readFrom(base64);
                    } catch (IOException e) {
                        throw new UnableToProduceResultException(e.getMessage(), e, ctx);
                    }
                }
                return null;
            }
            default:
                throw new ResultSetException(
                        "Unhandled column type " + rs.getMetaData().getColumnTypeName(i) +
                        "(" + columnType + ") for ByteString",
                        null, ctx);
        }
    }
}