package com.airlock.iam.base.infrastructure.db;

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

public final class BooleanPersistencyHelper {
   private BooleanPersistencyHelper() {
      throw new AssertionError();
   }

   public static void setBoolean(PreparedStatement statement, int position, boolean value) throws SQLException {
      statement.setInt(position, value ? 1 : 0);
   }

   public static boolean getBoolean(ResultSet resultSet, int columnIndex) throws SQLException {
      return resultSet.getInt(columnIndex) != 0;
   }

   public static boolean getBoolean(ResultSet resultSet, String columnName) throws SQLException {
      return resultSet.getInt(columnName) != 0;
   }

   public static boolean getBoolean(ResultSet resultSet, String columnName, boolean defaultValue) throws SQLException {
      boolean value = getBoolean(resultSet, columnName);
      return resultSet.wasNull() ? defaultValue : value;
   }

   public static boolean getBoolean(ResultSet resultSet, int columnNumber, boolean defaultValue) throws SQLException {
      boolean value = getBoolean(resultSet, columnNumber);
      return resultSet.wasNull() ? defaultValue : value;
   }
}
