KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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

45 public class UniqueSingleColumnConstraint extends Constraint {
46   private final static L10N L = new L10N(UniqueSingleColumnConstraint.class);
47   private final Column _uniqueColumn;
48
49   /**
50    * Creates a uniqueness constraint.
51    */

52   public UniqueSingleColumnConstraint(Column column)
53   {
54     _uniqueColumn = column;
55   }
56
57   /**
58    * validate the constraint.
59    */

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

115   private void validateIndex(TableIterator []sourceRows,
116                  QueryContext context, Transaction xa)
117     throws SQLException JavaDoc
118   {
119     try {
120       Column column = _uniqueColumn;
121       TableIterator sourceRow = sourceRows[0];
122
123       byte []sourceBuffer = sourceRow.getBuffer();
124       int sourceOffset = sourceRow.getRowOffset();
125       
126       byte []buffer = context.getBuffer();
127
128       BTree index = column.getIndex();
129
130       /*
131       int length = column.evalToBuffer(sourceBuffer, sourceOffset,
132                        buffer, 0);
133
134       if (length <= 0)
135     return;
136
137       long value = index.lookup(buffer, 0, length,
138                 context.getTransaction());
139                 
140       */

141
142       // currently this is a static length. See StringColumn.
143
int length = column.getLength();
144       int offset = sourceOffset + _uniqueColumn.getColumnOffset();
145       long value = index.lookup(sourceBuffer, offset, length,
146                 context.getTransaction());
147
148       if (value != 0) {
149     Table table = sourceRow.getTable();
150
151     throw new SQLException JavaDoc(L.l("'{0}' in {1}.{2} fails uniqueness constraint with block address {3}.",
152                    column.getString(sourceBuffer,
153                             sourceOffset),
154                    table.getName(),
155                    column.getName(),
156                    ("" + (value / Store.BLOCK_SIZE)
157                     + "." + (value % Store.BLOCK_SIZE))));
158       }
159     } catch (IOException JavaDoc e) {
160       throw new SQLExceptionWrapper(e);
161     }
162   }
163 }
164
Popular Tags