1 33 package smallsql.database; 34 35 import java.sql.*; 36 import java.util.ArrayList ; 37 import java.util.List ; 38 39 40 44 class MemoryResult extends DataSource { 45 46 ExpressionValue[] currentRow; 47 private final Columns columns = new Columns(); 48 private int rowIdx = -1; 49 private List rowList = new ArrayList (); 51 54 MemoryResult(){} 55 56 57 60 MemoryResult(Object [][] data, int colCount) throws SQLException{ 61 for(int c=0; c<colCount; c++){ 62 Column column = new Column(); 63 column.setDataType(SQLTokenizer.NULL); 64 columns.add( column ); 65 } 66 for(int r=0; r<data.length; r++){ 67 Object [] row = data[r]; 68 ExpressionValue[] rowValues = new ExpressionValue[row.length]; 69 addRow(rowValues); 70 for(int c=0; c<colCount; c++){ 71 ExpressionValue expr = rowValues[c] = new ExpressionValue(); 72 expr.set( row[c], -1); 73 Column column = columns.get(c); 74 if(expr.getDataType() != SQLTokenizer.NULL){ 75 column.setDataType(expr.getDataType()); 76 } 77 if(expr.getPrecision() > column.getPrecision()){ 78 column.setPrecision(expr.getPrecision()); 79 } 80 } 81 } 82 } 83 84 final void addRow(ExpressionValue[] row){ 85 rowList.add(row); 86 } 87 88 89 93 final Column getColumn(int colIdx){ 94 return columns.get(colIdx); 95 } 96 97 98 102 final void addColumn(Column column){ 103 columns.add(column); 104 } 105 106 111 112 113 final boolean isScrollable(){ 114 return true; 115 } 116 117 118 final void beforeFirst(){ 119 rowIdx = -1; 120 currentRow = null; 121 } 122 123 final boolean isBeforeFirst(){ 124 return rowIdx < 0; 125 } 126 127 final boolean isFirst(){ 128 return rowIdx == 0 && currentRow != null; 129 } 130 131 final boolean first(){ 132 rowIdx = 0; 133 return move(); 134 } 135 136 final boolean previous(){ 137 if(rowIdx-- < 0) rowIdx = -1; 138 return move(); 139 } 140 141 final boolean next(){ 142 rowIdx++; 143 return move(); 144 } 145 146 final boolean last(){ 147 rowIdx = rowList.size() - 1; 148 return move(); 149 } 150 151 152 final boolean isLast(){ 153 return rowIdx == rowList.size() - 1 && currentRow != null; 154 } 155 156 final boolean isAfterLast(){ 157 return rowIdx >= rowList.size(); 158 } 159 160 final void afterLast(){ 161 rowIdx = rowList.size(); 162 currentRow = null; 163 } 164 165 166 final boolean absolute(int row) throws SQLException{ 167 if(row == 0) throw Utils.createSQLException("Row 0 is invalid for method absolute()."); 168 rowIdx = (row > 0) ? 169 Math.min( row, rowList.size() ) : 170 Math.min( row +rowList.size(), -1 ); 171 return move(); 172 } 173 174 175 final boolean relative(int rows){ 176 if(rows == 0) return (currentRow != null); 177 rowIdx = Math.min( Math.max( rowIdx + rows, -1), rowList.size()); 178 return move(); 179 } 180 181 182 final int getRow(){ 183 return currentRow == null ? 0 : rowIdx+1; 184 } 185 186 187 final long getRowPosition(){ 188 return rowIdx; 189 } 190 191 192 final void setRowPosition(long rowPosition) throws Exception { 193 rowIdx = (int)rowPosition; 194 move(); 195 } 196 197 198 final boolean rowInserted(){ 199 return false; 200 } 201 202 203 final boolean rowDeleted(){ 204 return false; 205 } 206 207 208 void nullRow(){ 209 throw new Error (); 210 } 211 212 213 void noRow(){ 214 currentRow = null; 215 } 216 217 218 final private boolean move(){ 219 if(rowIdx < rowList.size() && rowIdx >= 0){ 220 currentRow = (ExpressionValue[])rowList.get(rowIdx); 221 return true; 222 } 223 currentRow = null; 224 return false; 225 } 226 227 232 233 boolean isNull( int colIdx ) throws Exception { 234 return get( colIdx ).isNull(); 235 } 236 237 238 boolean getBoolean( int colIdx ) throws Exception { 239 return get( colIdx ).getBoolean(); 240 } 241 242 int getInt( int colIdx ) throws Exception { 243 return get( colIdx ).getInt(); 244 } 245 246 long getLong( int colIdx ) throws Exception { 247 return get( colIdx ).getLong(); 248 } 249 250 float getFloat( int colIdx ) throws Exception { 251 return get( colIdx ).getFloat(); 252 } 253 254 double getDouble( int colIdx ) throws Exception { 255 return get( colIdx ).getDouble(); 256 } 257 258 long getMoney( int colIdx ) throws Exception { 259 return get( colIdx ).getMoney(); 260 } 261 262 MutableNumeric getNumeric( int colIdx ) throws Exception { 263 return get( colIdx ).getNumeric(); 264 } 265 266 Object getObject( int colIdx ) throws Exception { 267 return get( colIdx ).getObject(); 268 } 269 270 String getString( int colIdx ) throws Exception { 271 return get( colIdx ).getString(); 272 } 273 274 275 byte[] getBytes( int colIdx ) throws Exception { 276 return get( colIdx ).getBytes(); 277 } 278 279 280 int getDataType( int colIdx ){ 281 return columns.get( colIdx ).getDataType(); 282 } 284 285 286 final TableView getTableView(){ 287 return null; 288 } 289 290 291 final void deleteRow() throws Exception { 292 throw Utils.createSQLException("ResultSet is read only."); 293 } 294 295 296 final void updateRow(Expression[] updateValues) throws Exception { 297 throw Utils.createSQLException("ResultSet is read only."); 298 } 299 300 301 final void insertRow(Expression[] updateValues) throws Exception { 302 throw Utils.createSQLException("ResultSet is read only."); 303 } 304 305 306 307 308 311 312 317 private Expression get(int colIdx) throws Exception { 318 if(currentRow == null) throw Utils.createSQLException("No current row."); 319 return currentRow[ colIdx ]; 320 } 321 322 323 326 int getRowCount(){ 327 return rowList.size(); 328 } 329 330 331 void execute() throws Exception { 332 rowList.clear(); 333 } 334 } 335 | Popular Tags |