File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\table\LinkSchema.java.html

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