Source code: com/rohanclan/ashpool/core/TableManager.java
1 /*
2 * Ashpool - XML Database
3 * Copyright (C) 2003 Rob Rohan
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 *
19 * TableManager.java
20 *
21 * Created on February 1, 2003, 10:15 AM
22 */
23
24 package com.rohanclan.ashpool.core;
25
26 import java.io.*;
27 import com.rohanclan.ashpool.core.*;
28
29 /**
30 *
31 * @author rob
32 */
33 public class TableManager {
34 public static final String TABLEEXT = ".xml";
35 public static final String TABLESCHEMA = ".xsd";
36 public static final String TABLECOMPRESS = ".xmz";
37
38 public static final byte TYPE_TABLE = 0;
39 public static final byte TYPE_SCHEMA = 1;
40
41
42 private java.io.File datastore;
43 private boolean readonly = false;
44
45 /** Creates a new instance of TableManager */
46 public TableManager(File datasource) throws Exception {
47 if(!datasource.exists()){
48 throw new Exception("The datastore does not exist: " + datasource.toString());
49 }
50 if(!datasource.isDirectory()){
51 throw new Exception("The datastore is not a directory: " + datasource.toString());
52 }
53 if(!datasource.canWrite()){
54 System.err.println("The datastore is read only.");
55 readonly = true;
56 }
57
58 this.datastore = datasource;
59 }
60
61 /** read only datastore? */
62 public boolean isReadOnly(){
63 return readonly;
64 }
65
66 /** get the string ext from the defined byte */
67 private String extFromByte(byte type){
68 switch(type){
69 case TYPE_TABLE:
70 return TABLEEXT;
71 case TYPE_SCHEMA:
72 return TABLESCHEMA;
73 }
74 return ".unk";
75 }
76
77 /** create a table from a stream */
78 public void createTable(String tablename, java.io.InputStream is, byte type)
79 throws IOException{
80
81 //get the proper extention
82 String tabletype = extFromByte(type);
83
84 java.io.FileOutputStream fops = new java.io.FileOutputStream(getDatastoreURI()
85 + System.getProperty("file.separator")
86 + tablename + tabletype
87 );
88
89 StringBuffer filebuff = new StringBuffer();
90 int x;
91 while((x = is.read()) != -1){
92 filebuff.append((char)x);
93 if(filebuff.length() == 1024){
94 fops.write(filebuff.toString().getBytes());
95 filebuff.delete(0, filebuff.length());
96 }
97 }
98 //last flush
99 fops.write(filebuff.toString().getBytes());
100
101 fops.flush();
102 fops.close();
103 }
104
105
106 /** create a new table from a string */
107 public void createTable(String tablename, String Data, byte type) throws IOException{
108 createTable(tablename, new java.io.ByteArrayInputStream(Data.getBytes()), type);
109 }
110
111
112 /** get the datastore (the directory) as a file object */
113 public java.io.File getDatastore(){
114 return this.datastore;
115 }
116
117 /** get the datastore (the directory) as a String */
118 public String getDatastoreURI(){
119 return datastore.getPath().toString();
120 }
121
122 /** get a table as an input stream */
123 public java.io.InputStream getTableInputStream(String tablename) throws IOException {
124 return getTableInputStream(tablename, TableManager.TYPE_TABLE);
125 }
126
127 /** get a schema as an input stream */
128 public java.io.InputStream getSchemaInputStream(String tablename) throws IOException {
129 return getTableInputStream(tablename, TableManager.TYPE_SCHEMA);
130 }
131
132 /** get a table (xml file) as an input stream */
133 public java.io.InputStream getTableInputStream(String tablename, byte type) throws IOException {
134 File fhndl = getTableFile(tablename, type);
135 return new java.io.FileInputStream(fhndl);
136 }
137
138 /** get a table (xml file) as a file object */
139 public java.io.File getTableFile(String tablename, byte type){
140 return new File(getTableFullPath(tablename, type));
141 }
142
143 /** get a table (xml file) as a string path */
144 public String getTableFullPath(String tablename, byte type){
145 switch(type){
146 case TableManager.TYPE_TABLE:
147 return getDatastoreURI() + System.getProperty("file.separator") + tablename + TABLEEXT;
148 case TableManager.TYPE_SCHEMA:
149 return getDatastoreURI() + System.getProperty("file.separator") + tablename + TABLESCHEMA;
150 }
151
152 return "thisfiledoesntexist";
153 }
154
155 /** returns the (possible) tables in this datastore */
156 //AResultSet rs;
157 public void getTables(AResultSet rs){
158 //TABLE_CAT String => table catalog (may be <code>null</code>)
159 //TABLE_SCHEM String => table schema (may be <code>null</code>)
160 //TABLE_NAME String => table name
161 //TABLE_TYPE String => table type. Typical types are "TABLE",
162 // "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY",
163 // "LOCAL TEMPORARY", "ALIAS", "SYNONYM".
164 //REMARKS String => explanatory comment on the table
165 //TYPE_CAT String => the types catalog (may be <code>null</code>)
166 //TYPE_SCHEM String => the types schema (may be <code>null</code>)
167 //TYPE_NAME String => type name (may be <code>null</code>)
168 //SELF_REFERENCING_COL_NAME String => name of the designated
169 // "identifier" column of a typed table (may be <code>null</code>)
170 //REF_GENERATION
171
172 String validtable = "X";
173
174 java.util.Vector vTable = new java.util.Vector();
175 java.util.Vector vSchema = new java.util.Vector();
176 java.util.Vector vName = new java.util.Vector();
177 java.util.Vector vSize = new java.util.Vector();
178 java.util.Vector vNull = new java.util.Vector();
179
180 File list[] = getFileTables();
181
182 for(int r=0; r<list.length; r++){
183 if(list[r].getName().endsWith(TABLEEXT) || list[r].getName().endsWith(TABLECOMPRESS)){
184
185 //if(list[r].getName().endsWith(TABLEEXT)){
186 validtable = "TABLE";
187 vName.add(
188 list[r].getName().substring(
189 0, list[r].getName().length() - TABLEEXT.length()
190 )
191 );
192
193 //if there is a schema for this table add it to the list
194 if(new java.io.File(getDatastoreURI() + System.getProperty("file.separator")
195 + list[r].getName().substring(0, list[r].getName().length() - TABLEEXT.length())
196 + TABLESCHEMA).exists()){
197
198 vSchema.add(list[r].getName().substring(0, list[r].getName().length() - TABLEEXT.length()));
199 }else{
200 vSchema.add("");
201 }
202
203 vTable.add(validtable);
204 vSize.add("File Size: " + list[r].length() + " bytes (" + (list[r].length() / 1024) + "K)");
205 vNull.add("");
206 }
207 }
208
209 rs.addColumn("TABLE_CAT", vNull, java.sql.Types.VARCHAR);
210 rs.addColumn("TABLE_SCHEM", vSchema, java.sql.Types.VARCHAR);
211 rs.addColumn("TABLE_NAME", vName, java.sql.Types.VARCHAR);
212 rs.addColumn("TABLE_TYPE", vTable, java.sql.Types.VARCHAR);
213 rs.addColumn("REMARKS", vSize, java.sql.Types.VARCHAR);
214 rs.addColumn("TYPE_CAT", vNull, java.sql.Types.VARCHAR);
215 rs.addColumn("TYPE_SCHEM", vNull, java.sql.Types.VARCHAR);
216 rs.addColumn("TYPE_NAME", vNull, java.sql.Types.VARCHAR);
217 rs.addColumn("SELF_REFERENCING_COL_NAME", vNull, java.sql.Types.VARCHAR);
218 rs.addColumn("REF_GENERATION", vNull, java.sql.Types.VARCHAR);
219
220 }
221
222
223 /** get a file array of tables */
224 public File[] getFileTables(){
225 File filelist[] = null;
226 try{
227
228 filelist = datastore.listFiles();
229
230 }catch(Exception e){
231 System.err.println("Can not list tables: " + e.toString());
232 e.printStackTrace(System.err);
233 }
234
235 //return tables;
236 return filelist;
237 }
238
239 /** returns true if the passed table has a schema */
240 public boolean hasSchema(String tablename){
241 return getTableFile(tablename, TableManager.TYPE_SCHEMA).exists();
242 }
243
244 ///////////////////////////////////////////////////////////////////////////
245 // commands
246
247 /** drop a table from the datastore, and try to drop the schema too */
248 public boolean doDropTable(String tablename){
249 try{
250 File fhndl = getTableFile(tablename, TableManager.TYPE_TABLE);
251
252 if(!fhndl.exists()){
253 throw new Exception("File (table) doesn't exist.");
254 }
255 if(fhndl.isDirectory()){
256 throw new Exception("Table given is not a file - it's a directory.");
257 }
258 if(!fhndl.canRead() || !fhndl.canWrite()){
259 throw new Exception("Can not read or write to the file (table)");
260 }
261 if(!fhndl.delete()){
262 throw new Exception("Failed to drop the table for some file system reason.");
263 }else{
264 //remove the schema too
265 if(hasSchema(tablename)){
266 getTableFile(tablename, TableManager.TYPE_SCHEMA).delete();
267 }
268 return true;
269 }
270 }catch(Exception e){
271 System.err.println("Can not remove the file " + tablename + TABLEEXT);
272 System.err.println(" from the datastore " + datastore + ".");
273 System.err.println(e.toString());
274 }
275 return false;
276 }
277
278 public boolean doCreatePhysicalTable(String tablename){
279 return true;
280 }
281
282 }