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