001/*
002 * (C) Copyright 2006-2011 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.ResultSet;
024import java.sql.SQLException;
025import java.sql.Statement;
026import java.util.ArrayList;
027import java.util.HashMap;
028import java.util.List;
029import java.util.Map;
030
031import org.apache.commons.logging.Log;
032import org.apache.commons.logging.LogFactory;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * @author Florent Guillaume
037 */
038public class DatabaseOracle extends DatabaseHelper {
039
040    private static final Log log = LogFactory.getLog(DatabaseOracle.class);
041
042    public static DatabaseHelper INSTANCE = new DatabaseOracle();
043
044    private static final String DEF_SERVER = "localhost";
045
046    private static final String DEF_URL = "jdbc:oracle:thin:@" + DEF_SERVER + ":1521:XE";
047
048    private static final String DEF_USER = "nuxeo";
049
050    private static final String DEF_PASSWORD = "nuxeo";
051
052    private static final String CONTRIB_XML = "OSGI-INF/test-repo-repository-oracle-contrib.xml";
053
054    private static final String DRIVER = "oracle.jdbc.OracleDriver";
055
056    private void setProperties() {
057        setProperty(URL_PROPERTY, DEF_URL);
058        setProperty(USER_PROPERTY, DEF_USER);
059        setProperty(PASSWORD_PROPERTY, DEF_PASSWORD);
060        setProperty(DRIVER_PROPERTY, DRIVER);
061        setProperty(ID_TYPE_PROPERTY, DEF_ID_TYPE);
062    }
063
064    @Override
065    public void setUp() throws SQLException {
066        super.setUp();
067        try {
068            Class.forName(DRIVER);
069        } catch (ReflectiveOperationException e) {
070            throw new RuntimeException(e);
071        }
072        setProperties();
073        Connection connection = getConnection(Framework.getProperty(URL_PROPERTY),
074                Framework.getProperty(USER_PROPERTY), Framework.getProperty(PASSWORD_PROPERTY));
075        doOnAllTables(connection, null, Framework.getProperty(USER_PROPERTY).toUpperCase(),
076                "DROP TABLE \"%s\" CASCADE CONSTRAINTS PURGE");
077        dropSequences(connection);
078        connection.close();
079    }
080
081    public void dropSequences(Connection connection) throws SQLException {
082        List<String> sequenceNames = new ArrayList<String>();
083        Statement st = connection.createStatement();
084        ResultSet rs = st.executeQuery("SELECT SEQUENCE_NAME FROM USER_SEQUENCES");
085        while (rs.next()) {
086            String sequenceName = rs.getString(1);
087            if (sequenceName.indexOf('$') != -1) {
088                continue;
089            }
090            sequenceNames.add(sequenceName);
091        }
092        rs.close();
093        for (String sequenceName : sequenceNames) {
094            String sql = String.format("DROP SEQUENCE \"%s\"", sequenceName);
095            log.trace("SQL: " + sql);
096            st.execute(sql);
097        }
098        st.close();
099    }
100
101    @Override
102    public String getDeploymentContrib() {
103        return CONTRIB_XML;
104    }
105
106    @Override
107    public RepositoryDescriptor getRepositoryDescriptor() {
108        RepositoryDescriptor descriptor = new RepositoryDescriptor();
109        descriptor.xaDataSourceName = "oracle.jdbc.xa.client.OracleXADataSource";
110        Map<String, String> properties = new HashMap<String, String>();
111        properties.put("URL", Framework.getProperty(URL_PROPERTY));
112        properties.put("User", Framework.getProperty(USER_PROPERTY));
113        properties.put("Password", Framework.getProperty(PASSWORD_PROPERTY));
114        descriptor.properties = properties;
115        descriptor.idType = Framework.getProperty(ID_TYPE_PROPERTY);
116        return descriptor;
117    }
118
119    @Override
120    public boolean supportsClustering() {
121        return true;
122    }
123
124    @Override
125    public boolean supportsSoftDelete() {
126        return true;
127    }
128
129    @Override
130    public boolean supportsSequenceId() {
131        return true;
132    }
133
134}