KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* Copyright (c) 2001-2005, 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.util;
33
34 import java.sql.ResultSet JavaDoc;
35 import java.sql.ResultSetMetaData JavaDoc;
36 import java.sql.SQLException JavaDoc;
37
38 // brian.porter@siteforce.de 20020703 - make sure date is loaded in the required format
39
// Stephan Frind 20040508 - improvements
40

41 /**
42  * Conversions from Oracle databases
43  *
44  * @author Nichola Bazin
45  * @version 1.7.0
46  */

47 class OracleTransferHelper extends TransferHelper {
48
49     private final int ORACLE = 0;
50     private final int HSQLDB = 1;
51     String JavaDoc[][] Funcs = {
52         {
53             "now()", "\'now\'"
54         }
55     };
56
57     OracleTransferHelper() {
58
59         super();
60
61         System.out.println("simple init of OracleTransferHelper");
62     }
63
64     OracleTransferHelper(TransferDb database, Traceable t, String JavaDoc q) {
65         super(database, t, q);
66     }
67
68     void set(TransferDb database, Traceable t, String JavaDoc q) {
69
70         super.set(database, t, q);
71
72         // set the Dateformat for our connection
73
String JavaDoc dateFormatStmnt =
74             "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'";
75
76         System.out.println("dateFormatStmnt: " + dateFormatStmnt);
77
78         try {
79             tracer.trace("Executing " + dateFormatStmnt);
80             database.execute(dateFormatStmnt);
81         } catch (Exception JavaDoc e) {
82             tracer.trace("Ignoring error " + e.getMessage());
83             System.out.println("Ignoring error " + e.getMessage());
84         }
85     }
86
87     String JavaDoc fixupColumnDefRead(TransferTable t, ResultSetMetaData JavaDoc meta,
88                               String JavaDoc columnType, ResultSet JavaDoc columnDesc,
89                               int columnIndex) throws SQLException JavaDoc {
90         return fixupColumnDefRead(t.Stmts.sDestTable, meta, columnType,
91                                   columnDesc, columnIndex);
92     }
93
94     String JavaDoc fixupColumnDefWrite(TransferTable t, ResultSetMetaData JavaDoc meta,
95                                String JavaDoc columnType, ResultSet JavaDoc columnDesc,
96                                int columnIndex) throws SQLException JavaDoc {
97
98         if (columnType.equals("SERIAL")) {
99             String JavaDoc SeqName = new String JavaDoc("_" + columnDesc.getString(4)
100                                         + "_seq");
101             int spaceleft = 31 - SeqName.length();
102
103             if (t.Stmts.sDestTable.length() > spaceleft) {
104                 SeqName = t.Stmts.sDestTable.substring(0, spaceleft)
105                           + SeqName;
106             } else {
107                 SeqName = t.Stmts.sDestTable + SeqName;
108             }
109
110             String JavaDoc DropSequence = "DROP SEQUENCE " + SeqName + ";";
111
112             t.Stmts.sDestDrop += DropSequence;
113         }
114
115         for (int Idx = 0; Idx < Funcs.length; Idx++) {
116             String JavaDoc HSQLDB_func = Funcs[Idx][HSQLDB];
117             int iStartPos = columnType.indexOf(HSQLDB_func);
118
119             if (iStartPos >= 0) {
120                 String JavaDoc NewColumnType = columnType.substring(0, iStartPos);
121
122                 NewColumnType += Funcs[Idx][ORACLE];
123                 NewColumnType += columnType.substring(iStartPos
124                                                       + HSQLDB_func.length());
125                 columnType = NewColumnType;
126             }
127         }
128
129         return (columnType);
130     }
131
132     void beginDataTransfer() {
133
134         try {
135             db.setAutoCommit(false);
136         } catch (Exception JavaDoc e) {}
137     }
138
139     void endDataTransfer() {
140
141         try {
142             db.commit();
143         } catch (Exception JavaDoc e) {}
144     }
145
146     String JavaDoc fixupColumnDefRead(String JavaDoc aTableName, ResultSetMetaData JavaDoc meta,
147                               String JavaDoc columnType, ResultSet JavaDoc columnDesc,
148                               int columnIndex) throws SQLException JavaDoc {
149
150         String JavaDoc SeqName = new String JavaDoc("_" + columnDesc.getString(4) + "_seq");
151         int spaceleft = 31 - SeqName.length();
152
153         if (aTableName.length() > spaceleft) {
154             SeqName = aTableName.substring(0, spaceleft) + SeqName;
155         } else {
156             SeqName = aTableName + SeqName;
157         }
158
159         String JavaDoc CompareString = "nextval(\'\"" + SeqName + "\"\'";
160
161         if (columnType.indexOf(CompareString) >= 0) {
162
163             // We just found a increment
164
columnType = "SERIAL";
165         }
166
167         for (int Idx = 0; Idx < Funcs.length; Idx++) {
168             String JavaDoc ORACLE_func = Funcs[Idx][ORACLE];
169             int iStartPos = columnType.indexOf(ORACLE_func);
170
171             if (iStartPos >= 0) {
172                 String JavaDoc NewColumnType = columnType.substring(0, iStartPos);
173
174                 NewColumnType += Funcs[Idx][HSQLDB];
175                 NewColumnType += columnType.substring(iStartPos
176                                                       + ORACLE_func.length());
177                 columnType = NewColumnType;
178             }
179         }
180
181         return (columnType);
182     }
183 }
184
Popular Tags