1 13 package com.sqlmagic.tinysql; 14 15 import java.util.*; 16 import java.lang.*; 17 import java.io.*; 18 19 20 28 public class dbfFileTable extends tinySQLTable 29 { 30 private String fullPath,fileName; 31 private DBFHeader dbfHeader = null; private RandomAccessFile ftbl; public boolean fileOpen=false; 34 final static String dbfExtension = ".DBF"; 35 36 final static int FIELD_NAME_INDEX = 0; final static int FIELD_TYPE_INDEX = 11; final static int IMU_INDEX = 12; final static int FIELD_LENGTH_INDEX = 16; final static int DECIMAL_COUNT_INDEX = 17; final static int FIELD_RESERVED_INDEX = 18; 44 51 final static int IS_DELETED_INDEX = 0; final static char RECORD_IS_DELETED = '*'; 54 final static char RECORD_IS_NOT_DELETED = ' '; 55 58 int currentRecordNumber = 0; 63 private String currentRowCache = null; 64 67 boolean eof = false; 68 final boolean debug=false; 69 78 dbfFileTable( String dDir, String table_name ) throws tinySQLException 79 { 80 int aliasAt; 81 aliasAt = table_name.indexOf("->"); 82 if ( aliasAt > -1 ) 83 { 84 table = table_name.substring(0,aliasAt); 85 tableAlias = table_name.substring(aliasAt + 2); 86 } else { 87 table = table_name; 88 tableAlias = table_name; 89 } 90 93 fileName = table; 94 if (!fileName.toUpperCase().endsWith(dbfExtension) ) 95 fileName = fileName + dbfExtension; 96 fullPath = dDir + File.separator + fileName; 97 if ( debug ) 98 System.out.println("dbfFileTable: fileName=" + fileName + " table=" 99 + table + " fullPath=" + fullPath); 100 103 column_info = open_dbf(); 104 } 105 108 public boolean isOpen() 109 { 110 return fileOpen; 111 } 112 116 public void close() throws tinySQLException 117 { 118 try 119 { 120 if ( debug ) System.out.println("Closing " + toString()); 121 ftbl.close(); 122 fileOpen = false; 123 } catch (IOException e) { 124 throw new tinySQLException(e.getMessage()); 125 } 126 } 127 133 public int ColSize(String colName) throws tinySQLException 134 { 135 tsColumn coldef = getColumn(colName); 136 return coldef.size; 137 } 138 141 public int GetRowCount() 142 { 143 return dbfHeader.numRecords; 144 } 145 148 public int ColDec(String colName) throws tinySQLException 149 { 150 tsColumn coldef = getColumn(colName); 151 return coldef.decimalPlaces; 152 } 153 161 public int ColType(String colName) throws tinySQLException 162 { 163 tsColumn coldef = getColumn(colName); 164 return coldef.type; 165 } 166 169 public tsColumn getColumn(String colName) throws tinySQLException 170 { 171 int foundDot; 172 String columnName; 173 columnName = colName; 174 foundDot = columnName.indexOf("."); 175 if ( foundDot > -1 ) 176 columnName = columnName.substring(foundDot+1); 177 tsColumn coldef = (tsColumn) column_info.get(columnName); 178 if ( coldef == (tsColumn)null ) 179 throw new tinySQLException("Column " + columnName + " does not" 180 + " exist in table " + table); 181 return coldef; 182 } 183 190 public void UpdateCurrentRow(Vector c, Vector v) throws tinySQLException 191 { 192 199 for (int i = 0; i < v.size(); i++) 200 { 201 205 String column = ((String )c.elementAt(i)).toUpperCase(); 206 String value = (String )v.elementAt(i); 207 UpdateCol(column, value); 208 } 209 } 210 215 public void GoTop() throws tinySQLException 216 { 217 currentRowCache = null; 218 currentRecordNumber = 0; 219 eof = false; 220 } 221 226 public boolean NextRecord() throws tinySQLException 227 { 228 currentRowCache = null; 229 if (currentRecordNumber < dbfHeader.numRecords) 230 { 231 currentRecordNumber++; 232 eof = false; 233 return true; 234 } else { 235 eof = true; 236 return false; 237 } 238 } 239 247 public void InsertRow(Vector c, Vector v) throws tinySQLException 248 { 249 try 250 { 251 254 ftbl.seek( ftbl.length() ); 255 ftbl.write(RECORD_IS_NOT_DELETED); 256 259 for (int i = 1; i < dbfHeader.recordLength; i++) 260 { 261 ftbl.write(' '); 262 } 263 int numRec = (int)dbfHeader.numRecords + 1; 264 currentRecordNumber = numRec; 265 dbfHeader.setNumRecords(ftbl, numRec); 266 } catch (Exception e) { 267 if ( debug ) e.printStackTrace(); 268 throw new tinySQLException(e.getMessage()); 269 } 270 if (c != null && v != null) 271 UpdateCurrentRow(c, v); 272 else 273 dbfHeader.setTimestamp(ftbl); 274 } 275 281 public String GetCol(String colName) throws tinySQLException 282 { 283 int foundDot; 284 String columnName; 285 columnName = colName; 286 foundDot = columnName.indexOf("."); 287 if ( foundDot > -1 ) 288 columnName = columnName.substring(foundDot + 1); 289 tsColumn coldef = (tsColumn) column_info.get(columnName); 290 if (currentRowCache == null) 291 currentRowCache = _GetCol(ftbl, dbfHeader, currentRecordNumber); 292 293 return getColumn (coldef, currentRowCache); 294 } 295 303 public static String getColumn (tsColumn coldef, String row) 304 { 305 if (coldef == null) 306 return row.substring (0,1); 307 return row.substring(coldef.position, coldef.position + coldef.size); 308 } 309 319 public static String _GetCol(RandomAccessFile ff, DBFHeader dbfHeader, 320 int currentRow) throws tinySQLException 321 { 322 try 323 { 324 328 ff.seek(dbfHeader.headerLength + (currentRow - 1) * dbfHeader.recordLength); 329 333 byte[] b = new byte[dbfHeader.recordLength]; 334 ff.readFully(b); 335 return new String (b, Utils.encode); } catch (Exception e) { 337 throw new tinySQLException(e.getMessage()); 338 } 339 } 340 348 public void UpdateCol( String colName, String value ) throws tinySQLException 349 { 350 try 351 { 352 355 if (colName.equals("_DELETED")) return; 356 359 tsColumn column = (tsColumn) column_info.get(colName); 360 if (column == null) 361 throw new tinySQLException("Can't update field=" + colName); 362 366 ftbl.seek(dbfHeader.headerLength + (currentRecordNumber - 1) * dbfHeader.recordLength + column.position); 367 370 value = Utils.forceToSize(value, column.size, " "); 371 byte[] b = value.getBytes(Utils.encode); 372 ftbl.write(b); 373 dbfHeader.setTimestamp(ftbl); 374 } catch (Exception e) { 375 throw new tinySQLException(e.getMessage()); 376 } 377 } 378 384 public void DeleteRow() throws tinySQLException 385 { 386 try 387 { 388 ftbl.seek(dbfHeader.headerLength + (currentRecordNumber - 1) * dbfHeader.recordLength); 389 ftbl.write(RECORD_IS_DELETED); 390 } catch (Exception e) { 391 throw new tinySQLException(e.getMessage()); 392 } 393 } 394 399 public boolean isDeleted() throws tinySQLException 400 { 401 return ((GetCol("_DELETED")).charAt(0) == RECORD_IS_DELETED); } 403 406 public static boolean isDeleted(RandomAccessFile ff, DBFHeader dbfHeader, int currentRow) throws tinySQLException 407 { 408 char del = _GetCol(ff, dbfHeader, currentRow).charAt(0); return del == RECORD_IS_DELETED; 410 } 411 415 public static boolean isDeleted(String record) 416 { 417 if (record.charAt(IS_DELETED_INDEX) == RECORD_IS_DELETED) 418 return true; return false; } 421 429 public int getRecordLength() 430 { 431 return dbfHeader.recordLength; 432 } 433 public String toString() 434 { 435 StringBuffer outputBuffer; 436 outputBuffer = new StringBuffer (); 437 outputBuffer.append("Table " + table + ", path " + fullPath 438 + ", file " + ftbl.toString()); 439 return outputBuffer.toString(); 440 } 441 451 Hashtable open_dbf() throws tinySQLException 452 { 453 try 454 { 455 File f = new File (fullPath); 456 if (debug) System.out.println("Try to open " + f.getAbsolutePath()); 457 if (!f.exists() ) 458 { 459 throw new tinySQLException ("Unable to open " + f.getAbsolutePath() 460 + " - does not exist. or can't be read."); 461 } else if (!f.canRead () ) { 462 throw new tinySQLException ("Unable to open " + f.getAbsolutePath() 463 + " - file can't be read (permissions?)."); 464 } 465 if (f.canWrite ()) 466 { 467 ftbl = new RandomAccessFile(f, "rw"); 468 } else { 469 473 ftbl = new RandomAccessFile(f, "r"); 474 } 475 478 dbfHeader = new DBFHeader(ftbl); 479 482 Hashtable coldef_list = new Hashtable(); 483 int locn = 0; for (int i = 1; i <= dbfHeader.numFields; i++) 485 { 486 tsColumn coldef = dbfFile.readColdef(ftbl, table, i, locn); 487 locn += coldef.size; coldef_list.put(coldef.name, coldef); 489 } 490 fileOpen = true; 491 return coldef_list; 492 } catch (Exception e) { 493 if ( debug ) e.printStackTrace(); 494 throw new tinySQLException(e.getMessage()); 495 } 496 } 497 } 498 | Popular Tags |