01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.openjpa.jdbc.schema;
20:
21: /**
22: * Represents a table primary key. It can also represent a partial key,
23: * aligning with the key information available from
24: * {@link java.sql.DatabaseMetaData}.
25: *
26: * @author Abe White
27: */
28: public class PrimaryKey extends LocalConstraint {
29:
30: private boolean _logical = false;
31:
32: /**
33: * Default constructor.
34: */
35: public PrimaryKey() {
36: }
37:
38: /**
39: * Constructor.
40: *
41: * @param name the name of the primary key, if any
42: * @param table the table of the primary key
43: */
44: public PrimaryKey(String name, Table table) {
45: super (name, table);
46: }
47:
48: public boolean isLogical() {
49: return _logical;
50: }
51:
52: public void setLogical(boolean logical) {
53: _logical = logical;
54: }
55:
56: void remove() {
57: // check all foreign keys in the schema group, removing ones that
58: // reference this primary key
59: Table table = getTable();
60: if (table != null && table.getSchema() != null
61: && table.getSchema().getSchemaGroup() != null) {
62: ForeignKey[] fks = table.getSchema().getSchemaGroup()
63: .findExportedForeignKeys(this );
64: for (int i = 0; i < fks.length; i++)
65: fks[i].getTable().removeForeignKey(fks[i]);
66: }
67: super .remove();
68: }
69:
70: public void addColumn(Column col) {
71: super .addColumn(col);
72: col.setPrimaryKey(true);
73: if (!_logical)
74: col.setNotNull(true);
75: }
76:
77: /**
78: * Return true if the structure of this primary key matches that of
79: * the given one (same table, same columns).
80: */
81: public boolean equalsPrimaryKey(PrimaryKey pk) {
82: return equalsLocalConstraint(pk);
83: }
84: }
|