01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.table;
07:
08: import java.sql.Connection;
09: import java.sql.ResultSet;
10: import java.sql.SQLException;
11: import java.sql.Statement;
12: import java.sql.Types;
13:
14: import org.h2.tools.SimpleResultSet;
15: import org.h2.util.JdbcUtils;
16: import org.h2.util.StringUtils;
17:
18: /**
19: * A utility class to create table links for a whole schema.
20: */
21: public class LinkSchema {
22:
23: /**
24: * Link all tables of a schema to the database.
25: *
26: * @param conn the connection to the database where the links are to be created
27: * @param targetSchema the schema name where the objects should be created
28: * @param driver the driver class name of the linked database
29: * @param url the database URL of the linked database
30: * @param user the user name
31: * @param password the password
32: * @param sourceSchema the schema where the existing tables are
33: * @return a result set with the created tables
34: */
35: public static ResultSet linkSchema(Connection conn,
36: String targetSchema, String driver, String url,
37: String user, String password, String sourceSchema)
38: throws SQLException {
39: Connection c2 = null;
40: Statement stat = null;
41: ResultSet rs = null;
42: SimpleResultSet result = new SimpleResultSet();
43: result.addColumn("TABLE_NAME", Types.VARCHAR,
44: Integer.MAX_VALUE, 0);
45: try {
46: c2 = JdbcUtils.getConnection(driver, url, user, password);
47: stat = conn.createStatement();
48: stat.execute("CREATE SCHEMA IF NOT EXISTS "
49: + StringUtils.quoteIdentifier(targetSchema));
50: rs = c2.getMetaData().getTables(null, sourceSchema, null,
51: null);
52: while (rs.next()) {
53: String table = rs.getString("TABLE_NAME");
54: StringBuffer buff = new StringBuffer();
55: buff.append("DROP TABLE IF EXISTS ");
56: buff.append(StringUtils.quoteIdentifier(targetSchema));
57: buff.append('.');
58: buff.append(StringUtils.quoteIdentifier(table));
59: String sql = buff.toString();
60: stat.execute(sql);
61: buff = new StringBuffer();
62: buff.append("CREATE LINKED TABLE ");
63: buff.append(StringUtils.quoteIdentifier(targetSchema));
64: buff.append('.');
65: buff.append(StringUtils.quoteIdentifier(table));
66: buff.append('(');
67: buff.append(StringUtils.quoteStringSQL(driver));
68: buff.append(", ");
69: buff.append(StringUtils.quoteStringSQL(url));
70: buff.append(", ");
71: buff.append(StringUtils.quoteStringSQL(user));
72: buff.append(", ");
73: buff.append(StringUtils.quoteStringSQL(password));
74: buff.append(", ");
75: buff.append(StringUtils.quoteStringSQL(table));
76: buff.append(')');
77: sql = buff.toString();
78: stat.execute(sql);
79: result.addRow(new String[] { table });
80: }
81: } finally {
82: JdbcUtils.closeSilently(rs);
83: JdbcUtils.closeSilently(c2);
84: JdbcUtils.closeSilently(stat);
85: }
86: return result;
87: }
88: }
|