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.sql.Statement;
026import java.util.HashMap;
027import java.util.Map;
028
029import org.nuxeo.runtime.api.Framework;
030
031/**
032 * @author Florent Guillaume
033 */
034public class DatabasePostgreSQL extends DatabaseHelper {
035
036    public static DatabaseHelper INSTANCE = new DatabasePostgreSQL();
037
038    private static final String DEF_SERVER = "localhost";
039
040    private static final String DEF_PORT = "5432";
041
042    private static final String DEF_USER = "nuxeo";
043
044    private static final String DEF_PASSWORD = "nuxeo";
045
046    private static final String CONTRIB_XML = "OSGI-INF/test-repo-repository-postgresql-contrib.xml";
047
048    private static final String DRIVER = "org.postgresql.Driver";
049
050    protected void setProperties() {
051        String db = setProperty(DATABASE_PROPERTY, databaseName);
052        String server = setProperty(SERVER_PROPERTY, DEF_SERVER);
053        String port = setProperty(PORT_PROPERTY, DEF_PORT);
054        String user = setProperty(USER_PROPERTY, DEF_USER);
055        String password = setProperty(PASSWORD_PROPERTY, DEF_PASSWORD);
056        // for sql directory tests
057        String driver = setProperty(DRIVER_PROPERTY, DRIVER);
058        String url = String.format("jdbc:postgresql://%s:%s/%s", server, port, db);
059        setProperty(URL_PROPERTY, url);
060        setProperty(ID_TYPE_PROPERTY, DEF_ID_TYPE);
061    }
062
063    @Override
064    public void setUp() throws SQLException {
065        super.setUp();
066        try {
067            Class.forName(DRIVER);
068        } catch (ReflectiveOperationException e) {
069            throw new RuntimeException(e);
070        }
071        setProperties();
072        Connection connection = DriverManager.getConnection(Framework.getProperty(URL_PROPERTY),
073                Framework.getProperty(USER_PROPERTY), Framework.getProperty(PASSWORD_PROPERTY));
074        try {
075            doOnAllTables(connection, null, "public", "DROP TABLE \"%s\" CASCADE");
076            Statement st = connection.createStatement();
077            executeSql(st, "DROP SEQUENCE IF EXISTS hierarchy_seq");
078            st.close();
079        } finally {
080            connection.close();
081        }
082    }
083
084    @Override
085    public String getDeploymentContrib() {
086        return CONTRIB_XML;
087    }
088
089    @Override
090    public RepositoryDescriptor getRepositoryDescriptor() {
091        RepositoryDescriptor descriptor = new RepositoryDescriptor();
092        descriptor.xaDataSourceName = "org.postgresql.xa.PGXADataSource";
093        Map<String, String> properties = new HashMap<String, String>();
094        properties.put("ServerName", Framework.getProperty(SERVER_PROPERTY));
095        properties.put("PortNumber", Framework.getProperty(PORT_PROPERTY));
096        properties.put("DatabaseName", Framework.getProperty(DATABASE_PROPERTY));
097        properties.put("User", Framework.getProperty(USER_PROPERTY));
098        properties.put("Password", Framework.getProperty(PASSWORD_PROPERTY));
099        descriptor.properties = properties;
100        descriptor.setFulltextAnalyzer("french");
101        descriptor.setPathOptimizationsEnabled(true);
102        descriptor.setAclOptimizationsEnabled(true);
103        descriptor.idType = Framework.getProperty(ID_TYPE_PROPERTY);
104        return descriptor;
105    }
106
107    @Override
108    public boolean supportsClustering() {
109        return true;
110    }
111
112    @Override
113    public boolean supportsSoftDelete() {
114        return true;
115    }
116
117    @Override
118    public boolean supportsSequenceId() {
119        return true;
120    }
121
122    @Override
123    public boolean supportsArrayColumns() {
124        return true;
125    }
126
127}