Пример #1
0
        internal override void CreateTable(String tableName, java.util.Vector <Object> v)
        {//    throws IOException, tinySQLException {
            //---------------------------------------------------
            // determin meta data ....
            int numCols      = v.size();
            int recordLength = 1;        // 1 byte for the flag field

            for (int i = 0; i < numCols; i++)
            {
                TsColumn coldef = ((TsColumn)v.elementAt(i));
                recordLength += coldef.size;
            }

            //---------------------------------------------------
            // create the new dBase file ...
            DBFHeader dbfHeader = new DBFHeader(numCols, recordLength);

            java.io.RandomAccessFile ftbl = dbfHeader.create(dataDir, tableName);

            //---------------------------------------------------
            // write out the rest of the columns' definition.
            for (int i = 0; i < v.size(); i++)
            {
                TsColumn coldef = ((TsColumn)v.elementAt(i));
                Utils.log("CREATING COL=" + coldef.name);
                writeColdef(ftbl, coldef);
            }

            ftbl.write((byte)0x0d); // header section ends with CR (carriage return)

            ftbl.close();
        }
Пример #2
0
        /*
         * Rename columns
         *
         * ALTER TABLE table RENAME war TO peace
         */
        internal override void AlterTableRenameCol(String tableName, String oldColname, String newColname)
        //throws tinySQLException
        {
            String fullpath = dataDir + java.io.File.separator + tableName + DBFFileTable.dbfExtension;

            try
            {
                java.io.RandomAccessFile ftbl = new java.io.RandomAccessFile(fullpath, "rw");

                DBFHeader dbfHeader = new DBFHeader(ftbl); // read the first 32 bytes ...

                int locn = 0;                              // offset of the current column
                for (int iCol = 1; iCol <= dbfHeader.numFields; iCol++)
                {
                    TsColumn coldef = readColdef(ftbl, tableName, iCol, locn);
                    if (coldef.name.equals(oldColname))
                    {
                        Utils.log("Replacing column name '" + oldColname + "' with '" + newColname + "'");
                        ftbl.seek((iCol - 1) * 32 + 32);
                        ftbl.write(Utils.forceToSize(newColname,
                                                     DBFFileTable.FIELD_TYPE_INDEX - DBFFileTable.FIELD_NAME_INDEX,
                                                     (byte)0));
                        ftbl.close();
                        return;
                    }
                }
                ftbl.close();
                throw new TinySQLException("Renaming of column name '" + oldColname + "' to '" + newColname + "' failed, no column '" + oldColname + "' found");
            }
            catch (Exception e)
            {
                throw new TinySQLException(e.getMessage());
            }
        }
Пример #3
0
        /**
         * Retrieve a record (=row)
         * @param dbfHeader dBase meta info
         * @param recordNumber starts with 1
         * @return the String with the complete record
         *         or null if the record is marked as deleted
         * @see tinySQLTable#GetCol
         */
        public String GetRecord(java.io.RandomAccessFile ff, DBFHeader dbfHeader, int recordNumber) //throws tinySQLException
        {
            if (recordNumber < 1)
            {
                throw new TinySQLException("Internal error - current record number < 1");
            }

            try
            {
                // seek the starting offset of the current record,
                // as indicated by recordNumber
                ff.seek(dbfHeader.headerLength + (recordNumber - 1) * dbfHeader.recordLength);

                // fully read a byte array out to the length of
                // the record.
                byte[] b = new byte[dbfHeader.recordLength];
                ff.readFully(b);

                // make it into a String
                String record = new java.lang.StringJ(b, Utils.encode);

                // remove deleted records
                if (DBFFileTable.isDeleted(record))
                {
                    return(null);
                }

                return(record);
            }
            catch (Exception e)
            {
                throw new TinySQLException(e.getMessage());
            }
        }
Пример #4
0
        /*
         * 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 <*****@*****.**> This function retrieves a
         * row, perhaps the name should changed to reflect the new function.
         */
        public static String _GetCol(java.io.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 java.lang.StringJ(b, Utils.encode)); // "Cp437"
            }
            catch (Exception e)
            {
                throw new TinySQLException(e.getMessage());
            }
        }
