001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: /*
021: * Sun Public License Notice
022: *
023: * The contents of this file are subject to the Sun Public License
024: * Version 1.0 (the "License"). You may not use this file except in
025: * compliance with the License. A copy of the License is available at
026: * http://www.sun.com/
027: *
028: * The Original Code is NetBeans. The Initial Developer of the Original
029: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
030: * Microsystems, Inc. All Rights Reserved.
031: */
032:
033: package org.netbeans.modules.sql.project.dbmodel;
034:
035: import java.sql.DatabaseMetaData;
036: import java.sql.ResultSet;
037: import java.sql.SQLException;
038:
039: import java.util.ArrayList;
040: import java.util.Collections;
041: import java.util.List;
042:
043: //Internationalization
044: import java.util.Locale;
045: import java.text.MessageFormat;
046: import java.util.ResourceBundle;
047:
048: /**
049: * Captures database foreign key metadata associated with a specific database
050: * table column.
051: *
052: * @author Jonathan Giron
053: * @version
054: */
055: public class ForeignKeyColumn extends KeyColumn {
056: private static final String RS_PK_NAME = "PK_NAME"; // NOI18N
057:
058: private static final String RS_PKCATALOG_NAME = "PKTABLE_CAT"; // NOI18N
059:
060: private static final String RS_PKSCHEMA_NAME = "PKTABLE_SCHEM"; // NOI18N
061:
062: private static final String RS_PKTABLE_NAME = "PKTABLE_NAME"; // NOI18N
063:
064: private static final String RS_PKCOLUMN_NAME = "PKCOLUMN_NAME"; // NOI18N
065:
066: private static final String RS_FK_NAME = "FK_NAME"; // NOI18N
067:
068: private static final String RS_FKCOLUMN_NAME = "FKCOLUMN_NAME"; // NOI18N
069:
070: private static final String RS_UPDATE_RULE = "UPDATE_RULE"; // NOI18N
071:
072: private static final String RS_DELETE_RULE = "DELETE_RULE"; // NOI18N
073:
074: private static final String RS_DEFERRABILITY = "DEFERRABILITY"; // NOI18N
075: /*
076: * name of catalog containing foreign table whose primary key column is
077: * associated with this foreign key
078: */
079: private String importCatalogName;
080:
081: /*
082: * name of schema referencing foreign table whose primary key column is
083: * associated with this foreign key
084: */
085: private String importSchemaName;
086:
087: /*
088: * name of foreign table whose primary key column is associated with this
089: * foreign key
090: */
091: private String importTableName;
092:
093: /* name of primary key column assocaited with this foreign key */
094: private String importColumnName;
095:
096: /* name of import (primary) key associated with this foreign key */
097: private String importKeyName;
098:
099: /* short flag indicating applicable update rule for this constraint */
100: private short updateRule;
101:
102: /* short flag indicating applicable delete rule for this constraint */
103: private short deleteRule;
104:
105: /* short flag indicating policy on evaluation of this constraint */
106: private short deferrability;
107:
108: /**
109: * Creates a List of ForeignKeyColumn instances from the given ResultSet.
110: *
111: * @param rs ResultSet containing foreign key metadata as obtained from
112: * DatabaseMetaData
113: * @return List of ForeignKeyColumn instances based from metadata in rs
114: *
115: * @throws SQLException if SQL error occurs while reading in data from
116: * given ResultSet
117: */
118: public static List createForeignKeyColumnList(ResultSet rs)
119: throws SQLException {
120: if (rs == null) {
121: Locale locale = Locale.getDefault();
122: ResourceBundle cMessages = ResourceBundle.getBundle(
123: "com/stc/oracle/builder/Bundle", locale); // NO i18n
124: throw new IllegalArgumentException(cMessages
125: .getString("ERROR_NULL_RS")
126: + "(ERROR_NULL_RS)");
127: }
128:
129: List fkColumns = Collections.EMPTY_LIST;
130:
131: if (rs != null && rs.next()) {
132: fkColumns = new ArrayList();
133:
134: do {
135: fkColumns.add(new ForeignKeyColumn(rs));
136: } while (rs.next());
137: }
138:
139: return fkColumns;
140: }
141:
142: /**
143: * Creates an instance of ForeignKeyColumn with the given values.
144: *
145: * @param fkName name of FK
146: * @param fkColumn name of column assocaited with FK
147: * @param pkName name of PK that this FK imports
148: * @param pkColumn name of column that this FK imports
149: * @param pkTable name of table containing column that this FK imports
150: * @param pkSchema name of schema containing table with PK that this FK imports
151: * @param pkCatalog name of catalog containing table with PK that this FK imports
152: * @param colSequence sequence of this column within (composite) primary key
153: * @param updateFlag applicable update rule for this FK; one of
154: * java.sql.DatabaseMetaData.importedKeyNoAction,
155: * java.sql.DatabaseMetaData.importedKeyCascade,
156: * java.sql.DatabaseMetaData.importedKeySetNull,
157: * java.sql.DatabaseMetaData#importedKeySetDefault, or
158: * java.sql.DatabaseMetaData#importedKeyRestrict
159: *
160: * @param deleteFlag applicable delete rule for this FK; one of
161: * java.sql.DatabaseMetaData.importedKeyNoAction,
162: * java.sql.DatabaseMetaData.importedKeyCascade,
163: * java.sql.DatabaseMetaData.importedKeySetNull,
164: * java.sql.DatabaseMetaData.importedKeyRestrict, or
165: * java.sql.DatabaseMetaData.importedKeySetDefault
166: *
167: * @param deferFlag deferrability flag for this FK; one of
168: * java.sql.DatabaseMetaData.importedKeyInitiallyDeferred,
169: * java.sql.DatabaseMetaData.importedKeyInitiallyImmediate, or
170: * java.sql.DatabaseMetaData.importedKeyNotDeferrable
171: *
172: * @see java.sql.DatabaseMetaData#importedKeyCascade
173: * @see java.sql.DatabaseMetaData#importedKeyInitiallyDeferred
174: * @see java.sql.DatabaseMetaData#importedKeyInitiallyImmediate
175: * @see java.sql.DatabaseMetaData#importedKeyNoAction
176: * @see java.sql.DatabaseMetaData#importedKeyNotDeferrable
177: * @see java.sql.DatabaseMetaData#importedKeyRestrict
178: * @see java.sql.DatabaseMetaData#importedKeySetNull
179: * @see java.sql.DatabaseMetaData#importedKeySetDefault
180: */
181: public ForeignKeyColumn(String fkName, String fkColumn,
182: String pkName, String pkColumn, String pkTable,
183: String pkSchema, String pkCatalog, short colSequence,
184: short updateFlag, short deleteFlag, short deferFlag) {
185: super (fkName, fkColumn, colSequence);
186:
187: importKeyName = pkName;
188: importCatalogName = pkCatalog;
189: importSchemaName = pkSchema;
190: importTableName = pkTable;
191: importColumnName = pkColumn;
192:
193: setUpdateRule(updateFlag);
194: setDeleteRule(deleteFlag);
195: setDeferrability(deferFlag);
196: }
197:
198: public ForeignKeyColumn(ForeignKeyColumn fkCol) {
199: super (fkCol.getName(), fkCol.getColumnName(), fkCol
200: .getColumnSequence());
201:
202: importKeyName = fkCol.getImportKeyName();
203: importCatalogName = fkCol.getImportCatalogName();
204: importSchemaName = fkCol.getImportSchemaName();
205: importTableName = fkCol.getImportTableName();
206: importColumnName = fkCol.getImportColumnName();
207:
208: setUpdateRule(fkCol.getUpdateRule());
209: setDeleteRule(fkCol.getDeleteRule());
210: setDeferrability(fkCol.getDeferrability());
211: }
212:
213: private ForeignKeyColumn(ResultSet rs) throws SQLException {
214: if (rs == null) {
215: Locale locale = Locale.getDefault();
216: ResourceBundle cMessages = ResourceBundle.getBundle(
217: "com/stc/oracle/builder/Bundle", locale); // NO i18n
218: throw new IllegalArgumentException(cMessages
219: .getString("ERROR_VALID_RS")
220: + "(ERROR_VALID_RS)");
221: }
222:
223: importCatalogName = rs.getString(RS_PKCATALOG_NAME);
224: importSchemaName = rs.getString(RS_PKSCHEMA_NAME);
225: importTableName = rs.getString(RS_PKTABLE_NAME);
226: importColumnName = rs.getString(RS_PKCOLUMN_NAME);
227: importKeyName = rs.getString(RS_PK_NAME);
228:
229: columnName = rs.getString(RS_FKCOLUMN_NAME);
230: keyName = rs.getString(RS_FK_NAME);
231:
232: sequenceNum = rs.getShort(RS_SEQUENCE_NUM);
233:
234: updateRule = rs.getShort(RS_UPDATE_RULE);
235: deleteRule = rs.getShort(RS_DELETE_RULE);
236: deferrability = rs.getShort(RS_DEFERRABILITY);
237: }
238:
239: /**
240: * Gets name of catalog containing the import table which, in turn,
241: * contains the imported (primary) key associated with this foreign
242: * key.
243: *
244: * @return name of catalog containing the imported primary key's
245: * encapsulating table
246: */
247: public String getImportCatalogName() {
248: return importCatalogName;
249: }
250:
251: /**
252: * Gets name of schema containing the import table which, in turn,
253: * contains the imported (primary) key associated with this foreign
254: * key.
255: *
256: * @return name of schema containing the imported primary key's
257: * encapsulating table
258: */
259: public String getImportSchemaName() {
260: return importSchemaName;
261: }
262:
263: /**
264: * Gets name of import table containing imported (primary) key
265: * associated with this foreign key.
266: *
267: * @return name of table containing imported primary key
268: */
269: public String getImportTableName() {
270: return importTableName;
271: }
272:
273: /**
274: * Gets name of import column contained within imported (primary) key
275: * associated with this foreign key.
276: *
277: * @return name of imported column
278: */
279: public String getImportColumnName() {
280: return importColumnName;
281: }
282:
283: /**
284: * Gets key name of imported (primary) key associated with this foreign
285: * key.
286: *
287: * @return name of imported primary key
288: */
289: public String getImportKeyName() {
290: return importKeyName;
291: }
292:
293: /**
294: * Gets update rule.
295: *
296: * @return update rule; one of
297: * java.sql.DatabaseMetaData.importedKeyNoAction,
298: * java.sql.DatabaseMetaData.importedKeyCascade,
299: * java.sql.DatabaseMetaData.importedKeySetNull,
300: * java.sql.DatabaseMetaData.importedKeyRestrict, or
301: * java.sql.DatabaseMetaData.importedKeySetDefault.
302: *
303: * @see java.sql.DatabaseMetaData#importedKeyNoAction
304: * @see java.sql.DatabaseMetaData#importedKeyCascade
305: * @see java.sql.DatabaseMetaData#importedKeySetNull
306: * @see java.sql.DatabaseMetaData#importedKeyRestrict
307: * @see java.sql.DatabaseMetaData#importedKeySetDefault
308: */
309: public short getUpdateRule() {
310: return updateRule;
311: }
312:
313: /**
314: * Gets delete rule.
315: *
316: * @return update rule; one of
317: * java.sql.DatabaseMetaData.importedKeyNoAction,
318: * java.sql.DatabaseMetaData.importedKeyCascade,
319: * java.sql.DatabaseMetaData.importedKeySetNull,
320: * java.sql.DatabaseMetaData.importedKeyRestrict, or
321: * java.sql.DatabaseMetaData.importedKeySetDefault.
322: *
323: * @see java.sql.DatabaseMetaData#importedKeyNoAction
324: * @see java.sql.DatabaseMetaData#importedKeyCascade
325: * @see java.sql.DatabaseMetaData#importedKeySetNull
326: * @see java.sql.DatabaseMetaData#importedKeyRestrict
327: * @see java.sql.DatabaseMetaData#importedKeySetDefault
328: */
329: public short getDeleteRule() {
330: return deleteRule;
331: }
332:
333: /**
334: * Gets deferrability flag.
335: *
336: * @return deferrability flag; one of
337: * java.sql.DatabaseMetaData.importedKeyInitiallyDeferred,
338: * java.sql.DatabaseMetaData.importedKeyInitiallyImmediate, or
339: * java.sql.DatabaseMetaData.importedKeyNotDeferrable
340: *
341: * @see java.sql.DatabaseMetaData#importedKeyInitiallyDeferred,
342: * @see java.sql.DatabaseMetaData#importedKeyInitiallyImmediate, or
343: * @see java.sql.DatabaseMetaData#importedKeyNotDeferrable
344: */
345: public short getDeferrability() {
346: return deferrability;
347: }
348:
349: private void setUpdateRule(short newRule) {
350: switch (newRule) {
351: case DatabaseMetaData.importedKeyNoAction:
352: case DatabaseMetaData.importedKeyCascade:
353: case DatabaseMetaData.importedKeySetNull:
354: case DatabaseMetaData.importedKeySetDefault:
355: case DatabaseMetaData.importedKeyRestrict:
356: updateRule = newRule;
357: break;
358:
359: default:
360: Locale locale = Locale.getDefault();
361: ResourceBundle cMessages = ResourceBundle.getBundle(
362: "com/stc/oracle/builder/Bundle", locale); // NO i18n
363: throw new IllegalArgumentException(cMessages
364: .getString("ERROR_VALID_RULE")
365: + "(ERROR_VALID_RULE)");
366: }
367: }
368:
369: private void setDeleteRule(short newRule) {
370: switch (newRule) {
371: case DatabaseMetaData.importedKeyNoAction:
372: case DatabaseMetaData.importedKeyCascade:
373: case DatabaseMetaData.importedKeySetNull:
374: case DatabaseMetaData.importedKeySetDefault:
375: case DatabaseMetaData.importedKeyRestrict:
376: deleteRule = newRule;
377: break;
378:
379: default:
380: Locale locale = Locale.getDefault();
381: ResourceBundle cMessages = ResourceBundle.getBundle(
382: "com/stc/oracle/builder/Bundle", locale); // NO i18n
383: throw new IllegalArgumentException(cMessages
384: .getString("ERROR_VALID_RULE")
385: + "(ERROR_VALID_RULE)");
386: }
387: }
388:
389: private void setDeferrability(short newFlag) {
390: switch (newFlag) {
391: case DatabaseMetaData.importedKeyInitiallyDeferred:
392: case DatabaseMetaData.importedKeyInitiallyImmediate:
393: case DatabaseMetaData.importedKeyNotDeferrable:
394: deferrability = newFlag;
395: break;
396:
397: default:
398: System.err
399: .println("Received unrecognized value for newFlag, but carrying on with it anyway.");
400: deferrability = newFlag;
401: break;
402: }
403: }
404: }
|