<commit_before>/*
 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     "<NAME> (aka matic) <<EMAIL>>"
 */
package org.nuxeo.ecm.platform.tag.persistence;

import static org.nuxeo.ecm.platform.tag.entity.TaggingConstants.TAGGING_TABLE_COLUMN_AUTHOR;
import static org.nuxeo.ecm.platform.tag.entity.TaggingConstants.TAGGING_TABLE_COLUMN_CREATION_DATE;
import static org.nuxeo.ecm.platform.tag.entity.TaggingConstants.TAGGING_TABLE_COLUMN_DOCUMENT_ID;
import static org.nuxeo.ecm.platform.tag.entity.TaggingConstants.TAGGING_TABLE_COLUMN_ID;
import static org.nuxeo.ecm.platform.tag.entity.TaggingConstants.TAGGING_TABLE_COLUMN_IS_PRIVATE;
import static org.nuxeo.ecm.platform.tag.entity.TaggingConstants.TAGGING_TABLE_COLUMN_TAG_ID;
import static org.nuxeo.ecm.platform.tag.entity.TaggingConstants.TAGGING_TABLE_NAME;

import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Environment;
import org.hibernate.cfg.Settings;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.DialectFactory;
import org.hibernate.dialect.Oracle10gDialect;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.ejb.packaging.PersistenceMetadata;
import org.hibernate.ejb.packaging.PersistenceXmlLoader;
import org.hibernate.impl.SessionImpl;
import org.nuxeo.ecm.platform.tag.sql.Column;
import org.nuxeo.ecm.platform.tag.sql.Table;
import org.nuxeo.runtime.api.DataSourceHelper;

/**
 * @author "<NAME> (aka matic) <<EMAIL>>"
 *
 */
public class TagSchemaUpdater {

    public static final Log log = LogFactory.getLog(TagSchemaUpdater.class);

    public final AnnotationConfiguration configuration = new AnnotationConfiguration();

    public final PersistenceMetadata metadata = doLoadMetadata();

    public final Properties connectionProperties = new Properties();

    public TagSchemaUpdater() {
        doSetup();
    }

    public TagSchemaUpdater(Properties properties) {
        connectionProperties.putAll(properties);
        doSetup();
    }

    protected void doSetup() {
        doSetupAnnotedClasses();
        doSetupConnectionProperties();
    }

    protected void doSetupAnnotedClasses() {
        for (String mappedClassName : metadata.getClasses()) {
            try {
                configuration.addAnnotatedClass(Class.forName(mappedClassName));
            } catch (Exception e) {
                throw new Error("Cannot load class " + mappedClassName, e);
            }
        }
    }

    protected void doSetupConnectionProperties() {
        if (connectionProperties.get(Environment.URL) != null) {
            return;
        }
        String jtaDatasource = metadata.getJtaDatasource();
        connectionProperties.setProperty(Environment.DATASOURCE, jtaDatasource);
    }

    protected PersistenceMetadata doLoadMetadata() {
        Enumeration<URL> xmls;
        try {
            xmls = TagSchemaUpdater.class.getClassLoader().getResources("META-INF/persistence.xml");
        } catch (IOException e1) {
            throw new Error("No persistence.xml files in class path", e1);
        }
        while (xmls.hasMoreElements()) {
            URL url = xmls.nextElement();
            List<PersistenceMetadata> metadataFiles = null;
            try {
                metadataFiles = PersistenceXmlLoader.deploy(
                        url, Collections.EMPTY_MAP, configuration.getEntityResolver());
            } catch (Exception e) {
                log.warn("Cannot load " + url);
                continue;
            }
            for (PersistenceMetadata metadata : metadataFiles) {
                if (metadata.getName().equals("nxtags")) {
                    return metadata;
                }
            }
        }
        throw new Error("cannot find nxtags persistence unit");
    }

    public static Dialect determineDialect(SessionImpl session) {
        try {
            DatabaseMetaData meta = session.getFactory().getConnectionProvider().getConnection().getMetaData();
            return DialectFactory.determineDialect(meta.getDatabaseProductName(), meta.getDatabaseMajorVersion());
        } catch (SQLException e) {
            throw new Error("Cannot determine dialect", e);
        }
    }

