All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nuiton.topia.service.sql.batch.actions.DropSchemaAction Maven / Gradle / Ivy

package org.nuiton.topia.service.sql.batch.actions;

/*
 * #%L
 * ObServe Toolkit :: ToPIA Extension
 * %%
 * Copyright (C) 2008 - 2017 IRD, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.dialect.Dialect;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.nuiton.topia.persistence.TopiaException;

/**
 * Created on 01/01/16.
 *
 * @author Tony Chemit - [email protected]
 * @since 3.0.1
 */
public class DropSchemaAction extends AbstractSchemaAction {

    /** Logger */
    private static final Log log = LogFactory.getLog(DropSchemaAction.class);

    public static final String DROP_SCHEMA_STATEMENT = "\nDROP SCHEMA %s;";

    public DropSchemaAction(DropSchemaRequest request, boolean showSql) {
        super(request, showSql);
    }

    @Override
    protected String produceSql(Class dialectType, Path temporaryDirectory) throws IOException {

        try {

            Path sqlScriptFile = temporaryDirectory.resolve("dropSchema_" + System.nanoTime() + ".sql");

            generateSqlInFile(dialectType, sqlScriptFile, SchemaExport.Action.DROP);

            String sqlStatements = new String(Files.readAllBytes(sqlScriptFile), StandardCharsets.UTF_8);
            Files.delete(sqlScriptFile);

            if (request.isDropSchema()) {
                for (String schemaName : getSchemaNames()) {
                    sqlStatements += String.format(DROP_SCHEMA_STATEMENT, schemaName);
                }
            }
            if (showSql) {
                if (log.isInfoEnabled()) {
                    log.info(sqlStatements);
                }
            }
            return sqlStatements;

        } catch (HibernateException eee) {
            throw new TopiaException(String.format("Could not drop schema for reason: %s", eee.getMessage()), eee);
        }

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy