Example usage for java.sql DatabaseMetaData storesUpperCaseIdentifiers

List of usage examples for java.sql DatabaseMetaData storesUpperCaseIdentifiers

Introduction

In this page you can find the example usage for java.sql DatabaseMetaData storesUpperCaseIdentifiers.

Prototype

boolean storesUpperCaseIdentifiers() throws SQLException;

Source Link

Document

Retrieves whether this database treats mixed case unquoted SQL identifiers as case insensitive and stores them in upper case.

Usage

From source file:org.dspace.storage.rdbms.DatabaseUtils.java

/**
 * Return the canonical name for a database identifier based on whether this
 * database defaults to storing identifiers in uppercase or lowercase.
 *
 * @param connection //from w  w  w. ja v  a 2  s .  co m
 *     Current Database Connection
 * @param dbIdentifier 
 *     Identifier to canonicalize (may be a table name, column name, etc)
 * @return The canonical name of the identifier.
 * @throws SQLException
 *     An exception that provides information on a database access error or other errors.
 */
public static String canonicalize(Connection connection, String dbIdentifier) throws SQLException {
    // Avoid any null pointers
    if (dbIdentifier == null)
        return null;

    DatabaseMetaData meta = connection.getMetaData();

    // Check how this database stores its identifiers, etc.
    // i.e. lowercase vs uppercase (by default we assume mixed case)
    if (meta.storesLowerCaseIdentifiers()) {
        return StringUtils.lowerCase(dbIdentifier);

    } else if (meta.storesUpperCaseIdentifiers()) {
        return StringUtils.upperCase(dbIdentifier);
    } else // Otherwise DB doesn't care about case
    {
        return dbIdentifier;
    }
}

From source file:org.jtester.module.database.support.DbSupport.java

/**
 * Determines the case the database uses to store non-quoted identifiers.
 * This will use the connections database metadata to determine the correct
 * case./*from   w  w w  .j  a  v a  2 s .c  o  m*/
 * 
 * @param storedIdentifierCase
 *            The stored case: possible values 'lower_case', 'upper_case',
 *            'mixed_case' and 'auto'
 * @return The stored case, not null
 */
private StoredIdentifierCase determineStoredIdentifierCase(String storedIdentifierCase) {
    if ("lower_case".equals(storedIdentifierCase)) {
        return LOWER_CASE;
    } else if ("upper_case".equals(storedIdentifierCase)) {
        return UPPER_CASE;
    } else if ("mixed_case".equals(storedIdentifierCase)) {
        return MIXED_CASE;
    } else if (!"auto".equals(storedIdentifierCase)) {
        throw new JTesterException(
                "Unknown value " + storedIdentifierCase + " for property " + PROPKEY_STORED_IDENTIFIER_CASE
                        + ". It should be one of lower_case, upper_case, mixed_case or auto.");
    }

    Connection connection = null;
    try {
        connection = getSQLHandler().getDataSource().getConnection();

        DatabaseMetaData databaseMetaData = connection.getMetaData();
        if (databaseMetaData.storesUpperCaseIdentifiers()) {
            return UPPER_CASE;
        } else if (databaseMetaData.storesLowerCaseIdentifiers()) {
            return LOWER_CASE;
        } else {
            return MIXED_CASE;
        }
    } catch (SQLException e) {
        throw new JTesterException("Unable to determine stored identifier case.", e);
    } finally {
        closeQuietly(connection, null, null);
    }
}

From source file:org.mule.module.db.internal.resolver.param.StoredProcedureParamTypeResolver.java

private String getStoredProcedureName(DatabaseMetaData dbMetaData, String sqlText) throws SQLException {
    Matcher matcher = storedProcedureMatcher.matcher(sqlText);

    if (!matcher.matches()) {
        throw new SQLException("Unable to detect stored procedure name from " + sqlText);
    }//from www.j  ava 2 s  .co  m

    String storedProcedureName = matcher.group(2);

    if (dbMetaData.storesUpperCaseIdentifiers()) {
        return storedProcedureName.toUpperCase();
    } else {
        return storedProcedureName;
    }
}

From source file:org.springframework.jdbc.core.metadata.GenericCallMetaDataProvider.java

@Override
public void initializeWithMetaData(DatabaseMetaData databaseMetaData) throws SQLException {
    try {//from www .j a v  a 2  s .  co m
        setSupportsCatalogsInProcedureCalls(databaseMetaData.supportsCatalogsInProcedureCalls());
    } catch (SQLException ex) {
        if (logger.isWarnEnabled()) {
            logger.warn(
                    "Error retrieving 'DatabaseMetaData.supportsCatalogsInProcedureCalls': " + ex.getMessage());
        }
    }
    try {
        setSupportsSchemasInProcedureCalls(databaseMetaData.supportsSchemasInProcedureCalls());
    } catch (SQLException ex) {
        if (logger.isWarnEnabled()) {
            logger.warn(
                    "Error retrieving 'DatabaseMetaData.supportsSchemasInProcedureCalls': " + ex.getMessage());
        }
    }
    try {
        setStoresUpperCaseIdentifiers(databaseMetaData.storesUpperCaseIdentifiers());
    } catch (SQLException ex) {
        if (logger.isWarnEnabled()) {
            logger.warn("Error retrieving 'DatabaseMetaData.storesUpperCaseIdentifiers': " + ex.getMessage());
        }
    }
    try {
        setStoresLowerCaseIdentifiers(databaseMetaData.storesLowerCaseIdentifiers());
    } catch (SQLException ex) {
        if (logger.isWarnEnabled()) {
            logger.warn("Error retrieving 'DatabaseMetaData.storesLowerCaseIdentifiers': " + ex.getMessage());
        }
    }
}

