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_KIND = "mysql"; // or mariadb
036
037    private static final String DEF_URL = "jdbc:" + DEF_KIND + "://localhost:3306/" + DEFAULT_DATABASE_NAME;
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_MYSQL = "com.mysql.jdbc.Driver";
046
047    private static final String DRIVER_MARIADB = "org.mariadb.jdbc.Driver";
048
049    private void setProperties() {
050        String url = setProperty(URL_PROPERTY, DEF_URL);
051        setProperty(USER_PROPERTY, DEF_USER);
052        setProperty(PASSWORD_PROPERTY, DEF_PASSWORD);
053        String driver = url.startsWith("jdbc:mariadb:") ? DRIVER_MARIADB : DRIVER_MYSQL;
054        setProperty(DRIVER_PROPERTY, driver);
055    }
056
057    @Override
058    public void setUp() throws SQLException {
059        super.setUp();
060        setProperties();
061        String driver = Framework.getProperty(DRIVER_PROPERTY);
062        try {
063            Class.forName(driver);
064        } catch (ReflectiveOperationException e) {
065            throw new RuntimeException(e);
066        }
067        Connection connection = DriverManager.getConnection(Framework.getProperty(URL_PROPERTY),
068                Framework.getProperty(USER_PROPERTY), Framework.getProperty(PASSWORD_PROPERTY));
069        doOnAllTables(connection, null, null, "DROP TABLE `%s` CASCADE");
070        connection.close();
071    }
072
073    @Override
074    public String getDeploymentContrib() {
075        return CONTRIB_XML;
076    }
077
078    @Override
079    public RepositoryDescriptor getRepositoryDescriptor() {
080        RepositoryDescriptor descriptor = new RepositoryDescriptor();
081        return descriptor;
082    }
083
084    @Override
085    public int getRecursiveRemovalDepthLimit() {
086        // Stupid MySQL limitations:
087        // "Cascading operations may not be nested more than 15 levels deep."
088        // "Currently, triggers are not activated by cascaded foreign key
089        // actions."
090        // Use a bit less that 15 to cater for complex properties
091        return 13;
092    }
093
094    @Override
095    public boolean supportsClustering() {
096        return true;
097    }
098
099}