Source code: org/hsqldb/TextTable.java
1 /* Copyright (c) 2001-2002, 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 java.sql.SQLException;
35
36 // tony_lai@users 20020820 - patch 595099 by tlai@users use user define PK name
37
38 /**
39 * Class declaration
40 * @author sqlbob@users (RMP)
41 * @version 1.7.0
42 */
43 class TextTable extends org.hsqldb.Table {
44
45 private String readRoots = "";
46 private String emptyRoots = "";
47 private String dataSource = "";
48 private String firstLine = "";
49 private boolean isReversed = false;
50
51 /**
52 * Constructor declaration
53 *
54 * @param db
55 * @param isTemp is a temp text table
56 * @param name
57 * @exception SQLException Description of the Exception
58 */
59 TextTable(Database db, HsqlName name, int type,
60 Session session) throws SQLException {
61 super(db, name, type, session);
62 }
63
64 private void openCache(String source, boolean isDesc,
65 boolean isRdOnly) throws SQLException {
66
67 // Close old cache:
68 if (!dataSource.equals("")) {
69 dDatabase.logger.closeTextCache(tableName.name);
70 }
71
72 cCache = null;
73
74 int count = getIndexCount();
75
76 for (int i = 0; i < count; i++) {
77 getIndex(i).setRoot(null);
78 }
79
80 // Open new cache:
81 if ((source != null) &&!source.equals("")) {
82 try {
83 cCache = dDatabase.logger.openTextCache(tableName.name,
84 source, isRdOnly, isDesc);
85
86 int freePos = cCache.getFreePos();
87 String roots = readRoots;
88
89 if (freePos <= TextCache.NL.length()) {
90 roots = emptyRoots;
91 }
92
93 roots += freePos;
94
95 super.setIndexRoots(roots);
96 } catch (SQLException e) {
97 if (!dataSource.equals(source) || (isDesc != isReversed)
98 || (isRdOnly != isReadOnly)) {
99
100 // Restore old cache.
101 openCache(dataSource, isReversed, isReadOnly);
102 } else {
103 if (cCache != null) {
104 cCache.closeFile();
105 }
106
107 dataSource = "";
108 isReversed = false;
109 }
110
111 throw (e);
112 }
113 }
114
115 if (source == null) {
116 source = "";
117 }
118
119 dataSource = source;
120 isReversed = (isDesc &&!source.equals(""));
121 }
122
123 boolean equals(String other, Session c) {
124
125 boolean isEqual = super.equals(other, c);
126
127 if (isEqual && isReversed) {
128 try {
129 openCache(dataSource, isReversed, isReadOnly);
130 } catch (SQLException e) {
131 return false;
132 }
133 }
134
135 return (isEqual);
136 }
137
138 boolean equals(String other) {
139
140 boolean isEqual = super.equals(other);
141
142 if (isEqual && isReversed) {
143 try {
144 openCache(dataSource, isReversed, isReadOnly);
145 } catch (SQLException e) {
146 return false;
147 }
148 }
149
150 return (isEqual);
151 }
152
153 protected void setDataSource(String source, boolean isDesc,
154 Session s) throws SQLException {
155
156 if (isTemp) {
157 Trace.check(s.getId() == ownerSession.getId(),
158 Trace.ACCESS_IS_DENIED);
159 } else {
160 s.checkAdmin();
161 }
162
163 //-- Open if descending, direction changed, or file changed.
164 if (isDesc || (isDesc != isReversed) ||!dataSource.equals(source)) {
165 openCache(source, isDesc, isReadOnly);
166 }
167
168 if (isReversed) {
169 isReadOnly = true;
170 }
171 }
172
173 protected String getDataSource() throws SQLException {
174 return (dataSource);
175 }
176
177 protected boolean isDescDataSource() throws SQLException {
178 return (isReversed);
179 }
180
181 void setDataReadOnly(boolean value) throws SQLException {
182
183 if (isReversed && value == true) {
184 throw Trace.error(Trace.DATA_IS_READONLY);
185 }
186
187 openCache(dataSource, isReversed, value);
188
189 isReadOnly = value;
190 }
191
192 /**
193 * Method declaration
194 *
195 * @return
196 * @throws SQLException
197 */
198 String getIndexRoots() throws SQLException {
199
200 Trace.doAssert(isCached, "Table.getIndexRootData");
201
202 return (readRoots + "0");
203 }
204
205 /**
206 * Method declaration
207 *
208 * @param s
209 * @throws SQLException
210 */
211 void setIndexRoots(String s) throws SQLException {
212
213 Trace.check(isCached, Trace.TABLE_NOT_FOUND);
214
215 // Ignore
216 }
217
218 boolean isIndexCached() {
219 return false;
220 }
221
222 protected Table duplicate() throws SQLException {
223 return (new TextTable(dDatabase, tableName, tableType, ownerSession));
224 }
225
226 void indexRow(Row r, boolean inserted) throws SQLException {
227
228 if (inserted) {
229 super.indexRow(r, true);
230 } else {
231 Node n = r.getNextNode(null);
232 Node primary = getPrimaryIndex().insertUncached(n);
233
234 if (primary == n) {
235
236 // Not already indexed.
237 n = r.getNextNode(n);
238
239 for (int i = 1; n != null; i++) {
240 getIndex(i).insertUncached(n);
241
242 n = r.getNextNode(n);
243 }
244 } else {
245 r.setPrimaryNode(primary);
246 }
247 }
248 }
249
250 void checkUpdate(int col[], Result deleted,
251 Result inserted) throws SQLException {
252 Trace.check(!dataSource.equals(""), Trace.UNKNOWN_DATA_SOURCE);
253 super.checkUpdate(col, deleted, inserted);
254 }
255
256 void insert(Object row[], Session c) throws SQLException {
257 Trace.check(!dataSource.equals(""), Trace.UNKNOWN_DATA_SOURCE);
258 super.insert(row, c);
259 }
260
261 void delete(Object row[], Session c) throws SQLException {
262 Trace.check(!dataSource.equals(""), Trace.UNKNOWN_DATA_SOURCE);
263 super.delete(row, c);
264 }
265
266 /**
267 * Method declaration
268 *
269 * @param column
270 * @param name
271 * @param unique
272 * @return Description of the Return Value
273 * @throws SQLException
274 */
275 Index createIndexPrivate(int column[], HsqlName name,
276 boolean unique) throws SQLException {
277
278 readRoots += "0 ";
279 emptyRoots += "-1 ";
280
281 return (super.createIndexPrivate(column, name, unique));
282 }
283
284 // tony_lai@users 20020820 - patch 595099
285 void createPrimaryKey(String pkName, int[] columns) throws SQLException {
286
287 if ((columns == null)
288 || ((columns.length == 1)
289 && getColumn(columns[0]).columnName.name.equals(
290 DEFAULT_PK))) {
291 super.createPrimaryKey(null, columns);
292 } else {
293 throw (Trace.error(Trace.SECOND_PRIMARY_KEY));
294 }
295 }
296 }