Example usage for java.sql DatabaseMetaData storesLowerCaseIdentifiers

List of usage examples for java.sql DatabaseMetaData storesLowerCaseIdentifiers

Introduction

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

Prototype

boolean storesLowerCaseIdentifiers() throws SQLException;

Source Link

Document

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

Usage

From source file:org.dashbuilder.dataprovider.backend.sql.JDBCUtils.java

public static String fixCase(Connection connection, String id) {
    try {/*  w w w.  j  av  a2 s .co m*/
        DatabaseMetaData meta = connection.getMetaData();
        if (meta.storesLowerCaseIdentifiers()) {
            return changeCaseExcludeQuotes(id, false);
        }
        if (meta.storesUpperCaseIdentifiers()) {
            return changeCaseExcludeQuotes(id, true);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return id;
}

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 //ww w  .  j  a va2 s .  c om
 *     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.//  w  w w. j av  a 2 s. co 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.springframework.jdbc.core.metadata.GenericCallMetaDataProvider.java

@Override
public void initializeWithMetaData(DatabaseMetaData databaseMetaData) throws SQLException {
    try {/*from  w ww  .j a  va 2s  .c  o  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 w  w .j  av a 2 s  . c  o m*/
        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// w  w  w. j a va2  s. c  o  m
 */
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);
    }
}