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