001: /*
002: *
003: * textFile - an extension of tinySQL for text file access
004: *
005: * Copyright 1996, Brian C. Jepson
006: * (bjepson@ids.net)
007: *
008: * $Author: davis $
009: * $Date: 2004/12/18 21:25:06 $
010: * $Revision: 1.1 $
011: *
012: * This library is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU Lesser General Public
014: * License as published by the Free Software Foundation; either
015: * version 2.1 of the License, or (at your option) any later version.
016: *
017: * This library is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
020: * Lesser General Public License for more details.
021: *
022: * You should have received a copy of the GNU Lesser General Public
023: * License along with this library; if not, write to the Free Software
024: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
025: */
026:
027: package com.sqlmagic.tinysql;
028:
029: import java.util.*;
030: import java.lang.*;
031: import java.io.*;
032:
033: public class textFile extends tinySQL {
034:
035: // the data directory where textFile stores its files
036: //
037: static String dataDir = System.getProperty("user.home")
038: + "/.tinySQL";
039:
040: /**
041: * Creates a table given the name and a vector of
042: * column definition (tsColumn) arrays.<br>
043: *
044: * @param table_name the name of the table
045: * @param v a Vector containing arrays of column definitions.
046: * @see tinySQL#CreateTable
047: */
048: void CreateTable(String table_name, Vector v) throws IOException,
049: tinySQLException {
050:
051: // make the data directory, if it needs to be make
052: //
053: mkDataDirectory();
054:
055: // perform an implicit drop table.
056: //
057: DropTable(table_name);
058:
059: // create the table definition file
060: //
061: FileOutputStream fdef = new FileOutputStream(dataDir + "/"
062: + table_name + ".def");
063:
064: // open it as a DataOutputStream
065: //
066: DataOutputStream def = new DataOutputStream(fdef);
067:
068: // write out the column definition for the _DELETED column
069: //
070: def.writeBytes("CHAR|_DELETED|1\n");
071:
072: // write out the rest of the columns' definition. The
073: // definition consists of datatype, column name, and
074: // size delimited by a pipe symbol
075: //
076: for (int i = 0; i < v.size(); i++) {
077: def.writeBytes(((tsColumn) v.elementAt(i)).type + "|");
078: def.writeBytes(((tsColumn) v.elementAt(i)).name + "|");
079: def.writeBytes(((tsColumn) v.elementAt(i)).size + "\n");
080: }
081:
082: // flush the DataOutputStream and jiggle the handle
083: //
084: def.flush();
085:
086: // close the file
087: //
088: fdef.close();
089: }
090:
091: /**
092: *
093: * Return a tinySQLTable object, given a table name.
094: *
095: * @param table_name
096: * @see tinySQL#getTable
097: *
098: */
099: tinySQLTable getTable(String table_name) throws tinySQLException {
100: return (tinySQLTable) new textFileTable(dataDir, table_name);
101: }
102:
103: /**
104: * Creates new Columns in table_name, given a vector of
105: * column definition (tsColumn) arrays.<br>
106: *
107: * ALTER TABLE table [ * ] ADD [ COLUMN ] column type
108: *
109: * @param table_name the name of the table
110: * @param v a Vector containing arrays of column definitions.
111: * @see tinySQL#AlterTableAddCol
112: */
113: void AlterTableAddCol(String table_name, Vector v)
114: throws IOException, tinySQLException {
115:
116: throw new tinySQLException("ALTER TABLE ADD is not supported");
117: }
118:
119: /**
120: * Changes the name of a column
121: *
122: * ALTER TABLE table [ * ] RENAME [ COLUMN ] TO [NEW_COLUMN]
123: *
124: */
125: void AlterTableRenameCol(String table_name, String oldName,
126: String newName) throws tinySQLException {
127:
128: throw new tinySQLException(
129: "ALTER TABLE RENAME is not supported");
130: }
131:
132: /**
133: * Deletes Columns from table_name, given a vector of
134: * column definition (tsColumn) arrays.<br>
135: *
136: * ALTER TABLE table DROP [ COLUMN ] column { RESTRICT | CASCADE }
137: *
138: * @param table_name the name of the table
139: * @param v a Vector containing arrays of column definitions.
140: * @see tinySQL#CreateTable
141: */
142: void AlterTableDropCol(String table_name, Vector v)
143: throws IOException, tinySQLException {
144:
145: throw new tinySQLException("ALTER TABLE DROP is not supported");
146: }
147:
148: /**
149: *
150: * Drop a named table by deleting it and its associated
151: * .def file.
152: *
153: * @param fname table name
154: * @see tinySQL#DropTable
155: *
156: */
157: void DropTable(String fname) throws tinySQLException {
158:
159: try {
160:
161: Utils.delFile(dataDir, fname);
162: Utils.delFile(dataDir, fname + ".def");
163:
164: } catch (Exception e) {
165: throw new tinySQLException(e.getMessage());
166: }
167:
168: }
169:
170: /*
171: *
172: * Make the data directory unless it already exists
173: *
174: */
175: void mkDataDirectory() throws NullPointerException {
176:
177: File dd = new File(dataDir);
178:
179: if (!dd.exists()) {
180: dd.mkdir();
181: }
182:
183: }
184:
185: /*
186: * regression test, does no longer work
187: */
188: public static void main(String argv[]) {
189: // textFile foo = new textFile();
190: // tsResultSet trs = null;
191: // try {
192: // trs = foo.sqlexec("CREATE TABLE test (name CHAR(10))");
193: // trs = foo.sqlexec("INSERT INTO test (name) VALUES('test')");
194: // trs = foo.sqlexec("SELECT name FROM test");
195: // } catch (Exception e) {
196: // e.printStackTrace();
197: // }
198: //
199: // tsRow row = trs.rowAt(0);
200: //
201: // tsColumn column = trs.columnAtIndex(0);
202: // String colval = row.columnAsString(column);
203: //
204: // if (colval.startsWith("test")) {
205: // System.out.println("textFile driver installed correctly.");
206: // } else {
207: // System.out.println("Test was not successful :-(");
208: // System.out.println("Got \"" + colval + "\", expected \"test\"");
209: // }
210: //
211: }
212: }
|