1 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 40 49 class TextTable extends org.hsqldb.Table { 50 51 private String dataSource = ""; 52 private boolean isReversed = false; 53 54 63 TextTable(Database db, HsqlNameManager.HsqlName name, 64 int type) throws HsqlException { 65 super(db, name, type); 66 } 67 68 75 private void openCache(String dataSourceNew, boolean isReversedNew, 76 boolean isReadOnlyNew) throws HsqlException { 77 78 if (dataSourceNew == null) { 79 dataSourceNew = ""; 80 } 81 82 database.logger.closeTextCache(this); 84 85 cache = null; 86 87 clearAllRows(null); 88 89 if (dataSourceNew.length() > 0) { 91 try { 92 cache = database.logger.openTextCache(this, dataSourceNew, 93 isReadOnlyNew, 94 isReversedNew); 95 96 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 openCache(dataSource, isReversed, isReadOnly); 128 } else { 129 if (cache != null) { 130 cache.close(false); 131 } 132 133 cache = null; 135 dataSource = ""; 136 isReversed = false; 137 } 138 139 throw Trace.error(Trace.TEXT_FILE, new Object [] { 143 new Integer (linenumber), e.getMessage() 144 }); 145 } 146 } 147 148 dataSource = dataSourceNew; 149 isReversed = (isReversedNew && dataSourceNew.length() > 0); 150 } 151 152 156 protected void setDataSource(Session s, String 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 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 getDataSource() { 184 return dataSource; 185 } 186 187 protected boolean isDescDataSource() { 188 return isReversed; 189 } 190 191 public void setHeader(String 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 getHeader() { 203 204 String header = cache == null ? null 205 : ((TextCache) cache).getHeader(); 206 207 return header == null ? null 208 : StringConverter.toQuotedString(header, '\"', 209 true); 210 } 211 212 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 s) throws HsqlException { 251 252 } 254 } 255 | Popular Tags |