⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbffiletable.java

📁 TinySQL是一个轻量级的纯java数据库引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
 *       Write out a blank record
 */
         for (int i = 1; i < dbfHeader.recordLength; i++) 
         {
            ftbl.write(' ');
         }
         int numRec = (int)dbfHeader.numRecords + 1;
         currentRecordNumber = numRec;
         dbfHeader.setNumRecords(ftbl, numRec);
      } catch (Exception e) {
         if ( tinySQLGlobals.DEBUG ) e.printStackTrace();
         throw new tinySQLException(e.getMessage());
      }
      if (c != null && v != null)
         UpdateCurrentRow(c, v);
      else
         dbfHeader.setTimestamp(ftbl);
   }
/*
 * Retrieve a column's string value from the current row.
 *
 * @param column the column name
 * @see tinySQLTable#GetCol
 */
   public String GetCol(String colName) throws tinySQLException
   {
      int foundDot;
      String columnName;
      columnName = colName;
      foundDot = columnName.indexOf(".");
      if ( foundDot > -1 ) 
         columnName = columnName.substring(foundDot + 1);
      tsColumn coldef = (tsColumn) column_info.get(columnName);
      if (currentRowCache == null)
        currentRowCache = _GetCol(ftbl, dbfHeader, currentRecordNumber);

      return getColumn (coldef, currentRowCache);
   }
/*
 * Extracts a column from the given row. The row is given as a string.
 * If coldef is null, the special delete-flag is returned (Position 0 of a row).
 *
 * @param coldef the column definition, which tells what content to extract from the row
 * @param row the row as an string contains all column data
 * @returns a substring of row.
 */
   public static String getColumn (tsColumn coldef, String row)
   {
      if ( row == (String)null ) System.out.println("Row is null");
      else if ( row.length() == 0 ) System.out.println("Row has 0 length");
      if (coldef == null)
         return row.substring (0,1);
      return row.substring(coldef.position, coldef.position + coldef.size);
   }
/*
 * Retrieve a column's string value from the given row and given colName
 * @param ff the file handle
 * @param colName the column name
 * @param the wanted record (starts with 1)
 * @see tinySQLTable#GetCol
 *
 * @author Thomas Morgner <mgs@sherito.org> This function retrieves a
 * row, perhaps the name should changed to reflect the new function.
 */
   public static String _GetCol(RandomAccessFile ff, DBFHeader dbfHeader,
      int currentRow) throws tinySQLException 
   {
      try
      {
/*
 *       Seek the starting offset of the current record,
 *       as indicated by currentRow
 */
         ff.seek(dbfHeader.headerLength + (currentRow - 1) * dbfHeader.recordLength);
/*
 *       Fully read a byte array out to the length of the record and convert
 *       it into a String.
 */
         byte[] b = new byte[dbfHeader.recordLength];
         ff.readFully(b);
         return new String(b, Utils.encode); // "Cp437"
      } catch (Exception e) {
         throw new tinySQLException(e.getMessage());
      }
   }
/*
 * Update a single column.
 *
 * @param column the column name
 * @param value the String value with which update the column
 * @see tinySQLTable#UpdateCol
 *
 */
   public void UpdateCol( String colName, String value ) throws tinySQLException
   {
      String shortColumnName;
      try 
      {
/*
 *       If it's the pseudo column _DELETED, return
 */
         if (colName.equals("_DELETED")) return;
/*
 *       Retrieve the tsColumn object which corresponds to this column.
 */
         shortColumnName = tinySQLGlobals.getShortName(colName);
         tsColumn column = (tsColumn) column_info.get(shortColumnName);
         if (column == null)
            throw new tinySQLException("Can't update field=" + colName);
         if ( Utils.isDateColumn(column.type) )
         {
/*
 *          Convert non-blank dates to the standard YYYYMMDD format.
 */
            if ( value.trim().length() > 0 ) 
               value = UtilString.dateValue(value);
         }
/*
 *       Seek the starting offset of the current record,
 *       as indicated by currentRecordNumber
 */
         ftbl.seek(dbfHeader.headerLength + (currentRecordNumber - 1) * dbfHeader.recordLength + column.position);
/*
 *       Enforce the correct column length, transform to byte and write to file
 */
         value = Utils.forceToSize(value, column.size, " ");
         byte[] b = value.getBytes(Utils.encode);
         ftbl.write(b);
         dbfHeader.setTimestamp(ftbl);
      } catch (Exception e) {
         throw new tinySQLException(e.getMessage());
      }
   }
