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