1 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 44 public class UniqueIndexColumnConstraint extends Constraint { 45 private final static L10N L = new L10N(UniqueIndexColumnConstraint.class); 46 47 private final Column _column; 48 49 52 public UniqueIndexColumnConstraint(Column column) 53 { 54 _column = column; 55 } 56 57 60 public void validate(TableIterator []sourceRows, 61 QueryContext context, Transaction xa) 62 throws SQLException 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 (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 e) { 92 throw new SQLExceptionWrapper(e); 93 } 94 } 95 } 96 | Popular Tags |