1 34 package smallsql.database; 35 36 37 40 final class Distinct extends RowSource { 41 42 final private Expressions columns; 43 final private RowSource rowSource; 44 private Index index; 45 private int row; 46 47 Distinct(RowSource rowSource, Expressions columns){ 48 this.rowSource = rowSource; 49 this.columns = columns; 50 } 51 52 53 final void execute() throws Exception { 54 rowSource.execute(); 55 index = new Index(true); 56 } 57 58 59 final boolean isScrollable() { 60 return false; 61 } 62 63 64 final void beforeFirst() throws Exception { 65 rowSource.beforeFirst(); 66 row = 0; 67 } 68 69 70 final boolean first() throws Exception { 71 beforeFirst(); 72 return next(); 73 } 74 75 76 final boolean next() throws Exception { 77 while(true){ 78 boolean isNext = rowSource.next(); 79 if(!isNext) return false; 80 81 Long oldRowOffset = (Long )index.findRows(columns, null); 82 long newRowOffset = rowSource.getRowPosition(); 83 if(oldRowOffset == null){ 84 index.addValues( newRowOffset, columns); 85 row++; 86 return true; 87 }else 88 if(oldRowOffset.longValue() == newRowOffset){ 89 row++; 90 return true; 91 } 92 } 93 } 94 95 96 final void afterLast() throws Exception { 97 rowSource.afterLast(); 98 row = 0; 99 } 100 101 102 final int getRow() throws Exception { 103 return row; 104 } 105 106 107 final long getRowPosition() { 108 return rowSource.getRowPosition(); 109 } 110 111 112 final void setRowPosition(long rowPosition) throws Exception { 113 rowSource.setRowPosition(rowPosition); 114 } 115 116 117 final void nullRow() { 118 rowSource.nullRow(); 119 row = 0; 120 } 121 122 123 final void noRow() { 124 rowSource.noRow(); 125 row = 0; 126 } 127 128 129 final boolean rowInserted(){ 130 return rowSource.rowInserted(); 131 } 132 133 134 final boolean rowDeleted() { 135 return rowSource.rowDeleted(); 136 } 137 } 138 | Popular Tags |