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

📄 dbffiletable.java

📁 TinySQL是一个轻量级的纯java数据库引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *
 * Extension of tinySQLTable which manipulates dbf files.
 *
 * Copyright 1996 John Wiley & Sons, Inc. 
 * See the COPYING file for redistribution details.
 *
 * $Author: davis $
 * $Date: 2004/12/18 21:29:47 $
 * $Revision: 1.1 $
 *
 */
package com.sqlmagic.tinysql;

import java.util.*;
import java.lang.*;
import java.io.*;


/**
dBase read/write access <br> 
@author Brian Jepson <bjepson@home.com>
@author Marcel Ruff <ruff@swand.lake.de> Added write access to dBase and JDK 2 support
@author Thomas Morgner <mgs@sherito.org> Added caching for the current read row. A row
is now read only once and substrings are generated by each call to GetCol. Incredibly 
increased read speed when little memory is available and disks are slow.
*/
public class dbfFileTable extends tinySQLTable
{
   private String fullPath,fileName;   
   private DBFHeader dbfHeader = null; // the first 32 bytes of the dBase file
   private RandomAccessFile ftbl;  // access handle to the dBase file
   public boolean fileOpen=false;
   final static String dbfExtension = ".DBF";

   // dBase III column info offsets (one for each column):
   final static int FIELD_NAME_INDEX = 0;       // 0-10 column name, ASCIIZ - null padded
   final static int FIELD_TYPE_INDEX = 11;      // 11-11
   final static int IMU_INDEX = 12;             // 12-15 (in memory use)
   final static int FIELD_LENGTH_INDEX = 16;    // 16-16 (max field length = 254)
   final static int DECIMAL_COUNT_INDEX = 17;   // 17-17
   final static int FIELD_RESERVED_INDEX = 18;  // 18-31

/* 
 * The header section ends with carriage return CR.
 *
 * The data records have fixed length (from LENGTH_OF_REC_INDEX)
 * the field entries in a record have fixed length (from FIELD_LENGTH_INDEX)
 * all number and dates are stored as ASCII characters
 */
   final static int IS_DELETED_INDEX = 0;    // '*': is deleted
                                      // ' ': is not deleted
   final static char RECORD_IS_DELETED = '*';
   final static char RECORD_IS_NOT_DELETED = ' ';
/*
 * Current record
 */
   int currentRecordNumber = 0;      // current record, starts with 1!
/*
 * The cache holds a single row as string and is read only once,
 * and discarded when the cursor moves
 */
   private String currentRowCache = null;
/*
 * End of file flag
 */
   boolean eof = false;
/*
 *
 * Constructs a dbfFileTable. This is only called by getTable()
 * in dbfFile.java.
 *
 * @param dDir data directory
 * @param table_name the name of the table
 *
 */
   dbfFileTable( String dDir, String table_name ) throws tinySQLException 
   {
      int aliasAt;
      aliasAt = table_name.indexOf("->");
      if ( aliasAt > -1 ) 
      {
         table = table_name.substring(0,aliasAt);
         tableAlias = table_name.substring(aliasAt + 2);
      } else {
         table = table_name;
         tableAlias = table_name;
      }
/*
 *    The full path to the file
 */
      fileName = table;
      if (!fileName.toUpperCase().endsWith(dbfExtension) )
        fileName = fileName + dbfExtension;
      fullPath = dDir + File.separator + fileName;  
      if ( tinySQLGlobals.DEBUG ) 
         System.out.println("dbfFileTable: fileName=" + fileName + "\nTable="
         + table + "\nfullPath=" + fullPath);
/*
 *    Open the DBF file
 */
      column_info = open_dbf();
   }
/*
 * Check if the file is open.
 */
   public boolean isOpen()
   {
      return fileOpen;
   }
/*
 * Close method. Try not to call this until you are sure 
 * the object is about to go out of scope.
 */
   public void close() throws tinySQLException 
   {
      try 
      {
         if ( tinySQLGlobals.DEBUG )
            System.out.println("Closing " + toString());
         ftbl.close();
         fileOpen = false;
      } catch (IOException e) {
         throw new tinySQLException(e.getMessage());
      }
   }
/*
 * Returns the size of a column
 *
 * @param column name of the column
 * @see tinySQLTable#ColSize
 */
   public int ColSize(String colName) throws tinySQLException 
   {
      tsColumn coldef = getColumn(colName);
      return coldef.size;
   }
/*
 * Returns the number of rows in the table
 */
   public int GetRowCount()
   {
      return dbfHeader.numRecords;
   }
/*
 * Returns the decimal places for a column
 */
   public int ColDec(String colName) throws tinySQLException
   {
      tsColumn coldef = getColumn(colName);
      return coldef.decimalPlaces;
   }
/*
 * Returns the datatype of a column.
 *
 * @param column name of the column.
 * @see tinySQLTable#ColType
 *
 * @changed to return java.sql.Types
 */
   public int ColType(String colName) throws tinySQLException 
   {
      tsColumn coldef = getColumn(colName);
      return coldef.type;
   }
/*
 *  Get a column object for the named column.
 */
   public tsColumn getColumn(String colName) throws tinySQLException
   {
     int foundDot;
     String columnName;
     columnName = colName;
     foundDot = columnName.indexOf(".");
     if ( foundDot > -1 )
        columnName = columnName.substring(foundDot+1);
     columnName = tinySQLGlobals.getShortName(columnName);
     tsColumn coldef = (tsColumn) column_info.get(columnName);
     if ( coldef == (tsColumn)null )
        throw new tinySQLException("Column " + columnName + " does not"
        + " exist in table " + table);
     return coldef;
   }
/*
 * Updates the current row in the table.
 *
 * @param c Ordered Vector of column names
 * @param v Ordered Vector (must match order of c) of values
 * @see tinySQLTable#UpdateCurrentRow
 */
   public void UpdateCurrentRow(Vector c, Vector v) throws tinySQLException 
   {
/*
 *    The Vectors v and c are expected to have the 
 *    same number of elements. It is also expected
 *    that the elements correspond to each other,
 *    such that value 1 of Vector v corresponds to
 *    column 1 of Vector c, and so forth.
 */
      for (int i = 0; i < v.size(); i++) 
      {
/*
 *       Get the column name and the value, and
 *       invoke UpdateCol() to update it.
 */
         String column = ((String)c.elementAt(i)).toUpperCase();
         String value =  (String)v.elementAt(i);
         UpdateCol(column, value);
      }
   }
/*
 * Position the record pointer at the top of the table.
 *
 * @see tinySQLTable#GoTop
 */
   public void GoTop() throws tinySQLException 
   {
      currentRowCache = null;
      currentRecordNumber = 0;
      eof           = false;
   }
/*
 * Advance the record pointer to the next record.
 *
 * @see tinySQLTable#NextRecord
 */
   public boolean NextRecord() throws tinySQLException 
   {
      currentRowCache = null;
      if (currentRecordNumber < dbfHeader.numRecords) 
      {
         currentRecordNumber++;
         eof = false;
         return true;
      } else {
         eof = true;
         return false;
      }
   }
/*
 * Insert a row. If c or v == null, insert a blank row
 *
 * @param c Ordered Vector of column names
 * @param v Ordered Vector (must match order of c) of values
 * @see tinySQLTable#InsertRow()
 *
 */
   public void InsertRow(Vector c, Vector v) throws tinySQLException 
   {
      try 
      {
/*
 *       Go to the end of the file, then write out the not deleted indicator
 */
         ftbl.seek( ftbl.length() );
         ftbl.write(RECORD_IS_NOT_DELETED);
/*

⌨️ 快捷键说明

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