01: /*
02: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
03: *
04: * This file is part of Resin(R) Open Source
05: *
06: * Each copy or derived work must preserve the copyright notice and this
07: * notice unmodified.
08: *
09: * 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;
39: import java.sql.SQLException;
40:
41: /**
42: * Validity constraints.
43: */
44: public class UniqueIndexColumnConstraint extends Constraint {
45: private final static L10N L = new L10N(
46: UniqueIndexColumnConstraint.class);
47:
48: private final Column _column;
49:
50: /**
51: * Creates a uniqueness constraint.
52: */
53: public UniqueIndexColumnConstraint(Column column) {
54: _column = column;
55: }
56:
57: /**
58: * validate the constraint.
59: */
60: public void validate(TableIterator[] sourceRows,
61: QueryContext context, Transaction xa) throws SQLException {
62: try {
63: TableIterator sourceRow = sourceRows[0];
64:
65: byte[] sourceBuffer = sourceRow.getBuffer();
66: int sourceOffset = sourceRow.getRowOffset();
67:
68: byte[] buffer = context.getBuffer();
69:
70: int length = _column.getLength();
71:
72: if (length <= 0)
73: return;
74:
75: BTree index = _column.getIndex();
76:
77: long value = index.lookup(buffer, 0, length, context
78: .getTransaction());
79:
80: if (value != 0) {
81: Table table = sourceRow.getTable();
82:
83: throw new SQLException(
84: L
85: .l(
86: "`{0}' in {1}.{2} fails uniqueness constraint.",
87: _column.getString(sourceBuffer,
88: sourceOffset), table
89: .getName(), _column
90: .getName()));
91: }
92: } catch (IOException e) {
93: throw new SQLExceptionWrapper(e);
94: }
95: }
96: }
|