001: /* Copyright (c) 2001-2005, The HSQL Development 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 HSQL Development 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 HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
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:
031: package org.hsqldb.util;
032:
033: import java.sql.ResultSet;
034: import java.sql.ResultSetMetaData;
035: import java.sql.SQLException;
036:
037: // brian.porter@siteforce.de 20020703 - make sure date is loaded in the required format
038: // Stephan Frind 20040508 - improvements
039:
040: /**
041: * Conversions from Oracle databases
042: *
043: * @author Nichola Bazin
044: * @version 1.7.0
045: */
046: class OracleTransferHelper extends TransferHelper {
047:
048: private final int ORACLE = 0;
049: private final int HSQLDB = 1;
050: String[][] Funcs = { { "now()", "\'now\'" } };
051:
052: OracleTransferHelper() {
053:
054: super ();
055:
056: System.out.println("simple init of OracleTransferHelper");
057: }
058:
059: OracleTransferHelper(TransferDb database, Traceable t, String q) {
060: super (database, t, q);
061: }
062:
063: void set(TransferDb database, Traceable t, String q) {
064:
065: super .set(database, t, q);
066:
067: // set the Dateformat for our connection
068: String dateFormatStmnt = "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'";
069:
070: System.out.println("dateFormatStmnt: " + dateFormatStmnt);
071:
072: try {
073: tracer.trace("Executing " + dateFormatStmnt);
074: database.execute(dateFormatStmnt);
075: } catch (Exception e) {
076: tracer.trace("Ignoring error " + e.getMessage());
077: System.out.println("Ignoring error " + e.getMessage());
078: }
079: }
080:
081: String fixupColumnDefRead(TransferTable t, ResultSetMetaData meta,
082: String columnType, ResultSet columnDesc, int columnIndex)
083: throws SQLException {
084: return fixupColumnDefRead(t.Stmts.sDestTable, meta, columnType,
085: columnDesc, columnIndex);
086: }
087:
088: String fixupColumnDefWrite(TransferTable t, ResultSetMetaData meta,
089: String columnType, ResultSet columnDesc, int columnIndex)
090: throws SQLException {
091:
092: if (columnType.equals("SERIAL")) {
093: String SeqName = new String("_" + columnDesc.getString(4)
094: + "_seq");
095: int spaceleft = 31 - SeqName.length();
096:
097: if (t.Stmts.sDestTable.length() > spaceleft) {
098: SeqName = t.Stmts.sDestTable.substring(0, spaceleft)
099: + SeqName;
100: } else {
101: SeqName = t.Stmts.sDestTable + SeqName;
102: }
103:
104: String DropSequence = "DROP SEQUENCE " + SeqName + ";";
105:
106: t.Stmts.sDestDrop += DropSequence;
107: }
108:
109: for (int Idx = 0; Idx < Funcs.length; Idx++) {
110: String HSQLDB_func = Funcs[Idx][HSQLDB];
111: int iStartPos = columnType.indexOf(HSQLDB_func);
112:
113: if (iStartPos >= 0) {
114: String NewColumnType = columnType.substring(0,
115: iStartPos);
116:
117: NewColumnType += Funcs[Idx][ORACLE];
118: NewColumnType += columnType.substring(iStartPos
119: + HSQLDB_func.length());
120: columnType = NewColumnType;
121: }
122: }
123:
124: return (columnType);
125: }
126:
127: void beginDataTransfer() {
128:
129: try {
130: db.setAutoCommit(false);
131: } catch (Exception e) {
132: }
133: }
134:
135: void endDataTransfer() {
136:
137: try {
138: db.commit();
139: } catch (Exception e) {
140: }
141: }
142:
143: String fixupColumnDefRead(String aTableName,
144: ResultSetMetaData meta, String columnType,
145: ResultSet columnDesc, int columnIndex) throws SQLException {
146:
147: String SeqName = new String("_" + columnDesc.getString(4)
148: + "_seq");
149: int spaceleft = 31 - SeqName.length();
150:
151: if (aTableName.length() > spaceleft) {
152: SeqName = aTableName.substring(0, spaceleft) + SeqName;
153: } else {
154: SeqName = aTableName + SeqName;
155: }
156:
157: String CompareString = "nextval(\'\"" + SeqName + "\"\'";
158:
159: if (columnType.indexOf(CompareString) >= 0) {
160:
161: // We just found a increment
162: columnType = "SERIAL";
163: }
164:
165: for (int Idx = 0; Idx < Funcs.length; Idx++) {
166: String ORACLE_func = Funcs[Idx][ORACLE];
167: int iStartPos = columnType.indexOf(ORACLE_func);
168:
169: if (iStartPos >= 0) {
170: String NewColumnType = columnType.substring(0,
171: iStartPos);
172:
173: NewColumnType += Funcs[Idx][HSQLDB];
174: NewColumnType += columnType.substring(iStartPos
175: + ORACLE_func.length());
176: columnType = NewColumnType;
177: }
178: }
179:
180: return (columnType);
181: }
182: }
|