01: /*
02: * OracleSynonymReader.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.oracle;
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:
22: import workbench.db.TableIdentifier;
23: import workbench.resource.Settings;
24: import workbench.util.SqlUtil;
25:
26: /**
27: *
28: * @author support@sql-workbench.net
29: */
30: public class OracleSynonymReader implements SynonymReader {
31: public OracleSynonymReader() {
32: }
33:
34: /**
35: * The Oracle driver already returns the SYNONYMS in the getTables() call
36: */
37: public List<String> getSynonymList(Connection con, String owner)
38: throws SQLException {
39: return Collections.emptyList();
40: }
41:
42: public TableIdentifier getSynonymTable(Connection con,
43: String anOwner, String aSynonym) throws SQLException {
44: StringBuilder sql = new StringBuilder(400);
45: sql
46: .append("SELECT s.synonym_name, s.table_owner, s.table_name, s.db_link, o.object_type, s.owner ");
47: sql.append("FROM all_synonyms s, all_objects o ");
48: sql.append("where s.table_name = o.object_name ");
49: sql.append("and s.table_owner = o.owner ");
50: sql.append("and ((s.synonym_name = ? AND s.owner = ?) ");
51: sql.append(" or (s.synonym_name = ? AND s.owner = 'PUBLIC')) ");
52: sql.append("ORDER BY decode(s.owner, 'PUBLIC',9,1)");
53:
54: PreparedStatement stmt = null;
55:
56: ResultSet rs = null;
57:
58: TableIdentifier result = null;
59: try {
60: stmt = con.prepareStatement(sql.toString());
61: stmt.setString(1, aSynonym);
62: stmt.setString(2, anOwner == null ? con.getMetaData()
63: .getUserName() : anOwner);
64: stmt.setString(3, aSynonym);
65:
66: rs = stmt.executeQuery();
67: if (rs.next()) {
68: String owner = rs.getString(2);
69: String table = rs.getString(3);
70: String dblink = rs.getString(4);
71: String type = rs.getString(5);
72: if (dblink != null)
73: table = table + "@" + dblink;
74: result = new TableIdentifier(null, owner, table);
75: result.setType(type);
76: }
77: } finally {
78: SqlUtil.closeAll(rs, stmt);
79: }
80:
81: return result;
82: }
83:
84: public String getSynonymSource(Connection con, String anOwner,
85: String aSynonym) throws SQLException {
86: TableIdentifier id = getSynonymTable(con, anOwner, aSynonym);
87: StringBuilder result = new StringBuilder(200);
88: String nl = Settings.getInstance()
89: .getInternalEditorLineEnding();
90: result.append("CREATE SYNONYM ");
91: result.append(aSynonym);
92: result.append(nl + " FOR ");
93: result.append(id.getTableExpression());
94: result.append(';');
95: result.append(nl);
96: return result.toString();
97: }
98:
99: }
|