001: /*
002: * Copyright (C) 2002 Christian Sell
003: * csell@users.sourceforge.net
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * created by cse, 24.09.2002 17:30:57
020: */
021: package net.sourceforge.squirrel_sql.client.session.parser.kernel;
022:
023: import java.sql.SQLException;
024: import java.util.ArrayList;
025: import java.util.List;
026:
027: import net.sourceforge.squirrel_sql.fw.sql.SQLDatabaseMetaData;
028: import net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo;
029:
030: /**
031: * requirements for objects that maintain schema information
032: */
033: public interface SQLSchema {
034: /**
035: * a descriptor which groups together information about a database table
036: */
037: public class Table implements Cloneable, Comparable<Table> {
038: public final String catalog;
039: public final String schema;
040: public final String name;
041: public final String compositeName;
042: public transient String alias;
043:
044: private SQLDatabaseMetaData dmd;
045: private String[] columns;
046:
047: public static String createCompositeName(String catalog,
048: String schema, String name) {
049:
050: StringBuffer sbuf = new StringBuffer();
051: if (catalog != null) {
052: sbuf.append(catalog);
053: sbuf.append(".");
054: }
055: if (schema != null) {
056: sbuf.append(schema);
057: sbuf.append(".");
058: }
059: sbuf.append(name);
060: return sbuf.toString();
061: }
062:
063: public Table clone(String alias) {
064: try {
065: Table newTable = (Table) super .clone();
066: newTable.alias = alias;
067: return newTable;
068: } catch (CloneNotSupportedException e) {
069: e.printStackTrace();
070: return null;
071: }
072: }
073:
074: public Table(String catalog, String schema, String name,
075: SQLDatabaseMetaData dmd) {
076: this .catalog = catalog;
077: this .schema = schema;
078: this .name = name;
079: this .compositeName = createCompositeName(catalog, schema,
080: name);
081: this .dmd = dmd;
082: }
083:
084: public Table(String catalog, String schema, String name) {
085: this (catalog, schema, name, null);
086: }
087:
088: public Table(String schema, String name) {
089: this (null, schema, name, null);
090: }
091:
092: public Table(String name) {
093: this (null, null, name, null);
094: }
095:
096: public void setColumns(String[] columns) {
097: this .columns = columns;
098: }
099:
100: public void setColumns(List<String> columns) {
101: this .columns = columns.toArray(new String[columns.size()]);
102: }
103:
104: public String[] getColumns() {
105: if (columns == null)
106: loadColumns();
107: return columns;
108: }
109:
110: public String[] getColumns(String prefix) {
111: String[] cols = getColumns();
112: if (prefix == null)
113: return cols;
114:
115: List<String> list = new ArrayList<String>(cols.length);
116: for (int i = 0; i < cols.length; i++)
117: if (cols[i].startsWith(prefix))
118: list.add(cols[i]);
119: return list.toArray(new String[list.size()]);
120: }
121:
122: public String getCompositeName() {
123: return compositeName;
124: }
125:
126: public boolean hasAlias() {
127: return alias != null;
128: }
129:
130: public String toString() {
131: return alias == null ? compositeName : compositeName + " "
132: + alias;
133: }
134:
135: public int hashCode() {
136: return compositeName.hashCode();
137: }
138:
139: public boolean equals(Object other) {
140: if (other instanceof Table) {
141: Table o = (Table) other;
142: if (o.compositeName.equals(compositeName)) {
143: return alias == o.alias || alias != null
144: && alias.equals(o.alias);
145: } else
146: return false;
147: } else
148: return false;
149: }
150:
151: public boolean equals(String catalog, String schema,
152: String table) {
153: return (this .catalog == catalog || (this .catalog != null
154: && catalog != null && this .catalog.equals(catalog)))
155: && (this .schema == schema || (this .schema != null
156: && schema != null && this .schema
157: .equals(schema)))
158: && (this .name.equals(table));
159: }
160:
161: public boolean matches(String catalog, String schema,
162: String name) {
163: return (catalog == null || (this .catalog != null && this .catalog
164: .startsWith(catalog)))
165: && (schema == null || (this .schema != null && this .schema
166: .startsWith(schema)))
167: && (name == null || this .name.startsWith(name));
168: }
169:
170: public int compareTo(Table other) {
171: if (alias != null) {
172: return other.alias != null ? alias
173: .compareTo(other.alias) : -1;
174: } else if (other.alias != null) {
175: return 1;
176: }
177: return compositeName.compareTo(other.compositeName);
178: }
179:
180: protected void loadColumns() {
181: try {
182: List<String> cols = new ArrayList<String>();
183: TableColumnInfo[] infos = dmd.getColumnInfo(catalog,
184: schema, name);
185: for (int i = 0; i < infos.length; i++) {
186: cols.add(infos[i].getColumnName());
187: }
188: setColumns(cols);
189: } catch (SQLException e) {
190: throw new RuntimeException(e.getMessage());
191: }
192: }
193: }
194:
195: /**
196: * lookup a table descriptor which exactly matches the parameters
197: * @param catalog optional
198: * @param schema optional
199: * @param name required
200: * @return the table descriptor
201: */
202: Table getTable(String catalog, String schema, String name);
203:
204: /**
205: * Return tables matching a pattern. If a <em>pattern</em> is null it is not
206: * considered. If all patterns are null, all tables are returned
207: * @param catalog catalog name pattern (optional)
208: * @param schema schema name pattern (optional)
209: * @param name name pattern (optional)
210: * @return descriptors for tables matching the parameters
211: */
212: List<Table> getTables(String catalog, String schema, String name);
213:
214: /**
215: * @param alias an alias, possibly also the table name itself
216: * @return the table registered under the given alias/name
217: */
218: Table getTableForAlias(String alias);
219: }
|