KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sqlmagic > tinysql > textFile


1 /*
2  *
3  * textFile - an extension of tinySQL for text file access
4  *
5  * Copyright 1996, Brian C. Jepson
6  * (bjepson@ids.net)
7  *
8  * $Author: davis $
9  * $Date: 2004/12/18 21:25:06 $
10  * $Revision: 1.1 $
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  */

26
27 package com.sqlmagic.tinysql;
28
29 import java.util.*;
30 import java.lang.*;
31 import java.io.*;
32
33 public class textFile extends tinySQL {
34
35   // the data directory where textFile stores its files
36
//
37
static String JavaDoc dataDir = System.getProperty("user.home") + "/.tinySQL";
38
39   /**
40    * Creates a table given the name and a vector of
41    * column definition (tsColumn) arrays.<br>
42    *
43    * @param table_name the name of the table
44    * @param v a Vector containing arrays of column definitions.
45    * @see tinySQL#CreateTable
46    */

47   void CreateTable ( String JavaDoc table_name, Vector v )
48     throws IOException, tinySQLException {
49
50     // make the data directory, if it needs to be make
51
//
52
mkDataDirectory();
53
54     // perform an implicit drop table.
55
//
56
DropTable(table_name);
57
58     // create the table definition file
59
//
60
FileOutputStream fdef =
61        new FileOutputStream( dataDir + "/" + table_name + ".def" );
62
63     // open it as a DataOutputStream
64
//
65
DataOutputStream def = new DataOutputStream (fdef);
66
67     // write out the column definition for the _DELETED column
68
//
69
def.writeBytes("CHAR|_DELETED|1\n");
70
71     // write out the rest of the columns' definition. The
72
// definition consists of datatype, column name, and
73
// size delimited by a pipe symbol
74
//
75
for (int i = 0; i < v.size(); i++) {
76        def.writeBytes( ((tsColumn) v.elementAt(i)).type + "|");
77        def.writeBytes( ((tsColumn) v.elementAt(i)).name + "|");
78        def.writeBytes( ((tsColumn) v.elementAt(i)).size + "\n");
79     }
80
81     // flush the DataOutputStream and jiggle the handle
82
//
83
def.flush();
84
85     // close the file
86
//
87
fdef.close();
88   }
89
90   /**
91    *
92    * Return a tinySQLTable object, given a table name.
93    *
94    * @param table_name
95    * @see tinySQL#getTable
96    *
97    */

98   tinySQLTable getTable (String JavaDoc table_name) throws tinySQLException {
99     return (tinySQLTable) new textFileTable (dataDir, table_name);
100   }
101
102   /**
103    * Creates new Columns in table_name, given a vector of
104    * column definition (tsColumn) arrays.<br>
105    *
106    * ALTER TABLE table [ * ] ADD [ COLUMN ] column type
107    *
108    * @param table_name the name of the table
109    * @param v a Vector containing arrays of column definitions.
110    * @see tinySQL#AlterTableAddCol
111    */

112   void AlterTableAddCol ( String JavaDoc table_name, Vector v )
113     throws IOException, tinySQLException {
114
115       throw new tinySQLException("ALTER TABLE ADD is not supported");
116   }
117
118
119   /**
120    * Deletes Columns from table_name, given a vector of
121    * column definition (tsColumn) arrays.<br>
122    *
123    * ALTER TABLE table DROP [ COLUMN ] column { RESTRICT | CASCADE }
124    *
125    * @param table_name the name of the table
126    * @param v a Vector containing arrays of column definitions.
127    * @see tinySQL#CreateTable
128    */

129   void AlterTableDropCol ( String JavaDoc table_name, Vector v )
130     throws IOException, tinySQLException {
131
132       throw new tinySQLException("ALTER TABLE DROP is not supported");
133   }
134
135
136   /**
137    *
138    * Drop a named table by deleting it and its associated
139    * .def file.
140    *
141    * @param fname table name
142    * @see tinySQL#DropTable
143    *
144    */

145   void DropTable (String JavaDoc fname) throws tinySQLException {
146
147     try {
148
149       Utils.delFile(dataDir, fname);
150       Utils.delFile(dataDir, fname + ".def");
151
152     } catch (Exception JavaDoc e) {
153       throw new tinySQLException(e.getMessage());
154     }
155
156   }
157
158
159   /*
160    *
161    * Make the data directory unless it already exists
162    *
163    */

164   void mkDataDirectory() throws NullPointerException JavaDoc {
165
166     File dd = new File( dataDir );
167
168     if (!dd.exists()) {
169       dd.mkdir();
170     }
171
172   }
173
174
175   /*
176    * regression test, does no longer work
177    */

178   public static void main(String JavaDoc argv[]) {
179 // textFile foo = new textFile();
180
// tsResultSet trs = null;
181
// try {
182
// trs = foo.sqlexec("CREATE TABLE test (name CHAR(10))");
183
// trs = foo.sqlexec("INSERT INTO test (name) VALUES('test')");
184
// trs = foo.sqlexec("SELECT name FROM test");
185
// } catch (Exception e) {
186
// e.printStackTrace();
187
// }
188
//
189
// tsRow row = trs.rowAt(0);
190
//
191
// tsColumn column = trs.columnAtIndex(0);
192
// String colval = row.columnAsString(column);
193
//
194
// if (colval.startsWith("test")) {
195
// System.out.println("textFile driver installed correctly.");
196
// } else {
197
// System.out.println("Test was not successful :-(");
198
// System.out.println("Got \"" + colval + "\", expected \"test\"");
199
// }
200
//
201
}
202 }
203
204
Popular Tags