001 /** 002 * Copyright (c) 2003 Daffodil Software Ltd all rights reserved, 003 * Modifications Copyright (c) 2008 Regiscope Digital Imaging Co, LLC, All rights reserved. 004 * This program is free software; you can redistribute it and/or modify 005 * it under the terms of version 2 of the GNU General Public License as 006 * published by the Free Software Foundation. 007 * There are special exceptions to the terms and conditions of the GPL 008 * as it is applied to this software. See the GNU General Public License for more details. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with this program; if not, write to the Free Software 017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018 */ 019 020 package org.dbreplicator.replication; 021 022 import java.sql.*; 023 024 import org.dbreplicator.replication.DBHandler.*; 025 import org.apache.log4j.Logger; 026 import java.io.*; 027 028 /** 029 * This class is used for finding out the database type. And according to the 030 * identified database the handeling is done differently. 031 */ 032 033 public class Utility { 034 035 public static int insertCount = 0; 036 public static final int DataBase_General = 0; 037 public static final int DataBase_DaffodilDB = 1; 038 public static final int DataBase_Oracle = 2; 039 public static final int DataBase_SqlServer = 3; 040 public static final int DataBase_PointBase = 4; 041 public static final int DataBase_Cloudscape = 5; 042 public static final int DataBase_PostgreSQL = 6; 043 public static final int DataBase_DB2 = 7; 044 public static final int DataBase_Sybase = 8; 045 public static final int DataBase_Firebird = 9; 046 public static final int DataBase_MySQL = 10; 047 048 public static final int CommonMetaDataInfo = 1; 049 public static final int pgMetaDataInfo = 2; 050 public static final int CloudScapeMetaDataInfo = 3; 051 052 private static String Daffodil_ProductName = "DaffodilDB"; 053 private static String Oracle_ProductName = "Oracle"; 054 private static String SqlServer_ProductName = "Microsoft SQL Server"; 055 private static String PointBase_ProductName = "PointBase"; 056 private static String Cloudscape_ProductName = "Apache Derby"; 057 private static String PostgreSQL_ProductName = "PostgreSQL"; 058 private static String DB2_ProductName = "DB2/NT"; 059 060 //New String to handle DB2 on an AIX Platform -- Scott Shealy 3/21/2005 061 private static String DB2_6000_ProductName = "DB2/6000"; 062 private static String DB2_AS_400_ProductName = "DB2 UDB"; 063 064 private static String Sybase_ASE = "Adaptive Server Enterprise"; 065 private static String Sybase_ASA = "Adaptive Server AnyWhere"; 066 public static String FireBird_ProductName = "Firebird"; 067 public static String MySQL_ProductName = "MySQL"; 068 069 070 public static boolean createTransactionLogFile = true; 071 072 protected static Logger log = Logger.getLogger(Utility.class.getName()); 073 074 /** 075 * This method returns the perticular object of Handler for perticular 076 * database. So that different database operations can be handeled 077 * differently. 078 * 079 * @param connectionPool 080 * @param pubsubName 081 * @return DatabaseHandler 082 * @throws RepException 083 */ 084 085 public static AbstractDataBaseHandler getDatabaseHandler(ConnectionPool 086 connectionPool, String pubsubName) throws RepException { 087 Connection con = connectionPool.getConnection(pubsubName); 088 String vendorName = getVendorName(con); 089 connectionPool.returnConnection(con); 090 log.info(vendorName); 091 if (vendorName.equalsIgnoreCase(PointBase_ProductName)) { 092 return new PointBaseHandler(connectionPool); 093 } 094 else if (vendorName.equalsIgnoreCase(Daffodil_ProductName)) { 095 return new DaffodilDBHandler(connectionPool); 096 } 097 else if (vendorName.equalsIgnoreCase(Oracle_ProductName)) { 098 return new OracleHandler(connectionPool); 099 } 100 else if (vendorName.equalsIgnoreCase(Cloudscape_ProductName)) { 101 return new CloudScapeHandler(connectionPool); 102 } 103 else if (vendorName.equalsIgnoreCase(SqlServer_ProductName)) { 104 return new SQLServerHandler(connectionPool); 105 } 106 else if (vendorName.equalsIgnoreCase(PostgreSQL_ProductName)) { 107 return new PostgreSQLHandler(connectionPool); 108 } 109 else if (vendorName.equalsIgnoreCase(Sybase_ASA) || 110 vendorName.equalsIgnoreCase(Sybase_ASE)) { 111 return new SybaseHandler(connectionPool); 112 } 113 114 else if (vendorName.toUpperCase().startsWith("DB2")) { 115 return new DB2Handler(connectionPool); 116 } 117 //New String to handle DB2 on an AIX Platform -- Scott Shealy 3/21/2005 118 else if (vendorName.equalsIgnoreCase(DB2_6000_ProductName)) { 119 return new DB2Handler(connectionPool); 120 } 121 else if (vendorName.equalsIgnoreCase(FireBird_ProductName)) { 122 return new FireBirdDatabaseHandler(connectionPool); 123 } 124 else if (vendorName.equalsIgnoreCase(MySQL_ProductName)) { 125 return new MYSQLHandler(connectionPool); 126 } 127 128 else { 129 return new GeneralDataBaseHandler(connectionPool); 130 } 131 } 132 133 public static AbstractDataBaseHandler getDatabaseHandler(ConnectionPool 134 connectionPool, Connection connection) throws RepException { 135 String vendorName = getVendorName(connection); 136 if (vendorName.equalsIgnoreCase(PointBase_ProductName)) { 137 return new PointBaseHandler(connectionPool); 138 } 139 else if (vendorName.equalsIgnoreCase(Daffodil_ProductName)) { 140 return new DaffodilDBHandler(connectionPool); 141 } 142 else if (vendorName.equalsIgnoreCase(Oracle_ProductName)) { 143 return new OracleHandler(connectionPool); 144 } 145 else if (vendorName.equalsIgnoreCase(Cloudscape_ProductName)) { 146 return new CloudScapeHandler(connectionPool); 147 } 148 else if (vendorName.equalsIgnoreCase(SqlServer_ProductName)) { 149 return new SQLServerHandler(connectionPool); 150 } 151 else if (vendorName.equalsIgnoreCase(Sybase_ASA) || 152 vendorName.equalsIgnoreCase(Sybase_ASE)) { 153 return new SybaseHandler(connectionPool); 154 } 155 else if (vendorName.equalsIgnoreCase(PostgreSQL_ProductName)) { 156 return new PostgreSQLHandler(connectionPool); 157 } 158 else if (vendorName.toUpperCase().equalsIgnoreCase("DB2")) { 159 return new DB2Handler(connectionPool); 160 } 161 //New String to handle DB2 on an AIX Platform -- Scott Shealy 3/21/2005 162 else if (vendorName.equalsIgnoreCase(DB2_6000_ProductName)) { 163 return new DB2Handler(connectionPool); 164 } 165 else if (vendorName.equalsIgnoreCase(MySQL_ProductName)) { 166 return new MYSQLHandler(connectionPool); 167 } 168 169 else { 170 return new GeneralDataBaseHandler(connectionPool); 171 } 172 } 173 174 public static MetaDataInfo getDatabaseMataData(Connection connection) throws 175 RepException { 176 String vendorName = getVendorName(connection); 177 MetaDataInfo metaData = null; 178 if (vendorName.equalsIgnoreCase(PointBase_ProductName)) { 179 metaData = new CommonMetaDataInfo(connection); 180 } 181 else if (vendorName.equalsIgnoreCase(Daffodil_ProductName)) { 182 metaData = new CommonMetaDataInfo(connection); 183 } 184 else if (vendorName.equalsIgnoreCase(Oracle_ProductName)) { 185 metaData = new CommonMetaDataInfo(connection); 186 } 187 else if (vendorName.equalsIgnoreCase(Cloudscape_ProductName)) { 188 metaData = new CloudscapeMataDataInfo(connection); 189 } 190 else if (vendorName.equalsIgnoreCase(SqlServer_ProductName)) { 191 metaData = new CommonMetaDataInfo(connection); 192 } 193 else if (vendorName.equalsIgnoreCase(PostgreSQL_ProductName)) { 194 metaData = new pgMetaDataInfo(connection); 195 } 196 else if (vendorName.toUpperCase().equalsIgnoreCase("DB2")) { 197 metaData = new CommonMetaDataInfo(connection); 198 } 199 else if (vendorName.toUpperCase().equalsIgnoreCase( 200 "Adaptive Server Anywhere") || 201 vendorName.toUpperCase().equalsIgnoreCase( 202 "Adaptive Server Enterprise")) 203 204 { 205 206 metaData = new pgMetaDataInfo(connection); 207 } 208 209 //New String to handle DB2 on an AIX Platform -- Scott Shealy 3/21/2005 210 else if (vendorName.equalsIgnoreCase(DB2_6000_ProductName)) { 211 metaData = new CommonMetaDataInfo(connection); 212 } 213 else if (vendorName.equalsIgnoreCase(DB2_AS_400_ProductName)) { 214 metaData = new CommonMetaDataInfo(connection); 215 } 216 217 else if (vendorName.equalsIgnoreCase("Firebird")) { 218 metaData = new CommonMetaDataInfo(connection); 219 } 220 else if (vendorName.equalsIgnoreCase(MySQL_ProductName)) { 221 metaData = new CommonMetaDataInfo(connection); 222 } 223 224 else { 225 metaData = new CommonMetaDataInfo(connection); 226 } 227 return metaData; 228 } 229 230 /** 231 * This method was implemented for the same reason to identify the databse. 232 * but it is different from getDataBaseHandler because it returns the vendorType 233 * by checking the vendor name. This string is passed at the subscriber end where 234 * by this vendor type a proper handler is chosen. 235 * 236 * @param connectionPool 237 * @param pubsubName 238 * @return string VendorType 239 * @throws RepException 240 */ 241 242 public static int getVendorType(ConnectionPool connectionPool, 243 String pubsubName) throws RepException { 244 Connection con = connectionPool.getConnection(pubsubName); 245 try { 246 String vendorName = getVendorName(con); 247 if (vendorName.equalsIgnoreCase(PointBase_ProductName)) { 248 return DataBase_SqlServer; 249 } 250 else if (vendorName.equalsIgnoreCase(Daffodil_ProductName)) { 251 return DataBase_DaffodilDB; 252 } 253 else if (vendorName.equalsIgnoreCase(Oracle_ProductName)) { 254 return DataBase_Oracle; 255 } 256 // else if(vendorName.equalsIgnoreCase("DBMS:cloudscape")) 257 else if (vendorName.equalsIgnoreCase(Cloudscape_ProductName)) { 258 return DataBase_Cloudscape; 259 } 260 else if (vendorName.equalsIgnoreCase(SqlServer_ProductName)) { 261 return DataBase_SqlServer; 262 } 263 else if (vendorName.equalsIgnoreCase(PostgreSQL_ProductName)) { 264 return DataBase_PostgreSQL; 265 } 266 else if (vendorName.toUpperCase().startsWith("DB2")) { 267 return DataBase_DB2; 268 } 269 else if (vendorName.equalsIgnoreCase(DB2_6000_ProductName)) { 270 return DataBase_DB2; 271 } 272 else if (vendorName.equalsIgnoreCase(DB2_AS_400_ProductName)) { 273 return DataBase_DB2; 274 } 275 else if (vendorName.toUpperCase().equalsIgnoreCase( 276 "Adaptive Server Anywhere") || 277 vendorName.toUpperCase().equalsIgnoreCase( 278 "Adaptive Server Enterprise")) { 279 return DataBase_Sybase; 280 } 281 else if (vendorName.equalsIgnoreCase("Firebird")) { 282 return DataBase_Firebird; 283 } 284 else if (vendorName.equalsIgnoreCase(MySQL_ProductName)) { 285 return DataBase_MySQL; 286 } 287 288 else { 289 return DataBase_General; 290 } 291 } 292 finally { 293 connectionPool.returnConnection(con); 294 connectionPool.removeSubPubFromMap(pubsubName); 295 } 296 } 297 298 /** 299 * This method helps in identifying the databse. It accomplishses this work 300 * by getting the connection of the specified sub or pub and then by taking 301 * the database product name. 302 * 303 * @param connectionPool 304 * @param pubsubName 305 * @return 306 * @throws RepException 307 */ 308 309 private static String getVendorName(Connection connection) throws 310 RepException { 311 try { 312 return connection.getMetaData().getDatabaseProductName(); 313 } 314 catch (SQLException ex) { 315 throw new RepException("REP006", new Object[] {ex.getMessage()}); 316 } 317 } 318 319 public static AbstractDataBaseHandler getDatabaseHandler(int tgtVendorType) { 320 switch (tgtVendorType) { 321 case DataBase_DaffodilDB: 322 return new DaffodilDBHandler(); 323 case DataBase_Oracle: 324 return new OracleHandler(); 325 case DataBase_SqlServer: 326 return new SQLServerHandler(); 327 case DataBase_PointBase: 328 return new PointBaseHandler(); 329 case DataBase_Cloudscape: 330 return new CloudScapeHandler(); 331 case DataBase_PostgreSQL: 332 return new PostgreSQLHandler(); 333 case DataBase_DB2: 334 return new DB2Handler(); 335 case DataBase_Sybase: 336 return new SybaseHandler(); 337 case DataBase_Firebird: 338 return new FireBirdDatabaseHandler(); 339 case DataBase_MySQL: 340 return new MYSQLHandler(); 341 case DataBase_General: 342 default: 343 return new GeneralDataBaseHandler(); 344 } 345 } 346 347 348 349 }

