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.obiba.runtime.upgrade.support.DatabaseMetadataUtil.java
/** * Returns the database product name./* w ww . j a v a 2s .c om*/ * * @return database product name */ public String getDatabaseProductName() { String databaseProductName; try { databaseProductName = (String) JdbcUtils.extractDatabaseMetaData(dataSource, new DatabaseMetaDataCallback() { @Override public Object processMetaData(DatabaseMetaData dbmd) throws SQLException, MetaDataAccessException { return dbmd.getDatabaseProductName(); } }); } catch (MetaDataAccessException e) { throw new RuntimeException(e); } return databaseProductName; }
From source file:org.obiba.runtime.upgrade.support.DatabaseMetadataUtil.java
/** * Indicates whether the specified table exists. * * @param tableName the table name//ww w. j av a2 s. c o m * @return <code>true</code> if the table exists */ public boolean isTableExists(final String tableName) { boolean tablePresent = false; try { tablePresent = (Boolean) JdbcUtils.extractDatabaseMetaData(dataSource, new DatabaseMetaDataCallback() { @Override public Object processMetaData(DatabaseMetaData dbmd) throws SQLException, MetaDataAccessException { return dbmd.getTables(null, null, tableName, null).next(); } }); } catch (MetaDataAccessException ex) { throw new RuntimeException(ex); } return tablePresent; }
From source file:org.jtalks.poulpe.util.databasebackup.persistence.DbTableLister.java
/** * Returns the list of all database table names which database contains from given Data source via JDBC. * //w w w. j ava2 s . c o m * @return a List of Strings where every String instance represents a table name from the database. * @throws SQLException * is thrown if there is an error during collaborating with the database. */ @SuppressWarnings("unchecked") private List<String> getTableNames() throws MetaDataAccessException { Validate.notNull(dataSource, "dataSource must not be null"); return Collections.unmodifiableList( (List<String>) JdbcUtils.extractDatabaseMetaData(dataSource, new DatabaseMetaDataCallback() { @Override public Object processMetaData(DatabaseMetaData dmd) throws SQLException, MetaDataAccessException { List<String> tableList = new ArrayList<String>(); ResultSet rs = dmd.getTables(null, null, null, new String[] { "TABLE" }); while (rs.next()) { tableList.add(rs.getString("TABLE_NAME")); } return tableList; } })); }
From source file:org.jtalks.poulpe.util.databasebackup.persistence.DbTableKeys.java
/** * Obtain from the database a list of tables' unique keys. * //from w w w. j a v a2s .co m * @return A list of {@link UniqueKey} object represented unique keys. * @throws SQLException * Is thrown in case any errors during work with database occur. */ @SuppressWarnings("unchecked") public Set<UniqueKey> getUniqueKeys() throws SQLException { Set<UniqueKey> tableUniqueKeySet = null; try { tableUniqueKeySet = (Set<UniqueKey>) JdbcUtils.extractDatabaseMetaData(dataSource, new KeyListProcessor(tableName, new TableKeyPerformer() { @Override public ResultSet getResultSet(DatabaseMetaData dmd, String tableName) throws SQLException { return dmd.getIndexInfo(null, null, tableName, true, true); } @Override public void addKeyToSet(ResultSet rs, Set<TableKey> keySet) throws SQLException { if (rs.getString(INDEX_NAME) != null && rs.getString(COLUMN_NAME) != null) { UniqueKey key = new UniqueKey(rs.getString(INDEX_NAME), rs.getString(COLUMN_NAME)); if (!isPrimaryKey(key)) { keySet.add(key); } } } })); Map<String, UniqueKey> resultMap = new HashMap<String, UniqueKey>(); for (UniqueKey uniqueKey : tableUniqueKeySet) { if (resultMap.containsKey(uniqueKey.getIndexName())) { Set<String> existingColumns = new HashSet<String>( resultMap.get(uniqueKey.getIndexName()).getColumnNameSet()); existingColumns.addAll(uniqueKey.getColumnNameSet()); resultMap.put(uniqueKey.getIndexName(), new UniqueKey(uniqueKey.getIndexName(), existingColumns)); } else { resultMap.put(uniqueKey.getIndexName(), uniqueKey); } } tableUniqueKeySet.clear(); for (UniqueKey key : resultMap.values()) { tableUniqueKeySet.add(key); } } catch (MetaDataAccessException e) { throw new SQLException(e); } return tableUniqueKeySet; }
From source file:org.obiba.runtime.upgrade.support.DatabaseMetadataUtil.java
/** * Indicates whether the specified table contains the specified column. * * @param tableName the table name// w ww . j a v a 2 s . com * @param columnName the column name * @return <code>true</code> if the column exists in the table */ public boolean hasColumn(final String tableName, final String columnName) { boolean columnPresent = false; try { columnPresent = (Boolean) JdbcUtils.extractDatabaseMetaData(dataSource, new DatabaseMetaDataCallback() { @Override public Object processMetaData(DatabaseMetaData dbmd) throws SQLException, MetaDataAccessException { return dbmd.getColumns(null, null, tableName, columnName).next(); } }); } catch (MetaDataAccessException ex) { throw new RuntimeException(ex); } return columnPresent; }
From source file:org.grails.orm.hibernate.support.HibernateDialectDetectorFactoryBean.java
public void afterPropertiesSet() throws MetaDataAccessException { Assert.notNull(dataSource, "Data source is not set!"); Assert.notNull(vendorNameDialectMappings, "Vendor name/dialect mappings are not set!"); Connection connection = null; String dbName = (String) JdbcUtils.extractDatabaseMetaData(dataSource, "getDatabaseProductName"); try {//from w ww. j av a2s .c om connection = DataSourceUtils.getConnection(dataSource); try { final DialectFactory dialectFactory = createDialectFactory(); final Connection finalConnection = connection; DialectResolutionInfoSource infoSource = new DialectResolutionInfoSource() { @Override public DialectResolutionInfo getDialectResolutionInfo() { try { return new DatabaseMetaDataDialectResolutionInfoAdapter(finalConnection.getMetaData()); } catch (SQLException e) { throw new CouldNotDetermineHibernateDialectException( "Could not determine Hibernate dialect", e); } } }; hibernateDialect = dialectFactory.buildDialect(hibernateProperties, infoSource); hibernateDialectClassName = hibernateDialect.getClass().getName(); } catch (HibernateException e) { hibernateDialectClassName = vendorNameDialectMappings.getProperty(dbName); } if (!StringUtils.hasText(hibernateDialectClassName)) { throw new CouldNotDetermineHibernateDialectException( "Could not determine Hibernate dialect for database name [" + dbName + "]!"); } } finally { DataSourceUtils.releaseConnection(connection, dataSource); } }
From source file:info.naiv.lab.java.tool.sqlite.exporter.component.Source.java
/** * * @return @throws MetaDataAccessException * @throws IOException/*from ww w .j ava2 s . com*/ */ public List<TableInfo> getCreateSchemaSql() throws MetaDataAccessException, IOException { DataSource dataSource = jdbcTemplate.getDataSource(); List<TableInfo> sqls = (List<TableInfo>) JdbcUtils.extractDatabaseMetaData(dataSource, new SchemaCreator()); return sqls; }
From source file:org.tonguetied.administration.AdministrationServiceTest.java
@Test public final void testCreateDatabase() throws Exception { // dropSchema(); final String[] schemas = new String[] { loadSchema("/hsql-schema.sql"), loadSchema("/initial-data.sql") }; administrationService.createDatabase(schemas); Set<String> tables = (Set<String>) JdbcUtils.extractDatabaseMetaData(jdbcTemplate.getDataSource(), new GetTableNames()); assertEquals(TABLES.length, tables.size()); for (int i = 0; i < TABLES.length; i++) assertTrue("table " + TABLES[i] + " not found", tables.contains(TABLES[i].toUpperCase(Locale.getDefault()))); User admin = getUserRepository().getUser("admin"); assertNotNull(admin);/*w ww . j a va 2 s . co m*/ Language language = getLanguageRepository().getLanguage(LanguageCode.DEFAULT); assertNotNull(language); Country country = getCountryRepository().getCountry(CountryCode.DEFAULT); assertNotNull(country); Bundle bundle = getBundleRepository().getDefaultBundle(); assertNotNull(bundle); }
From source file:de.hybris.platform.util.database.TableNameDatabaseMetaDataCallbackTest.java
@Test public void testDropOnlyTablesWithMyPrefix() throws MetaDataAccessException { final String currentPrefix = "dr2_"; final String otherPrefix = "other_"; final String nonePrefix = ""; createTable(dataSource, currentPrefix + "foo"); createTable(dataSource, currentPrefix + "bar"); createTable(dataSource, currentPrefix + "boo"); createTable(dataSource, otherPrefix + "foo"); createTable(dataSource, otherPrefix + "bar"); createTable(dataSource, otherPrefix + "boo"); createTable(dataSource, nonePrefix + "foo"); createTable(dataSource, nonePrefix + "bar"); createTable(dataSource, nonePrefix + "boo"); final TableNameDatabaseMetaDataCallback tablesFilterCallback = new TableNameDatabaseMetaDataCallback( defaultQueryProvider, drop2Tenant) { @Override/*from w w w. j av a 2 s . co m*/ Collection<SlaveTenant> getSlaveTenants() { return allSlaves; } }; 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(nonePrefix + "foo", tables)); Assert.assertFalse(containsIgnoreCase(nonePrefix + "bar", tables)); Assert.assertFalse(containsIgnoreCase(nonePrefix + "boo", tables)); testOmmitAdminTables(); }
From source file:org.geowebcache.diskquota.jdbc.SQLDialect.java
/** * Checks if the specified table exists// ww w. jav a 2 s. c o m * * @param template * @param tableName * @return */ private boolean tableExists(SimpleJdbcTemplate template, final String schema, final String tableName) { try { DataSource ds = ((JdbcAccessor) template.getJdbcOperations()).getDataSource(); return (Boolean) JdbcUtils.extractDatabaseMetaData(ds, new DatabaseMetaDataCallback() { public Object processMetaData(DatabaseMetaData dbmd) throws SQLException, MetaDataAccessException { ResultSet rs = null; try { rs = dbmd.getTables(null, schema, tableName.toLowerCase(), null); boolean exists = rs.next(); rs.close(); if (exists) { return true; } rs = dbmd.getTables(null, schema, tableName, null); return rs.next(); } finally { if (rs != null) { rs.close(); } } } }); } catch (MetaDataAccessException e) { return false; } }