Example usage for org.springframework.jdbc.support DatabaseMetaDataCallback DatabaseMetaDataCallback

List of usage examples for org.springframework.jdbc.support DatabaseMetaDataCallback DatabaseMetaDataCallback

Introduction

In this page you can find the example usage for org.springframework.jdbc.support DatabaseMetaDataCallback DatabaseMetaDataCallback.

Prototype

DatabaseMetaDataCallback

Source Link

Usage

From source file:org.obiba.runtime.upgrade.support.DatabaseMetadataUtil.java

/**
 * Returns the database product name.//w  w  w  .  j  av  a2s .c  o m
 *
 * @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/*from  w  w w .  j av  a 2s.  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.
 * //from   w w w.  j a va2s  . co  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.obiba.runtime.upgrade.support.DatabaseMetadataUtil.java

/**
 * Indicates whether the specified table contains the specified column.
 *
 * @param tableName the table name//from  w ww.  j  a va  2 s.c  o m
 * @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.geowebcache.diskquota.jdbc.SQLDialect.java

/**
 * Checks if the specified table exists//from w  w w. j  a v a  2s. co  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;
    }
}

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.// w w  w .  j  a v  a 2  s .c om
 * @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.//  ww w .  j  a va  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;
}

From source file:org.easyrec.store.dao.web.impl.LoaderDAOMysqlImpl.java

@Override
public void testConnection(String url, String username, String password) throws Exception {

    HikariConfig config = new HikariConfig();
    config.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
    config.setJdbcUrl(url);/*  w ww.j a  v a 2 s.com*/
    config.setUsername(username);
    config.setPassword(password);
    config.setPoolName("easyrecPool");
    config.addDataSourceProperty("url", url);
    HikariDataSource ds = new HikariDataSource(config);

    setDataSource(ds);
    sqlScriptService.setDataSource(ds);

    boolean tablesOk = false;

    DatabaseMetaDataCallback callback = new DatabaseMetaDataCallback() {
        public Object processMetaData(DatabaseMetaData dbmd) throws SQLException, MetaDataAccessException {
            ResultSet rs = dbmd.getTables(null, null, "operator", null);
            return rs.next();
        }
    };

    tablesOk = (Boolean) JdbcUtils.extractDatabaseMetaData(ds, callback);
}

From source file:org.easyrec.store.dao.web.impl.LoaderDAOMysqlImpl.java

@Override
public void createDB() throws Exception {

    HikariDataSource bds = (HikariDataSource) getDataSource();
    boolean tablesOk = false;

    DatabaseMetaDataCallback callback = new DatabaseMetaDataCallback() {
        public Object processMetaData(DatabaseMetaData dbmd) throws SQLException, MetaDataAccessException {
            ResultSet rs = dbmd.getTables(null, null, "operator", null);
            return rs.next();
        }//ww w. j  a v  a 2s .  c  o  m
    };

    tablesOk = (Boolean) JdbcUtils.extractDatabaseMetaData(bds, callback);
    sqlScriptService.executeSqlScript(dbCreationFile.getInputStream());
}

From source file:org.easyrec.store.dao.web.impl.LoaderDAOMysqlImpl.java

@Override
public void migrateDB() throws Exception {

    HikariDataSource bds = (HikariDataSource) getDataSource();
    boolean tablesOk = false;

    DatabaseMetaDataCallback callback = new DatabaseMetaDataCallback() {
        public Object processMetaData(DatabaseMetaData dbmd) throws SQLException, MetaDataAccessException {
            ResultSet rs = dbmd.getTables(null, null, "operator", null);
            return rs.next();
        }/*from   w w  w. ja va  2 s.  co m*/
    };

    tablesOk = (Boolean) JdbcUtils.extractDatabaseMetaData(bds, callback);
    Float installedVersion = checkVersion();

    for (String migrateFile : migrateFiles) {
        logger.info("migrate File: " + migrateFile);
        Float scriptVersion = Float.parseFloat(migrateFile.substring(migrateFile.lastIndexOf("_") + 1));
        logger.info("scriptVersion: " + scriptVersion);
        if (installedVersion < scriptVersion) {
            File f = new File(dbMigrateFolder.getFile(), migrateFile + ".sql");
            if (f.exists()) {
                logger.info("Executing migrate script: " + f.getName());
                sqlScriptService.executeSqlScript(new FileSystemResource(f).getInputStream());
            }
            if (scriptVersion == 0.96) {
                update_0_96f();
            }
            if (scriptVersion == 0.98) {
                update_0_98();
            }
            if (scriptVersion == 1.00) {
                update_1_00();
            }
        }
    }

    //        if (installedVersion < 0.96f) {
    //            update_0_96f();
    //            // logs are not converted from ruleminerlog -> plugin_log
    //        }
    //
    //        if (installedVersion < 0.98) {
    //            update_0_98();
    //        }

    //updateVersion(); // done in migrate script!
}