001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.jdbc.schema;
020:
021: /**
022: * A table constraint. This class is closely aligned with the constraint
023: * information available from {@link java.sql.DatabaseMetaData}.
024: *
025: * @author Abe White
026: */
027: public abstract class Constraint extends ReferenceCounter {
028:
029: private String _name = null;
030: private String _fullName = null;
031: private Table _table = null;
032: private String _tableName = null;
033: private String _schemaName = null;
034: private String _columnName = null;
035: private boolean _deferred = false;
036:
037: /**
038: * Default constructor.
039: */
040: Constraint() {
041: }
042:
043: /**
044: * Constructor.
045: *
046: * @param name the name of the constraint, or null if none
047: * @param table the local table of the constraint
048: */
049: Constraint(String name, Table table) {
050: setName(name);
051: if (table != null) {
052: setTableName(table.getName());
053: setSchemaName(table.getSchemaName());
054: }
055: _table = table;
056: }
057:
058: /**
059: * Called when the constraint is removed from the owning table.
060: * Invalidates the constraint.
061: */
062: void remove() {
063: _table = null;
064: }
065:
066: /**
067: * Return the table of this constraint.
068: */
069: public Table getTable() {
070: return _table;
071: }
072:
073: /**
074: * Return the column's table name.
075: */
076: public String getTableName() {
077: return _tableName;
078: }
079:
080: /**
081: * Set the column's table name. You can only call this method on
082: * columns whose table object is not set.
083: */
084: public void setTableName(String name) {
085: if (getTable() != null)
086: throw new IllegalStateException();
087: _tableName = name;
088: _fullName = null;
089: }
090:
091: /**
092: * Return the column table's schema name.
093: */
094: public String getSchemaName() {
095: return _schemaName;
096: }
097:
098: /**
099: * Set the column table's schema name. You can only call this method on
100: * columns whose tbale object is not set.
101: */
102: public void setSchemaName(String schema) {
103: if (getTable() != null)
104: throw new IllegalStateException();
105: _schemaName = schema;
106: }
107:
108: /**
109: * Return the column's name.
110: */
111: public String getColumnName() {
112: return _columnName;
113: }
114:
115: /**
116: * Set the column's name. You can only call this method on
117: * columns whose table object is not set.
118: */
119: public void setColumnName(String name) {
120: if (getTable() != null)
121: throw new IllegalStateException();
122: _columnName = name;
123: }
124:
125: /**
126: * Return the name of the constraint.
127: */
128: public String getName() {
129: return _name;
130: }
131:
132: /**
133: * Set the name of the constraint. This method cannot be called if the
134: * constraint already belongs to a table.
135: */
136: public void setName(String name) {
137: if (getTable() != null)
138: throw new IllegalStateException();
139: _name = name;
140: _fullName = null;
141: }
142:
143: /**
144: * Return the full name of the constraint.
145: */
146: public String getFullName() {
147: if (_fullName == null) {
148: String name = getName();
149: if (name == null)
150: return null;
151: String tname = getTableName();
152: if (tname == null)
153: return name;
154: _fullName = tname + "." + name;
155: }
156: return _fullName;
157: }
158:
159: /**
160: * Return whether this constraint is a logical constraint only; i.e.
161: * if it does not exist in the database.
162: */
163: public abstract boolean isLogical();
164:
165: /**
166: * Return true if this is a deferred constraint.
167: */
168: public boolean isDeferred() {
169: return _deferred;
170: }
171:
172: /**
173: * Make this constrain deferred.
174: */
175: public void setDeferred(boolean deferred) {
176: _deferred = deferred;
177: }
178:
179: public String toString() {
180: if (getName() != null)
181: return getName();
182:
183: String name = getClass().getName();
184: name = name.substring(name.lastIndexOf('.') + 1);
185: return "<" + name.toLowerCase() + ">";
186: }
187: }
|