KickJava   Java API By Example, From Geeks To Geeks.

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


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.Transaction;
35 import com.caucho.sql.SQLExceptionWrapper;
36 import com.caucho.util.L10N;
37
38 import java.io.IOException JavaDoc;
39 import java.sql.SQLException JavaDoc;
40
41 /**
42  * Validity constraints.
43  */

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

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

60   public void validate(TableIterator []sourceRows,
61                QueryContext context, Transaction xa)
62     throws SQLException JavaDoc
63   {
64     try {
65       TableIterator sourceRow = sourceRows[0];
66
67       byte []sourceBuffer = sourceRow.getBuffer();
68       int sourceOffset = sourceRow.getRowOffset();
69       
70       byte []buffer = context.getBuffer();
71
72       int length = _column.getLength();
73
74       if (length <= 0)
75     return;
76
77       BTree index = _column.getIndex();
78
79       long value = index.lookup(buffer, 0, length,
80                 context.getTransaction());
81
82       if (value != 0) {
83     Table table = sourceRow.getTable();
84     
85     throw new SQLException JavaDoc(L.l("`{0}' in {1}.{2} fails uniqueness constraint.",
86                    _column.getString(sourceBuffer,
87                              sourceOffset),
88                    table.getName(),
89                    _column.getName()));
90       }
91     } catch (IOException JavaDoc e) {
92       throw new SQLExceptionWrapper(e);
93     }
94   }
95 }
96
Popular Tags