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: import java.sql.Types;
037:
038: // fredt@users 20020215 - patch 516309 by Nicolas Bazin - transfer PostgresSQL
039: // sqlbob@users 20020325 - patch 1.7.0 - reengineering
040:
041: /**
042: * Conversions from PostgresSQL databases
043: *
044: * @author Nichola Bazin
045: * @version 1.7.0
046: */
047: class PostgresTransferHelper extends TransferHelper {
048:
049: private final int PostgreSQL = 0;
050: private final int HSQLDB = 1;
051: String[][] Funcs = { { "now()", "\'now\'" } };
052:
053: PostgresTransferHelper() {
054: super ();
055: }
056:
057: PostgresTransferHelper(TransferDb database, Traceable t, String q) {
058: super (database, t, q);
059: }
060:
061: int convertToType(int type) {
062:
063: if (type == Types.DECIMAL) {
064: type = Types.NUMERIC;
065:
066: tracer.trace("Converted DECIMAL to NUMERIC");
067: }
068:
069: return (type);
070: }
071:
072: String fixupColumnDefRead(TransferTable t, ResultSetMetaData meta,
073: String columnType, ResultSet columnDesc, int columnIndex)
074: throws SQLException {
075:
076: String SeqName = new String("_" + columnDesc.getString(4)
077: + "_seq");
078: int spaceleft = 31 - SeqName.length();
079:
080: if (t.Stmts.sDestTable.length() > spaceleft) {
081: SeqName = t.Stmts.sDestTable.substring(0, spaceleft)
082: + SeqName;
083: } else {
084: SeqName = t.Stmts.sDestTable + SeqName;
085: }
086:
087: String CompareString = "nextval(\'\"" + SeqName + "\"\'";
088:
089: if (columnType.indexOf(CompareString) >= 0) {
090:
091: // We just found a increment
092: columnType = "SERIAL";
093: }
094:
095: for (int Idx = 0; Idx < Funcs.length; Idx++) {
096: String PostgreSQL_func = Funcs[Idx][PostgreSQL];
097: int iStartPos = columnType.indexOf(PostgreSQL_func);
098:
099: if (iStartPos >= 0) {
100: String NewColumnType = columnType.substring(0,
101: iStartPos);
102:
103: NewColumnType += Funcs[Idx][HSQLDB];
104: NewColumnType += columnType.substring(iStartPos
105: + PostgreSQL_func.length());
106: columnType = NewColumnType;
107: }
108: }
109:
110: return (columnType);
111: }
112:
113: String fixupColumnDefWrite(TransferTable t, ResultSetMetaData meta,
114: String columnType, ResultSet columnDesc, int columnIndex)
115: throws SQLException {
116:
117: if (columnType.equals("SERIAL")) {
118: String SeqName = new String("_" + columnDesc.getString(4)
119: + "_seq");
120: int spaceleft = 31 - SeqName.length();
121:
122: if (t.Stmts.sDestTable.length() > spaceleft) {
123: SeqName = t.Stmts.sDestTable.substring(0, spaceleft)
124: + SeqName;
125: } else {
126: SeqName = t.Stmts.sDestTable + SeqName;
127: }
128:
129: String DropSequence = "DROP SEQUENCE " + SeqName + ";";
130:
131: t.Stmts.sDestDrop += DropSequence;
132: }
133:
134: for (int Idx = 0; Idx < Funcs.length; Idx++) {
135: String HSQLDB_func = Funcs[Idx][HSQLDB];
136: int iStartPos = columnType.indexOf(HSQLDB_func);
137:
138: if (iStartPos >= 0) {
139: String NewColumnType = columnType.substring(0,
140: iStartPos);
141:
142: NewColumnType += Funcs[Idx][PostgreSQL];
143: NewColumnType += columnType.substring(iStartPos
144: + HSQLDB_func.length());
145: columnType = NewColumnType;
146: }
147: }
148:
149: return (columnType);
150: }
151:
152: void beginDataTransfer() {
153:
154: try {
155: db.setAutoCommit(false);
156: } catch (Exception e) {
157: }
158: }
159:
160: void endDataTransfer() {
161:
162: try {
163: db.commit();
164: db.execute("VACUUM ANALYZE");
165: } catch (Exception e) {
166: }
167: }
168: }
|