001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.db.table;
030:
031: import com.caucho.db.sql.QueryContext;
032: import com.caucho.db.store.Transaction;
033: import com.caucho.sql.SQLExceptionWrapper;
034: import com.caucho.util.L10N;
035:
036: import java.io.IOException;
037: import java.sql.SQLException;
038:
039: /**
040: * Validity constraints.
041: */
042: public class UniqueConstraint extends Constraint {
043: private final static L10N L = new L10N(UniqueConstraint.class);
044: private final Column[] _uniqueSet;
045:
046: /**
047: * Creates a uniqueness constraint.
048: */
049: public UniqueConstraint(Column[] uniqueSet) {
050: _uniqueSet = uniqueSet;
051: }
052:
053: /**
054: * validate the constraint.
055: */
056: public void validate(TableIterator[] sourceRows,
057: QueryContext queryContext, Transaction xa)
058: throws SQLException {
059: TableIterator sourceRow = sourceRows[0];
060: String value = null;
061:
062: Table table = sourceRow.getTable();
063: TableIterator iter = table.createTableIterator();
064:
065: byte[] sourceBuffer = sourceRow.getBuffer();
066: int sourceOffset = sourceRow.getRowOffset();
067:
068: iter.init(queryContext);
069:
070: try {
071: while (iter.next()) {
072: byte[] iterBuffer = iter.getBuffer();
073:
074: iter.prevRow();
075:
076: while (iter.nextRow()) {
077: int iterOffset = iter.getRowOffset();
078:
079: if (iterBuffer == sourceBuffer
080: && iterOffset == sourceOffset)
081: continue;
082:
083: boolean isMatch = true;
084:
085: for (int i = 0; i < _uniqueSet.length; i++) {
086: Column column = _uniqueSet[i];
087:
088: int columnOffset = column.getColumnOffset();
089:
090: if (!column.isEqual(iterBuffer, iterOffset,
091: sourceBuffer, sourceOffset)) {
092: isMatch = false;
093: break;
094: }
095: }
096:
097: if (isMatch)
098: throw new SQLException(
099: L
100: .l(
101: "`{0}' in {1}.{2} fails uniqueness constraint.",
102: _uniqueSet[0]
103: .getString(
104: iterBuffer,
105: iterOffset),
106: table.getName(),
107: _uniqueSet[0].getName()));
108: }
109: }
110: } catch (IOException e) {
111: throw new SQLExceptionWrapper(e);
112: }
113: }
114: }
|