    public void update() {
        String dataSource = connectionProperties.getProperty("hibernate.connection.datasource");
        // change JNDI name prefix if needed
        dataSource = DataSourceHelper.getDataSourceJNDIName(dataSource);
        configuration.setProperties(connectionProperties);
        Settings settings = configuration.buildSettings();
        Table table = new Table(TAGGING_TABLE_NAME);
        Column column = new Column(TAGGING_TABLE_COLUMN_ID, Types.VARCHAR);
        column.setPrimary(true);
        column.setNullable(false);
        table.addColumn(column);
        column = new Column(TAGGING_TABLE_COLUMN_TAG_ID, Types.VARCHAR);
        table.addColumn(column);
        column = new Column(TAGGING_TABLE_COLUMN_AUTHOR, Types.VARCHAR);
        table.addColumn(column);
        column = new Column(TAGGING_TABLE_COLUMN_DOCUMENT_ID, Types.VARCHAR);
        table.addColumn(column);
        column = new Column(TAGGING_TABLE_COLUMN_CREATION_DATE, Types.DATE);
        table.addColumn(column);
        column = new Column(TAGGING_TABLE_COLUMN_IS_PRIVATE, Types.INTEGER);
        table.addColumn(column);
        Dialect dialect = settings.getDialect();
        String script = table.getCreateSql(dialect);
        try {
            Connection connection = settings.getConnectionProvider().getConnection();
            ResultSet result = connection.getMetaData().getTables(null, null, table.getName(), null);
            if (!result.next()) {
                log.debug("creating TagService tables");
                Statement statement = connection.createStatement();
                statement.execute(script);
            }else {
                log.debug("TagService tables already exist");
            }
        } catch (SQLException e) {
            throw new Error("Cannot update schema", e);
        }
    }

}
<commit_msg>NXP-3871: fix init test for lowercase-using database<commit_after>/*
 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     "<NAME> (aka matic) <<EMAIL>>"
 */
package org.nuxeo.ecm.platform.tag.persistence;

import static org.nuxeo.ecm.platform.tag.entity.TaggingConstants.TAGGING_TABLE_COLUMN_AUTHOR;
import static org.nuxeo.ecm.platform.tag.entity.TaggingConstants.TAGGING_TABLE_COLUMN_CREATION_DATE;
import static org.nuxeo.ecm.platform.tag.entity.TaggingConstants.TAGGING_TABLE_COLUMN_DOCUMENT_ID;
import static org.nuxeo.ecm.platform.tag.entity.TaggingConstants.TAGGING_TABLE_COLUMN_ID;
import static org.nuxeo.ecm.platform.tag.entity.TaggingConstants.TAGGING_TABLE_COLUMN_IS_PRIVATE;
import static org.nuxeo.ecm.platform.tag.entity.TaggingConstants.TAGGING_TABLE_COLUMN_TAG_ID;
import static org.nuxeo.ecm.platform.tag.entity.TaggingConstants.TAGGING_TABLE_NAME;

import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Environment;
import org.hibernate.cfg.Settings;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.DialectFactory;
import org.hibernate.dialect.Oracle10gDialect;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.ejb.packaging.PersistenceMetadata;
import org.hibernate.ejb.packaging.PersistenceXmlLoader;
import org.hibernate.impl.SessionImpl;
import org.nuxeo.ecm.platform.tag.sql.Column;
import org.nuxeo.ecm.platform.tag.sql.Table;
import org.nuxeo.runtime.api.DataSourceHelper;

/**
 * @author "<NAME> (aka matic) <<EMAIL>>"
 *
 */
public class TagSchemaUpdater {

    public static final Log log = LogFactory.getLog(TagSchemaUpdater.class);

    public final AnnotationConfiguration configuration = new AnnotationConfiguration();

    public final PersistenceMetadata metadata = doLoadMetadata();

    public final Properties connectionProperties = new Properties();

    public TagSchemaUpdater() {
        doSetup();
    }

    public TagSchemaUpdater(Properties properties) {
        connectionProperties.putAll(properties);
        doSetup();
    }

