KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.sql.Types JavaDoc;
38
39 // fredt@users 20020215 - patch 516309 by Nicolas Bazin - transfer PostgresSQL
40
// sqlbob@users 20020325 - patch 1.7.0 - reengineering
41

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

48 class PostgresTransferHelper extends TransferHelper {
49
50     private final int PostgreSQL = 0;
51     private final int HSQLDB = 1;
52     String JavaDoc[][] Funcs = {
53         {
54             "now()", "\'now\'"
55         }
56     };
57
58     PostgresTransferHelper() {
59         super();
60     }
61
62     PostgresTransferHelper(TransferDb database, Traceable t, String JavaDoc q) {
63         super(database, t, q);
64     }
65
66     int convertToType(int type) {
67
68         if (type == Types.DECIMAL) {
69             type = Types.NUMERIC;
70
71             tracer.trace("Converted DECIMAL to NUMERIC");
72         }
73
74         return (type);
75     }
76
77     String JavaDoc fixupColumnDefRead(TransferTable t, ResultSetMetaData JavaDoc meta,
78                               String JavaDoc columnType, ResultSet JavaDoc columnDesc,
79                               int columnIndex) throws SQLException JavaDoc {
80
81         String JavaDoc SeqName = new String JavaDoc("_" + columnDesc.getString(4) + "_seq");
82         int spaceleft = 31 - SeqName.length();
83
84         if (t.Stmts.sDestTable.length() > spaceleft) {
85             SeqName = t.Stmts.sDestTable.substring(0, spaceleft) + SeqName;
86         } else {
87             SeqName = t.Stmts.sDestTable + SeqName;
88         }
89
90         String JavaDoc CompareString = "nextval(\'\"" + SeqName + "\"\'";
91
92         if (columnType.indexOf(CompareString) >= 0) {
93
94             // We just found a increment
95
columnType = "SERIAL";
96         }
97
98         for (int Idx = 0; Idx < Funcs.length; Idx++) {
99             String JavaDoc PostgreSQL_func = Funcs[Idx][PostgreSQL];
100             int iStartPos = columnType.indexOf(PostgreSQL_func);
101
102             if (iStartPos >= 0) {
103                 String JavaDoc NewColumnType = columnType.substring(0, iStartPos);
104
105                 NewColumnType += Funcs[Idx][HSQLDB];
106                 NewColumnType +=
107                     columnType.substring(iStartPos
108                                          + PostgreSQL_func.length());
109                 columnType = NewColumnType;
110             }
111         }
112
113         return (columnType);
114     }
115
116     String JavaDoc fixupColumnDefWrite(TransferTable t, ResultSetMetaData JavaDoc meta,
117                                String JavaDoc columnType, ResultSet JavaDoc columnDesc,
118                                int columnIndex) throws SQLException JavaDoc {
119
120         if (columnType.equals("SERIAL")) {
121             String JavaDoc SeqName = new String JavaDoc("_" + columnDesc.getString(4)
122                                         + "_seq");
123             int spaceleft = 31 - SeqName.length();
124
125             if (t.Stmts.sDestTable.length() > spaceleft) {
126                 SeqName = t.Stmts.sDestTable.substring(0, spaceleft)
127                           + SeqName;
128             } else {
129                 SeqName = t.Stmts.sDestTable + SeqName;
130             }
131
132             String JavaDoc DropSequence = "DROP SEQUENCE " + SeqName + ";";
133
134             t.Stmts.sDestDrop += DropSequence;
135         }
136
137         for (int Idx = 0; Idx < Funcs.length; Idx++) {
138             String JavaDoc HSQLDB_func = Funcs[Idx][HSQLDB];
139             int iStartPos = columnType.indexOf(HSQLDB_func);
140
141             if (iStartPos >= 0) {
142                 String JavaDoc NewColumnType = columnType.substring(0, iStartPos);
143
144                 NewColumnType += Funcs[Idx][PostgreSQL];
145                 NewColumnType += columnType.substring(iStartPos
146                                                       + HSQLDB_func.length());
147                 columnType = NewColumnType;
148             }
149         }
150
151         return (columnType);
152     }
153
154     void beginDataTransfer() {
155
156         try {
157             db.setAutoCommit(false);
158         } catch (Exception JavaDoc e) {}
159     }
160
161     void endDataTransfer() {
162
163         try {
164             db.commit();
165             db.execute("VACUUM ANALYZE");
166         } catch (Exception JavaDoc e) {}
167     }
168 }
169
Popular Tags