01: /*
02: * Db2SynonymReader.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.db.ibm;
13:
14: import java.sql.Connection;
15: import java.sql.PreparedStatement;
16: import java.sql.ResultSet;
17: import java.sql.SQLException;
18: import java.util.Collections;
19: import java.util.List;
20: import workbench.db.SynonymReader;
21: import workbench.db.TableIdentifier;
22: import workbench.resource.Settings;
23: import workbench.util.SqlUtil;
24:
25: /**
26: * A class to retrieve synonym definitions from a DB2 database.
27: * @author support@sql-workbench.net
28: */
29: public class Db2SynonymReader implements SynonymReader {
30:
31: public Db2SynonymReader() {
32: }
33:
34: /**
35: * Returns an empty list, as the standard JDBC driver
36: * alread returns synonyms in the getTables() method.
37: *
38: * @return an empty list
39: */
40: public List<String> getSynonymList(Connection con, String owner)
41: throws SQLException {
42: return Collections.emptyList();
43: }
44:
45: public TableIdentifier getSynonymTable(Connection con,
46: String anOwner, String aSynonym) throws SQLException {
47: StringBuilder sql = new StringBuilder(200);
48:
49: sql
50: .append("SELECT base_tabschema, base_tabname FROM syscat.tables ");
51: sql
52: .append(" WHERE TYPE = 'A' and tabname = ? and tabschema = ?");
53:
54: PreparedStatement stmt = con.prepareStatement(sql.toString());
55: stmt.setString(1, aSynonym);
56: stmt.setString(2, anOwner);
57:
58: ResultSet rs = stmt.executeQuery();
59: String table = null;
60: String owner = null;
61: TableIdentifier result = null;
62: try {
63: if (rs.next()) {
64: owner = rs.getString(1);
65: table = rs.getString(2);
66: if (table != null) {
67: result = new TableIdentifier(null, owner, table);
68: }
69: }
70: } finally {
71: SqlUtil.closeAll(rs, stmt);
72: }
73:
74: return result;
75: }
76:
77: public String getSynonymSource(Connection con, String anOwner,
78: String aSynonym) throws SQLException {
79: TableIdentifier id = getSynonymTable(con, anOwner, aSynonym);
80: StringBuilder result = new StringBuilder(200);
81: String nl = Settings.getInstance()
82: .getInternalEditorLineEnding();
83: result.append("CREATE ALIAS ");
84: result.append(aSynonym);
85: result.append(nl + " FOR ");
86: result.append(id.getTableExpression());
87: result.append(';');
88: result.append(nl);
89:
90: return result.toString();
91: }
92:
93: }
|