From source file:org.springframework.jdbc.core.metadata.GenericTableMetaDataProvider.java

@Override
public void initializeWithMetaData(DatabaseMetaData databaseMetaData) throws SQLException {
    try {//from   w  ww .j ava2s. c om
        if (databaseMetaData.supportsGetGeneratedKeys()) {
            logger.debug("GetGeneratedKeys is supported");
            setGetGeneratedKeysSupported(true);
        } else {
            logger.debug("GetGeneratedKeys is not supported");
            setGetGeneratedKeysSupported(false);
        }
    } catch (SQLException ex) {
        if (logger.isWarnEnabled()) {
            logger.warn("Error retrieving 'DatabaseMetaData.getGeneratedKeys': " + ex.getMessage());
        }
    }
    try {
        String databaseProductName = databaseMetaData.getDatabaseProductName();
        if (this.productsNotSupportingGeneratedKeysColumnNameArray.contains(databaseProductName)) {
            if (logger.isDebugEnabled()) {
                logger.debug("GeneratedKeysColumnNameArray is not supported for " + databaseProductName);
            }
            setGeneratedKeysColumnNameArraySupported(false);
        } else {
            if (isGetGeneratedKeysSupported()) {
                if (logger.isDebugEnabled()) {
                    logger.debug("GeneratedKeysColumnNameArray is supported for " + databaseProductName);
                }
                setGeneratedKeysColumnNameArraySupported(true);
            } else {
                setGeneratedKeysColumnNameArraySupported(false);
            }
        }
    } catch (SQLException ex) {
        if (logger.isWarnEnabled()) {
            logger.warn("Error retrieving 'DatabaseMetaData.getDatabaseProductName': " + ex.getMessage());
        }
    }

    try {
        this.databaseVersion = databaseMetaData.getDatabaseProductVersion();
    } catch (SQLException ex) {
        if (logger.isWarnEnabled()) {
            logger.warn("Error retrieving 'DatabaseMetaData.getDatabaseProductVersion': " + ex.getMessage());
        }
    }

    try {
        setStoresUpperCaseIdentifiers(databaseMetaData.storesUpperCaseIdentifiers());
    } catch (SQLException ex) {
        if (logger.isWarnEnabled()) {
            logger.warn("Error retrieving 'DatabaseMetaData.storesUpperCaseIdentifiers': " + ex.getMessage());
        }
    }

    try {
        setStoresLowerCaseIdentifiers(databaseMetaData.storesLowerCaseIdentifiers());
    } catch (SQLException ex) {
        if (logger.isWarnEnabled()) {
            logger.warn("Error retrieving 'DatabaseMetaData.storesLowerCaseIdentifiers': " + ex.getMessage());
        }
    }
}

From source file:org.unitils.core.dbsupport.DbSupport.java

/**
 * Determines the case the database uses to store non-quoted identifiers. This will use the connections
 * database metadata to determine the correct case.
 *
 * @param storedIdentifierCase The stored case: possible values 'lower_case', 'upper_case', 'mixed_case' and 'auto'
 * @return The stored case, not null//from   w  ww . j a v  a  2  s.  com
 */
private StoredIdentifierCase determineStoredIdentifierCase(String storedIdentifierCase) {
    if ("lower_case".equals(storedIdentifierCase)) {
        return LOWER_CASE;
    } else if ("upper_case".equals(storedIdentifierCase)) {
        return UPPER_CASE;
    } else if ("mixed_case".equals(storedIdentifierCase)) {
        return MIXED_CASE;
    } else if (!"auto".equals(storedIdentifierCase)) {
        throw new UnitilsException(
                "Unknown value " + storedIdentifierCase + " for property " + PROPKEY_STORED_IDENTIFIER_CASE
                        + ". It should be one of lower_case, upper_case, mixed_case or auto.");
    }

    Connection connection = null;
    try {
        connection = getSQLHandler().getDataSource().getConnection();

        DatabaseMetaData databaseMetaData = connection.getMetaData();
        if (databaseMetaData.storesUpperCaseIdentifiers()) {
            return UPPER_CASE;
        } else if (databaseMetaData.storesLowerCaseIdentifiers()) {
            return LOWER_CASE;
        } else {
            return MIXED_CASE;
        }
    } catch (SQLException e) {
        throw new UnitilsException("Unable to determine stored identifier case.", e);
    } finally {
        closeQuietly(connection, null, null);
    }
}