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