001: /* Copyright (c) 1995-2000, The Hypersonic SQL Group.
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the Hypersonic SQL Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE HYPERSONIC SQL GROUP,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: *
030: * This software consists of voluntary contributions made by many individuals
031: * on behalf of the Hypersonic SQL Group.
032: *
033: *
034: * For work added by the HSQL Development Group:
035: *
036: * Copyright (c) 2001-2005, The HSQL Development Group
037: * All rights reserved.
038: *
039: * Redistribution and use in source and binary forms, with or without
040: * modification, are permitted provided that the following conditions are met:
041: *
042: * Redistributions of source code must retain the above copyright notice, this
043: * list of conditions and the following disclaimer.
044: *
045: * Redistributions in binary form must reproduce the above copyright notice,
046: * this list of conditions and the following disclaimer in the documentation
047: * and/or other materials provided with the distribution.
048: *
049: * Neither the name of the HSQL Development Group nor the names of its
050: * contributors may be used to endorse or promote products derived from this
051: * software without specific prior written permission.
052: *
053: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
054: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
055: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
056: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
057: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
058: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
059: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
060: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
061: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
062: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
063: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
064: */
065:
066: package org.hsqldb.util;
067:
068: import java.io.Serializable;
069: import java.sql.SQLException;
070: import java.util.Hashtable;
071:
072: // fredt@users 20011220 - patch 481239 by xponsard@users - enhancements
073: // enhancements to support saving and loading of transfer settings,
074: // transfer of blobs, and catalog and schema names in source db
075: // changes by fredt to allow saving and loading of transfer settings
076: // fredt@users 20020215 - patch 516309 by Nicolas Bazin - enhancements
077: // sqlbob@users 20020325 - patch 1.7.0 - reengineering
078: // fredt@users 20040508 - patch 1.7.2 - bug fixes
079:
080: /**
081: * Transfers data from one database to another
082: *
083: * @author Thomas Mueller (Hypersonic SQL Group)
084: * @version 1.7.2
085: * @since Hypersonic SQL
086: */
087: class TransferTable implements Serializable {
088:
089: Hashtable hTypes;
090: DataAccessPoint sourceDb;
091: DataAccessPoint destDb;
092: SQLStatements Stmts = null;
093: Traceable tracer;
094:
095: TransferTable(DataAccessPoint src, String name, String schema,
096: String type, Traceable t) {
097:
098: Stmts = new SQLStatements();
099: sourceDb = src;
100: Stmts.sSchema = "";
101:
102: if (schema != null && schema.length() > 0) {
103: Stmts.sSchema = schema;
104: }
105:
106: Stmts.sType = type;
107: Stmts.sDatabaseToConvert = src.databaseToConvert;
108: Stmts.sSourceTable = Stmts.sDestTable = name;
109: tracer = t;
110:
111: if (Stmts.sType.compareTo("TABLE") == 0) {
112: Stmts.sSourceSelect = "SELECT * FROM "
113: + src.helper.formatName(Stmts.sSourceTable) + ";";
114: } else if (Stmts.sType.compareTo("VIEW") == 0) {
115: Stmts.sSourceSelect = "";
116: }
117: }
118:
119: void setDest(String _Schema, DataAccessPoint dest) throws Exception {
120:
121: destDb = dest;
122:
123: dest.helper.setSchema(_Schema);
124: }
125:
126: /**
127: * extractTableStructure
128: *
129: * @param Source
130: * @param Destination
131: * @throws Exception
132: */
133: void extractTableStructure(DataAccessPoint Source,
134: DataAccessPoint Destination) throws Exception {
135: initTypes();
136: Source.getTableStructure(this , Destination);
137: }
138:
139: /**
140: * Method declaration
141: *
142: *
143: * @param t
144: *
145: * @throws SQLException
146: */
147: void transferStructure() throws Exception {
148:
149: String Statement = new String("");
150:
151: if (destDb.helper.needTransferTransaction()) {
152: try {
153: destDb.setAutoCommit(false);
154: } catch (Exception e) {
155: }
156: }
157:
158: if (Stmts.bTransfer == false) {
159: tracer.trace("Table " + Stmts.sSourceTable
160: + " not transfered");
161:
162: return;
163: }
164:
165: tracer
166: .trace("Table " + Stmts.sSourceTable
167: + ": start transfer");
168:
169: try {
170: if (Stmts.bDropIndex) {
171: if (Stmts.sDestDropIndex.charAt(Stmts.sDestDropIndex
172: .length() - 1) != ';') {
173: Stmts.sDestDropIndex += ";";
174: }
175:
176: int lastsemicolon = 0;
177: int nextsemicolon = Stmts.sDestDropIndex.indexOf(';');
178:
179: while (nextsemicolon > lastsemicolon) {
180: Statement = Stmts.sDestDropIndex.substring(
181: lastsemicolon, nextsemicolon);
182:
183: while (Statement.charAt(Statement.length() - 1) == ';') {
184: Statement = Statement.substring(0, Statement
185: .length() - 1);
186: }
187:
188: try {
189: tracer.trace("Executing " + Statement);
190: destDb.execute(Statement);
191: } catch (Exception e) {
192: tracer
193: .trace("Ignoring error "
194: + e.getMessage());
195: }
196:
197: lastsemicolon = nextsemicolon + 1;
198: nextsemicolon = lastsemicolon
199: + Stmts.sDestDropIndex.substring(
200: lastsemicolon).indexOf(';');
201: }
202: }
203:
204: if (Stmts.bDelete) {
205: if (Stmts.sDestDelete
206: .charAt(Stmts.sDestDelete.length() - 1) != ';') {
207: Stmts.sDestDelete += ";";
208: }
209:
210: int lastsemicolon = 0;
211: int nextsemicolon = Stmts.sDestDelete.indexOf(';');
212:
213: while (nextsemicolon > lastsemicolon) {
214: Statement = Stmts.sDestDelete.substring(
215: lastsemicolon, nextsemicolon);
216:
217: while (Statement.charAt(Statement.length() - 1) == ';') {
218: Statement = Statement.substring(0, Statement
219: .length() - 1);
220: }
221:
222: try {
223: tracer.trace("Executing " + Statement);
224: destDb.execute(Statement);
225: } catch (Exception e) {
226: tracer
227: .trace("Ignoring error "
228: + e.getMessage());
229: }
230:
231: lastsemicolon = nextsemicolon + 1;
232: nextsemicolon = lastsemicolon
233: + Stmts.sDestDelete
234: .substring(lastsemicolon).indexOf(
235: ';');
236: }
237: }
238:
239: if (Stmts.bDrop) {
240: if (Stmts.sDestDrop
241: .charAt(Stmts.sDestDrop.length() - 1) != ';') {
242: Stmts.sDestDrop += ";";
243: }
244:
245: int lastsemicolon = 0;
246: int nextsemicolon = Stmts.sDestDrop.indexOf(';');
247:
248: while (nextsemicolon > lastsemicolon) {
249: Statement = Stmts.sDestDrop.substring(
250: lastsemicolon, nextsemicolon);
251:
252: while (Statement.charAt(Statement.length() - 1) == ';') {
253: Statement = Statement.substring(0, Statement
254: .length() - 1);
255: }
256:
257: try {
258: tracer.trace("Executing " + Statement);
259: destDb.execute(Statement);
260: } catch (Exception e) {
261: tracer
262: .trace("Ignoring error "
263: + e.getMessage());
264: }
265:
266: lastsemicolon = nextsemicolon + 1;
267: nextsemicolon = lastsemicolon
268: + Stmts.sDestDrop.substring(lastsemicolon)
269: .indexOf(';');
270: }
271: }
272:
273: if (Stmts.bCreate) {
274: if (Stmts.sDestCreate
275: .charAt(Stmts.sDestCreate.length() - 1) != ';') {
276: Stmts.sDestCreate += ";";
277: }
278:
279: int lastsemicolon = 0;
280: int nextsemicolon = Stmts.sDestCreate.indexOf(';');
281:
282: while (nextsemicolon > lastsemicolon) {
283: Statement = Stmts.sDestCreate.substring(
284: lastsemicolon, nextsemicolon);
285:
286: while (Statement.charAt(Statement.length() - 1) == ';') {
287: Statement = Statement.substring(0, Statement
288: .length() - 1);
289: }
290:
291: tracer.trace("Executing " + Statement);
292: destDb.execute(Statement);
293:
294: lastsemicolon = nextsemicolon + 1;
295: nextsemicolon = lastsemicolon
296: + Stmts.sDestCreate
297: .substring(lastsemicolon).indexOf(
298: ';');
299: }
300: }
301: } catch (Exception e) {
302: try {
303: if (!destDb.getAutoCommit()) {
304: destDb.rollback();
305: }
306: } catch (Exception e1) {
307: }
308:
309: throw (e);
310: }
311:
312: if (!destDb.getAutoCommit()) {
313: destDb.commit();
314:
315: try {
316: destDb.setAutoCommit(true);
317: } catch (Exception e) {
318: }
319: }
320: }
321:
322: void transferData(int iMaxRows) throws Exception, SQLException {
323:
324: if (destDb.helper.needTransferTransaction()) {
325: try {
326: destDb.setAutoCommit(false);
327: } catch (Exception e) {
328: }
329: }
330:
331: try {
332: if (Stmts.bInsert) {
333: if (destDb.helper.needTransferTransaction()) {
334: try {
335: destDb.setAutoCommit(false);
336: } catch (Exception e) {
337: }
338: }
339:
340: tracer.trace("Executing " + Stmts.sSourceSelect);
341:
342: TransferResultSet r = sourceDb
343: .getData(Stmts.sSourceSelect);
344:
345: tracer.trace("Start transfering data...");
346: destDb.beginDataTransfer();
347: tracer.trace("Executing " + Stmts.sDestInsert);
348: destDb.putData(Stmts.sDestInsert, r, iMaxRows);
349: destDb.endDataTransfer();
350: tracer.trace("Finished");
351:
352: if (!destDb.getAutoCommit()) {
353: destDb.commit();
354:
355: try {
356: destDb.setAutoCommit(true);
357: } catch (Exception e) {
358: }
359: }
360: }
361: } catch (Exception e) {
362: try {
363: if (!destDb.getAutoCommit()) {
364: destDb.rollback();
365: }
366: } catch (Exception e1) {
367: }
368:
369: throw (e);
370: }
371:
372: if (!destDb.getAutoCommit()) {
373: destDb.commit();
374:
375: try {
376: destDb.setAutoCommit(true);
377: } catch (Exception e) {
378: }
379: }
380: }
381:
382: /**
383: * Method declaration
384: *
385: *
386: * @param t
387: *
388: * @throws SQLException
389: */
390: void transferAlter() throws Exception {
391:
392: String Statement = new String("");
393:
394: if (destDb.helper.needTransferTransaction()) {
395: try {
396: destDb.setAutoCommit(false);
397: } catch (Exception e) {
398: }
399: }
400:
401: if (Stmts.bTransfer == false) {
402: tracer.trace("Table " + Stmts.sSourceTable
403: + " not transfered");
404:
405: return;
406: }
407:
408: tracer.trace("Table " + Stmts.sSourceTable + ": start alter");
409:
410: try {
411: if (Stmts.bCreateIndex) {
412: if (Stmts.sDestCreateIndex
413: .charAt(Stmts.sDestCreateIndex.length() - 1) != ';') {
414: Stmts.sDestCreateIndex += ";";
415: }
416:
417: int lastsemicolon = 0;
418: int nextsemicolon = Stmts.sDestCreateIndex.indexOf(';');
419:
420: while (nextsemicolon > lastsemicolon) {
421: Statement = Stmts.sDestCreateIndex.substring(
422: lastsemicolon, nextsemicolon);
423:
424: while (Statement.charAt(Statement.length() - 1) == ';') {
425: Statement = Statement.substring(0, Statement
426: .length() - 1);
427: }
428:
429: try {
430: tracer.trace("Executing "
431: + Stmts.sDestCreateIndex);
432: destDb.execute(Statement);
433: } catch (Exception e) {
434: tracer
435: .trace("Ignoring error "
436: + e.getMessage());
437: }
438:
439: lastsemicolon = nextsemicolon + 1;
440: nextsemicolon = lastsemicolon
441: + Stmts.sDestCreateIndex.substring(
442: lastsemicolon).indexOf(';');
443: }
444: }
445:
446: if (Stmts.bAlter) {
447: if (Stmts.sDestAlter
448: .charAt(Stmts.sDestAlter.length() - 1) != ';') {
449: Stmts.sDestAlter += ";";
450: }
451:
452: int lastsemicolon = 0;
453: int nextsemicolon = Stmts.sDestAlter.indexOf(';');
454:
455: while (nextsemicolon > lastsemicolon) {
456: Statement = Stmts.sDestAlter.substring(
457: lastsemicolon, nextsemicolon);
458:
459: while (Statement.charAt(Statement.length() - 1) == ';') {
460: Statement = Statement.substring(0, Statement
461: .length() - 1);
462: }
463:
464: try {
465: tracer.trace("Executing " + Statement);
466: destDb.execute(Statement);
467: } catch (Exception e) {
468: tracer
469: .trace("Ignoring error "
470: + e.getMessage());
471: }
472:
473: lastsemicolon = nextsemicolon + 1;
474: nextsemicolon = lastsemicolon
475: + Stmts.sDestAlter.substring(lastsemicolon)
476: .indexOf(';');
477: }
478: }
479: } catch (Exception e) {
480: try {
481: if (!destDb.getAutoCommit()) {
482: destDb.rollback();
483: }
484: } catch (Exception e1) {
485: }
486:
487: throw (e);
488: }
489:
490: if (!destDb.getAutoCommit()) {
491: destDb.commit();
492:
493: try {
494: destDb.setAutoCommit(true);
495: } catch (Exception e) {
496: }
497: }
498: }
499:
500: private void initTypes() throws SQLException {
501:
502: if (hTypes != null) {
503: return;
504: }
505:
506: hTypes = destDb.helper.getSupportedTypes();
507: }
508: }
|