All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.matteobertozzi.easerinsights.jdbc.sql.impl.SqlResultImpl Maven / Gradle / Ivy

The newest version!
/*
 * 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 io.github.matteobertozzi.easerinsights.jdbc.sql.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import io.github.matteobertozzi.easerinsights.jdbc.sql.SqlResultRow;
import io.github.matteobertozzi.easerinsights.jdbc.sql.SqlTableSchema.SqlField;
import io.github.matteobertozzi.easerinsights.jdbc.sql.SqlTableSchema.SqlFieldType;
import io.github.matteobertozzi.rednaco.collections.sets.HashIndexedArray;

public class SqlResultImpl implements SqlResultRow {
  private final HashIndexedArray paramIndex;
  private final ResultSet rs;

  public SqlResultImpl(final ResultSet rs, final SqlField[] fields) {
    this.paramIndex = new HashIndexedArray<>(fields);
    this.rs = rs;
  }

  @Override
  public byte getByte(final SqlField field) throws SQLException {
    verifyType(field, SqlFieldType.BYTE);
    return rs.getByte(getRsFieldIndex(field));
  }

  @Override
  public Byte getNullableByte(final SqlField field) throws SQLException {
    verifyNullableType(field, SqlFieldType.BYTE);
    final byte v = rs.getByte(getRsFieldIndex(field));
    return rs.wasNull() ? null : v;
  }

  @Override
  public short getShort(final SqlField field) throws SQLException {
    verifyType(field, SqlFieldType.SHORT);
    return rs.getShort(getRsFieldIndex(field));
  }

  @Override
  public Short getNullableShort(final SqlField field) throws SQLException {
    verifyNullableType(field, SqlFieldType.SHORT);
    final short v = rs.getShort(getRsFieldIndex(field));
    return rs.wasNull() ? null : v;
  }

  @Override
  public int getInt(final SqlField field) throws SQLException {
    verifyType(field, SqlFieldType.INT);
    return rs.getInt(getRsFieldIndex(field));
  }

  @Override
  public Integer getNullableInt(final SqlField field) throws SQLException {
    verifyNullableType(field, SqlFieldType.INT);
    final int v = rs.getInt(getRsFieldIndex(field));
    return rs.wasNull() ? null : v;
  }

  @Override
  public long getLong(final SqlField field) throws SQLException {
    verifyType(field, SqlFieldType.LONG);
    return rs.getLong(getRsFieldIndex(field));
  }

  @Override
  public Long getNullableLong(final SqlField field) throws SQLException {
    verifyNullableType(field, SqlFieldType.LONG);
    final long v = rs.getLong(getRsFieldIndex(field));
    return rs.wasNull() ? null : v;
  }

  @Override
  public float getFloat(final SqlField field) throws SQLException {
    verifyType(field, SqlFieldType.FLOAT);
    return rs.getFloat(getRsFieldIndex(field));
  }

  @Override
  public Float getNullableFloat(final SqlField field) throws SQLException {
    verifyNullableType(field, SqlFieldType.FLOAT);
    final float v = rs.getFloat(getRsFieldIndex(field));
    return rs.wasNull() ? null : v;
  }

  @Override
  public double getDouble(final SqlField field) throws SQLException {
    verifyType(field, SqlFieldType.DOUBLE);
    return rs.getDouble(getRsFieldIndex(field));
  }

  @Override
  public Double getNullableDouble(final SqlField field) throws SQLException {
    verifyNullableType(field, SqlFieldType.DOUBLE);
    final double v = rs.getDouble(getRsFieldIndex(field));
    return rs.wasNull() ? null : v;
  }

  @Override
  public boolean getBoolean(final SqlField field) throws SQLException {
    verifyType(field, SqlFieldType.BOOLEAN);
    return rs.getBoolean(getRsFieldIndex(field));
  }

  @Override
  public Boolean getNullableBoolean(final SqlField field) throws SQLException {
    verifyNullableType(field, SqlFieldType.BOOLEAN);
    final boolean v = rs.getBoolean(getRsFieldIndex(field));
    return rs.wasNull() ? null : v;
  }

  @Override
  public byte[] getBytes(final SqlField field) throws SQLException {
    verifyNullableType(field, SqlFieldType.BYTES);
    return rs.getBytes(getRsFieldIndex(field));
  }

  @Override
  public String getString(final SqlField field) throws SQLException {
    verifyNullableType(field, SqlFieldType.STRING);
    return rs.getString(getRsFieldIndex(field));
  }

  private int getRsFieldIndex(final SqlField field) {
    return 1 + paramIndex.getIndex(field);
  }

  private static void verifyType(final SqlField field, final SqlFieldType expectedType) {
    if (field.type() != expectedType) {
      throw new IllegalArgumentException("fetch for expected type: " + expectedType + " got " + field);
    }
    if (field.nullable()) {
      throw new IllegalArgumentException("fetch for non nullable type: " + expectedType + " got " + field);
    }
  }

  private static void verifyNullableType(final SqlField field, final SqlFieldType expectedType) {
    if (field.type() != expectedType) {
      throw new IllegalArgumentException("fetch for expected type: " + expectedType + " got " + field);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy