| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.expression.function.table; |
| 7 |
|
| 8 |
import java.sql.Connection; |
| 9 |
import java.sql.ResultSet; |
| 10 |
import java.sql.SQLException; |
| 11 |
import java.sql.Statement; |
| 12 |
|
| 13 |
import org.h2.api.ErrorCode; |
| 14 |
import org.h2.engine.SessionLocal; |
| 15 |
import org.h2.expression.Expression; |
| 16 |
import org.h2.message.DbException; |
| 17 |
import org.h2.result.ResultInterface; |
| 18 |
import org.h2.result.SimpleResult; |
| 19 |
import org.h2.util.JdbcUtils; |
| 20 |
import org.h2.util.StringUtils; |
| 21 |
import org.h2.value.TypeInfo; |
| 22 |
import org.h2.value.ValueVarchar; |
| 23 |
|
| 24 |
/** |
| 25 |
* A LINK_SCHEMA function. |
| 26 |
*/ |
| 27 |
public final class LinkSchemaFunction extends TableFunction { |
| 28 |
|
| 29 |
public LinkSchemaFunction() { |
| 30 |
super(new Expression[6]); |
| 31 |
} |
| 32 |
|
| 33 |
@Override |
| 34 |
public ResultInterface getValue(SessionLocal session) { |
| 35 |
session.getUser().checkAdmin(); |
| 36 |
String targetSchema = getValue(session, 0); |
| 37 |
String driver = getValue(session, 1); |
| 38 |
String url = getValue(session, 2); |
| 39 |
String user = getValue(session, 3); |
| 40 |
String password = getValue(session, 4); |
| 41 |
String sourceSchema = getValue(session, 5); |
| 42 |
if (targetSchema == null || driver == null || url == null || user == null || password == null |
| 43 |
|| sourceSchema == null) { |
| 44 |
return getValueTemplate(session); |
| 45 |
} |
| 46 |
Connection conn = session.createConnection(false); |
| 47 |
Connection c2 = null; |
| 48 |
Statement stat = null; |
| 49 |
ResultSet rs = null; |
| 50 |
SimpleResult result = new SimpleResult(); |
| 51 |
result.addColumn("TABLE_NAME", TypeInfo.TYPE_VARCHAR); |
| 52 |
try { |
| 53 |
c2 = JdbcUtils.getConnection(driver, url, user, password); |
| 54 |
stat = conn.createStatement(); |
| 55 |
stat.execute(StringUtils.quoteIdentifier(new StringBuilder("CREATE SCHEMA IF NOT EXISTS "), targetSchema) |
| 56 |
.toString()); |
| 57 |
// Workaround for PostgreSQL to avoid index names |
| 58 |
if (url.startsWith("jdbc:postgresql:")) { |
| 59 |
rs = c2.getMetaData().getTables(null, sourceSchema, null, |
| 60 |
new String[] { "TABLE", "LINKED TABLE", "VIEW", "EXTERNAL" }); |
| 61 |
} else { |
| 62 |
rs = c2.getMetaData().getTables(null, sourceSchema, null, null); |
| 63 |
} |
| 64 |
while (rs.next()) { |
| 65 |
String table = rs.getString("TABLE_NAME"); |
| 66 |
StringBuilder buff = new StringBuilder(); |
| 67 |
buff.append("DROP TABLE IF EXISTS "); |
| 68 |
StringUtils.quoteIdentifier(buff, targetSchema).append('.'); |
| 69 |
StringUtils.quoteIdentifier(buff, table); |
| 70 |
stat.execute(buff.toString()); |
| 71 |
buff.setLength(0); |
| 72 |
buff.append("CREATE LINKED TABLE "); |
| 73 |
StringUtils.quoteIdentifier(buff, targetSchema).append('.'); |
| 74 |
StringUtils.quoteIdentifier(buff, table).append('('); |
| 75 |
StringUtils.quoteStringSQL(buff, driver).append(", "); |
| 76 |
StringUtils.quoteStringSQL(buff, url).append(", "); |
| 77 |
StringUtils.quoteStringSQL(buff, user).append(", "); |
| 78 |
StringUtils.quoteStringSQL(buff, password).append(", "); |
| 79 |
StringUtils.quoteStringSQL(buff, sourceSchema).append(", "); |
| 80 |
StringUtils.quoteStringSQL(buff, table).append(')'); |
| 81 |
stat.execute(buff.toString()); |
| 82 |
result.addRow(ValueVarchar.get(table, session)); |
| 83 |
} |
| 84 |
} catch (SQLException e) { |
| 85 |
result.close(); |
| 86 |
throw DbException.convert(e); |
| 87 |
} finally { |
| 88 |
JdbcUtils.closeSilently(rs); |
| 89 |
JdbcUtils.closeSilently(c2); |
| 90 |
JdbcUtils.closeSilently(stat); |
| 91 |
} |
| 92 |
return result; |
| 93 |
} |
| 94 |
|
| 95 |
private String getValue(SessionLocal session, int index) { |
| 96 |
return args[index].getValue(session).getString(); |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public void optimize(SessionLocal session) { |
| 101 |
super.optimize(session); |
| 102 |
int len = args.length; |
| 103 |
if (len != 6) { |
| 104 |
throw DbException.get(ErrorCode.INVALID_PARAMETER_COUNT_2, getName(), "6"); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public ResultInterface getValueTemplate(SessionLocal session) { |
| 110 |
SimpleResult result = new SimpleResult(); |
| 111 |
result.addColumn("TABLE_NAME", TypeInfo.TYPE_VARCHAR); |
| 112 |
return result; |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public String getName() { |
| 117 |
return "LINK_SCHEMA"; |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public boolean isDeterministic() { |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
} |