001: /*
002: * (c) Copyright 2006 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.platform.db.model;
028:
029: import org.databene.commons.ObjectNotFoundException;
030: import org.databene.commons.OrderedMap;
031: import org.databene.model.depend.Dependent;
032:
033: import java.util.List;
034: import java.util.ArrayList;
035:
036: /**
037: * Created: 06.01.2007 08:58:49
038: */
039: public class DBTable implements Dependent<DBTable> {
040:
041: private DBCatalog catalog;
042: private DBSchema schema;
043: private String name;
044: private String doc;
045: private OrderedMap<String, DBColumn> columns;
046: private DBPrimaryKeyConstraint primaryKeyConstraint;
047: private List<DBUniqueConstraint> uniqueConstraints;
048: private List<DBForeignKeyConstraint> foreignKeyConstraints;
049: private OrderedMap<String, DBIndex> indexes;
050:
051: // constructors ----------------------------------------------------------------------------------------------------
052:
053: public DBTable() {
054: this (null);
055: }
056:
057: public DBTable(String name) {
058: this (null, name);
059: }
060:
061: public DBTable(DBCatalog catalog, String name) {
062: this .name = name;
063: this .catalog = catalog;
064: this .columns = new OrderedMap<String, DBColumn>();
065: this .primaryKeyConstraint = null;
066: this .doc = null;
067: this .schema = null;
068: this .indexes = new OrderedMap<String, DBIndex>();
069: this .uniqueConstraints = new ArrayList<DBUniqueConstraint>();
070: this .foreignKeyConstraints = new ArrayList<DBForeignKeyConstraint>();
071: }
072:
073: // properties ------------------------------------------------------------------------------------------------------
074:
075: public String getName() {
076: return name;
077: }
078:
079: public void setName(String name) {
080: this .name = name;
081: }
082:
083: public String getDoc() {
084: return doc;
085: }
086:
087: public void setDoc(String doc) {
088: this .doc = doc;
089: }
090:
091: public DBCatalog getCatalog() {
092: return catalog;
093: }
094:
095: public void setCatalog(DBCatalog catalog) {
096: this .catalog = catalog;
097: }
098:
099: public DBSchema getSchema() {
100: return schema;
101: }
102:
103: public void setSchema(DBSchema schema) {
104: this .schema = schema;
105: }
106:
107: public void setPrimaryKeyConstraint(
108: DBPrimaryKeyConstraint constraint) {
109: this .primaryKeyConstraint = constraint;
110: }
111:
112: public DBPrimaryKeyConstraint getPrimaryKeyConstraint() {
113: return primaryKeyConstraint;
114: }
115:
116: // column operations -----------------------------------------------------------------------------------------------
117:
118: public List<DBColumn> getColumns() {
119: return columns.values();
120: }
121:
122: public DBColumn[] getColumns(List<String> columnNames) {
123: List<DBColumn> list = new ArrayList<DBColumn>(columnNames
124: .size());
125: for (String columnName : columnNames) {
126: DBColumn column = getColumn(columnName);
127: if (column == null)
128: throw new IllegalArgumentException("Table '" + name
129: + "' does not have a column '" + columnName
130: + "'");
131: list.add(column);
132: }
133: DBColumn[] array = new DBColumn[columnNames.size()];
134: return list.toArray(array);
135: }
136:
137: public DBColumn getColumn(String columnName) {
138: DBColumn column = columns.get(columnName.toUpperCase());
139: if (column == null)
140: throw new ObjectNotFoundException("Column '" + columnName
141: + "' not found in table '" + this .getName() + "'");
142: return column;
143: }
144:
145: public void addColumn(DBColumn column) {
146: column.setTable(this );
147: columns.put(column.getName().toUpperCase(), column);
148: }
149:
150: // index operations ------------------------------------------------------------------------------------------------
151:
152: public List<DBIndex> getIndexes() {
153: return indexes.values();
154: }
155:
156: public DBIndex getIndex(String indexName) {
157: return indexes.get(indexName);
158: }
159:
160: public void addIndex(DBIndex index) {
161: indexes.put(index.getName(), index);
162: }
163:
164: public void removeIndex(DBIndex index) {
165: indexes.remove(index.getName());
166: }
167:
168: // uniqueConstraint operations -------------------------------------------------------------------------------------
169:
170: public List<DBUniqueConstraint> getUniqueConstraints() {
171: return uniqueConstraints;
172: }
173:
174: public void addUniqueConstraint(DBUniqueConstraint constraint) {
175: uniqueConstraints.add(constraint);
176: }
177:
178: public void removeUniqueConstraint(DBUniqueConstraint constraint) {
179: uniqueConstraints.remove(constraint);
180: }
181:
182: // ForeignKeyConstraint operations ---------------------------------------------------------------------------------
183:
184: public List<DBForeignKeyConstraint> getForeignKeyConstraints() {
185: return foreignKeyConstraints;
186: }
187:
188: public void addForeignKeyConstraint(
189: DBForeignKeyConstraint constraint) {
190: foreignKeyConstraints.add(constraint);
191: }
192:
193: public void removeForeignKeyConstraint(
194: DBForeignKeyConstraint constraint) {
195: foreignKeyConstraints.remove(constraint);
196: }
197:
198: // java.lang.Object overrides --------------------------------------------------------------------------------------
199:
200: @Override
201: public boolean equals(Object o) {
202: if (this == o)
203: return true;
204: if (o == null || getClass() != o.getClass())
205: return false;
206: final DBTable that = (DBTable) o;
207: return name.equals(that.name);
208: }
209:
210: @Override
211: public int hashCode() {
212: return name.hashCode();
213: }
214:
215: @Override
216: public String toString() {
217: return name;
218: }
219:
220: public int countProviders() {
221: return foreignKeyConstraints.size();
222: }
223:
224: public DBTable getProvider(int index) {
225: return foreignKeyConstraints.get(index).getForeignTable();
226: }
227:
228: public boolean requiresProvider(int index) {
229: return !foreignKeyConstraints.get(index).getForeignKeyColumns()
230: .get(0).getForeignKeyColumn().isNullable();
231: }
232:
233: }
|