001/* 002 * (C) Copyright 2006-2013 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Florent Guillaume 018 */ 019 020package org.nuxeo.ecm.core.storage.sql; 021 022import java.sql.Connection; 023import java.sql.DriverManager; 024import java.sql.ResultSet; 025import java.sql.SQLException; 026import java.sql.Statement; 027import java.util.ArrayList; 028import java.util.List; 029 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032import org.nuxeo.runtime.api.Framework; 033 034public class DatabaseDB2 extends DatabaseHelper { 035 036 private static final Log log = LogFactory.getLog(DatabaseDB2.class); 037 038 private static final String DEF_SERVER = "localhost"; 039 040 private static final String DEF_PORT = "3700"; 041 042 // 8 chars max 043 private static final String DEFAULT_DATABASE_NAME = "nuxeotst"; 044 045 private static final String DEF_USER = "db2inst1"; 046 047 private static final String DEF_PASSWORD = "db2inst1pw99"; 048 049 private static final String CONTRIB_XML = "OSGI-INF/test-repo-repository-db2-contrib.xml"; 050 051 private static final String DRIVER = "com.ibm.db2.jcc.DB2Driver"; 052 053 protected void setProperties() { 054 databaseName = DEFAULT_DATABASE_NAME; 055 setProperty(DATABASE_PROPERTY, databaseName); 056 setProperty(SERVER_PROPERTY, DEF_SERVER); 057 setProperty(PORT_PROPERTY, DEF_PORT); 058 setProperty(USER_PROPERTY, DEF_USER); 059 setProperty(PASSWORD_PROPERTY, DEF_PASSWORD); 060 // for sql directory tests 061 setProperty(DRIVER_PROPERTY, DRIVER); 062 String url = String.format("jdbc:db2://%s:%s/%s", Framework.getProperty(SERVER_PROPERTY), 063 Framework.getProperty(PORT_PROPERTY), Framework.getProperty(DATABASE_PROPERTY)); 064 setProperty(URL_PROPERTY, url); 065 } 066 067 @Override 068 public void setUp() throws SQLException { 069 super.setUp(); 070 try { 071 Class.forName(DRIVER); 072 } catch (ReflectiveOperationException e) { 073 throw new RuntimeException(e); 074 } 075 setProperties(); 076 Connection connection = DriverManager.getConnection(Framework.getProperty(URL_PROPERTY), 077 Framework.getProperty(USER_PROPERTY), Framework.getProperty(PASSWORD_PROPERTY)); 078 doOnAllTables(connection, null, Framework.getProperty(USER_PROPERTY).toUpperCase(), "DROP TABLE \"%s\""); 079 dropSequences(connection); 080 connection.close(); 081 } 082 083 public void dropSequences(Connection connection) throws SQLException { 084 List<String> sequenceNames = new ArrayList<String>(); 085 try (Statement st = connection.createStatement()) { 086 try (ResultSet rs = st.executeQuery("SELECT SEQUENCE_NAME FROM USER_SEQUENCES")) { 087 while (rs.next()) { 088 String sequenceName = rs.getString(1); 089 if (sequenceName.indexOf('$') != -1) { 090 continue; 091 } 092 sequenceNames.add(sequenceName); 093 } 094 } 095 for (String sequenceName : sequenceNames) { 096 String sql = String.format("DROP SEQUENCE \"%s\"", sequenceName); 097 log.trace("SQL: " + sql); 098 st.execute(sql); 099 } 100 } 101 } 102 103 @Override 104 public String getDeploymentContrib() { 105 return CONTRIB_XML; 106 } 107 108 @Override 109 public RepositoryDescriptor getRepositoryDescriptor() { 110 RepositoryDescriptor descriptor = new RepositoryDescriptor(); 111 return descriptor; 112 } 113 114 @Override 115 public boolean supportsClustering() { 116 return true; 117 } 118 119}