/* * 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()); } }
//throws IOException { /** * Opens the given file for reading, assuming the specified * encoding for file names. * * @param f the archive. * @param encoding the encoding to use for file names, use null * for the platform's default encoding * @param useUnicodeExtraFields whether to use InfoZIP Unicode * Extra Fields (if present) to set the file names. * * @throws IOException if an error occurs while reading the file. */ public ZipFile(java.io.File f, String encoding, bool useUnicodeExtraFields) //throws IOException { this.OFFSET_COMPARATOR = new IAC_OFFSET_COMPARATOR(this); this.encoding = encoding; this.zipEncoding = ZipEncodingHelper.getZipEncoding(encoding); this.useUnicodeExtraFields = useUnicodeExtraFields; archive = new java.io.RandomAccessFile(f, "r"); bool success = false; try { java.util.Map <ZipArchiveEntry, IAC_NameAndComment> entriesWithoutUTF8Flag = populateFromCentralDirectory(); resolveLocalFileHeaderData(entriesWithoutUTF8Flag); success = true; } finally { if (!success) { try { archive.close(); } catch (java.io.IOException) { // swallow, throw the original exception instead } } } }
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(); }
/* * Closes this ZIP file. This method is idempotent. * * @throws IOException * if an IOException occurs. */ public void close() // throws IOException { { java.io.RandomAccessFile raf = mRaf; if (raf != null) // Only close initialized instances { lock (raf) { mRaf = null; raf.close(); } if (fileToDeleteOnClose != null) { new java.io.File(fileName).delete(); /*AccessController.doPrivileged(new PrivilegedAction<Object>() { * public Object run() { * new File(fileName).delete(); * return null; * } * });*/ // fileToDeleteOnClose.delete(); fileToDeleteOnClose = null; } } }
/** * * close method. Try not to call this until you are sure * the object is about to go out of scope. * */ public override void close() {//throws tinySQLException { try { ftbl.close(); } catch (java.io.IOException e) { throw new TinySQLException(e.getMessage()); } }
/** * Closes this output stream and releases any system resources * associated with the stream. * * @exception IOException if an I/O error occurs. */ public override void close() //throws IOException { if (!finished) { finish(); } if (raf != null) { raf.close(); } if (outJ != null) { outJ.close(); } }
/* * Close method. Try not to call this until you are sure * the object is about to go out of scope. */ public override void close() //throws TinySQLException { try { if (TinySQLGlobals.DEBUG) { java.lang.SystemJ.outJ.println("Closing " + toString()); } ftbl.close(); fileOpen = false; } catch (java.io.IOException e) { throw new TinySQLException(e.getMessage()); } }
/** * Creates a new ZIP OutputStream writing to a File. Will use * random access if possible. * @param file the file to zip to * @throws IOException on error */ public ZipArchiveOutputStream(java.io.File file) //throws IOException { java.io.OutputStream o = null; java.io.RandomAccessFile _raf = null; try { _raf = new java.io.RandomAccessFile(file, "rw"); _raf.setLength(0); } catch (java.io.IOException) { if (_raf != null) { try { _raf.close(); } catch (java.io.IOException) { // ignore } _raf = null; } o = new java.io.FileOutputStream(file); } outJ = o; raf = _raf; }
/** * Closes the archive. * @throws IOException if an error occurs closing the archive. */ public void close() //throws IOException { archive.close(); }
/** * * 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()); } }
/** * 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()); } }