    protected void doSetup() {
        doSetupAnnotedClasses();
        doSetupConnectionProperties();
    }

    protected void doSetupAnnotedClasses() {
        for (String mappedClassName : metadata.getClasses()) {
            try {
                configuration.addAnnotatedClass(Class.forName(mappedClassName));
            } catch (Exception e) {
                throw new Error("Cannot load class " + mappedClassName, e);
            }
        }
    }

    protected void doSetupConnectionProperties() {
        if (connectionProperties.get(Environment.URL) != null) {
            return;
        }
        String jtaDatasource = metadata.getJtaDatasource();
        connectionProperties.setProperty(Environment.DATASOURCE, jtaDatasource);
    }

    protected PersistenceMetadata doLoadMetadata() {
        Enumeration<URL> xmls;
        try {
            xmls = TagSchemaUpdater.class.getClassLoader().getResources("META-INF/persistence.xml");
        } catch (IOException e1) {
            throw new Error("No persistence.xml files in class path", e1);
        }
        while (xmls.hasMoreElements()) {
            URL url = xmls.nextElement();
            List<PersistenceMetadata> metadataFiles = null;
            try {
                metadataFiles = PersistenceXmlLoader.deploy(
                        url, Collections.EMPTY_MAP, configuration.getEntityResolver());
            } catch (Exception e) {
                log.warn("Cannot load " + url);
                continue;
            }
            for (PersistenceMetadata metadata : metadataFiles) {
                if (metadata.getName().equals("nxtags")) {
                    return metadata;
                }
            }
        }
        throw new Error("cannot find nxtags persistence unit");
    }

    public static Dialect determineDialect(SessionImpl session) {
        try {
            DatabaseMetaData meta = session.getFactory().getConnectionProvider().getConnection().getMetaData();
            return DialectFactory.determineDialect(meta.getDatabaseProductName(), meta.getDatabaseMajorVersion());
        } catch (SQLException e) {
            throw new Error("Cannot determine dialect", e);
        }
    }

    public void update() {
        String dataSource = connectionProperties.getProperty("hibernate.connection.datasource");
        // change JNDI name prefix if needed
        dataSource = DataSourceHelper.getDataSourceJNDIName(dataSource);
        configuration.setProperties(connectionProperties);
        Settings settings = configuration.buildSettings();
        Table table = new Table(TAGGING_TABLE_NAME);
        Column column = new Column(TAGGING_TABLE_COLUMN_ID, Types.VARCHAR);
        column.setPrimary(true);
        column.setNullable(false);
        table.addColumn(column);
        column = new Column(TAGGING_TABLE_COLUMN_TAG_ID, Types.VARCHAR);
        table.addColumn(column);
        column = new Column(TAGGING_TABLE_COLUMN_AUTHOR, Types.VARCHAR);
        table.addColumn(column);
        column = new Column(TAGGING_TABLE_COLUMN_DOCUMENT_ID, Types.VARCHAR);
        table.addColumn(column);
        column = new Column(TAGGING_TABLE_COLUMN_CREATION_DATE, Types.DATE);
        table.addColumn(column);
        column = new Column(TAGGING_TABLE_COLUMN_IS_PRIVATE, Types.INTEGER);
        table.addColumn(column);
        Dialect dialect = settings.getDialect();
        String script = table.getCreateSql(dialect);
        try {
            Connection connection = settings.getConnectionProvider().getConnection();
            String name = table.getName();
            DatabaseMetaData md = connection.getMetaData();
            if (!md.storesUpperCaseIdentifiers()) {
                name = name.toLowerCase();
            }
            ResultSet result = md.getTables(null, null, name, null);
            if (!result.next()) {
                log.debug("creating TagService tables");
                Statement statement = connection.createStatement();
                statement.execute(script);
            } else {
                log.debug("TagService tables already exist");
            }
        } catch (SQLException e) {
            throw new Error("Cannot update schema", e);
        }
    }

}

{'fraction_non_alphanumeric': 0.09493803822726318, 'fraction_numerical': 0.002240425680879367, 'mean_word_length': 3.0776477305167, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2899457', 'n_tokens_mistral': 3997, 'n_tokens_neox': 3875, 'n_words': 836}