001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Florent Guillaume
011 */
012
013package org.nuxeo.ecm.core.storage.sql;
014
015import java.sql.Connection;
016import java.sql.DriverManager;
017import java.sql.Statement;
018import java.util.HashMap;
019import java.util.Map;
020
021import org.nuxeo.runtime.api.Framework;
022
023/**
024 * @author Florent Guillaume
025 */
026public class DatabasePostgreSQL extends DatabaseHelper {
027
028    public static DatabaseHelper INSTANCE = new DatabasePostgreSQL();
029
030    private static final String DEF_SERVER = "localhost";
031
032    private static final String DEF_PORT = "5432";
033
034    private static final String DEF_USER = "nuxeo";
035
036    private static final String DEF_PASSWORD = "nuxeo";
037
038    private static final String CONTRIB_XML = "OSGI-INF/test-repo-repository-postgresql-contrib.xml";
039
040    private static final String DRIVER = "org.postgresql.Driver";
041
042    protected void setProperties() {
043        Framework.getProperties().setProperty(REPOSITORY_PROPERTY, repositoryName);
044        String db = setProperty(DATABASE_PROPERTY, databaseName);
045        String server = setProperty(SERVER_PROPERTY, DEF_SERVER);
046        String port = setProperty(PORT_PROPERTY, DEF_PORT);
047        String user = setProperty(USER_PROPERTY, DEF_USER);
048        String password = setProperty(PASSWORD_PROPERTY, DEF_PASSWORD);
049        // for sql directory tests
050        String driver = setProperty(DRIVER_PROPERTY, DRIVER);
051        String url = String.format("jdbc:postgresql://%s:%s/%s", server, port, db);
052        setProperty(URL_PROPERTY, url);
053        setProperty(ID_TYPE_PROPERTY, DEF_ID_TYPE);
054    }
055
056    @Override
057    public void setUp() throws Exception {
058        super.setUp();
059        Class.forName(DRIVER);
060        setProperties();
061        Connection connection = DriverManager.getConnection(Framework.getProperty(URL_PROPERTY),
062                Framework.getProperty(USER_PROPERTY), Framework.getProperty(PASSWORD_PROPERTY));
063        try {
064            doOnAllTables(connection, null, "public", "DROP TABLE \"%s\" CASCADE");
065            Statement st = connection.createStatement();
066            executeSql(st, "DROP SEQUENCE IF EXISTS hierarchy_seq");
067            st.close();
068        } finally {
069            connection.close();
070        }
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        descriptor.xaDataSourceName = "org.postgresql.xa.PGXADataSource";
082        Map<String, String> properties = new HashMap<String, String>();
083        properties.put("ServerName", Framework.getProperty(SERVER_PROPERTY));
084        properties.put("PortNumber", Framework.getProperty(PORT_PROPERTY));
085        properties.put("DatabaseName", Framework.getProperty(DATABASE_PROPERTY));
086        properties.put("User", Framework.getProperty(USER_PROPERTY));
087        properties.put("Password", Framework.getProperty(PASSWORD_PROPERTY));
088        descriptor.properties = properties;
089        descriptor.fulltextAnalyzer = "french";
090        descriptor.setPathOptimizationsEnabled(true);
091        descriptor.setAclOptimizationsEnabled(true);
092        descriptor.idType = Framework.getProperty(ID_TYPE_PROPERTY);
093        return descriptor;
094    }
095
096    @Override
097    public boolean supportsClustering() {
098        return true;
099    }
100
101    @Override
102    public boolean supportsSoftDelete() {
103        return true;
104    }
105
106    @Override
107    public boolean supportsSequenceId() {
108        return true;
109    }
110
111    @Override
112    public boolean supportsArrayColumns() {
113        return true;
114    }
115
116}