List of usage examples for org.springframework.jdbc.support JdbcUtils extractDatabaseMetaData
@SuppressWarnings("unchecked") public static <T> T extractDatabaseMetaData(DataSource dataSource, final String metaDataMethodName) throws MetaDataAccessException
From source file:org.jtalks.poulpe.util.databasebackup.persistence.DbTableKeys.java
/** * Obtain from the database a list of tables' primary keys. * // ww w . jav a 2 s. c o m * @return A list of {@link UniqueKey} object represented foreign keys. * @throws SQLException * Is thrown in case any errors during work with database occur. */ @SuppressWarnings("unchecked") public Set<UniqueKey> getPrimaryKeys() throws SQLException { if (primaryKeys != null) { return primaryKeys; } Set<UniqueKey> tablePrimaryKeySet = null; try { tablePrimaryKeySet = (Set<UniqueKey>) JdbcUtils.extractDatabaseMetaData(dataSource, new KeyListProcessor(tableName, new TableKeyPerformer() { @Override public ResultSet getResultSet(DatabaseMetaData dmd, String tableName) throws SQLException { return dmd.getPrimaryKeys(null, null, tableName); } @Override public void addKeyToSet(ResultSet rs, Set<TableKey> keySet) throws SQLException { if (rs.getString(PK_NAME) != null && rs.getString(COLUMN_NAME) != null) { keySet.add(new UniqueKey(rs.getString(PK_NAME), rs.getString(COLUMN_NAME))); } } })); } catch (MetaDataAccessException e) { throw new SQLException(e); } primaryKeys = tablePrimaryKeySet; return tablePrimaryKeySet; }
From source file:grails.plugin.quartz2.LocalDataSourceJobStore.java
@Override public void initialize(ClassLoadHelper loadHelper, SchedulerSignaler signaler) throws SchedulerConfigException { // Absolutely needs thread-bound DataSource to initialize. this.dataSource = QuartzFactoryBean.getConfigTimeDataSource(); if (this.dataSource == null) { throw new SchedulerConfigException("No local DataSource found for configuration - " + "'dataSource' property must be set on SchedulerFactoryBean"); }// w ww .jav a2 s. c om // Configure transactional connection settings for Quartz. setDataSource(TX_DATA_SOURCE_PREFIX + getInstanceName()); setDontSetAutoCommitFalse(true); // Register transactional ConnectionProvider for Quartz. DBConnectionManager.getInstance().addConnectionProvider(TX_DATA_SOURCE_PREFIX + getInstanceName(), new ConnectionProvider() { public Connection getConnection() throws SQLException { // Return a transactional Connection, if any. return DataSourceUtils.doGetConnection(dataSource); } public void shutdown() { // Do nothing - a Spring-managed DataSource has its own lifecycle. } }); // Configure non-transactional connection settings for Quartz. setNonManagedTXDataSource(NON_TX_DATA_SOURCE_PREFIX + getInstanceName()); final DataSource nonTxDataSourceToUse = this.dataSource; // Register non-transactional ConnectionProvider for Quartz. DBConnectionManager.getInstance().addConnectionProvider(NON_TX_DATA_SOURCE_PREFIX + getInstanceName(), new ConnectionProvider() { public Connection getConnection() throws SQLException { // Always return a non-transactional Connection. return nonTxDataSourceToUse.getConnection(); } public void shutdown() { // Do nothing - a Spring-managed DataSource has its own lifecycle. } }); // No, if HSQL is the platform, we really don't want to use locks try { String productName = JdbcUtils.extractDatabaseMetaData(dataSource, "getDatabaseProductName").toString(); productName = JdbcUtils.commonDatabaseName(productName); if (productName != null && productName.toLowerCase().contains("hsql")) { setUseDBLocks(false); setLockHandler(new SimpleSemaphore()); } } catch (MetaDataAccessException e) { logWarnIfNonZero(1, "Could not detect database type. Assuming locks can be taken."); } super.initialize(loadHelper, signaler); }
From source file:de.hybris.platform.util.database.TableNameDatabaseMetaDataCallbackTest.java
@Test public void testOmmitAdminTables() throws MetaDataAccessException { ///* w w w . ja v a 2 s .com*/ final TableNameDatabaseMetaDataCallback tablesFilterCallback = new TableNameDatabaseMetaDataCallback( defaultQueryProvider, Registry.getCurrentTenantNoFallback()) { @Override Collection<SlaveTenant> getSlaveTenants() { return allSlaves; } }; final List<String> tables = (List<String>) JdbcUtils.extractDatabaseMetaData(dataSource, tablesFilterCallback); Assert.assertFalse(tables.contains("JGROUPSPING")); Assert.assertFalse(tables.contains(QueryProviderFactory.LOCK_TABLE)); }
From source file:com.saysth.commons.quartz.LocalDataSourceJobStore.java
@Override public void initialize(ClassLoadHelper loadHelper, SchedulerSignaler signaler) throws SchedulerConfigException { // Absolutely needs thread-bound DataSource to initialize. this.dataSource = SchedulerFactoryBean.getConfigTimeDataSource(); if (this.dataSource == null) { throw new SchedulerConfigException("No local DataSource found for configuration - " + "'dataSource' property must be set on SchedulerFactoryBean"); }/* w w w. j a va 2s.com*/ // Configure transactional connection settings for Quartz. setDataSource(TX_DATA_SOURCE_PREFIX + getInstanceName()); setDontSetAutoCommitFalse(true); // Register transactional ConnectionProvider for Quartz. DBConnectionManager.getInstance().addConnectionProvider(TX_DATA_SOURCE_PREFIX + getInstanceName(), new ConnectionProvider() { public Connection getConnection() throws SQLException { // Return a transactional Connection, if any. return DataSourceUtils.doGetConnection(dataSource); } public void shutdown() { // Do nothing - a Spring-managed DataSource has its own // lifecycle. } }); // Non-transactional DataSource is optional: fall back to default // DataSource if not explicitly specified. DataSource nonTxDataSource = SchedulerFactoryBean.getConfigTimeNonTransactionalDataSource(); final DataSource nonTxDataSourceToUse = (nonTxDataSource != null ? nonTxDataSource : this.dataSource); // Configure non-transactional connection settings for Quartz. setNonManagedTXDataSource(NON_TX_DATA_SOURCE_PREFIX + getInstanceName()); // Register non-transactional ConnectionProvider for Quartz. DBConnectionManager.getInstance().addConnectionProvider(NON_TX_DATA_SOURCE_PREFIX + getInstanceName(), new ConnectionProvider() { public Connection getConnection() throws SQLException { // Always return a non-transactional Connection. return nonTxDataSourceToUse.getConnection(); } public void shutdown() { // Do nothing - a Spring-managed DataSource has its own // lifecycle. } }); // No, if HSQL is the platform, we really don't want to use locks try { String productName = JdbcUtils.extractDatabaseMetaData(dataSource, "getDatabaseProductName").toString(); productName = JdbcUtils.commonDatabaseName(productName); if (productName != null && productName.toLowerCase().contains("hsql")) { setUseDBLocks(false); setLockHandler(new SimpleSemaphore()); } } catch (MetaDataAccessException e) { logWarnIfNonZero(1, "Could not detect database type. Assuming locks can be taken."); } super.initialize(loadHelper, signaler); }
From source file:org.jtalks.poulpe.util.databasebackup.persistence.DbTableKeys.java
/** * Obtains the list of tables foreign keys. * // w w w. jav a 2 s . c o m * @return A list of {@link ForeignKey} object represented foreign keys. * @throws SQLException * Is thrown in case any errors during work with database occur. */ @SuppressWarnings("unchecked") public Set<ForeignKey> getForeignKeys() throws SQLException { Set<ForeignKey> tableForeignKeySet = null; try { tableForeignKeySet = (Set<ForeignKey>) JdbcUtils.extractDatabaseMetaData(dataSource, new KeyListProcessor(tableName, new TableKeyPerformer() { @Override public ResultSet getResultSet(DatabaseMetaData dmd, String tableName) throws SQLException { return dmd.getImportedKeys(null, null, tableName); } @Override public void addKeyToSet(ResultSet rs, Set<TableKey> keySet) throws SQLException { if (rs.getString(FK_NAME) != null) { keySet.add(new ForeignKey(rs.getString(FK_NAME), rs.getString(FKCOLUMN_NAME), rs.getString(PKTABLE_NAME), rs.getString(PKCOLUMN_NAME))); } } })); } catch (MetaDataAccessException e) { throw new SQLException(e); } return tableForeignKeySet; }
From source file:de.hybris.platform.util.database.TableNameDatabaseMetaDataCallbackTest.java
@Test public void testOmmitAdminTablesForCustomTablePrefix() throws MetaDataAccessException { final String TABLE_PREFIX = "FoO_"; // play around with case sensitiveness final String before = Config.getParameter("db.tableprefix"); Mockito.reset(defaultQueryProvider); Mockito.when(defaultQueryProvider.getTableName()) .thenReturn(TABLE_PREFIX + QueryProviderFactory.LOCK_TABLE); try {/*ww w.jav a 2 s . com*/ Config.setParameter("db.tableprefix", TABLE_PREFIX); createTable(dataSource, TABLE_PREFIX + QueryProviderFactory.LOCK_TABLE); // final TableNameDatabaseMetaDataCallback tablesFilterCallback = new TableNameDatabaseMetaDataCallback( defaultQueryProvider, Registry.getCurrentTenantNoFallback()) { @Override Collection<SlaveTenant> getSlaveTenants() { return allSlaves; } }; final List<String> tables = (List<String>) JdbcUtils.extractDatabaseMetaData(dataSource, tablesFilterCallback); Assert.assertFalse(tables.contains("JGROUPSPING")); // Assert.assertTrue(tables.contains(QueryProviderFactory.LOCK_TABLE)); Assert.assertFalse(tables.contains(TABLE_PREFIX + QueryProviderFactory.LOCK_TABLE)); Assert.assertFalse(tables.contains((TABLE_PREFIX + QueryProviderFactory.LOCK_TABLE).toLowerCase())); Assert.assertFalse(tables.contains((TABLE_PREFIX + QueryProviderFactory.LOCK_TABLE).toUpperCase())); } finally { Config.setParameter("db.tableprefix", before); dropTable(dataSource, TABLE_PREFIX + QueryProviderFactory.LOCK_TABLE); } }
From source file:de.hybris.platform.util.database.TableNameDatabaseMetaDataCallbackTest.java
@Test public void testDropAllMyNonPrefixedTablesLeaveOthersWithPrefixes() throws MetaDataAccessException { final String currentPrefix = getTablePrefix(drop1Tenant); Assume.assumeTrue(org.apache.commons.lang.StringUtils.isBlank(currentPrefix));// empty prefix final String otherPrefix = getTablePrefix(drop2Tenant); final String yetAnotherPrefix = getTablePrefix(drop3Tenant); createTable(dataSource, "foo"); createTable(dataSource, "bar"); createTable(dataSource, "boo"); createTable(dataSource, otherPrefix + "foo"); createTable(dataSource, otherPrefix + "bar"); createTable(dataSource, otherPrefix + "boo"); createTable(dataSource, yetAnotherPrefix + "foo"); createTable(dataSource, yetAnotherPrefix + "bar"); createTable(dataSource, yetAnotherPrefix + "boo"); final TableNameDatabaseMetaDataCallback tablesFilterCallback = new TableNameDatabaseMetaDataCallback( defaultQueryProvider, drop1Tenant) { @Override//from w ww .j av a2 s .com Collection<SlaveTenant> getSlaveTenants() { return allSlaves; } @Override Tenant getMasterTenant() { // just to have different table prefix return drop3Tenant; } }; final List<String> tables = (List<String>) JdbcUtils.extractDatabaseMetaData(dataSource, tablesFilterCallback); Assert.assertTrue(containsIgnoreCase(currentPrefix + "foo", tables)); Assert.assertTrue(containsIgnoreCase(currentPrefix + "bar", tables)); Assert.assertTrue(containsIgnoreCase(currentPrefix + "boo", tables)); Assert.assertFalse(containsIgnoreCase(otherPrefix + "foo", tables)); Assert.assertFalse(containsIgnoreCase(otherPrefix + "bar", tables)); Assert.assertFalse(containsIgnoreCase(otherPrefix + "boo", tables)); Assert.assertFalse(containsIgnoreCase(yetAnotherPrefix + "foo", tables)); Assert.assertFalse(containsIgnoreCase(yetAnotherPrefix + "bar", tables)); Assert.assertFalse(containsIgnoreCase(yetAnotherPrefix + "boo", tables)); testOmmitAdminTables(); }
From source file:org.tonguetied.administration.AdministrationServiceTest.java
private void dropSchema() throws DataAccessException, MetaDataAccessException { executeSqlScript("/drop-schema.sql", true); Set<String> tables = (Set<String>) JdbcUtils.extractDatabaseMetaData(jdbcTemplate.getDataSource(), new GetTableNames()); assertTrue(tables.isEmpty());/*from w ww . ja va2 s . c om*/ }
From source file:org.zenoss.zep.dao.impl.DaoUtils.java
/** * Returns a list of column names in the specified table. * * @param dataSource DataSource to use.//from ww w . j a v a2 s.c o m * @param tableName Table name. * @return A list of column names in the table. * @throws MetaDataAccessException If an exception occurs. */ public static List<String> getColumnNames(final javax.sql.DataSource dataSource, final String tableName) throws MetaDataAccessException { final List<String> columnNames = new ArrayList<String>(); JdbcUtils.extractDatabaseMetaData(dataSource, new DatabaseMetaDataCallback() { @Override public Object processMetaData(DatabaseMetaData dbmd) throws SQLException, MetaDataAccessException { ResultSet rs = dbmd.getColumns(null, null, tableName, null); while (rs.next()) { String columnName = rs.getString("COLUMN_NAME"); columnNames.add(columnName); } rs.close(); return null; } }); return columnNames; }
From source file:org.zenoss.zep.dao.impl.DaoUtils.java
/** * Returns a map of column names to their JDBC type in the specified table. The map is returned in the order * returned by the getColumns query.//from ww w . ja v a 2s . c om * * @param dataSource DataSource to use. * @param tableName Table name. * @return A map of column names to the column types in the specified table. * @throws MetaDataAccessException If an exception occurs. */ public static Map<String, Integer> getColumnNamesAndTypes(final DataSource dataSource, final String tableName) throws MetaDataAccessException { final Map<String, Integer> columnNamesToTypes = new LinkedHashMap<String, Integer>(); JdbcUtils.extractDatabaseMetaData(dataSource, new DatabaseMetaDataCallback() { @Override public Object processMetaData(DatabaseMetaData dbmd) throws SQLException, MetaDataAccessException { ResultSet rs = dbmd.getColumns(null, null, tableName, null); while (rs.next()) { String columnName = rs.getString("COLUMN_NAME"); int columnType = rs.getInt("DATA_TYPE"); columnNamesToTypes.put(columnName, columnType); } rs.close(); return null; } }); return columnNamesToTypes; }