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

org.nkjmlab.sorm4j.context.DefaultTableMetaDataParser Maven / Gradle / Ivy

There is a newer version: 2.1.7
Show newest version
package org.nkjmlab.sorm4j.context;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.nkjmlab.sorm4j.common.ColumnMetaData;

public final class DefaultTableMetaDataParser implements TableMetaDataParser {
  /** @see DatabaseMetaData#getColumns(String, String, String, String) */
  @Override
  public List getAutoGeneratedColumns(DatabaseMetaData metaData, String tableName)
      throws SQLException {
    try (ResultSet resultSet =
        metaData.getColumns(null, getSchemaPattern(metaData), tableName, "%")) {
      final List columnsList = new ArrayList<>();
      while (resultSet.next()) {
        String columnName = resultSet.getString(4);
        String isAutoIncrement = resultSet.getString(23);
        String isGenerated = resultSet.getString(24);
        if (isAutoIncrement.equals("YES") || isGenerated.equals("YES")) {
          columnsList.add(columnName);
        }
      }
      return columnsList;
    }
  }

  @Override
  public List getColumnsMetaData(DatabaseMetaData metaData, String tableName)
      throws SQLException {
    try (ResultSet resultSet =
        metaData.getColumns(null, getSchemaPattern(metaData), tableName, "%")) {
      final List columnsList = new ArrayList<>();
      while (resultSet.next()) {
        String columnName = resultSet.getString(4);
        int dataType = resultSet.getInt(5);
        String typeName = resultSet.getString(6);
        int ordinalPosition = resultSet.getInt(17);
        String isNullable = resultSet.getString(18);
        String isAutoIncremented = resultSet.getString(23);
        String isGenerated = resultSet.getString(24);
        columnsList.add(
            new ColumnMetaData(
                columnName,
                dataType,
                typeName,
                ordinalPosition,
                isNullable,
                isAutoIncremented,
                isGenerated));
      }
      return columnsList;
    }
  }

  @Override
  public List getPrimaryKeys(DatabaseMetaData metaData, String tableName)
      throws SQLException {
    final List primaryKeysList = new ArrayList<>();
    try (ResultSet resultSet =
        metaData.getPrimaryKeys(null, getSchemaPattern(metaData), tableName)) {
      while (resultSet.next()) {
        final String columnName = resultSet.getString(4);
        primaryKeysList.add(columnName);
      }
      return primaryKeysList;
    }
  }

  /**
   * Gets schema pattern for accessing {@link DatabaseMetaData}.
   *
   * @param metaData
   * @return
   * @throws SQLException
   */
  private String getSchemaPattern(DatabaseMetaData metaData) throws SQLException {
    // oracle expects a pattern such as "%" to work
    return "Oracle".equalsIgnoreCase(metaData.getDatabaseProductName()) ? "%" : null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy