This is an automated email from the git hooks/post-receive script. New commit to branch feature/3860_introduce_topiasqlbatchsupport in repository topia. See http://git.nuiton.org/topia.git commit 4aae58d8a48a23739c275a72530f6eb0c36478cf Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Jan 5 08:26:39 2016 +0100 Add create schema actions --- .../sql/batch/actions/CreateSchemaAction.java | 89 ++++++++++++++++++++++ .../sql/batch/actions/CreateSchemaRequest.java | 61 +++++++++++++++ 2 files changed, 150 insertions(+) diff --git a/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/CreateSchemaAction.java b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/CreateSchemaAction.java new file mode 100644 index 0000000..ec3b881 --- /dev/null +++ b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/CreateSchemaAction.java @@ -0,0 +1,89 @@ +package org.nuiton.topia.service.sql.batch.actions; + +/* + * #%L + * ToPIA :: Service Sql batch + * %% + * Copyright (C) 2004 - 2016 CodeLutin, Tony Chemit + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import com.google.common.collect.ImmutableSet; +import org.hibernate.HibernateException; +import org.hibernate.cfg.Configuration; +import org.hibernate.cfg.Environment; +import org.hibernate.dialect.Dialect; +import org.hibernate.tool.hbm2ddl.SchemaExport; +import org.nuiton.topia.persistence.TopiaException; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Properties; + +/** + * Created on 01/01/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.0.1 + */ +public class CreateSchemaAction extends AbstractSchemaAction<CreateSchemaRequest> { + + public static final String CREATE_SCHEMA_STATEMENT = "CREATE SCHEMA %s;\n"; + + public CreateSchemaAction(CreateSchemaRequest request) { + super(request); + } + + protected String produceSql(Class<? extends Dialect> dialectType, Path temporaryDirectory) throws IOException { + + try { + + Path sqlScriptFile = temporaryDirectory.resolve("replicateSchema_" + System.nanoTime() + ".sql"); + + Configuration hibernateConfiguration = getSourcePersistenceContext().getHibernateSupport().getHibernateConfiguration(); + + Properties properties = new Properties(); + + properties.put(Environment.DIALECT, dialectType.getName()); + + new SchemaExport(hibernateConfiguration, properties) + .setOutputFile(sqlScriptFile.toFile().getAbsolutePath()) + .setDelimiter(";") + .create(false, false); + + String sqlStatements = ""; + if (request.isAddSchema()) { + + ImmutableSet<String> schemaNames = getSchemaNames(); + for (String schemaName : schemaNames) { + sqlStatements += String.format(CREATE_SCHEMA_STATEMENT, schemaName); + } + + } + + sqlStatements += new String(Files.readAllBytes(sqlScriptFile)); + Files.delete(sqlScriptFile); + return sqlStatements; + + } catch (HibernateException eee) { + throw new TopiaException(String.format("Could not create schema for reason: %s", eee.getMessage()), eee); + } + + } + +} diff --git a/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/CreateSchemaRequest.java b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/CreateSchemaRequest.java new file mode 100644 index 0000000..3f6acb3 --- /dev/null +++ b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/CreateSchemaRequest.java @@ -0,0 +1,61 @@ +package org.nuiton.topia.service.sql.batch.actions; + +/* + * #%L + * ToPIA :: Service Sql batch + * %% + * Copyright (C) 2004 - 2016 CodeLutin, Tony Chemit + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +/** + * Created on 01/01/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.0.1 + */ +public class CreateSchemaRequest extends AbstractSchemaRequest { + + protected boolean addSchema; + + public static Builder builder() { + return new Builder(); + } + + public boolean isAddSchema() { + return addSchema; + } + + protected void setAddSchema(boolean addSchema) { + this.addSchema = addSchema; + } + + public static class Builder extends AbstractSchemaRequestBuilder<CreateSchemaRequest, Builder> { + + public Builder() { + super(new CreateSchemaRequest()); + } + + public Builder setAddSchema(boolean addSchema) { + request.setAddSchema(addSchema); + return this; + } + + } + + +} -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.