001: package net.sourceforge.squirrel_sql.fw.sql;
002:
003: /*
004: * Copyright (C) 2001-2004 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.io.Serializable;
022: import java.sql.SQLException;
023:
024: import net.sourceforge.squirrel_sql.fw.dialects.DialectFactory;
025: import net.sourceforge.squirrel_sql.fw.util.StringUtilities;
026:
027: public class DatabaseObjectInfo implements IDatabaseObjectInfo,
028: Serializable {
029: /** Property names for this bean. */
030: public interface IPropertyNames {
031: /** Catalog name. */
032: String CATALOG_NAME = "catalogName";
033:
034: /** Schema name. */
035: String SCHEMA_NAME = "schemaName";
036:
037: /** Simple name. */
038: String SIMPLE_NAME = "simpleName";
039:
040: /** Qualified name. */
041: String QUALIFIED_NAME = "qualifiedName";
042: }
043:
044: static final long serialVersionUID = -7138016566181091160L;
045:
046: /** Catalog name. Can be <CODE>null</CODE> */
047: private final String _catalog;
048:
049: /** Schema name. Can be <CODE>null</CODE> */
050: private final String _schema;
051:
052: /** Simple object name. */
053: private final String _simpleName;
054:
055: /** Fully qualified name for this object. */
056: private final String _qualifiedName;
057:
058: /** Object type. @see DatabaseObjectType.*/
059: private DatabaseObjectType _dboType = DatabaseObjectType.OTHER;
060:
061: public DatabaseObjectInfo(String catalog, String schema,
062: String simpleName, DatabaseObjectType dboType,
063: ISQLDatabaseMetaData md) {
064: super ();
065: if (dboType == null) {
066: throw new IllegalArgumentException(
067: "DatabaseObjectType == null");
068: }
069: if (md == null) {
070: throw new IllegalArgumentException(
071: "SQLDatabaseMetaData == null");
072: }
073:
074: _catalog = catalog;
075: _schema = schema;
076: _simpleName = simpleName;
077: _qualifiedName = generateQualifiedName(md);
078: _dboType = dboType;
079: }
080:
081: /**
082: * Default constructor for using instances of this class to contain
083: * information about new objects that will be created soon.
084: */
085: public DatabaseObjectInfo(String catalog, String schema,
086: String simpleName) {
087: _catalog = catalog;
088: _schema = schema;
089: _simpleName = simpleName;
090: _qualifiedName = simpleName;
091: }
092:
093: public String toString() {
094: return getSimpleName();
095: }
096:
097: public String getCatalogName() {
098: return _catalog;
099: }
100:
101: public String getSchemaName() {
102: return _schema;
103: }
104:
105: public String getSimpleName() {
106: return _simpleName;
107: }
108:
109: public String getQualifiedName() {
110: return _qualifiedName;
111: }
112:
113: public DatabaseObjectType getDatabaseObjectType() {
114: return _dboType;
115: }
116:
117: protected String generateQualifiedName(ISQLConnection conn) {
118: return generateQualifiedName(conn.getSQLMetaData());
119: }
120:
121: /**
122: * Informix represents database objects in catalogs *and* schemas. So a table
123: * called <table> might be found in the <databasename> catalog, which lives in
124: * the <schemaname> schema and is addressed as:
125: *
126: * <databasename>:"<schemaname>".<table>
127: *
128: * It may also be referred to as simply <table>
129: *
130: * This method returns a qualifed name that meets this criteria.
131: *
132: * @return a valid Informix qualified name - if catalog *and* schema are not
133: * null/empty, this returns the database object name such as
134: * catalog:schema.simpleName. However, if either catalog or schema
135: * (or both) are null, this simply returns the simpleName
136: */
137: private String getInformixQualifiedName() {
138: StringBuilder result = new StringBuilder();
139: if (_catalog != null && _schema != null) {
140: result.append(_catalog);
141: result.append(":");
142: result.append("\"");
143: result.append(_schema);
144: result.append("\"");
145: result.append(".");
146: }
147: result.append(_simpleName);
148: return result.toString();
149: }
150:
151: protected String generateQualifiedName(final ISQLDatabaseMetaData md) {
152: String catSep = null;
153: String identifierQuoteString = null;
154: boolean supportsSchemasInDataManipulation = false;
155: boolean supportsCatalogsInDataManipulation = false;
156:
157: // check for Informix - it has very "special" qualified names
158: if (DialectFactory.isInformix(md)) {
159: return getInformixQualifiedName();
160: }
161:
162: try {
163: supportsSchemasInDataManipulation = md
164: .supportsSchemasInDataManipulation();
165: } catch (SQLException ignore) {
166: // Ignore.
167: }
168: try {
169: supportsCatalogsInDataManipulation = md
170: .supportsCatalogsInDataManipulation();
171: } catch (SQLException ignore) {
172: // Ignore.
173: }
174:
175: try {
176: // if (supportsCatalogsInDataManipulation)
177: // {
178: catSep = md.getCatalogSeparator();
179: // }
180: } catch (SQLException ignore) {
181: // Ignore.
182: }
183:
184: if (StringUtilities.isEmpty(catSep)) {
185: catSep = ".";
186: }
187:
188: try {
189: identifierQuoteString = md.getIdentifierQuoteString();
190: if (identifierQuoteString != null
191: && identifierQuoteString.equals(" ")) {
192: identifierQuoteString = null;
193: }
194: } catch (SQLException ignore) {
195: // Ignore.
196: }
197:
198: if (DialectFactory.isSyBase(md)) {
199: identifierQuoteString = checkSybaseIdentifierQuoteString(
200: md, identifierQuoteString);
201: }
202:
203: StringBuffer buf = new StringBuffer();
204: if (supportsCatalogsInDataManipulation
205: && !StringUtilities.isEmpty(_catalog)) {
206: if (identifierQuoteString != null) {
207: buf.append(identifierQuoteString);
208: }
209: buf.append(_catalog);
210: if (identifierQuoteString != null) {
211: buf.append(identifierQuoteString);
212: }
213: buf.append(catSep);
214: }
215:
216: boolean isHSQLDB = DialectFactory.isHSQL(md);
217: if (isHSQLDB
218: || (supportsSchemasInDataManipulation
219: && _schema != null && _schema.length() > 0)) {
220: if (identifierQuoteString != null) {
221: buf.append(identifierQuoteString);
222: }
223: buf.append(_schema);
224: if (identifierQuoteString != null) {
225: buf.append(identifierQuoteString);
226: }
227:
228: buf.append(catSep);
229: }
230:
231: if (identifierQuoteString != null) {
232: buf.append(identifierQuoteString);
233: }
234: String quoteExpandedName = SQLUtilities
235: .quoteIdentifier(_simpleName);
236: buf.append(quoteExpandedName);
237: if (identifierQuoteString != null) {
238: buf.append(identifierQuoteString);
239: }
240: return buf.toString();
241: }
242:
243: /**
244: * Checks for the presence of Sybase 12.x and if found, sets the identifier
245: * quote string to empty string. See bug:
246: *
247: * [ 1848924 ] Sybase object browser contents not displayed
248: *
249: * for more details.
250: *
251: * @param md the database metadata
252: * @param quoteString the identifer quote string that the driver reported.
253: * @return the same identifier quote string specified if not 12.x; otherwise
254: * empty string is returned.
255: */
256: private String checkSybaseIdentifierQuoteString(
257: final ISQLDatabaseMetaData md, final String quoteString) {
258: String result = quoteString;
259: String productName = null;
260: CharSequence sybaseTwelveVersionId = "12.";
261: try {
262: productName = md.getDatabaseProductVersion();
263: } catch (SQLException e) {
264: // ignore
265: }
266: if (productName != null) {
267: if (productName.contains(sybaseTwelveVersionId)) {
268: result = "";
269: }
270: }
271: return result;
272: }
273:
274: public boolean equals(Object obj) {
275: if (obj == null) {
276: return false;
277: }
278: if (obj.getClass() == this .getClass()) {
279: DatabaseObjectInfo info = (DatabaseObjectInfo) obj;
280: if ((info._catalog == null && _catalog == null)
281: || ((info._catalog != null && _catalog != null) && info._catalog
282: .equals(_catalog))) {
283: if ((info._qualifiedName == null && _qualifiedName == null)
284: || ((info._qualifiedName != null && _qualifiedName != null) && info._qualifiedName
285: .equals(_qualifiedName))) {
286: if ((info._schema == null && _schema == null)
287: || ((info._schema != null && _schema != null) && info._schema
288: .equals(_schema))) {
289: return ((info._simpleName == null && _simpleName == null) || ((info._simpleName != null && _simpleName != null) && info._simpleName
290: .equals(_simpleName)));
291: }
292:
293: }
294: }
295: }
296: return false;
297: }
298:
299: public int hashCode() {
300: return _qualifiedName.hashCode();
301: }
302:
303: public int compareTo(IDatabaseObjectInfo o) {
304: return _qualifiedName.compareTo(o.getQualifiedName());
305: }
306: }
|