1 30 31 32 package org.hsqldb.util; 33 34 import java.sql.ResultSet ; 35 import java.sql.ResultSetMetaData ; 36 import java.sql.SQLException ; 37 import java.sql.Types ; 38 39 42 48 class PostgresTransferHelper extends TransferHelper { 49 50 private final int PostgreSQL = 0; 51 private final int HSQLDB = 1; 52 String [][] Funcs = { 53 { 54 "now()", "\'now\'" 55 } 56 }; 57 58 PostgresTransferHelper() { 59 super(); 60 } 61 62 PostgresTransferHelper(TransferDb database, Traceable t, String 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 fixupColumnDefRead(TransferTable t, ResultSetMetaData meta, 78 String columnType, ResultSet columnDesc, 79 int columnIndex) throws SQLException { 80 81 String SeqName = new String ("_" + 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 CompareString = "nextval(\'\"" + SeqName + "\"\'"; 91 92 if (columnType.indexOf(CompareString) >= 0) { 93 94 columnType = "SERIAL"; 96 } 97 98 for (int Idx = 0; Idx < Funcs.length; Idx++) { 99 String PostgreSQL_func = Funcs[Idx][PostgreSQL]; 100 int iStartPos = columnType.indexOf(PostgreSQL_func); 101 102 if (iStartPos >= 0) { 103 String 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 fixupColumnDefWrite(TransferTable t, ResultSetMetaData meta, 117 String columnType, ResultSet columnDesc, 118 int columnIndex) throws SQLException { 119 120 if (columnType.equals("SERIAL")) { 121 String SeqName = new String ("_" + 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 DropSequence = "DROP SEQUENCE " + SeqName + ";"; 133 134 t.Stmts.sDestDrop += DropSequence; 135 } 136 137 for (int Idx = 0; Idx < Funcs.length; Idx++) { 138 String HSQLDB_func = Funcs[Idx][HSQLDB]; 139 int iStartPos = columnType.indexOf(HSQLDB_func); 140 141 if (iStartPos >= 0) { 142 String 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 e) {} 159 } 160 161 void endDataTransfer() { 162 163 try { 164 db.commit(); 165 db.execute("VACUUM ANALYZE"); 166 } catch (Exception e) {} 167 } 168 } 169 | Popular Tags |