Search in sources :

Example 1 with DatabaseMetaDataCallback

use of org.springframework.jdbc.support.DatabaseMetaDataCallback in project spring-framework by spring-projects.

the class CallMetaDataProviderFactory method createMetaDataProvider.

/**
	 * Create a CallMetaDataProvider based on the database metadata
	 * @param dataSource used to retrieve metadata
	 * @param context the class that holds configuration and metadata
	 * @return instance of the CallMetaDataProvider implementation to be used
	 */
public static CallMetaDataProvider createMetaDataProvider(DataSource dataSource, final CallMetaDataContext context) {
    try {
        return (CallMetaDataProvider) JdbcUtils.extractDatabaseMetaData(dataSource, new DatabaseMetaDataCallback() {

            @Override
            public Object processMetaData(DatabaseMetaData databaseMetaData) throws SQLException, MetaDataAccessException {
                String databaseProductName = JdbcUtils.commonDatabaseName(databaseMetaData.getDatabaseProductName());
                boolean accessProcedureColumnMetaData = context.isAccessCallParameterMetaData();
                if (context.isFunction()) {
                    if (!supportedDatabaseProductsForFunctions.contains(databaseProductName)) {
                        if (logger.isWarnEnabled()) {
                            logger.warn(databaseProductName + " is not one of the databases fully supported for function calls " + "-- supported are: " + supportedDatabaseProductsForFunctions);
                        }
                        if (accessProcedureColumnMetaData) {
                            logger.warn("Metadata processing disabled - you must specify all parameters explicitly");
                            accessProcedureColumnMetaData = false;
                        }
                    }
                } else {
                    if (!supportedDatabaseProductsForProcedures.contains(databaseProductName)) {
                        if (logger.isWarnEnabled()) {
                            logger.warn(databaseProductName + " is not one of the databases fully supported for procedure calls " + "-- supported are: " + supportedDatabaseProductsForProcedures);
                        }
                        if (accessProcedureColumnMetaData) {
                            logger.warn("Metadata processing disabled - you must specify all parameters explicitly");
                            accessProcedureColumnMetaData = false;
                        }
                    }
                }
                CallMetaDataProvider provider;
                if ("Oracle".equals(databaseProductName)) {
                    provider = new OracleCallMetaDataProvider(databaseMetaData);
                } else if ("DB2".equals(databaseProductName)) {
                    provider = new Db2CallMetaDataProvider((databaseMetaData));
                } else if ("Apache Derby".equals(databaseProductName)) {
                    provider = new DerbyCallMetaDataProvider((databaseMetaData));
                } else if ("PostgreSQL".equals(databaseProductName)) {
                    provider = new PostgresCallMetaDataProvider((databaseMetaData));
                } else if ("Sybase".equals(databaseProductName)) {
                    provider = new SybaseCallMetaDataProvider((databaseMetaData));
                } else if ("Microsoft SQL Server".equals(databaseProductName)) {
                    provider = new SqlServerCallMetaDataProvider((databaseMetaData));
                } else if ("HDB".equals(databaseProductName)) {
                    provider = new HanaCallMetaDataProvider((databaseMetaData));
                } else {
                    provider = new GenericCallMetaDataProvider(databaseMetaData);
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("Using " + provider.getClass().getName());
                }
                provider.initializeWithMetaData(databaseMetaData);
                if (accessProcedureColumnMetaData) {
                    provider.initializeWithProcedureColumnMetaData(databaseMetaData, context.getCatalogName(), context.getSchemaName(), context.getProcedureName());
                }
                return provider;
            }
        });
    } catch (MetaDataAccessException ex) {
        throw new DataAccessResourceFailureException("Error retrieving database metadata", ex);
    }
}
Also used : DatabaseMetaDataCallback(org.springframework.jdbc.support.DatabaseMetaDataCallback) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) DatabaseMetaData(java.sql.DatabaseMetaData) MetaDataAccessException(org.springframework.jdbc.support.MetaDataAccessException)

Example 2 with DatabaseMetaDataCallback

use of org.springframework.jdbc.support.DatabaseMetaDataCallback in project spring-framework by spring-projects.

the class TableMetaDataProviderFactory method createMetaDataProvider.

/**
	 * Create a TableMetaDataProvider based on the database metadata.
	 * @param dataSource used to retrieve metadata
	 * @param context the class that holds configuration and metadata
	 * @return instance of the TableMetaDataProvider implementation to be used
	 */
public static TableMetaDataProvider createMetaDataProvider(DataSource dataSource, TableMetaDataContext context) {
    try {
        return (TableMetaDataProvider) JdbcUtils.extractDatabaseMetaData(dataSource, new DatabaseMetaDataCallback() {

            @Override
            public Object processMetaData(DatabaseMetaData databaseMetaData) throws SQLException {
                String databaseProductName = JdbcUtils.commonDatabaseName(databaseMetaData.getDatabaseProductName());
                boolean accessTableColumnMetaData = context.isAccessTableColumnMetaData();
                TableMetaDataProvider provider;
                if ("Oracle".equals(databaseProductName)) {
                    provider = new OracleTableMetaDataProvider(databaseMetaData, context.isOverrideIncludeSynonymsDefault());
                } else if ("HSQL Database Engine".equals(databaseProductName)) {
                    provider = new HsqlTableMetaDataProvider(databaseMetaData);
                } else if ("PostgreSQL".equals(databaseProductName)) {
                    provider = new PostgresTableMetaDataProvider(databaseMetaData);
                } else if ("Apache Derby".equals(databaseProductName)) {
                    provider = new DerbyTableMetaDataProvider(databaseMetaData);
                } else {
                    provider = new GenericTableMetaDataProvider(databaseMetaData);
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("Using " + provider.getClass().getSimpleName());
                }
                provider.initializeWithMetaData(databaseMetaData);
                if (accessTableColumnMetaData) {
                    provider.initializeWithTableColumnMetaData(databaseMetaData, context.getCatalogName(), context.getSchemaName(), context.getTableName());
                }
                return provider;
            }
        });
    } catch (MetaDataAccessException ex) {
        throw new DataAccessResourceFailureException("Error retrieving database metadata", ex);
    }
}
Also used : MetaDataAccessException(org.springframework.jdbc.support.MetaDataAccessException) DatabaseMetaDataCallback(org.springframework.jdbc.support.DatabaseMetaDataCallback) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) DatabaseMetaData(java.sql.DatabaseMetaData)

Aggregations

DatabaseMetaData (java.sql.DatabaseMetaData)2 DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)2 DatabaseMetaDataCallback (org.springframework.jdbc.support.DatabaseMetaDataCallback)2 MetaDataAccessException (org.springframework.jdbc.support.MetaDataAccessException)2