KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > TextTable


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb;
33
34 import org.hsqldb.lib.FileUtil;
35 import org.hsqldb.lib.StringConverter;
36 import org.hsqldb.persist.TextCache;
37
38 // tony_lai@users 20020820 - patch 595099 - user define PK name
39

40 /**
41  * Subclass of Table to handle TEXT data source. <p>
42  *
43  * Extends Table to provide the notion of an SQL base table object whose
44  * data is read from and written to a text format data file.
45  *
46  * @author sqlbob@users (RMP)
47  * @version 1.8.0
48  */

49 class TextTable extends org.hsqldb.Table {
50
51     private String JavaDoc dataSource = "";
52     private boolean isReversed = false;
53
54     /**
55      * Constructs a new TextTable from the given arguments.
56      *
57      * @param db the owning database
58      * @param name the table's HsqlName
59      * @param type (normal or temp text table)
60      * @param sessionid the id of the owning session (for temp table)
61      * @exception HsqlException Description of the Exception
62      */

63     TextTable(Database db, HsqlNameManager.HsqlName name,
64               int type) throws HsqlException {
65         super(db, name, type);
66     }
67
68     /**
69      * This method does some of the work involved with managing the creation
70      * and openning of the cache, the rest is done in Log.java and
71      * TextCache.java.
72      *
73      * Better clarification of the role of the methods is needed.
74      */

75     private void openCache(String JavaDoc dataSourceNew, boolean isReversedNew,
76                            boolean isReadOnlyNew) throws HsqlException {
77
78         if (dataSourceNew == null) {
79             dataSourceNew = "";
80         }
81
82         // Close old cache:
83
database.logger.closeTextCache(this);
84
85         cache = null;
86
87         clearAllRows(null);
88
89         // Open new cache:
90
if (dataSourceNew.length() > 0) {
91             try {
92                 cache = database.logger.openTextCache(this, dataSourceNew,
93                                                       isReadOnlyNew,
94                                                       isReversedNew);
95
96                 // read and insert all the rows from the source file
97
CachedRow row = null;
98                 int nextpos = 0;
99
100                 if (((TextCache) cache).ignoreFirst) {
101                     nextpos += ((TextCache) cache).readHeaderLine();
102                 }
103
104                 while (true) {
105                     row = (CachedRow) rowStore.get(nextpos);
106
107                     if (row == null) {
108                         break;
109                     }
110
111                     nextpos = row.getPos() + row.getStorageSize();
112
113                     row.setNewNodes();
114                     insertFromTextSource(row);
115                 }
116             } catch (HsqlException e) {
117                 int linenumber = cache == null ? 0
118                                                : ((TextCache) cache)
119                                                    .getLineNumber();
120
121                 if (!dataSource.equals(dataSourceNew)
122                         || isReversedNew != isReversed
123                         || isReadOnlyNew != isReadOnly) {
124
125                     // Restore old cache.
126
// fredt - todo - recursion works - but code is not clear
127
openCache(dataSource, isReversed, isReadOnly);
128                 } else {
129                     if (cache != null) {
130                         cache.close(false);
131                     }
132
133                     //fredt added
134
cache = null;
135                     dataSource = "";
136                     isReversed = false;
137                 }
138
139                 // everything is in order here.
140
// At this point table should either have a valid (old) data
141
// source and cache or have an empty source and null cache.
142
throw Trace.error(Trace.TEXT_FILE, new Object JavaDoc[] {
143                     new Integer JavaDoc(linenumber), e.getMessage()
144                 });
145             }
146         }
147
148         dataSource = dataSourceNew;
149         isReversed = (isReversedNew && dataSourceNew.length() > 0);
150     }
151
152     /**
153      * High level command to assign a data source to the table definition.
154      * Reassigns only if the data source or direction has changed.
155      */

156     protected void setDataSource(Session s, String JavaDoc dataSourceNew,
157                                  boolean isReversedNew,
158                                  boolean newFile) throws HsqlException {
159
160         if (getTableType() == Table.TEMP_TEXT_TABLE) {
161             ;
162         } else {
163             s.checkAdmin();
164         }
165
166         dataSourceNew = dataSourceNew.trim();
167
168         if (newFile && FileUtil.exists(dataSourceNew)) {
169             throw Trace.error(Trace.TEXT_SOURCE_EXISTS, dataSourceNew);
170         }
171
172         //-- Open if descending, direction changed, or file changed.
173
if (isReversedNew || (isReversedNew != isReversed)
174                 ||!dataSource.equals(dataSourceNew)) {
175             openCache(dataSourceNew, isReversedNew, isReadOnly);
176         }
177
178         if (isReversed) {
179             isReadOnly = true;
180         }
181     }
182
183     protected String JavaDoc getDataSource() {
184         return dataSource;
185     }
186
187     protected boolean isDescDataSource() {
188         return isReversed;
189     }
190
191     public void setHeader(String JavaDoc header) throws HsqlException {
192
193         if (cache != null && ((TextCache) cache).ignoreFirst) {
194             ((TextCache) cache).setHeader(header);
195
196             return;
197         }
198
199         throw Trace.error(Trace.TEXT_TABLE_HEADER);
200     }
201
202     public String JavaDoc getHeader() {
203
204         String JavaDoc header = cache == null ? null
205                                       : ((TextCache) cache).getHeader();
206
207         return header == null ? null
208                               : StringConverter.toQuotedString(header, '\"',
209                               true);
210     }
211
212     /**
213      * Used by INSERT, DELETE, UPDATE operations. This class will return
214      * a more appropriate message when there is no data source.
215      */

216     void checkDataReadOnly() throws HsqlException {
217
218         if (dataSource.length() == 0) {
219             throw Trace.error(Trace.UNKNOWN_DATA_SOURCE);
220         }
221
222         if (isReadOnly) {
223             throw Trace.error(Trace.DATA_IS_READONLY);
224         }
225     }
226
227     void setDataReadOnly(boolean value) throws HsqlException {
228
229         if (isReversed && value == true) {
230             throw Trace.error(Trace.DATA_IS_READONLY);
231         }
232
233         openCache(dataSource, isReversed, value);
234
235         isReadOnly = value;
236     }
237
238     boolean isIndexCached() {
239         return false;
240     }
241
242     protected Table duplicate() throws HsqlException {
243         return new TextTable(database, tableName, getTableType());
244     }
245
246     void drop() throws HsqlException {
247         openCache("", false, false);
248     }
249
250     void setIndexRoots(String JavaDoc s) throws HsqlException {
251
252         // do nothing
253
}
254 }
255
Popular Tags