Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/hsqldb/util/PostgresTransferHelper.java


1   /* Copyright (c) 2001-2002, 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.*;
35  
36  // fredt@users 20020215 - patch 516309 by Nicolas Bazin - transfer PostgresSQL
37  // sqlbob@users 20020325 - patch 1.7.0 - reengineering
38  
39  /**
40   * Conversions from PostgresSQL databases
41   *
42   * @author Nichola Bazin
43   * @version 1.7.0
44   */
45  class PostgresTransferHelper extends TransferHelper {
46  
47      private final int PostgreSQL = 0;
48      private final int HSQLDB     = 1;
49      String[][]        Funcs      = {
50          {
51              "now()", "\'now\'"
52          }
53      };
54  
55      PostgresTransferHelper() {
56          super();
57      }
58  
59      PostgresTransferHelper(TransferDb database, Traceable t, String q) {
60          super(database, t, q);
61      }
62  
63      int convertToType(int type) {
64  
65          if (type == Types.DECIMAL) {
66              type = Types.NUMERIC;
67  
68              tracer.trace("Converted DECIMAL to NUMERIC");
69          }
70  
71          return (type);
72      }
73  
74      String fixupColumnDefRead(TransferTable t, ResultSetMetaData meta,
75                                String columnType, ResultSet columnDesc,
76                                int columnIndex) throws SQLException {
77  
78          String SeqName   = new String("_" + columnDesc.getString(4) + "_seq");
79          int    spaceleft = 31 - SeqName.length();
80  
81          if (t.Stmts.sDestTable.length() > spaceleft) {
82              SeqName = t.Stmts.sDestTable.substring(0, spaceleft) + SeqName;
83          } else {
84              SeqName = t.Stmts.sDestTable + SeqName;
85          }
86  
87          String CompareString = "nextval(\'\"" + SeqName + "\"\'";
88  
89          if (columnType.indexOf(CompareString) >= 0) {
90              /*
91              ** We just found a increment
92              */
93              columnType = "SERIAL";
94          }
95  
96          for (int Idx = 0; Idx < Funcs.length; Idx++) {
97              String PostgreSQL_func = Funcs[Idx][PostgreSQL];
98              int    iStartPos       = columnType.indexOf(PostgreSQL_func);
99  
100             if (iStartPos >= 0) {
101                 String NewColumnType = columnType.substring(0, iStartPos);
102 
103                 NewColumnType += Funcs[Idx][HSQLDB];
104                 NewColumnType +=
105                     columnType.substring(iStartPos
106                                          + PostgreSQL_func.length());
107                 columnType = NewColumnType;
108             }
109         }
110 
111         return (columnType);
112     }
113 
114     String fixupColumnDefWrite(TransferTable t, ResultSetMetaData meta,
115                                String columnType, ResultSet columnDesc,
116                                int columnIndex) throws SQLException {
117 
118         if (columnType.equals("SERIAL")) {
119             String SeqName = new String("_" + columnDesc.getString(4)
120                                         + "_seq");
121             int spaceleft = 31 - SeqName.length();
122 
123             if (t.Stmts.sDestTable.length() > spaceleft) {
124                 SeqName = t.Stmts.sDestTable.substring(0, spaceleft)
125                           + SeqName;
126             } else {
127                 SeqName = t.Stmts.sDestTable + SeqName;
128             }
129 
130             String DropSequence = "DROP SEQUENCE " + SeqName + ";";
131 
132             t.Stmts.sDestDrop += DropSequence;
133         }
134 
135         for (int Idx = 0; Idx < Funcs.length; Idx++) {
136             String HSQLDB_func = Funcs[Idx][HSQLDB];
137             int    iStartPos   = columnType.indexOf(HSQLDB_func);
138 
139             if (iStartPos >= 0) {
140                 String NewColumnType = columnType.substring(0, 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     void endDataTransfer() {
160 
161         try {
162             db.commit();
163             db.execute("VACUUM ANALYZE");
164         } catch (Exception e) {}
165     }
166 }