/*
 * Delete the current row.
 *
 * @see tinySQLTable#DeleteRow
 *
 */
   public void DeleteRow() throws tinySQLException
   {
      try
      {
         ftbl.seek(dbfHeader.headerLength + (currentRecordNumber - 1) * dbfHeader.recordLength);
         ftbl.write(RECORD_IS_DELETED);
      } catch (Exception e) {
         throw new tinySQLException(e.getMessage());
      }
   }
/* 
 * Is the current row deleted?
 *
 * @see tinySQLTable#isDeleted()
 */
   public boolean isDeleted() throws tinySQLException
   {
      return ((GetCol("_DELETED")).charAt(0) == RECORD_IS_DELETED); // "*";
   }
/*
 * Checks whether the row is deleted. 
 */
   public static boolean isDeleted(RandomAccessFile ff, DBFHeader dbfHeader, int currentRow) throws tinySQLException
   {
      char del = _GetCol(ff, dbfHeader, currentRow).charAt(0); // "_DELETED"
      return del == RECORD_IS_DELETED;
   }
/*
 * Check if record is marked as deleted
 * @param record the record string (the first byte '*' marks a deleted record)
 */
   public static boolean isDeleted(String record)
   {
      if (record.charAt(IS_DELETED_INDEX) == RECORD_IS_DELETED)
         return true;  // '*'
      return false;   // ' '
   }
/***************************************************************************
 *
 * End methods implemented from tinySQLTable.java
 * the rest of this stuff is private methods
 * for dbfFileTable.
 *
 * @return Length in bytes of one row or 0 if not known
 */
   public int getRecordLength() 
   {
      return dbfHeader.recordLength;
   }
   public String toString()
   {
      StringBuffer outputBuffer;
      outputBuffer = new StringBuffer();
      outputBuffer.append("Table " + table + ", path " + fullPath 
      + ", file " + ftbl.toString()); 
      return outputBuffer.toString();
   }
/*
 * opens a DBF file. This is based on Pratap Pereira's 
 * Xbase.pm perl module
 * @return column definition list (HashTable)
 *
 * @author Thomas Morgner <mgs@sherito.org> added check for
 * file exists, before the file is opened. Opening a non existing
 * file will create a new file, and we get errors while trying
 * to read the non-existend headers
 */
   Hashtable open_dbf() throws tinySQLException 
   {
      try
      {
         File f = new File (fullPath);
         if ( tinySQLGlobals.DEBUG ) 
            System.out.println("Try to open  " + f.getAbsolutePath());
         if (!f.exists() ) 
         {
       	    throw new tinySQLException ("Unable to open " + f.getAbsolutePath() 
            + " - does not exist. or can't be read.");
         } else if (!f.canRead () ) {
       	    throw new tinySQLException ("Unable to open " + f.getAbsolutePath()
            + " - file can't be read (permissions?).");
         }
         if (f.canWrite ())
         {
            ftbl = new RandomAccessFile(f, "rw");
         } else {
/*
 *          Open readonly if the file is not writeable. Needed for
 *          databases on CD-Rom
 */
            ftbl = new RandomAccessFile(f, "r");
         }
/*
 *       Read the first 32 bytes ...
 */
         dbfHeader = new DBFHeader(ftbl);
/*
 *       read the column info (each is a 32 byte bulk) ...
 */
         Hashtable coldef_list = new Hashtable();
         columnNameKeys = new Vector();
         int locn = 0; // offset of the current column
         for (int i = 1; i <= dbfHeader.numFields; i++) 
         {
            tsColumn coldef = dbfFile.readColdef(ftbl, table, i, locn);
            locn += coldef.size; // increment locn by the length of this field.
            coldef_list.put(coldef.name, coldef);
            columnNameKeys.addElement(coldef.name);
         }
         fileOpen = true;
         return coldef_list;
      } catch (Exception e) {
         if ( tinySQLGlobals.DEBUG ) e.printStackTrace();
         throw new tinySQLException(e.getMessage());
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -