1 26 27 package com.sqlmagic.tinysql; 28 29 import java.util.*; 30 import java.lang.*; 31 import java.io.*; 32 import java.sql.Types ; 33 34 37 public class textFileTable extends tinySQLTable { 38 39 public String dataDir; 42 43 RandomAccessFile ftbl; 46 47 int COLUMN_SIZE = 0; 50 int COLUMN_TYPE = 1; 51 int COLUMN_POS = 2; 52 53 long record_number = 0; long record_length; 56 65 textFileTable( String dDir, String table_name ) throws tinySQLException { 66 67 dataDir = dDir; table = table_name; 70 try { 73 ftbl = new RandomAccessFile(dataDir + "/" + table_name, "rw"); 74 } catch (Exception e) { 75 throw new tinySQLException("Could not open the file " + table + "."); 76 } 77 78 readColumnInfo(); 81 82 } 83 public int GetRowCount() { 84 return 0; 86 } 87 91 public int getRecordLength() { 92 return 0; 93 } 94 95 96 102 public void close() throws tinySQLException { 103 104 try { 105 ftbl.close(); 106 } catch (IOException e) { 107 throw new tinySQLException(e.getMessage()); 108 } 109 } 110 115 public boolean isOpen() throws tinySQLException { 116 117 return true; 118 } 119 120 128 public int ColSize(String column) { 129 130 String info[] = (String []) column_info.get(column); 133 134 return Integer.parseInt( info[COLUMN_SIZE] ); 137 138 } 139 public int ColDec(String column) { 140 return 0; 143 } 144 145 146 158 public int ColType(String column) { 159 160 String info[] = (String []) column_info.get(column); 163 164 return Integer.parseInt(info[COLUMN_TYPE]); 167 168 } 169 170 179 public void UpdateCurrentRow(Vector c, Vector v) throws tinySQLException { 180 181 for (int i = 0; i < v.size(); i++) { 188 189 String column = (String ) c.elementAt(i); 193 String value = (String ) v.elementAt(i); 194 UpdateCol(column, value); 195 } 196 197 } 198 199 206 public void GoTop() throws tinySQLException { 207 208 try { 209 ftbl.seek(0); 210 record_number = 0; 211 } catch (IOException e) { 212 throw new tinySQLException(e.getMessage()); 213 } 214 215 } 216 217 224 public boolean NextRecord() throws tinySQLException { 225 226 if (record_number > 0) { 231 232 try { 236 ftbl.seek( ftbl.getFilePointer() + record_length + 1); 237 } catch (IOException e) { 238 return false; 239 } 240 241 } 242 243 record_number++; 246 247 try { 250 if (ftbl.getFilePointer() == ftbl.length()) { 251 return false; 252 } 253 } catch (Exception e) { 254 throw new tinySQLException(e.getMessage()); 255 } 256 257 return true; 258 259 } 260 261 270 public void InsertRow(Vector c, Vector v) throws tinySQLException { 271 272 try { 273 274 ftbl.seek( ftbl.length() ); 277 278 ftbl.write('N'); 281 282 for (int i = 1; i < record_length; i++) { 285 ftbl.write(' '); 286 } 287 ftbl.write('\n'); 288 289 ftbl.seek( ftbl.getFilePointer() - (record_length + 1) ); 292 293 } catch (Exception e) { 294 throw new tinySQLException(e.getMessage()); 295 } 296 297 if (c != null && v != null) 298 UpdateCurrentRow(c, v); 299 } 300 301 309 public String GetCol(String column) throws tinySQLException { 310 311 try { 312 313 String info[] = (String []) column_info.get(column); 316 317 String datatype = info[COLUMN_TYPE]; 320 int size = Integer.parseInt(info[COLUMN_SIZE]); 321 int pos = Integer.parseInt(info[COLUMN_POS]); 322 323 long OldPosition = ftbl.getFilePointer(); 326 327 String line = ftbl.readLine(); 330 331 String result = line.substring(pos, pos + size); 335 336 ftbl.seek( OldPosition ); 339 340 if (datatype.equals("NUMERIC")) { 343 return result.trim(); 344 } else { 345 return result; 346 } 347 348 } catch (Exception e) { 349 throw new tinySQLException(e.getMessage()); 350 } 351 } 352 353 362 public void UpdateCol( String column, String value ) throws tinySQLException { 363 364 try { 365 366 String info[] = (String []) column_info.get(column); 369 370 String datatype = info[COLUMN_TYPE]; 373 long size = Long.parseLong(info[COLUMN_SIZE]); 374 long pos = Long.parseLong(info[COLUMN_POS]); 375 376 ftbl.seek( ftbl.getFilePointer() + pos ); 380 String writeval; 381 382 if (value.length() > (int) size) { 383 384 writeval = value.substring(0, (int) size); 388 389 } else { 390 391 StringBuffer pad = new StringBuffer (); 394 for (int p = 0; p < ((int) size) - value.length(); p++) { 395 pad.append(" "); 396 } 397 writeval = value + pad.toString(); 398 } 399 400 ftbl.writeBytes(writeval); 403 404 ftbl.seek( ftbl.getFilePointer() - (pos + (long) writeval.length()) ); 407 408 } catch (Exception e) { 409 e.printStackTrace(); 410 throw new tinySQLException(e.getMessage()); 411 } 412 } 413 414 421 public void DeleteRow() throws tinySQLException { 422 423 UpdateCol("_DELETED", "Y"); 426 427 } 428 429 436 public boolean isDeleted() throws tinySQLException { 437 438 return (GetCol("_DELETED")).equals("Y"); 441 } 442 443 448 454 void readColumnInfo() throws tinySQLException { 455 456 try { 457 458 column_info = new Hashtable(); 459 460 FileInputStream fdef = 464 new FileInputStream( dataDir + "/" + table + ".def" ); 465 466 Reader r = new BufferedReader( 469 new InputStreamReader(fdef)); 470 StreamTokenizer def = new StreamTokenizer (r); 471 472 def.whitespaceChars('|', '|'); 477 def.wordChars('0', 'z'); 478 def.eolIsSignificant(false); 479 def.parseNumbers(); 480 481 while ( def.nextToken() != def.TT_EOF ) { 484 485 String datatype = String.valueOf (Types.CHAR); 489 if (def.sval.equals ("NUMERIC")) 490 { 491 datatype = String.valueOf (Types.NUMERIC); 492 } 493 494 def.nextToken(); 497 String column = def.sval; 498 499 def.nextToken(); 502 long size = (new Double (def.nval)).longValue(); 503 504 String [] info = new String [3]; 507 508 info[COLUMN_TYPE] = datatype; 513 info[COLUMN_SIZE] = Long.toString(size); 514 info[COLUMN_POS] = Long.toString(record_length); 515 516 record_length += size; 519 520 column_info.put(column, info); 524 525 } 526 527 fdef.close(); 529 } catch (Exception e) { 530 531 throw new tinySQLException(e.getMessage()); 532 533 } 534 535 } 536 537 } 538 | Popular Tags |