001: /* ====================================================================
002: * The QueryForm License, Version 1.1
003: *
004: * Copyright (c) 1998 - 2004 David F. Glasser. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by
022: * David F. Glasser."
023: * Alternately, this acknowledgment may appear in the software itself,
024: * if and wherever such third-party acknowledgments normally appear.
025: *
026: * 4. The names "QueryForm" and "David F. Glasser" must
027: * not be used to endorse or promote products derived from this
028: * software without prior written permission. For written
029: * permission, please contact dglasser@pobox.com.
030: *
031: * 5. Products derived from this software may not be called "QueryForm",
032: * nor may "QueryForm" appear in their name, without prior written
033: * permission of David F. Glasser.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL DAVID F. GLASSER, THE APACHE SOFTWARE
039: * FOUNDATION OR ITS CONTRIBUTORS, OR ANY AUTHORS OR DISTRIBUTORS
040: * OF THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This product includes software developed by the
051: * Apache Software Foundation (http://www.apache.org/).
052: *
053: * ====================================================================
054: *
055: * $Source: /cvsroot/qform/qform/src/org/glasser/sql/TableInfo.java,v $
056: * $Revision: 1.7 $
057: * $Author: dglasser $
058: * $Date: 2005/01/26 02:22:58 $
059: *
060: * --------------------------------------------------------------------
061: */
062: package org.glasser.sql;
063:
064: import org.glasser.util.*;
065: import org.glasser.util.comparators.*;
066: import java.util.*;
067:
068: public class TableInfo {
069:
070: String cachedString = null;
071:
072: public final static MethodComparator NAME_COMPARATOR = new MethodComparator(
073: org.glasser.sql.TableInfo.class, "getTableName");
074:
075: protected String tableCat = null;
076:
077: protected String tableSchem = null;
078:
079: protected String tableName = null;
080:
081: protected String tableType = null;
082:
083: protected String remarks = null;
084:
085: protected String typeCat = null;
086:
087: protected String typeSchem = null;
088:
089: protected String typeName = null;
090:
091: protected Column[] columns = null;
092:
093: protected ForeignKey[] foreignKeys = null;
094:
095: protected ForeignKey[] exportedKeys = null;
096:
097: protected HashMap columnMap = null;
098:
099: protected String qualifiedTableName = null;
100:
101: public void setTableCat(String tableCat) {
102: this .tableCat = trimToNull(tableCat);
103: }
104:
105: public void setTableSchem(String tableSchem) {
106: qualifiedTableName = null;
107: this .tableSchem = trimToNull(tableSchem);
108: }
109:
110: public void setTableName(String tableName) {
111: qualifiedTableName = null;
112: this .tableName = trimToNull(tableName);
113: }
114:
115: public void setTableType(String tableType) {
116: this .tableType = trimToNull(tableType);
117: }
118:
119: public void setRemarks(String remarks) {
120: this .remarks = trimToNull(remarks);
121: }
122:
123: public void setTypeCat(String typeCat) {
124: this .typeCat = trimToNull(typeCat);
125: }
126:
127: public void setTypeSchem(String typeSchem) {
128: this .typeSchem = trimToNull(typeSchem);
129: }
130:
131: public void setTypeName(String typeName) {
132: this .typeName = trimToNull(typeName);
133: }
134:
135: public void setColumns(Column[] columns) {
136: columnMap = null;
137: this .columns = columns;
138: }
139:
140: public void setForeignKeys(ForeignKey[] foreignKeys) {
141: this .foreignKeys = foreignKeys;
142: }
143:
144: public void setExportedKeys(ForeignKey[] exportedKeys) {
145: this .exportedKeys = exportedKeys;
146: }
147:
148: public String getTableCat() {
149: return tableCat;
150: }
151:
152: public String getTableSchem() {
153: return tableSchem;
154: }
155:
156: public String getTableName() {
157: return tableName;
158: }
159:
160: public String getTableType() {
161: return tableType;
162: }
163:
164: public String getRemarks() {
165: return remarks;
166: }
167:
168: public String getTypeCat() {
169: return typeCat;
170: }
171:
172: public String getTypeSchem() {
173: return typeSchem;
174: }
175:
176: public String getTypeName() {
177: return typeName;
178: }
179:
180: public Column[] getColumns() {
181: return columns;
182: }
183:
184: public ForeignKey[] getForeignKeys() {
185: return foreignKeys;
186: }
187:
188: public ForeignKey[] getExportedKeys() {
189: return exportedKeys;
190: }
191:
192: protected synchronized HashMap getColumnMap() {
193: HashMap tempMap = columnMap; // thead safety
194: if (tempMap == null) {
195: tempMap = new HashMap();
196: Column[] temp = columns; // thread safety
197: for (int j = 0; temp != null && j < temp.length; j++) {
198: tempMap.put(temp[j].getColumnName(), temp[j]);
199: }
200: columnMap = tempMap;
201: }
202: return tempMap;
203: }
204:
205: public Column getColumn(String columnName) {
206: return (Column) getColumnMap().get(columnName);
207: }
208:
209: public String debugString() {
210:
211: StringBuffer buffer = new StringBuffer(200);
212: buffer.append("TableInfo[");
213: buffer.append("tableCat=");
214: buffer.append(tableCat);
215: buffer.append(",tableSchem=");
216: buffer.append(tableSchem);
217: buffer.append(",tableName=");
218: buffer.append(tableName);
219: buffer.append(",tableType=");
220: buffer.append(tableType);
221: buffer.append(",remarks=");
222: buffer.append(remarks);
223: buffer.append(",typeCat=");
224: buffer.append(typeCat);
225: buffer.append(",typeSchem=");
226: buffer.append(typeSchem);
227: buffer.append(",typeName=");
228: buffer.append(typeName);
229: buffer.append("]");
230: return buffer.toString();
231:
232: }
233:
234: public String toString() {
235: if (tableName != null)
236: return tableName;
237: else
238: return "TableInfo[tableName=null]";
239: }
240:
241: /**
242: * Returns the tableName enclosed in the provided "quote" characters, if
243: * the tableName contains a space or a hyphen, otherwise returns the tableName as-is.
244: * Normally, the openQuote and closeQuote args will both be regular double quotation
245: * marks ("), however, Microsoft databases use square brackets ([ and ]).
246: */
247: public String maybeQuoteTableName(char openQuote, char closeQuote) {
248:
249: if (tableName == null)
250: return null;
251:
252: if (tableName.indexOf(' ') < 0 && tableName.indexOf('-') < 0) {
253: return tableName;
254: }
255:
256: StringBuffer buffer = new StringBuffer(tableName.length() + 4);
257:
258: return buffer.append(openQuote).append(tableName).append(
259: closeQuote).toString();
260:
261: }
262:
263: /**
264: * Returns the tableSchem field enclosed in the provided "quote" characters, if
265: * the tableName contains a space or a hyphen, otherwise returns the tableSchem as-is.
266: * Normally, the openQuote and closeQuote args will both be regular double quotation
267: * marks ("), however, Microsoft databases use square brackets ([ and ]).
268: */
269: public String maybeQuoteTableSchem(char openQuote, char closeQuote) {
270:
271: if (tableSchem == null || tableSchem.trim().length() == 0)
272: return null;
273:
274: if (tableSchem.indexOf(' ') < 0 && tableSchem.indexOf('-') < 0) {
275: return tableSchem;
276: }
277:
278: StringBuffer buffer = new StringBuffer(tableSchem.length() + 4);
279:
280: return buffer.append(openQuote).append(tableSchem).append(
281: closeQuote).toString();
282:
283: }
284:
285: /**
286: * Returns the tableName prefixed by the tableSchem field and a dot, if the tableSchem field
287: * contains non-whitespace characters. If the tableSchem field is null or whitespace, then only
288: * the tableName is returned. Either the tableSchem or tableName may be enclosed in
289: * the provided quoting characters, if they contain spaces or hyphens.
290: */
291: public String getQualifiedTableName(char openQuote, char closeQuote) {
292:
293: String table = maybeQuoteTableName(openQuote, closeQuote);
294: if (table == null)
295: return null;
296:
297: String schem = maybeQuoteTableSchem(openQuote, closeQuote);
298: if (schem == null)
299: return table;
300:
301: StringBuffer buffer = new StringBuffer(table.length()
302: + schem.length() + 2);
303: return buffer.append(schem).append('.').append(table)
304: .toString();
305:
306: }
307:
308: /**
309: * Calls getQualifiedTableName(char, char) using double quotation marks as
310: * the openQuote and closeQuote.
311: */
312: public String getQualifiedTableName() {
313: return getQualifiedTableName('"', '"');
314: }
315:
316: /**
317: * Trims whitespace, and returns null if the String is zero-length
318: * or all whitespace.
319: */
320: public String trimToNull(String s) {
321: if (s == null || (s = s.trim()).length() == 0)
322: return null;
323: return s;
324: }
325:
326: }
|