1 65 66 67 package org.hsqldb.util; 68 69 import java.io.Serializable ; 70 import java.sql.SQLException ; 71 import java.util.Hashtable ; 72 73 81 88 class TransferTable implements Serializable { 89 90 Hashtable hTypes; 91 DataAccessPoint sourceDb; 92 DataAccessPoint destDb; 93 SQLStatements Stmts = null; 94 Traceable tracer; 95 96 TransferTable(DataAccessPoint src, String name, String schema, 97 String type, Traceable t) { 98 99 Stmts = new SQLStatements(); 100 sourceDb = src; 101 Stmts.sSchema = ""; 102 103 if (schema != null && schema.length() > 0) { 104 Stmts.sSchema = schema; 105 } 106 107 Stmts.sType = type; 108 Stmts.sDatabaseToConvert = src.databaseToConvert; 109 Stmts.sSourceTable = Stmts.sDestTable = name; 110 tracer = t; 111 112 if (Stmts.sType.compareTo("TABLE") == 0) { 113 Stmts.sSourceSelect = "SELECT * FROM " 114 + src.helper.formatName(Stmts.sSourceTable) 115 + ";"; 116 } else if (Stmts.sType.compareTo("VIEW") == 0) { 117 Stmts.sSourceSelect = ""; 118 } 119 } 120 121 void setDest(String _Schema, DataAccessPoint dest) throws Exception { 122 123 destDb = dest; 124 125 dest.helper.setSchema(_Schema); 126 } 127 128 135 void extractTableStructure(DataAccessPoint Source, 136 DataAccessPoint Destination) throws Exception { 137 initTypes(); 138 Source.getTableStructure(this, Destination); 139 } 140 141 149 void transferStructure() throws Exception { 150 151 String Statement = new String (""); 152 153 if (destDb.helper.needTransferTransaction()) { 154 try { 155 destDb.setAutoCommit(false); 156 } catch (Exception e) {} 157 } 158 159 if (Stmts.bTransfer == false) { 160 tracer.trace("Table " + Stmts.sSourceTable + " not transfered"); 161 162 return; 163 } 164 165 tracer.trace("Table " + Stmts.sSourceTable + ": start transfer"); 166 167 try { 168 if (Stmts.bDropIndex) { 169 if (Stmts.sDestDropIndex.charAt(Stmts.sDestDropIndex.length() 170 - 1) != ';') { 171 Stmts.sDestDropIndex += ";"; 172 } 173 174 int lastsemicolon = 0; 175 int nextsemicolon = Stmts.sDestDropIndex.indexOf(';'); 176 177 while (nextsemicolon > lastsemicolon) { 178 Statement = Stmts.sDestDropIndex.substring(lastsemicolon, 179 nextsemicolon); 180 181 while (Statement.charAt(Statement.length() - 1) == ';') { 182 Statement = Statement.substring(0, Statement.length() 183 - 1); 184 } 185 186 try { 187 tracer.trace("Executing " + Statement); 188 destDb.execute(Statement); 189 } catch (Exception e) { 190 tracer.trace("Ignoring error " + e.getMessage()); 191 } 192 193 lastsemicolon = nextsemicolon + 1; 194 nextsemicolon = lastsemicolon 195 + Stmts.sDestDropIndex.substring( 196 lastsemicolon).indexOf(';'); 197 } 198 } 199 200 if (Stmts.bDelete) { 201 if (Stmts.sDestDelete.charAt(Stmts.sDestDelete.length() - 1) 202 != ';') { 203 Stmts.sDestDelete += ";"; 204 } 205 206 int lastsemicolon = 0; 207 int nextsemicolon = Stmts.sDestDelete.indexOf(';'); 208 209 while (nextsemicolon > lastsemicolon) { 210 Statement = Stmts.sDestDelete.substring(lastsemicolon, 211 nextsemicolon); 212 213 while (Statement.charAt(Statement.length() - 1) == ';') { 214 Statement = Statement.substring(0, Statement.length() 215 - 1); 216 } 217 218 try { 219 tracer.trace("Executing " + Statement); 220 destDb.execute(Statement); 221 } catch (Exception e) { 222 tracer.trace("Ignoring error " + e.getMessage()); 223 } 224 225 lastsemicolon = nextsemicolon + 1; 226 nextsemicolon = lastsemicolon 227 + Stmts.sDestDelete.substring( 228 lastsemicolon).indexOf(';'); 229 } 230 } 231 232 if (Stmts.bDrop) { 233 if (Stmts.sDestDrop.charAt(Stmts.sDestDrop.length() - 1) 234 != ';') { 235 Stmts.sDestDrop += ";"; 236 } 237 238 int lastsemicolon = 0; 239 int nextsemicolon = Stmts.sDestDrop.indexOf(';'); 240 241 while (nextsemicolon > lastsemicolon) { 242 Statement = Stmts.sDestDrop.substring(lastsemicolon, 243 nextsemicolon); 244 245 while (Statement.charAt(Statement.length() - 1) == ';') { 246 Statement = Statement.substring(0, Statement.length() 247 - 1); 248 } 249 250 try { 251 tracer.trace("Executing " + Statement); 252 destDb.execute(Statement); 253 } catch (Exception e) { 254 tracer.trace("Ignoring error " + e.getMessage()); 255 } 256 257 lastsemicolon = nextsemicolon + 1; 258 nextsemicolon = lastsemicolon 259 + Stmts.sDestDrop.substring( 260 lastsemicolon).indexOf(';'); 261 } 262 } 263 264 if (Stmts.bCreate) { 265 if (Stmts.sDestCreate.charAt(Stmts.sDestCreate.length() - 1) 266 != ';') { 267 Stmts.sDestCreate += ";"; 268 } 269 270 int lastsemicolon = 0; 271 int nextsemicolon = Stmts.sDestCreate.indexOf(';'); 272 273 while (nextsemicolon > lastsemicolon) { 274 Statement = Stmts.sDestCreate.substring(lastsemicolon, 275 nextsemicolon); 276 277 while (Statement.charAt(Statement.length() - 1) == ';') { 278 Statement = Statement.substring(0, Statement.length() 279 - 1); 280 } 281 282 tracer.trace("Executing " + Statement); 283 destDb.execute(Statement); 284 285 lastsemicolon = nextsemicolon + 1; 286 nextsemicolon = lastsemicolon 287 + Stmts.sDestCreate.substring( 288 lastsemicolon).indexOf(';'); 289 } 290 } 291 } catch (Exception e) { 292 try { 293 if (!destDb.getAutoCommit()) { 294 destDb.rollback(); 295 } 296 } catch (Exception e1) {} 297 298 throw (e); 299 } 300 301 if (!destDb.getAutoCommit()) { 302 destDb.commit(); 303 304 try { 305 destDb.setAutoCommit(true); 306 } catch (Exception e) {} 307 } 308 } 309 310 void transferData(int iMaxRows) throws Exception , SQLException { 311 312 if (destDb.helper.needTransferTransaction()) { 313 try { 314 destDb.setAutoCommit(false); 315 } catch (Exception e) {} 316 } 317 318 try { 319 if (Stmts.bInsert) { 320 if (destDb.helper.needTransferTransaction()) { 321 try { 322 destDb.setAutoCommit(false); 323 } catch (Exception e) {} 324 } 325 326 tracer.trace("Executing " + Stmts.sSourceSelect); 327 328 TransferResultSet r = sourceDb.getData(Stmts.sSourceSelect); 329 330 tracer.trace("Start transfering data..."); 331 destDb.beginDataTransfer(); 332 tracer.trace("Executing " + Stmts.sDestInsert); 333 destDb.putData(Stmts.sDestInsert, r, iMaxRows); 334 destDb.endDataTransfer(); 335 tracer.trace("Finished"); 336 337 if (!destDb.getAutoCommit()) { 338 destDb.commit(); 339 340 try { 341 destDb.setAutoCommit(true); 342 } catch (Exception e) {} 343 } 344 } 345 } catch (Exception e) { 346 try { 347 if (!destDb.getAutoCommit()) { 348 destDb.rollback(); 349 } 350 } catch (Exception e1) {} 351 352 throw (e); 353 } 354 355 if (!destDb.getAutoCommit()) { 356 destDb.commit(); 357 358 try { 359 destDb.setAutoCommit(true); 360 } catch (Exception e) {} 361 } 362 } 363 364 372 void transferAlter() throws Exception { 373 374 String Statement = new String (""); 375 376 if (destDb.helper.needTransferTransaction()) { 377 try { 378 destDb.setAutoCommit(false); 379 } catch (Exception e) {} 380 } 381 382 if (Stmts.bTransfer == false) { 383 tracer.trace("Table " + Stmts.sSourceTable + " not transfered"); 384 385 return; 386 } 387 388 tracer.trace("Table " + Stmts.sSourceTable + ": start alter"); 389 390 try { 391 if (Stmts.bCreateIndex) { 392 if (Stmts.sDestCreateIndex.charAt( 393 Stmts.sDestCreateIndex.length() - 1) != ';') { 394 Stmts.sDestCreateIndex += ";"; 395 } 396 397 int lastsemicolon = 0; 398 int nextsemicolon = Stmts.sDestCreateIndex.indexOf(';'); 399 400 while (nextsemicolon > lastsemicolon) { 401 Statement = 402 Stmts.sDestCreateIndex.substring(lastsemicolon, 403 nextsemicolon); 404 405 while (Statement.charAt(Statement.length() - 1) == ';') { 406 Statement = Statement.substring(0, Statement.length() 407 - 1); 408 } 409 410 try { 411 tracer.trace("Executing " + Stmts.sDestCreateIndex); 412 destDb.execute(Statement); 413 } catch (Exception e) { 414 tracer.trace("Ignoring error " + e.getMessage()); 415 } 416 417 lastsemicolon = nextsemicolon + 1; 418 nextsemicolon = lastsemicolon 419 + Stmts.sDestCreateIndex.substring( 420 lastsemicolon).indexOf(';'); 421 } 422 } 423 424 if (Stmts.bAlter) { 425 if (Stmts.sDestAlter.charAt(Stmts.sDestAlter.length() - 1) 426 != ';') { 427 Stmts.sDestAlter += ";"; 428 } 429 430 int lastsemicolon = 0; 431 int nextsemicolon = Stmts.sDestAlter.indexOf(';'); 432 433 while (nextsemicolon > lastsemicolon) { 434 Statement = Stmts.sDestAlter.substring(lastsemicolon, 435 nextsemicolon); 436 437 while (Statement.charAt(Statement.length() - 1) == ';') { 438 Statement = Statement.substring(0, Statement.length() 439 - 1); 440 } 441 442 try { 443 tracer.trace("Executing " + Statement); 444 destDb.execute(Statement); 445 } catch (Exception e) { 446 tracer.trace("Ignoring error " + e.getMessage()); 447 } 448 449 lastsemicolon = nextsemicolon + 1; 450 nextsemicolon = lastsemicolon 451 + Stmts.sDestAlter.substring( 452 lastsemicolon).indexOf(';'); 453 } 454 } 455 } catch (Exception e) { 456 try { 457 if (!destDb.getAutoCommit()) { 458 destDb.rollback(); 459 } 460 } catch (Exception e1) {} 461 462 throw (e); 463 } 464 465 if (!destDb.getAutoCommit()) { 466 destDb.commit(); 467 468 try { 469 destDb.setAutoCommit(true); 470 } catch (Exception e) {} 471 } 472 } 473 474 private void initTypes() throws SQLException { 475 476 if (hTypes != null) { 477 return; 478 } 479 480 hTypes = destDb.helper.getSupportedTypes(); 481 } 482 } 483 | Popular Tags |