Search in sources :

Example 1 with LinkSchema

use of org.h2.table.LinkSchema in project h2database by h2database.

the class LinkSchema method linkSchema.

/**
 * Link all tables of a schema to the database.
 *
 * @param conn the connection to the database where the links are to be
 *            created
 * @param targetSchema the schema name where the objects should be created
 * @param driver the driver class name of the linked database
 * @param url the database URL of the linked database
 * @param user the user name
 * @param password the password
 * @param sourceSchema the schema where the existing tables are
 * @return a result set with the created tables
 */
public static ResultSet linkSchema(Connection conn, String targetSchema, String driver, String url, String user, String password, String sourceSchema) {
    Connection c2 = null;
    Statement stat = null;
    ResultSet rs = null;
    SimpleResultSet result = new SimpleResultSet();
    result.setAutoClose(false);
    result.addColumn("TABLE_NAME", Types.VARCHAR, Integer.MAX_VALUE, 0);
    try {
        c2 = JdbcUtils.getConnection(driver, url, user, password);
        stat = conn.createStatement();
        stat.execute("CREATE SCHEMA IF NOT EXISTS " + StringUtils.quoteIdentifier(targetSchema));
        // Workaround for PostgreSQL to avoid index names
        if (url.startsWith("jdbc:postgresql:")) {
            rs = c2.getMetaData().getTables(null, sourceSchema, null, new String[] { "TABLE", "LINKED TABLE", "VIEW", "EXTERNAL" });
        } else {
            rs = c2.getMetaData().getTables(null, sourceSchema, null, null);
        }
        while (rs.next()) {
            String table = rs.getString("TABLE_NAME");
            StringBuilder buff = new StringBuilder();
            buff.append("DROP TABLE IF EXISTS ").append(StringUtils.quoteIdentifier(targetSchema)).append('.').append(StringUtils.quoteIdentifier(table));
            stat.execute(buff.toString());
            buff = new StringBuilder();
            buff.append("CREATE LINKED TABLE ").append(StringUtils.quoteIdentifier(targetSchema)).append('.').append(StringUtils.quoteIdentifier(table)).append('(').append(StringUtils.quoteStringSQL(driver)).append(", ").append(StringUtils.quoteStringSQL(url)).append(", ").append(StringUtils.quoteStringSQL(user)).append(", ").append(StringUtils.quoteStringSQL(password)).append(", ").append(StringUtils.quoteStringSQL(sourceSchema)).append(", ").append(StringUtils.quoteStringSQL(table)).append(')');
            stat.execute(buff.toString());
            result.addRow(table);
        }
    } catch (SQLException e) {
        throw DbException.convert(e);
    } finally {
        JdbcUtils.closeSilently(rs);
        JdbcUtils.closeSilently(c2);
        JdbcUtils.closeSilently(stat);
    }
    return result;
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet)

Aggregations

Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 SimpleResultSet (org.h2.tools.SimpleResultSet)1