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    public static DatabaseHelper INSTANCE = new DatabaseDB2();
039
040    private static final String DEF_SERVER = "localhost";
041
042    private static final String DEF_PORT = "3700";
043
044    // 8 chars max
045    private static final String DEFAULT_DATABASE_NAME = "nuxeotst";
046
047    private static final String DEF_USER = "db2inst1";
048
049    private static final String DEF_PASSWORD = "db2inst1pw99";
050
051    private static final String CONTRIB_XML = "OSGI-INF/test-repo-repository-db2-contrib.xml";
052
053    private static final String DRIVER = "com.ibm.db2.jcc.DB2Driver";
054
055    protected void setProperties() {
056        databaseName = DEFAULT_DATABASE_NAME;
057        setProperty(DATABASE_PROPERTY, databaseName);
058        setProperty(SERVER_PROPERTY, DEF_SERVER);
059        setProperty(PORT_PROPERTY, DEF_PORT);
060        setProperty(USER_PROPERTY, DEF_USER);
061        setProperty(PASSWORD_PROPERTY, DEF_PASSWORD);
062        // for sql directory tests
063        setProperty(DRIVER_PROPERTY, DRIVER);
064        String url = String.format("jdbc:db2://%s:%s/%s", Framework.getProperty(SERVER_PROPERTY),
065                Framework.getProperty(PORT_PROPERTY), Framework.getProperty(DATABASE_PROPERTY));
066        setProperty(URL_PROPERTY, url);
067    }
068
069    @Override
070    public void setUp() throws SQLException {
071        super.setUp();
072        try {
073            Class.forName(DRIVER);
074        } catch (ReflectiveOperationException e) {
075            throw new RuntimeException(e);
076        }
077        setProperties();
078        Connection connection = DriverManager.getConnection(Framework.getProperty(URL_PROPERTY),
079                Framework.getProperty(USER_PROPERTY), Framework.getProperty(PASSWORD_PROPERTY));
080        doOnAllTables(connection, null, Framework.getProperty(USER_PROPERTY).toUpperCase(), "DROP TABLE \"%s\"");
081        dropSequences(connection);
082        connection.close();
083    }
084
085    public void dropSequences(Connection connection) throws SQLException {
086        List<String> sequenceNames = new ArrayList<String>();
087        Statement st = connection.createStatement();
088        ResultSet rs = st.executeQuery("SELECT SEQUENCE_NAME FROM USER_SEQUENCES");
089        while (rs.next()) {
090            String sequenceName = rs.getString(1);
091            if (sequenceName.indexOf('$') != -1) {
092                continue;
093            }
094            sequenceNames.add(sequenceName);
095        }
096        rs.close();
097        for (String sequenceName : sequenceNames) {
098            String sql = String.format("DROP SEQUENCE \"%s\"", sequenceName);
099            log.trace("SQL: " + sql);
100            st.execute(sql);
101        }
102        st.close();
103    }
104
105    @Override
106    public String getDeploymentContrib() {
107        return CONTRIB_XML;
108    }
109
110    @Override
111    public RepositoryDescriptor getRepositoryDescriptor() {
112        RepositoryDescriptor descriptor = new RepositoryDescriptor();
113        return descriptor;
114    }
115
116    @Override
117    public boolean supportsClustering() {
118        return true;
119    }
120
121}