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