001: /*
002: * DerbySynonymReader.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.db.derby;
013:
014: import java.sql.Connection;
015: import java.sql.PreparedStatement;
016: import java.sql.ResultSet;
017: import java.sql.SQLException;
018: import java.util.LinkedList;
019: import java.util.List;
020: import workbench.db.DbMetadata;
021: import workbench.db.SynonymReader;
022: import workbench.db.TableIdentifier;
023: import workbench.resource.Settings;
024: import workbench.util.SqlUtil;
025:
026: /**
027: * @author support@sql-workbench.net
028: */
029: public class DerbySynonymReader implements SynonymReader {
030: private DbMetadata meta;
031:
032: public DerbySynonymReader(DbMetadata dbMeta) {
033: this .meta = dbMeta;
034: }
035:
036: /**
037: * The DB2 JDBC driver returns Alias' automatically, so there
038: * is no need to retrieve them here
039: */
040: public List<String> getSynonymList(Connection con, String owner)
041: throws SQLException {
042: List<String> result = new LinkedList<String>();
043: String sql = "select a.alias "
044: + "from sys.sysaliases a, sys.sysschemas s \n"
045: + "where a.schemaid = s.schemaid \n"
046: + " and a.aliastype = 'S' " + " and s.schemaname = ?";
047:
048: PreparedStatement stmt = null;
049: ResultSet rs = null;
050: try {
051: stmt = con.prepareStatement(sql);
052: stmt.setString(1, owner);
053: rs = stmt.executeQuery();
054: while (rs.next()) {
055: String alias = rs.getString(1);
056: if (!rs.wasNull()) {
057: result.add(alias);
058: }
059: }
060: } finally {
061: SqlUtil.closeAll(rs, stmt);
062: }
063:
064: return result;
065: }
066:
067: public TableIdentifier getSynonymTable(Connection con,
068: String anOwner, String aSynonym) throws SQLException {
069: String sql = "select a.aliasinfo \n"
070: + "from sys.sysaliases a, sys.sysschemas s \n"
071: + "where a.schemaid = s.schemaid \n"
072: + " and a.alias = ?" + " and s.schemaname = ?";
073:
074: PreparedStatement stmt = con.prepareStatement(sql);
075: stmt.setString(1, aSynonym);
076: stmt.setString(2, anOwner);
077: ResultSet rs = stmt.executeQuery();
078: String table = null;
079: TableIdentifier result = null;
080: try {
081: if (rs.next()) {
082: table = rs.getString(1);
083: if (table != null) {
084: result = new TableIdentifier(table);
085: }
086: }
087: } finally {
088: SqlUtil.closeAll(rs, stmt);
089: }
090:
091: if (result != null) {
092: String type = this .meta.getObjectType(result);
093: result.setType(type);
094: }
095:
096: return result;
097: }
098:
099: public String getSynonymSource(Connection con, String anOwner,
100: String aSynonym) throws SQLException {
101: TableIdentifier id = getSynonymTable(con, anOwner, aSynonym);
102: StringBuilder result = new StringBuilder(200);
103: String nl = Settings.getInstance()
104: .getInternalEditorLineEnding();
105: result.append("CREATE SYNONYM ");
106: result.append(aSynonym);
107: result.append(nl + " FOR ");
108: result.append(id.getTableExpression());
109: result.append(';');
110: result.append(nl);
111:
112: return result.toString();
113: }
114:
115: }
|