1 34 package smallsql.database; 35 36 37 class Where extends RowSource { 38 39 final private RowSource rowSource; 40 final private Expression where; 41 private int row = 0; 42 private boolean isCurrentRow; 43 44 Where(RowSource rowSource, Expression where){ 45 this.rowSource = rowSource; 46 this.where = where; 47 } 48 49 RowSource getFrom(){ 50 return rowSource; 51 } 52 53 58 final private boolean isValidRow() throws Exception { 59 return where == null || rowSource.rowInserted() || where.getBoolean(); 60 } 61 62 63 final boolean isScrollable() { 64 return rowSource.isScrollable(); 65 } 66 67 68 final boolean isBeforeFirst(){ 69 return row == 0; 70 } 71 72 73 final boolean isFirst(){ 74 return row == 1 && isCurrentRow; 75 } 76 77 78 final boolean isLast() throws Exception { 79 if(!isCurrentRow) return false; 80 long rowPos = rowSource.getRowPosition(); 81 boolean isNext = next(); 82 rowSource.setRowPosition(rowPos); 83 return !isNext; 84 } 85 86 87 final boolean isAfterLast(){ 88 return row > 0 && !isCurrentRow; 89 } 90 91 92 final void beforeFirst() throws Exception { 93 rowSource.beforeFirst(); 94 row = 0; 95 } 96 97 98 final boolean first() throws Exception { 99 isCurrentRow = rowSource.first(); 100 while(isCurrentRow && !isValidRow()){ 101 isCurrentRow = rowSource.next(); 102 } 103 row = 1; 104 return isCurrentRow; 105 } 106 107 108 final boolean previous() throws Exception { 109 boolean oldIsCurrentRow = isCurrentRow; 110 do{ 111 isCurrentRow = rowSource.previous(); 112 }while(isCurrentRow && !isValidRow()); 113 if(oldIsCurrentRow || isCurrentRow) row--; 114 return isCurrentRow; 115 } 116 117 118 final boolean next() throws Exception { 119 boolean oldIsCurrentRow = isCurrentRow; 120 do{ 121 isCurrentRow = rowSource.next(); 122 }while(isCurrentRow && !isValidRow()); 123 if(oldIsCurrentRow || isCurrentRow) row++; 124 return isCurrentRow; 125 } 126 127 128 final boolean last() throws Exception { 129 while(next()){} 130 return previous(); 131 } 132 133 134 final void afterLast() throws Exception { 135 while(next()){} 136 } 137 138 139 final int getRow() throws Exception { 140 return isCurrentRow ? row : 0; 141 } 142 143 144 final long getRowPosition() { 145 return rowSource.getRowPosition(); 146 } 147 148 149 final void setRowPosition(long rowPosition) throws Exception { 150 rowSource.setRowPosition(rowPosition); 151 } 152 153 154 final void nullRow() { 155 rowSource.nullRow(); 156 row = 0; 157 } 158 159 160 final void noRow() { 161 rowSource.noRow(); 162 row = 0; 163 } 164 165 166 final boolean rowInserted() { 167 return rowSource.rowInserted(); 168 } 169 170 171 final boolean rowDeleted() { 172 return rowSource.rowDeleted(); 173 } 174 175 176 final void execute() throws Exception { 177 rowSource.execute(); 178 } 179 180 } 181 | Popular Tags |