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.DriverManager;
024import java.sql.SQLException;
025
026import org.nuxeo.runtime.api.Framework;
027
028/**
029 * @author Florent Guillaume
030 */
031public class DatabaseMySQL extends DatabaseHelper {
032
033    public static DatabaseHelper INSTANCE = new DatabaseMySQL();
034
035    private static final String DEF_URL = "jdbc:mysql://localhost:3306/nuxeojunittests";
036
037    private static final String DEF_USER = "nuxeo";
038
039    private static final String DEF_PASSWORD = "nuxeo";
040
041    private static final String CONTRIB_XML = "OSGI-INF/test-repo-repository-mysql-contrib.xml";
042
043    private static final String DRIVER = "com.mysql.jdbc.Driver";
044
045    private void setProperties() {
046        setProperty(URL_PROPERTY, DEF_URL);
047        setProperty(USER_PROPERTY, DEF_USER);
048        setProperty(PASSWORD_PROPERTY, DEF_PASSWORD);
049        setProperty(DRIVER_PROPERTY, DRIVER);
050    }
051
052    @Override
053    public void setUp() throws SQLException {
054        super.setUp();
055        try {
056            Class.forName(DRIVER);
057        } catch (ReflectiveOperationException e) {
058            throw new RuntimeException(e);
059        }
060        setProperties();
061        Connection connection = DriverManager.getConnection(Framework.getProperty(URL_PROPERTY),
062                Framework.getProperty(USER_PROPERTY), Framework.getProperty(PASSWORD_PROPERTY));
063        doOnAllTables(connection, null, null, "DROP TABLE `%s` CASCADE");
064        connection.close();
065    }
066
067    @Override
068    public String getDeploymentContrib() {
069        return CONTRIB_XML;
070    }
071
072    @Override
073    public RepositoryDescriptor getRepositoryDescriptor() {
074        RepositoryDescriptor descriptor = new RepositoryDescriptor();
075        return descriptor;
076    }
077
078    @Override
079    public boolean hasSubSecondResolution() {
080        return false;
081    }
082
083    @Override
084    public int getRecursiveRemovalDepthLimit() {
085        // Stupid MySQL limitations:
086        // "Cascading operations may not be nested more than 15 levels deep."
087        // "Currently, triggers are not activated by cascaded foreign key
088        // actions."
089        // Use a bit less that 15 to cater for complex properties
090        return 13;
091    }
092
093    @Override
094    public boolean supportsClustering() {
095        return true;
096    }
097
098}