KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > table > UniqueConstraint


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.db.table;
30
31 import com.caucho.db.sql.QueryContext;
32 import com.caucho.db.store.Transaction;
33 import com.caucho.sql.SQLExceptionWrapper;
34 import com.caucho.util.L10N;
35
36 import java.io.IOException JavaDoc;
37 import java.sql.SQLException JavaDoc;
38
39 /**
40  * Validity constraints.
41  */

42 public class UniqueConstraint extends Constraint {
43   private final static L10N L = new L10N(UniqueConstraint.class);
44   private final Column []_uniqueSet;
45
46   /**
47    * Creates a uniqueness constraint.
48    */

49   public UniqueConstraint(Column []uniqueSet)
50   {
51     _uniqueSet = uniqueSet;
52   }
53
54   /**
55    * validate the constraint.
56    */

57   public void validate(TableIterator []sourceRows,
58                QueryContext queryContext, Transaction xa)
59     throws SQLException JavaDoc
60   {
61     TableIterator sourceRow = sourceRows[0];
62     String JavaDoc value = null;
63
64     Table table = sourceRow.getTable();
65     TableIterator iter = table.createTableIterator();
66
67     byte []sourceBuffer = sourceRow.getBuffer();
68     int sourceOffset = sourceRow.getRowOffset();
69
70     iter.init(queryContext);
71
72     try {
73       while (iter.next()) {
74     byte []iterBuffer = iter.getBuffer();
75
76     iter.prevRow();
77     
78     while (iter.nextRow()) {
79       int iterOffset = iter.getRowOffset();
80
81       if (iterBuffer == sourceBuffer && iterOffset == sourceOffset)
82         continue;
83           
84       boolean isMatch = true;
85
86       for (int i = 0; i < _uniqueSet.length; i++) {
87         Column column = _uniqueSet[i];
88
89         int columnOffset = column.getColumnOffset();
90
91         if (! column.isEqual(iterBuffer, iterOffset,
92                  sourceBuffer, sourceOffset)) {
93           isMatch = false;
94           break;
95         }
96       }
97
98       if (isMatch)
99         throw new SQLException JavaDoc(L.l("`{0}' in {1}.{2} fails uniqueness constraint.",
100                        _uniqueSet[0].getString(iterBuffer, iterOffset),
101                        table.getName(),
102                        _uniqueSet[0].getName()));
103     }
104       }
105     } catch (IOException JavaDoc e) {
106       throw new SQLExceptionWrapper(e);
107     }
108   }
109 }
110
Popular Tags