View Javadoc

1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2005, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  package org.dbunit.database.search;
22  
23  import org.dbunit.util.search.Edge;
24  
25  /**
26   * Implementation of an edge representing a foreign key (FK)  relationship between two
27   * tables.<br>
28   * The <code>from</code> node is the table which have the FK, while the 
29   * <code>to</code> node is the table with the PK. In other words, the edge
30   * A->B means FK(A) = PK(B).<br>
31   * 
32   * <strong>NOTE:</strong> only single-column PKs are supported at this moment
33   *  
34   * @author Felipe Leme (dbunit@felipeal.net)
35   * @author Last changed by: $Author: gommma $
36   * @version $Revision: 826 $ $Date: 2008-10-05 19:42:03 +0200 (dom, 05 ott 2008) $
37   * @since 2.2 (Sep 9, 2005)
38   */
39  public class ForeignKeyRelationshipEdge extends Edge {
40  
41      private String fkColumn;
42      private String pkColumn;
43  
44      /**
45       * Creates an edge representing a FK.
46       * @param tableFrom table that has the FK
47       * @param tableTo table that has the PK
48       * @param fkColumn name of the FK column on tableFrom
49       * @param pkColumn name of the PK column on tableTo
50       */
51  
52      public ForeignKeyRelationshipEdge(String tableFrom, String tableTo, String fkColumn, String pkColumn) {
53          super(tableFrom, tableTo);
54          this.fkColumn = fkColumn;
55          this.pkColumn = pkColumn;
56      }
57  
58      /**
59       * Gets the name of the foreign key column in the relationship.
60       * @return name of the foreign key column in the relationship.
61       */
62      public String getFKColumn() {
63          return fkColumn;
64      }
65  
66      /**
67       * Gets the name of the primary key column in the relationship.
68       * @return name of the primary key column in the relationship.
69       */
70      public String getPKColumn() {
71          return pkColumn;
72      }
73      
74      public String toString() {
75          return getFrom() + "(" + getFKColumn() + ")->" + getTo() + "(" + getPKColumn() + ")";
76      }
77  
78      public int hashCode() {
79          final int prime = 31;
80          int result = super.hashCode();
81          result = prime * result
82                  + ((fkColumn == null) ? 0 : fkColumn.hashCode());
83          result = prime * result
84                  + ((pkColumn == null) ? 0 : pkColumn.hashCode());
85          return result;
86      }
87  
88      public boolean equals(Object obj) {
89          if (this == obj)
90              return true;
91          if (!super.equals(obj))
92              return false;
93          if (getClass() != obj.getClass())
94              return false;
95          ForeignKeyRelationshipEdge other = (ForeignKeyRelationshipEdge) obj;
96          if (fkColumn == null) {
97              if (other.fkColumn != null)
98                  return false;
99          } else if (!fkColumn.equals(other.fkColumn))
100             return false;
101         if (pkColumn == null) {
102             if (other.pkColumn != null)
103                 return false;
104         } else if (!pkColumn.equals(other.pkColumn))
105             return false;
106         return true;
107     }
108 
109 }