KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > util > TransferTable


1 /* Copyright (c) 1995-2000, The Hypersonic SQL 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 Hypersonic SQL 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 THE HYPERSONIC SQL GROUP,
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  * This software consists of voluntary contributions made by many individuals
31  * on behalf of the Hypersonic SQL Group.
32  *
33  *
34  * For work added by the HSQL Development Group:
35  *
36  * Copyright (c) 2001-2005, The HSQL Development Group
37  * All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions are met:
41  *
42  * Redistributions of source code must retain the above copyright notice, this
43  * list of conditions and the following disclaimer.
44  *
45  * Redistributions in binary form must reproduce the above copyright notice,
46  * this list of conditions and the following disclaimer in the documentation
47  * and/or other materials provided with the distribution.
48  *
49  * Neither the name of the HSQL Development Group nor the names of its
50  * contributors may be used to endorse or promote products derived from this
51  * software without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
54  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
57  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
60  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
61  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  */

65
66
67 package org.hsqldb.util;
68
69 import java.io.Serializable JavaDoc;
70 import java.sql.SQLException JavaDoc;
71 import java.util.Hashtable JavaDoc;
72
73 // fredt@users 20011220 - patch 481239 by xponsard@users - enhancements
74
// enhancements to support saving and loading of transfer settings,
75
// transfer of blobs, and catalog and schema names in source db
76
// changes by fredt to allow saving and loading of transfer settings
77
// fredt@users 20020215 - patch 516309 by Nicolas Bazin - enhancements
78
// sqlbob@users 20020325 - patch 1.7.0 - reengineering
79
// fredt@users 20040508 - patch 1.7.2 - bug fixes
80

81 /**
82  * Transfers data from one database to another
83  *
84  * @author Thomas Mueller (Hypersonic SQL Group)
85  * @version 1.7.2
86  * @since Hypersonic SQL
87  */

88 class TransferTable implements Serializable JavaDoc {
89
90     Hashtable JavaDoc hTypes;
91     DataAccessPoint sourceDb;
92     DataAccessPoint destDb;
93     SQLStatements Stmts = null;
94     Traceable tracer;
95
96     TransferTable(DataAccessPoint src, String JavaDoc name, String JavaDoc schema,
97                   String JavaDoc 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 JavaDoc _Schema, DataAccessPoint dest) throws Exception JavaDoc {
122
123         destDb = dest;
124
125         dest.helper.setSchema(_Schema);
126     }
127
128     /**
129      * extractTableStructure
130      *
131      * @param Source
132      * @param Destination
133      * @throws Exception
134      */

135     void extractTableStructure(DataAccessPoint Source,
136                                DataAccessPoint Destination) throws Exception JavaDoc {
137         initTypes();
138         Source.getTableStructure(this, Destination);
139     }
140
141     /**
142      * Method declaration
143      *
144      *
145      * @param t
146      *
147      * @throws SQLException
148      */

149     void transferStructure() throws Exception JavaDoc {
150
151         String JavaDoc Statement = new String JavaDoc("");
152
153         if (destDb.helper.needTransferTransaction()) {
154             try {
155                 destDb.setAutoCommit(false);
156             } catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc e) {
292             try {
293                 if (!destDb.getAutoCommit()) {
294                     destDb.rollback();
295                 }
296             } catch (Exception JavaDoc e1) {}
297
298             throw (e);
299         }
300
301         if (!destDb.getAutoCommit()) {
302             destDb.commit();
303
304             try {
305                 destDb.setAutoCommit(true);
306             } catch (Exception JavaDoc e) {}
307         }
308     }
309
310     void transferData(int iMaxRows) throws Exception JavaDoc, SQLException JavaDoc {
311
312         if (destDb.helper.needTransferTransaction()) {
313             try {
314                 destDb.setAutoCommit(false);
315             } catch (Exception JavaDoc e) {}
316         }
317
318         try {
319             if (Stmts.bInsert) {
320                 if (destDb.helper.needTransferTransaction()) {
321                     try {
322                         destDb.setAutoCommit(false);
323                     } catch (Exception JavaDoc 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 JavaDoc e) {}
343                 }
344             }
345         } catch (Exception JavaDoc e) {
346             try {
347                 if (!destDb.getAutoCommit()) {
348                     destDb.rollback();
349                 }
350             } catch (Exception JavaDoc e1) {}
351
352             throw (e);
353         }
354
355         if (!destDb.getAutoCommit()) {
356             destDb.commit();
357
358             try {
359                 destDb.setAutoCommit(true);
360             } catch (Exception JavaDoc e) {}
361         }
362     }
363
364     /**
365      * Method declaration
366      *
367      *
368      * @param t
369      *
370      * @throws SQLException
371      */

372     void transferAlter() throws Exception JavaDoc {
373
374         String JavaDoc Statement = new String JavaDoc("");
375
376         if (destDb.helper.needTransferTransaction()) {
377             try {
378                 destDb.setAutoCommit(false);
379             } catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc e) {
456             try {
457                 if (!destDb.getAutoCommit()) {
458                     destDb.rollback();
459                 }
460             } catch (Exception JavaDoc e1) {}
461
462             throw (e);
463         }
464
465         if (!destDb.getAutoCommit()) {
466             destDb.commit();
467
468             try {
469                 destDb.setAutoCommit(true);
470             } catch (Exception JavaDoc e) {}
471         }
472     }
473
474     private void initTypes() throws SQLException JavaDoc {
475
476         if (hTypes != null) {
477             return;
478         }
479
480         hTypes = destDb.helper.getSupportedTypes();
481     }
482 }
483
Popular Tags