001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.jdbc.metadata;
012:
013: import com.versant.core.jdbc.sql.JdbcNameGenerator;
014:
015: import java.io.Serializable;
016: import java.util.ArrayList;
017: import java.util.Collections;
018: import java.util.List;
019: import java.util.Arrays;
020:
021: /**
022: * A database foreign key constraint on a table.
023: *
024: * @keep-public
025: */
026: public final class JdbcConstraint implements Serializable {
027:
028: public String name;
029: /**
030: * The table containing the foreign key column.
031: */
032: public JdbcTable src;
033: /**
034: * The column(s) in src making up the foreign key. These must match
035: * the pk of dest (same order etc.).
036: */
037: public JdbcColumn[] srcCols;
038: /**
039: * The table the foreign key references.
040: */
041: public JdbcTable dest;
042:
043: public JdbcConstraint() {
044: }
045:
046: public String toString() {
047: StringBuffer s = new StringBuffer();
048: s.append(name);
049: s.append('(');
050: s.append(JdbcColumn.toNameString(srcCols));
051: s.append(") references ");
052: s.append(dest.name);
053: s.append('(');
054: s.append(JdbcColumn.toNameString(dest.pk));
055: s.append(')');
056: return s.toString();
057: }
058:
059: /**
060: * Generate a name for this constraint using namegen.
061: */
062: public void generateName(JdbcNameGenerator namegen) {
063: name = namegen.generateRefConstraintName(src.name, dest.name,
064: JdbcColumn.getColumnNames(srcCols), dest.getPkNames());
065: }
066:
067: public List getColumnList() {
068: if (srcCols == null) {
069: return Collections.EMPTY_LIST;
070: }
071: ArrayList list = new ArrayList(srcCols.length);
072: for (int i = 0; i < srcCols.length; i++) {
073: JdbcColumn col = srcCols[i];
074: list.add(col);
075: }
076: return list;
077: }
078:
079: public boolean isSameConstraint(JdbcConstraint constraint) {
080: return false;
081: }
082:
083: public boolean equals(Object o) {
084: if (this == o)
085: return true;
086: if (!(o instanceof JdbcConstraint))
087: return false;
088:
089: final JdbcConstraint jdbcConstraint = (JdbcConstraint) o;
090:
091: if (dest != null ? !dest.equals(jdbcConstraint.dest)
092: : jdbcConstraint.dest != null)
093: return false;
094: if (src != null ? !src.equals(jdbcConstraint.src)
095: : jdbcConstraint.src != null)
096: return false;
097: if (!Arrays.equals(srcCols, jdbcConstraint.srcCols))
098: return false;
099:
100: return true;
101: }
102:
103: public int hashCode() {
104: int result;
105: result = (src != null ? src.hashCode() : 0);
106: if (srcCols != null) {
107: for (int i = 0; i < srcCols.length; i++) {
108: JdbcColumn srcCol = srcCols[i];
109: result = 29 * result
110: + (srcCol != null ? srcCol.hashCode() : 0);
111: }
112: }
113: result = 29 * result + (dest != null ? dest.hashCode() : 0);
114: return result;
115: }
116: }
|