1 28 29 package com.caucho.db.table; 30 31 import com.caucho.db.sql.QueryContext; 32 import com.caucho.db.store.Transaction; 33 import com.caucho.sql.SQLExceptionWrapper; 34 import com.caucho.util.L10N; 35 36 import java.io.IOException ; 37 import java.sql.SQLException ; 38 39 42 public class UniqueConstraint extends Constraint { 43 private final static L10N L = new L10N(UniqueConstraint.class); 44 private final Column []_uniqueSet; 45 46 49 public UniqueConstraint(Column []uniqueSet) 50 { 51 _uniqueSet = uniqueSet; 52 } 53 54 57 public void validate(TableIterator []sourceRows, 58 QueryContext queryContext, Transaction xa) 59 throws SQLException 60 { 61 TableIterator sourceRow = sourceRows[0]; 62 String value = null; 63 64 Table table = sourceRow.getTable(); 65 TableIterator iter = table.createTableIterator(); 66 67 byte []sourceBuffer = sourceRow.getBuffer(); 68 int sourceOffset = sourceRow.getRowOffset(); 69 70 iter.init(queryContext); 71 72 try { 73 while (iter.next()) { 74 byte []iterBuffer = iter.getBuffer(); 75 76 iter.prevRow(); 77 78 while (iter.nextRow()) { 79 int iterOffset = iter.getRowOffset(); 80 81 if (iterBuffer == sourceBuffer && iterOffset == sourceOffset) 82 continue; 83 84 boolean isMatch = true; 85 86 for (int i = 0; i < _uniqueSet.length; i++) { 87 Column column = _uniqueSet[i]; 88 89 int columnOffset = column.getColumnOffset(); 90 91 if (! column.isEqual(iterBuffer, iterOffset, 92 sourceBuffer, sourceOffset)) { 93 isMatch = false; 94 break; 95 } 96 } 97 98 if (isMatch) 99 throw new SQLException (L.l("`{0}' in {1}.{2} fails uniqueness constraint.", 100 _uniqueSet[0].getString(iterBuffer, iterOffset), 101 table.getName(), 102 _uniqueSet[0].getName())); 103 } 104 } 105 } catch (IOException e) { 106 throw new SQLExceptionWrapper(e); 107 } 108 } 109 } 110 | Popular Tags |