Пример #5
0
        /*
         * opens a DBF file. This is based on Pratap Pereira's
         * Xbase.pm perl module
         * @return column definition list (java.util.Hashtable<Object,Object>)
         *
         * @author Thomas Morgner <*****@*****.**> 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
         */
        java.util.Hashtable <Object, Object> open_dbf() //throws TinySQLException
        {
            try
            {
                java.io.File f = new java.io.File(fullPath);
                if (TinySQLGlobals.DEBUG)
                {
                    java.lang.SystemJ.outJ.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 java.io.RandomAccessFile(f, "rw");
                }
                else
                {
                    /*
                     *          Open readonly if the file is not writeable. Needed for
                     *          databases on CD-Rom
                     */
                    ftbl = new java.io.RandomAccessFile(f, "r");
                }

                /*
                 *       Read the first 32 bytes ...
                 */
                dbfHeader = new DBFHeader(ftbl);

                /*
                 *       read the column info (each is a 32 byte bulk) ...
                 */
                java.util.Hashtable <Object, Object> coldef_list = new java.util.Hashtable <Object, Object>();
                columnNameKeys = new java.util.Vector <Object>();
                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)
                {
                    java.lang.SystemJ.err.println(e.ToString());                      // e.printStackTrace();
                }
                throw new TinySQLException(e.getMessage());
            }
        }
Пример #6
0
        /*
         * Checks whether the row is deleted.
         */
        public static bool isDeleted(java.io.RandomAccessFile ff, DBFHeader dbfHeader, int currentRow) //throws TinySQLException
        {
            char del = _GetCol(ff, dbfHeader, currentRow).charAt(0);                                   // "_DELETED"

            return(del == RECORD_IS_DELETED);
        }
Пример #7
0
 /**
  *
  * The DBF File class provides read-only access to DBF
  * files, so this baby should throw an exception.
  *
  * @param fname table name
  * @see tinySQL#DropTable
  *
  */
 internal override void DropTable(String fname)
 {//throws tinySQLException {
     DBFHeader.dropTable(dataDir, fname);
 }
Пример #8
0
        /**
         *
         * Deletes Columns from tableName, given a vector of
         * column definition (tsColumn) arrays.<br>
         *
         * ALTER TABLE table DROP [ COLUMN ] column { RESTRICT | CASCADE }
         *
         * @param tableName the name of the table
         * @param v a Vector containing arrays of column definitions.
         * @see tinySQL#AlterTableDropCol
         *
         */
        internal override void AlterTableDropCol(String tableName, java.util.Vector <Object> v)
        {//throws IOException, tinySQLException {
            // rename the file ...
            String fullpath = dataDir + java.io.File.separator + tableName + DBFFileTable.dbfExtension;
            String tmppath  = dataDir + java.io.File.separator + tableName + "-tmp" + DBFFileTable.dbfExtension;

            if (Utils.renameFile(fullpath, tmppath) == false)
            {
                throw new TinySQLException("ALTER TABLE DROP COL error in renaming " + fullpath);
            }

            try
            {
                // open the old file ...
                java.io.RandomAccessFile ftbl_tmp = new java.io.RandomAccessFile(tmppath, "r");

                // read the first 32 bytes ...
                DBFHeader dbfHeader_tmp = new DBFHeader(ftbl_tmp);

                // read the column info ...
                java.util.Vector <Object> coldef_list = new java.util.Vector <Object>(dbfHeader_tmp.numFields - v.size());
                int locn = 0; // offset of the current column

                nextCol : for (int i = 1; i <= dbfHeader_tmp.numFields; i++)
                {
                    TsColumn coldef = readColdef(ftbl_tmp, tableName, i, locn);

                    // remove the DROP columns from the existing cols ...
                    for (int jj = 0; jj < v.size(); jj++)
                    {
                        String colName = (String)v.elementAt(jj);
                        if (coldef.name.equals(colName))
                        {
                            Utils.log("Dropping " + colName);
                            goto nextCol;
                        }
                    }

                    locn += coldef.size; // increment locn by the length of this field.
                    // Utils.log("Recycling " + coldef.name);
                    coldef_list.addElement(coldef);
                }

                // create the new table ...
                CreateTable(tableName, coldef_list);

                // copy the data from old to new

                // opening new created dBase file ...
                java.io.RandomAccessFile ftbl = new java.io.RandomAccessFile(fullpath, "rw");
                ftbl.seek(ftbl.length()); // go to end of file

                int numRec = 0;
                for (int iRec = 1; iRec <= dbfHeader_tmp.numRecords; iRec++)
                {
                    if (DBFFileTable.isDeleted(ftbl_tmp, dbfHeader_tmp, iRec) == true)
                    {
                        continue;
                    }

                    numRec++;

                    ftbl.write(DBFFileTable.RECORD_IS_NOT_DELETED);  // write flag

                    // Read the whole column into the table's cache
                    String column = DBFFileTable._GetCol(ftbl_tmp, dbfHeader_tmp, iRec);

                    for (int iCol = 0; iCol < coldef_list.size(); iCol++) // write columns
                    {
                        TsColumn coldef = (TsColumn)coldef_list.elementAt(iCol);

                        // Extract column values from cache
                        String value = DBFFileTable.getColumn(coldef, column);
                        java.lang.SystemJ.outJ.println("From cache column value" + value);

                        value = Utils.forceToSize(value, coldef.size, " "); // enforce the correct column length

                        byte[] b = value.getBytes(Utils.encode);            // transform to byte and write to file
                        ftbl.write(b);
                    }
                }

                ftbl_tmp.close();

                // remove temp file
                java.io.File f = new java.io.File(tmppath);
                if (f.exists())
                {
                    f.delete();
                }

                DBFHeader.writeNumRecords(ftbl, numRec);
                ftbl.close();
            }
            catch (Exception e)
            {
                throw new TinySQLException(e.getMessage());
            }
        }
