001: /*
002: * This is free software, licensed under the Gnu Public License (GPL)
003: * get a copy from <http://www.gnu.org/licenses/gpl.html>
004: * @version $Id: Table.java,v 1.5 2004/09/22 11:49:32 magrokosmos Exp $
005: * @author <a href="mailto:martin.grotzke@javakaffee.de">Martin Grotzke</a>
006: */
007: package henplus.sqlmodel;
008:
009: import henplus.util.ListMap;
010:
011: import java.util.HashSet;
012: import java.util.Iterator;
013: import java.util.ListIterator;
014: import java.util.Set;
015:
016: public final class Table implements Comparable {
017:
018: private String _name;
019: private ListMap /*<String, Column>*/_columns;
020:
021: // private PrimaryKey _pk;
022:
023: // FIXME: add notion of schema.
024:
025: public Table(String name) {
026: _name = name;
027: _columns = new ListMap();
028: }
029:
030: public String getName() {
031: return _name;
032: }
033:
034: public void setName(String string) {
035: _name = string;
036: }
037:
038: public void addColumn(Column column) {
039: _columns.put(column.getName(), column);
040: }
041:
042: public ListIterator getColumnIterator() {
043: ListIterator result = null;
044: if (_columns != null) {
045: result = _columns.valuesListIterator();
046: }
047: return result;
048: }
049:
050: public Column getColumnByName(String name, boolean ignoreCase) {
051: Column result = null;
052: if (_columns != null) {
053: result = (Column) _columns.get(name);
054: if (result == null && ignoreCase) {
055: final Iterator iter = _columns.keysListIterator();
056: while (iter.hasNext()) {
057: String colName = (String) iter.next();
058: if (colName.equalsIgnoreCase(name)) {
059: result = (Column) _columns.get(colName);
060: break;
061: }
062: }
063: }
064: }
065: return result;
066: }
067:
068: /**
069: * @return <code>true</code>, if this <code>Table</code> has any foreign key, otherwise <code>false</code>.
070: */
071: public boolean hasForeignKeys() {
072: return getForeignKeys() != null;
073: }
074:
075: /**
076: * @return A <code>Set</code> of <code>ColumnFkInfo</code> objects or <code>null</code>.
077: */
078: public Set/*<ColumnFkInfo>*/getForeignKeys() {
079: Set result = null;
080:
081: if (_columns != null) {
082: final Iterator iter = _columns.values().iterator();
083: while (iter.hasNext()) {
084: Column c = (Column) iter.next();
085: if (c.getFkInfo() != null) {
086: if (result == null)
087: result = new HashSet();
088: result.add(c.getFkInfo());
089: }
090: }
091: }
092:
093: return result;
094: }
095:
096: /* (non-Javadoc)
097: * @see java.lang.Object#toString()
098: */
099: public String toString() {
100: return _name;
101: }
102:
103: public int hashCode() {
104: return _name.hashCode();
105: }
106:
107: /* (non-Javadoc)
108: * @see java.lang.Object#equals(java.lang.Object)
109: */
110: public boolean equals(Object other) {
111: boolean result = false;
112:
113: if (other == this )
114: result = true;
115:
116: else if (other instanceof Table) {
117: Table o = (Table) other;
118:
119: if (_name != null && _name.equals(o.getName()))
120: result = true;
121:
122: else if (_name == null && o.getName() == null)
123: result = true;
124: }
125:
126: return result;
127: }
128:
129: /* (non-Javadoc)
130: * @see java.lang.Comparable#compareTo(java.lang.Object)
131: */
132: public int compareTo(Object other) {
133: int result = 0;
134: if (other instanceof Table) {
135: Table o = (Table) other;
136: result = _name.compareTo(o.getName());
137: }
138: return result;
139: }
140:
141: /*
142: public boolean columnIsPartOfPk(String column) {
143: boolean result = false;
144: if (_pk != null) {
145: result = _pk.columnParticipates(column);
146: }
147: return result;
148: }
149: */
150:
151: /**
152: * @return
153: */
154: /*
155: public PrimaryKey getPk() {
156: return _pk;
157: }
158: */
159:
160: /**
161: * @param key
162: */
163: /*
164: public void setPk(PrimaryKey key) {
165: _pk = key;
166: }
167: */
168:
169: }
|