001: /*
002: * SQLeonardo :: java database frontend
003: * Copyright (C) 2004 nickyb@users.sourceforge.net
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or (at your option) any later version.
009: *
010: * This program 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
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: *
019: */
020:
021: package nickyb.sqleonardo.environment.ctrl.define;
022:
023: import java.sql.DatabaseMetaData;
024: import java.sql.ResultSet;
025: import java.sql.ResultSetMetaData;
026: import java.sql.SQLException;
027: import java.util.ArrayList;
028:
029: import nickyb.sqleonardo.common.jdbc.ConnectionAssistant;
030: import nickyb.sqleonardo.common.jdbc.ConnectionHandler;
031: import nickyb.sqleonardo.environment.Application;
032: import nickyb.sqleonardo.environment.ctrl.content.UpdateModel;
033: import nickyb.sqleonardo.querybuilder.QueryBuilder;
034: import nickyb.sqleonardo.querybuilder.QueryModel;
035: import nickyb.sqleonardo.querybuilder.syntax.QueryTokens;
036: import nickyb.sqleonardo.querybuilder.syntax.SQLFormatter;
037:
038: public class TableMetaData {
039: private String keycah;
040: private String schema;
041: private String name;
042: private String type;
043:
044: private ArrayList alColumns;
045: private ArrayList alPrimaryKeys;
046: private ArrayList alImportedKeys;
047: private ArrayList alExportedKeys;
048:
049: public TableMetaData(String keycah, String schema, String name) {
050: this (keycah, schema, name, null);
051: }
052:
053: public TableMetaData(String keycah, String schema, String name,
054: String type) {
055: this .keycah = keycah;
056: this .schema = schema;
057: this .name = name;
058: this .type = type;
059: }
060:
061: public QueryModel createQueryModel() {
062: QueryModel qm = new QueryModel();
063:
064: QueryTokens.Table table = new QueryTokens.Table(schema, name);
065: qm.getQueryExpression().getQuerySpecification().addFromClause(
066: table);
067:
068: ArrayList al = this .getColumns();
069: for (int i = 0; i < al.size(); i++) {
070: QueryTokens.Column column = new QueryTokens.Column(table,
071: this .getColumnProperty(i,
072: TableMetaData.IDX_COL_COLUMN_NAME));
073: qm.getQueryExpression().getQuerySpecification()
074: .addSelectList(column);
075: }
076:
077: return qm;
078: }
079:
080: public UpdateModel createUpdateModel() {
081: QueryTokens.Table table = new QueryTokens.Table(schema, name);
082:
083: ArrayList al = this .getPrimaryKeys();
084: QueryTokens.Column[] pk = new QueryTokens.Column[al.size()];
085:
086: for (int i = 0; i < al.size(); i++) {
087: pk[i] = new QueryTokens.Column(table, this
088: .getColumnProperty(i,
089: TableMetaData.IDX_COL_COLUMN_NAME));
090: }
091:
092: UpdateModel um = new UpdateModel();
093: um.setTable(table);
094: um.setRowIdentifier(pk);
095:
096: return um;
097: }
098:
099: public String getName() {
100: return name;
101: }
102:
103: public String getSchema() {
104: return schema;
105: }
106:
107: public String getHandlerKey() {
108: if (ConnectionAssistant.hasHandler(keycah)) {
109: QueryBuilder.identifierQuoteString = ConnectionAssistant
110: .getHandler(keycah).getObject(
111: "$identifierQuoteString").toString();
112: QueryBuilder.maxColumnNameLength = ((Integer) ConnectionAssistant
113: .getHandler(keycah).getObject(
114: "$maxColumnNameLength")).intValue();
115: }
116:
117: return keycah;
118: }
119:
120: public boolean isPrimaryKey(String column) {
121: for (int i = 0; i < this .getPrimaryKeys().size(); i++) {
122: if (this .getPrimaryKeyProperty(i,
123: TableMetaData.IDX_PK_COLUMN_NAME).equals(column))
124: return true;
125: }
126:
127: return false;
128: }
129:
130: public ArrayList getColumns() {
131: if (alColumns == null) {
132: alColumns = new ArrayList();
133:
134: try {
135: ConnectionHandler ch = ConnectionAssistant
136: .getHandler(this .getHandlerKey());
137: DatabaseMetaData dbmd = ch.get().getMetaData();
138:
139: String catalog = schema == null ? null : ch.get()
140: .getCatalog();
141: copy(dbmd.getColumns(catalog, schema, name, "%"),
142: alColumns);
143: } catch (SQLException e) {
144: Application.println("[ TableMetaData::getColumns() ]\n"
145: + e);
146: }
147: }
148:
149: return alColumns;
150: }
151:
152: public String getColumnProperty(int row, int idx) {
153: return ((String[]) this .getColumns().get(row))[idx];
154: }
155:
156: public String getPrimaryKeyProperty(int row, int idx) {
157: return ((String[]) this .getPrimaryKeys().get(row))[idx];
158: }
159:
160: public ArrayList getPrimaryKeys() {
161: if (alPrimaryKeys == null) {
162: alPrimaryKeys = new ArrayList();
163:
164: try {
165: ConnectionHandler ch = ConnectionAssistant
166: .getHandler(this .getHandlerKey());
167: DatabaseMetaData dbmd = ch.get().getMetaData();
168:
169: String catalog = schema == null ? null : ch.get()
170: .getCatalog();
171: copy(dbmd.getPrimaryKeys(catalog, schema, name),
172: alPrimaryKeys);
173: } catch (SQLException e) {
174: Application
175: .println("[ TableMetaData::getPrimaryKeys() ]\n"
176: + e);
177: }
178: }
179:
180: return alPrimaryKeys;
181: }
182:
183: public String getImportedKeyProperty(int row, int idx) {
184: return ((String[]) this .getImportedKeys().get(row))[idx];
185: }
186:
187: public ArrayList getImportedKeys() {
188: if (alImportedKeys == null) {
189: alImportedKeys = new ArrayList();
190:
191: try {
192: ConnectionHandler ch = ConnectionAssistant
193: .getHandler(this .getHandlerKey());
194: DatabaseMetaData dbmd = ch.get().getMetaData();
195:
196: String catalog = schema == null ? null : ch.get()
197: .getCatalog();
198: copy(dbmd.getImportedKeys(catalog, schema, name),
199: alImportedKeys);
200: } catch (SQLException e) {
201: Application
202: .println("[ TableMetaData::getImportedKeys() ]\n"
203: + e);
204: }
205: }
206:
207: return alImportedKeys;
208: }
209:
210: public String getExportedKeyProperty(int row, int idx) {
211: return ((String[]) this .getExportedKeys().get(row))[idx];
212: }
213:
214: public ArrayList getExportedKeys() {
215: if (alExportedKeys == null) {
216: alExportedKeys = new ArrayList();
217:
218: try {
219: ConnectionHandler ch = ConnectionAssistant
220: .getHandler(this .getHandlerKey());
221: DatabaseMetaData dbmd = ch.get().getMetaData();
222:
223: String catalog = schema == null ? null : ch.get()
224: .getCatalog();
225: copy(dbmd.getExportedKeys(catalog, schema, name),
226: alExportedKeys);
227: } catch (SQLException e) {
228: Application
229: .println("[ TableMetaData::getExportedKeys() ]\n"
230: + e);
231: }
232: }
233:
234: return alExportedKeys;
235: }
236:
237: public String getType() {
238: if (type == null) {
239: try {
240: ConnectionHandler ch = ConnectionAssistant
241: .getHandler(this .getHandlerKey());
242: DatabaseMetaData dbmd = ch.get().getMetaData();
243:
244: String catalog = schema == null ? null : ch.get()
245: .getCatalog();
246: ResultSet rs = dbmd.getTables(catalog, schema, name,
247: null);
248:
249: if (rs.next()) {
250: type = rs.getString(4);
251: }
252: } catch (SQLException e) {
253: Application
254: .println("[ TableMetaData::getExportedKeys() ]\n"
255: + e);
256: }
257: }
258:
259: return type;
260: }
261:
262: public String toString() {
263: return schema != null ? schema + SQLFormatter.DOT
264: + this .getName() : this .getName();
265: }
266:
267: private static void copy(ResultSet rs, ArrayList al)
268: throws SQLException {
269: ResultSetMetaData rsmd = rs.getMetaData();
270: while (rs.next()) {
271: String[] row = new String[rsmd.getColumnCount()];
272: al.add(row);
273:
274: for (int i = 0; i < row.length; i++)
275: row[i] = rs.getString(i + 1);
276: }
277: rs.close();
278: }
279:
280: /* COLUMN */
281: public static final int IDX_COL_TABLE_CAT = 0;
282: public static final int IDX_COL_TABLE_SCHEM = 1;
283: public static final int IDX_COL_TABLE_NAME = 2;
284: public static final int IDX_COL_COLUMN_NAME = 3;
285: public static final int IDX_COL_DATA_TYPE = 4;
286: public static final int IDX_COL_TYPE_NAME = 5;
287: public static final int IDX_COL_COLUMN_SIZE = 6;
288: public static final int IDX_COL_BUFFER_LENGTH = 7;
289: public static final int IDX_COL_DECIMAL_DIGITS = 8;
290: public static final int IDX_COL_NUM_PREC_RADIX = 9;
291: public static final int IDX_COL_NULLABLE = 10;
292: public static final int IDX_COL_REMARKS = 11;
293: public static final int IDX_COL_COLUMN_DEF = 12;
294: public static final int IDX_COL_SQL_DATA_TYPE = 13;
295: public static final int IDX_COL_SQL_DATETIME_SUB = 14;
296: public static final int IDX_COL_CHAR_OCTET_LENGTH = 15;
297: public static final int IDX_COL_ORDINAL_POSITION = 16;
298: public static final int IDX_COL_IS_NULLABLE = 17;
299:
300: /* PRiMARY KEY */
301: public static final int IDX_PK_TABLE_CAT = 0;
302: public static final int IDX_PK_TABLE_SCHEM = 1;
303: public static final int IDX_PK_TABLE_NAME = 2;
304: public static final int IDX_PK_COLUMN_NAME = 3;
305: public static final int IDX_PK_KEY_SEQ = 4;
306: public static final int IDX_PK_PK_NAME = 5;
307:
308: /* RELATiONSHiP (iMPORTED&EXPORTED KEY) */
309: public static final int IDX_REL_PKTABLE_CAT = 0;
310: public static final int IDX_REL_PKTABLE_SCHEM = 1;
311: public static final int IDX_REL_PKTABLE_NAME = 2;
312: public static final int IDX_REL_PKCOLUMN_NAME = 3;
313: public static final int IDX_REL_FKTABLE_CAT = 4;
314: public static final int IDX_REL_FKTABLE_SCHEM = 5;
315: public static final int IDX_REL_FKTABLE_NAME = 6;
316: public static final int IDX_REL_FKCOLUMN_NAME = 7;
317: public static final int IDX_REL_KEY_SEQ = 8;
318: public static final int IDX_REL_UPDATE_RULE = 9;
319: public static final int IDX_REL_DELETE_RULE = 10;
320: public static final int IDX_REL_FK_NAME = 11;
321: public static final int IDX_REL_PK_NAME = 12;
322: public static final int IDX_REL_DEFERRABILITY = 13;
323: }
|