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