Пример #9
0
        /**
         * Creates new Columns in tableName, given a vector of
         * column definition (tsColumn) arrays.<br>
         * It is necessary to copy the whole file to do this task.
         *
         * ALTER TABLE table [ * ] ADD [ COLUMN ] column type
         *
         * @param tableName the name of the table
         * @param v a Vector containing arrays of column definitions.
         * @see tinySQL#AlterTableAddCol
         */
        internal override void AlterTableAddCol(String tableName, java.util.Vector <Object> v)
        {//throws IOException, tinySQLException {
            // rename the file ...
            String fullpath = dataDir + java.io.File.separator + tableName + DBFFileTable.dbfExtension;
            String tmppath  = dataDir + java.io.File.separator + tableName + "_tmp_tmp" + DBFFileTable.dbfExtension;

            if (Utils.renameFile(fullpath, tmppath) == false)
            {
                throw new TinySQLException("ALTER TABLE ADD COL error in renaming " + fullpath);
            }

            try
            {
                // open the old file ...
                java.io.RandomAccessFile ftbl_tmp = new java.io.RandomAccessFile(tmppath, "r");

                // read the first 32 bytes ...
                DBFHeader dbfHeader_tmp = new DBFHeader(ftbl_tmp);

                // read the column info ...
                java.util.Vector <Object> coldef_list = new java.util.Vector <Object>(dbfHeader_tmp.numFields + v.size());
                int locn = 0; // offset of the current column
                for (int i = 1; i <= dbfHeader_tmp.numFields; i++)
                {
                    TsColumn coldef = readColdef(ftbl_tmp, tableName, i, locn);
                    locn += coldef.size; // increment locn by the length of this field.
                    coldef_list.addElement(coldef);
                }

                // add the new column definitions to the existing ...
                for (int jj = 0; jj < v.size(); jj++)
                {
                    coldef_list.addElement(v.elementAt(jj));
                }

                // create the new table ...
                CreateTable(tableName, coldef_list);

                // copy the data from old to new

                // opening new created dBase file ...
                java.io.RandomAccessFile ftbl = new java.io.RandomAccessFile(fullpath, "rw");
                ftbl.seek(ftbl.length()); // go to end of file

                int numRec = 0;
                for (int iRec = 1; iRec <= dbfHeader_tmp.numRecords; iRec++)
                {
                    String str = GetRecord(ftbl_tmp, dbfHeader_tmp, iRec);

                    // Utils.log("Copy of record#" + iRec + " str='" + str + "' ...");

                    if (str == null)
                    {
                        continue;                           // record was marked as deleted, ignore it
                    }
                    ftbl.write(str.getBytes(Utils.encode)); // write original record
                    numRec++;

                    for (int iCol = 0; iCol < v.size(); iCol++) // write added columns
                    {
                        TsColumn coldef = (TsColumn)v.elementAt(iCol);

                        // enforce the correct column length
                        String value = Utils.forceToSize(coldef.defaultVal, coldef.size, " ");

                        // transform to byte and write to file
                        byte[] b = value.getBytes(Utils.encode);
                        ftbl.write(b);
                    }
                }

                ftbl_tmp.close();

                DBFHeader.writeNumRecords(ftbl, numRec);
                ftbl.close();

                Utils.delFile(tmppath);
            }
            catch (Exception e)
            {
                throw new TinySQLException(e.getMessage());
            }
        }