KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > metadata > JdbcConstraint


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.metadata;
13
14 import com.versant.core.jdbc.sql.JdbcNameGenerator;
15
16 import java.io.Serializable JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Arrays JavaDoc;
21
22 /**
23  * A database foreign key constraint on a table.
24  *
25  * @keep-public
26  */

27 public final class JdbcConstraint implements Serializable JavaDoc {
28
29     public String JavaDoc name;
30     /**
31      * The table containing the foreign key column.
32      */

33     public JdbcTable src;
34     /**
35      * The column(s) in src making up the foreign key. These must match
36      * the pk of dest (same order etc.).
37      */

38     public JdbcColumn[] srcCols;
39     /**
40      * The table the foreign key references.
41      */

42     public JdbcTable dest;
43
44     public JdbcConstraint() {
45     }
46
47     public String JavaDoc toString() {
48         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
49         s.append(name);
50         s.append('(');
51         s.append(JdbcColumn.toNameString(srcCols));
52         s.append(") references ");
53         s.append(dest.name);
54         s.append('(');
55         s.append(JdbcColumn.toNameString(dest.pk));
56         s.append(')');
57         return s.toString();
58     }
59
60     /**
61      * Generate a name for this constraint using namegen.
62      */

63     public void generateName(JdbcNameGenerator namegen) {
64         name = namegen.generateRefConstraintName(src.name, dest.name, JdbcColumn.getColumnNames(
65                 srcCols),
66                 dest.getPkNames());
67     }
68
69     public List JavaDoc getColumnList() {
70         if (srcCols == null) {
71             return Collections.EMPTY_LIST;
72         }
73         ArrayList JavaDoc list = new ArrayList JavaDoc(srcCols.length);
74         for (int i = 0; i < srcCols.length; i++) {
75             JdbcColumn col = srcCols[i];
76             list.add(col);
77         }
78         return list;
79     }
80
81     public boolean isSameConstraint(JdbcConstraint constraint) {
82         return false;
83     }
84
85     public boolean equals(Object JavaDoc o) {
86         if (this == o) return true;
87         if (!(o instanceof JdbcConstraint)) return false;
88
89         final JdbcConstraint jdbcConstraint = (JdbcConstraint)o;
90
91         if (dest != null ? !dest.equals(jdbcConstraint.dest) : jdbcConstraint.dest != null) return false;
92         if (src != null ? !src.equals(jdbcConstraint.src) : jdbcConstraint.src != null) return false;
93         if (!Arrays.equals(srcCols, jdbcConstraint.srcCols)) return false;
94
95         return true;
96     }
97
98     public int hashCode() {
99         int result;
100         result = (src != null ? src.hashCode() : 0);
101         if (srcCols != null) {
102             for (int i = 0; i < srcCols.length; i++) {
103                 JdbcColumn srcCol = srcCols[i];
104                 result = 29 * result + (srcCol != null ? srcCol.hashCode() : 0);
105             }
106         }
107         result = 29 * result + (dest != null ? dest.hashCode() : 0);
108         return result;
109     }
110 }
111
112
113
Popular Tags