branch feature/3860_introduce_topiasqlbatchsupport created (now e57b090)
This is an automated email from the git hooks/post-receive script. New change to branch feature/3860_introduce_topiasqlbatchsupport in repository topia. See http://git.nuiton.org/topia.git at e57b090 Add TopiaSqlBatchService and his default implementation This branch includes the following new commits: new 7a4fee1 Introduce TopiaMetadataModel new e2c019a Use gson to load and store TopiaMetadataModel new 4c7bf77 Introduce TopiaMetadataModelSupport and add this new support to TopiaApplicationContext new b8ff4e8 Introduce TopiaMetadataModelGenerator and include it into default Topia templates new da2bcfd Add also getEntityEnum by name in TopiaEntityEnumProvider new a36406f Generate metadata model location path new 12d177e Add withShell method on metadata entity new b866192 Add new topia-sql-batch module new 4f594a9 Add TopiaSqlTable API new d254cc8 Add Sql action abstract classes new 3bd7535 Add create schema actions new 8da3fb2 Add drop schem action new e6ff976 Add replicate tables action new 716a078 Add update tables action new a36739d Add delete tables action new e57b090 Add TopiaSqlBatchService and his default implementation The 16 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit 8da3fb281570e66562bea9f73e86bfff9a4d6761 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Jan 5 08:26:53 2016 +0100 Add drop schem action commit e6ff976ddb0739ffe8e8db25289916769f0852e0 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Jan 5 08:27:10 2016 +0100 Add replicate tables action commit 716a0786731f1f138717e53d9c1ba71e6b97224f Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Jan 5 08:27:25 2016 +0100 Add update tables action commit a36739d0fefc9fb5beb2fc40a2d7ec475ec82dae Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Jan 5 08:27:40 2016 +0100 Add delete tables action commit e57b090066962ecdf5b5ddbc660531cef20bbc94 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Jan 5 08:29:01 2016 +0100 Add TopiaSqlBatchService and his default implementation commit 3bd75359b81a81fed510b666b6c77d7b5093f77d Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Jan 5 08:26:39 2016 +0100 Add create schema actions commit d254cc8250b234e9447a32942f30961f2c756c98 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Jan 5 08:26:11 2016 +0100 Add Sql action abstract classes commit 4f594a9bb3f2c11c89da84f228e6d9481adbc570 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Jan 5 08:25:22 2016 +0100 Add TopiaSqlTable API commit b8661923b3920a2fa6d7c83f57b178102b4734b1 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Jan 5 08:24:59 2016 +0100 Add new topia-sql-batch module commit 12d177efab307d8188934d9dbdbed340376d8689 Author: Tony CHEMIT <chemit@codelutin.com> Date: Mon Jan 4 20:45:41 2016 +0100 Add withShell method on metadata entity commit a36406fa0ec05512680c02d4e3b990e3659c52a6 Author: Tony CHEMIT <chemit@codelutin.com> Date: Mon Jan 4 20:44:35 2016 +0100 Generate metadata model location path commit da2bcfd4396b8f4ec049079a5d8bfabeebd75a99 Author: Tony CHEMIT <chemit@codelutin.com> Date: Mon Jan 4 17:26:50 2016 +0100 Add also getEntityEnum by name in TopiaEntityEnumProvider commit b8ff4e829a893687069952964de30ff00481dc3b Author: Tony CHEMIT <chemit@codelutin.com> Date: Mon Jan 4 13:39:07 2016 +0100 Introduce TopiaMetadataModelGenerator and include it into default Topia templates commit 4c7bf7758c4c683188098085904b0fd12a20b84a Author: Tony CHEMIT <chemit@codelutin.com> Date: Mon Jan 4 13:36:34 2016 +0100 Introduce TopiaMetadataModelSupport and add this new support to TopiaApplicationContext commit e2c019ac5963fa3c3aa89572a78f47cfea4589b8 Author: Tony CHEMIT <chemit@codelutin.com> Date: Mon Jan 4 13:35:18 2016 +0100 Use gson to load and store TopiaMetadataModel commit 7a4fee1fb91979649397f9994d648b6df317ca13 Author: Tony CHEMIT <chemit@codelutin.com> Date: Mon Jan 4 13:33:37 2016 +0100 Introduce TopiaMetadataModel -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
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 3bd75359b81a81fed510b666b6c77d7b5093f77d 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>.
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 e6ff976ddb0739ffe8e8db25289916769f0852e0 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Jan 5 08:27:10 2016 +0100 Add replicate tables action --- .../sql/batch/actions/ReplicateTablesAction.java | 144 +++++++++++++++++++++ .../sql/batch/actions/ReplicateTablesRequest.java | 45 +++++++ 2 files changed, 189 insertions(+) diff --git a/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/ReplicateTablesAction.java b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/ReplicateTablesAction.java new file mode 100644 index 0000000..537b3c5 --- /dev/null +++ b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/ReplicateTablesAction.java @@ -0,0 +1,144 @@ +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 org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.topia.service.sql.batch.tables.TopiaSqlTable; + +import java.io.IOException; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.util.List; + +/** + * Created on 01/01/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.0.1 + */ +public class ReplicateTablesAction extends AbstractTablesAction<ReplicateTablesRequest> { + + public static final String INSERT_STATEMENT = "INSERT INTO %s.%s(%s) VALUES (%%s);\n"; + /** + * Logger. + */ + private static final Log log = LogFactory.getLog(ReplicateTablesAction.class); + + public ReplicateTablesAction(ReplicateTablesRequest request) { + super(request); + } + + @Override + protected void executeOnTable(ReplicateTablesRequest request, TopiaSqlTable table, PreparedStatement readStatement) throws SQLException { + + ResultSet readResultSet = readStatement.getResultSet(); + + ResultSetMetaData readResultSetMetaData = readResultSet.getMetaData(); + int columnCount = readResultSetMetaData.getColumnCount(); + + List<String> columnNames = getColumnNames(readResultSetMetaData, columnCount); + + boolean useOutputWriter = useOutputWriter(); + boolean useOutputDb = useOutputDb(); + + PreparedStatement writeStatement = null; + + String insertStatementSql = newInsertStatementSql(table, columnNames); + + if (useOutputDb) { + + String arguments = generateWildcardArguments(columnNames); + String sql = String.format(insertStatementSql, arguments).trim(); + writeStatement = targetConnection.prepareStatement(sql); + + } + + int fetchSize = request.getFetchSize(); + String tableName = table.getFullyTableName(); + + long index = 0; + while (readResultSet.next()) { + + if (log.isTraceEnabled()) { + log.trace("Copy " + readResultSet.getString(1)); + } + + if (useOutputDb) { + + writeStatement.clearParameters(); + for (int i = 1; i <= columnCount; i++) { + Object object = readResultSet.getObject(i); + writeStatement.setObject(i, object); + } + writeStatement.addBatch(); + + } + + if (useOutputWriter) { + + try { + + String arguments = generateSqlArguments(readResultSet, columnNames); + String sql = String.format(insertStatementSql, arguments); + writer.append(sql); + + } catch (IOException e) { + throw new RuntimeException("Could not copyRow", e); + } + + } + + if ((++index % fetchSize) == 0) { + flush(writeStatement, writer, tableName, index); + } + + } + + flush(writeStatement, writer, tableName, index); + + } + + protected String newInsertStatementSql(TopiaSqlTable table, List<String> columnNames) throws SQLException { + + StringBuilder columnNamesBuilder = new StringBuilder(); + + for (String columnName : columnNames) { + columnNamesBuilder.append(", ").append(columnName); + } + + String sql = String.format(INSERT_STATEMENT, + table.getSchemaName(), + table.getTableName(), + columnNamesBuilder.substring(2)); + if (log.isDebugEnabled()) { + log.debug("Insert sql: " + sql); + } + + return sql; + + } + +} diff --git a/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/ReplicateTablesRequest.java b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/ReplicateTablesRequest.java new file mode 100644 index 0000000..9e45067 --- /dev/null +++ b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/ReplicateTablesRequest.java @@ -0,0 +1,45 @@ +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 ReplicateTablesRequest extends AbstractTablesRequest { + + public static Builder builder() { + return new Builder(); + } + + public static class Builder extends AbstractTablesRequestBuilder<Builder, ReplicateTablesRequest> { + + public Builder() { + super(new ReplicateTablesRequest()); + } + + } +} + -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
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 716a0786731f1f138717e53d9c1ba71e6b97224f Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Jan 5 08:27:25 2016 +0100 Add update tables action --- .../sql/batch/actions/UpdateTablesAction.java | 200 +++++++++++++++++++++ .../sql/batch/actions/UpdateTablesRequest.java | 45 +++++ 2 files changed, 245 insertions(+) diff --git a/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/UpdateTablesAction.java b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/UpdateTablesAction.java new file mode 100644 index 0000000..edf4bf0 --- /dev/null +++ b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/UpdateTablesAction.java @@ -0,0 +1,200 @@ +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.Lists; +import org.apache.commons.io.output.ByteArrayOutputStream; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.topia.persistence.TopiaEntity; +import org.nuiton.topia.service.sql.batch.tables.TopiaSqlTable; + +import javax.sql.rowset.serial.SerialBlob; +import java.io.IOException; +import java.sql.Blob; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.util.Date; +import java.util.List; + +/** + * Created on 01/01/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.0.1 + */ +public class UpdateTablesAction extends AbstractTablesAction<UpdateTablesRequest> { + + public static final String UPDATE_STATEMENT = "UPDATE %s.%s %s WHERE topiaid='%%s'"; + /** + * Logger. + */ + private static final Log log = LogFactory.getLog(UpdateTablesAction.class); + + public UpdateTablesAction(UpdateTablesRequest request) { + super(request); + } + + @Override + protected void executeOnTable(UpdateTablesRequest request, TopiaSqlTable table, PreparedStatement readStatement) throws SQLException { + + ResultSet readResultSet = readStatement.getResultSet(); + + ResultSetMetaData readResultSetMetaData = readResultSet.getMetaData(); + int columnCount = readResultSetMetaData.getColumnCount(); + + List<String> columnNames = Lists.newArrayList(getColumnNames(readResultSetMetaData, columnCount)); + + String topiaIdColumnName = TopiaEntity.PROPERTY_TOPIA_ID.toLowerCase(); + int topiaIdColumnIndex = columnNames.indexOf(topiaIdColumnName); + columnNames.remove(topiaIdColumnName); + + boolean useOutputWriter = useOutputWriter(); + boolean useOutputDb = useOutputDb(); + + PreparedStatement writeStatement = null; + + String updateStatementSql = newUpdateStatementSql(table, columnNames); + + if (useOutputDb) { + + String arguments = generateWildcardArguments(columnNames); + String sql = String.format(updateStatementSql, arguments, "?"); + writeStatement = targetConnection.prepareStatement(sql); + + } + + int fetchSize = request.getFetchSize(); + String tableName = table.getFullyTableName(); + + long index = 0; + while (readResultSet.next()) { + + String topiaId = readResultSet.getString(topiaIdColumnIndex); + if (log.isTraceEnabled()) { + log.trace("Update " + readResultSet.getString(1)); + } + + + if (useOutputDb) { + + writeStatement.clearParameters(); + int i = 1; + for (String columnName : columnNames) { + Object object = readResultSet.getObject(columnName); + writeStatement.setObject(i++, object); + } + writeStatement.setString(columnCount + 1, topiaId); + writeStatement.addBatch(); + + } + + if (useOutputWriter) { + + try { + + String arguments = generateArguments(columnNames, readResultSet); + String sql = String.format(updateStatementSql, arguments, topiaId); + writer.append(sql); + + } catch (IOException e) { + throw new RuntimeException("Could not updateRow", e); + } + + } + + if ((++index % fetchSize) == 0) { + flush(writeStatement, writer, tableName, index); + } + + } + + flush(writeStatement, writer, tableName, index); + + } + + protected String generateArguments(Iterable<String> columnNames, ResultSet readResultSet) throws SQLException, IOException { + + String statement = ""; + + for (String columnName : columnNames) { + + statement += ", SET " + columnName + " = "; + Object columnValue = readResultSet.getObject(columnName); + if (columnValue == null) { + statement += "NULL"; + continue; + } + + if (columnValue instanceof String) { + String stringValue = (String) columnValue; + statement += "'" + stringValue.replaceAll("'", "''") + "'"; + continue; + } + + if (columnValue instanceof Date) { + statement += "'" + columnValue + "'"; + continue; + } + + if (columnValue instanceof Blob) { + Blob blob = (Blob) columnValue; + SerialBlob serialBlob = new SerialBlob(blob); + try (ByteArrayOutputStream stringWriter = new ByteArrayOutputStream((int) serialBlob.length())) { + stringWriter.write(serialBlob.getBinaryStream()); + statement += "'" + new String(stringWriter.toByteArray()) + "'"; + } + continue; + } + + statement += columnValue; + + } + + return statement.substring(2); + + } + + protected String newUpdateStatementSql(TopiaSqlTable table, Iterable<String> columnNames) throws SQLException { + + StringBuilder columnNamesBuilder = new StringBuilder(); + + for (String columnName : columnNames) { + columnNamesBuilder.append(", SET ").append(columnName).append(" = ?"); + } + + String sql = String.format(UPDATE_STATEMENT, + table.getSchemaName(), + table.getTableName(), + columnNamesBuilder.substring(2)); + if (log.isDebugEnabled()) { + log.debug("Insert sql: " + sql); + } + + return sql; + + } + +} diff --git a/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/UpdateTablesRequest.java b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/UpdateTablesRequest.java new file mode 100644 index 0000000..703b5cd --- /dev/null +++ b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/UpdateTablesRequest.java @@ -0,0 +1,45 @@ +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 UpdateTablesRequest extends AbstractTablesRequest { + + public static Builder builder() { + return new Builder(); + } + + public static class Builder extends AbstractTablesRequestBuilder<Builder, UpdateTablesRequest> { + + public Builder() { + super(new UpdateTablesRequest()); + } + + } +} + -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
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 a36739d0fefc9fb5beb2fc40a2d7ec475ec82dae Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Jan 5 08:27:40 2016 +0100 Add delete tables action --- .../sql/batch/actions/DeleteTablesAction.java | 139 +++++++++++++++++++++ .../sql/batch/actions/DeleteTablesRequest.java | 46 +++++++ 2 files changed, 185 insertions(+) diff --git a/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/DeleteTablesAction.java b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/DeleteTablesAction.java new file mode 100644 index 0000000..f703ec8 --- /dev/null +++ b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/DeleteTablesAction.java @@ -0,0 +1,139 @@ +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 org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.topia.persistence.TopiaEntity; +import org.nuiton.topia.persistence.TopiaException; +import org.nuiton.topia.service.sql.batch.tables.TopiaSqlTable; + +import java.io.IOException; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.util.List; + +/** + * Created on 01/01/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.0.1 + */ +public class DeleteTablesAction extends AbstractTablesAction<DeleteTablesRequest> { + + public static final String DELETE_STATEMENT = "DELETE FROM %s.%s WHERE topiaId = '%%s'"; + /** + * Logger. + */ + private static final Log log = LogFactory.getLog(DeleteTablesAction.class); + + public DeleteTablesAction(DeleteTablesRequest request) { + super(request); + } + + @Override + protected void executeOnTable(DeleteTablesRequest request, TopiaSqlTable table, PreparedStatement readStatement) throws SQLException { + + ResultSet readResultSet = readStatement.getResultSet(); + + ResultSetMetaData readResultSetMetaData = readResultSet.getMetaData(); + int columnCount = readResultSetMetaData.getColumnCount(); + + List<String> columnNames = getColumnNames(readResultSetMetaData, columnCount); + + String topiaIdColumnName = TopiaEntity.PROPERTY_TOPIA_ID.toLowerCase(); + int topiaIdColumnIndex = columnNames.indexOf(topiaIdColumnName); + + boolean useOutputWriter = useOutputWriter(); + boolean useOutputDb = useOutputDb(); + + PreparedStatement writeStatement = null; + + String deleteStatementSql = newDeleteStatementSql(table); + + if (useOutputDb) { + + String sql = String.format(deleteStatementSql, "?"); + writeStatement = targetConnection.prepareStatement(sql); + + } + + int fetchSize = request.getFetchSize(); + String tableName = table.getFullyTableName(); + + long index = 0; + while (readResultSet.next()) { + + String topiaId = readResultSet.getString(topiaIdColumnIndex); + + if (log.isTraceEnabled()) { + log.trace("Delete " + topiaId); + } + + if (useOutputDb) { + + writeStatement.clearParameters(); + writeStatement.setObject(1, topiaId); + writeStatement.addBatch(); + + } + + if (useOutputWriter) { + + try { + + String sql = String.format(deleteStatementSql, topiaId); + writer.append(sql); + + } catch (IOException e) { + throw new TopiaException("Could not deleteRow", e); + } + + } + + if ((++index % fetchSize) == 0) { + flush(writeStatement, writer, tableName, index); + } + + } + + flush(writeStatement, writer, tableName, index); + + } + + protected String newDeleteStatementSql(TopiaSqlTable table) throws SQLException { + + String sql = String.format(DELETE_STATEMENT, + table.getSchemaName(), + table.getTableName()); + if (log.isDebugEnabled()) { + log.debug("Delete sql: " + sql); + } + + return sql; + + } + +} diff --git a/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/DeleteTablesRequest.java b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/DeleteTablesRequest.java new file mode 100644 index 0000000..690e67c --- /dev/null +++ b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/actions/DeleteTablesRequest.java @@ -0,0 +1,46 @@ +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 DeleteTablesRequest extends AbstractTablesRequest { + + + public static Builder builder() { + return new Builder(); + } + + public static class Builder extends AbstractTablesRequestBuilder<Builder, DeleteTablesRequest> { + + public Builder() { + super(new DeleteTablesRequest()); + } + + } +} + -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
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 e57b090066962ecdf5b5ddbc660531cef20bbc94 Author: Tony CHEMIT <chemit@codelutin.com> Date: Tue Jan 5 08:29:01 2016 +0100 Add TopiaSqlBatchService and his default implementation --- .../topia/service/sql/batch/SqlRequests.java | 401 +++++++++++++++++++++ .../service/sql/batch/TopiaSqlBatchService.java | 64 ++++ .../sql/batch/TopiaSqlBatchServiceImpl.java | 175 +++++++++ 3 files changed, 640 insertions(+) diff --git a/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/SqlRequests.java b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/SqlRequests.java new file mode 100644 index 0000000..f6f3286 --- /dev/null +++ b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/SqlRequests.java @@ -0,0 +1,401 @@ +package org.nuiton.topia.service.sql.batch; + +/* + * #%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.dialect.Dialect; +import org.nuiton.topia.persistence.TopiaApplicationContext; +import org.nuiton.topia.service.sql.batch.actions.AbstractSchemaRequest; +import org.nuiton.topia.service.sql.batch.actions.AbstractSqlRequest; +import org.nuiton.topia.service.sql.batch.actions.AbstractTablesRequest; +import org.nuiton.topia.service.sql.batch.actions.CreateSchemaRequest; +import org.nuiton.topia.service.sql.batch.actions.DeleteTablesRequest; +import org.nuiton.topia.service.sql.batch.actions.DropSchemaRequest; +import org.nuiton.topia.service.sql.batch.actions.ReplicateTablesRequest; +import org.nuiton.topia.service.sql.batch.actions.TopiaSqlTableSelectArgument; +import org.nuiton.topia.service.sql.batch.actions.UpdateTablesRequest; +import org.nuiton.topia.service.sql.batch.tables.TopiaSqlTables; + +import java.io.Writer; +import java.nio.file.Path; +import java.util.Iterator; + +/** + * Created on 04/01/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.0.1 + */ +public class SqlRequests implements Iterable<AbstractSqlRequest> { + + protected final ImmutableSet<AbstractSqlRequest> requests; + + protected SqlRequests(ImmutableSet<AbstractSqlRequest> requests) { + this.requests = requests; + } + + public static Builder builder() { + return new BuilderImpl(); + } + + public static <R extends AbstractSqlRequest> SqlRequests of(R request) { + return builder() + .from(request.getSourceTopiaApplicationContext()) + .to(request.getTargetTopiaApplicationContext()) + .to(request.getWriter()) + .addRequest(request) + .build(); + } + + @Override + public Iterator<AbstractSqlRequest> iterator() { + return requests.iterator(); + } + + public interface Builder extends BuilderAddRequestStep { + + Builder from(TopiaApplicationContext sourceTopiaApplicationContext); + + Builder to(TopiaApplicationContext targetTopiaApplicationContext); + + Builder to(Writer writer); + + } + + interface BuilderAddRequestStep { + + CreateSchemaRequestBuilder createSchemaBuilder(); + + DropSchemaRequestBuilder dropSchemaBuilder(); + + ReplicateTablesRequestBuilder replicateTablesBuilder(); + + UpdateTablesRequestBuilder updateTablesBuilder(); + + DeleteTablesRequestBuilder deleteTablesBuilder(); + + BuilderAddRequestStep addCreateSchema(CreateSchemaRequest request); + + BuilderAddRequestStep addDropSchema(DropSchemaRequest request); + + BuilderAddRequestStep addReplicateTables(ReplicateTablesRequest request); + + BuilderAddRequestStep addUpdateTables(UpdateTablesRequest request); + + BuilderAddRequestStep addDeleteTables(DeleteTablesRequest request); + + <R extends AbstractSqlRequest> BuilderAddRequestStep addRequest(R request); + + Builder flush(); + + SqlRequests build(); + + } + + protected static class BuilderImpl implements Builder { + + protected final ImmutableSet.Builder<AbstractSqlRequest> requestsBuilder = ImmutableSet.builder(); + protected TopiaApplicationContext sourceTopiaApplicationContext; + protected TopiaApplicationContext targetTopiaApplicationContext; + protected Writer writer; + + @Override + public Builder from(TopiaApplicationContext sourceTopiaApplicationContext) { + this.sourceTopiaApplicationContext = sourceTopiaApplicationContext; + return this; + } + + @Override + public Builder to(TopiaApplicationContext targetTopiaApplicationContext) { + this.targetTopiaApplicationContext = targetTopiaApplicationContext; + return this; + } + + @Override + public Builder to(Writer writer) { + this.writer = writer; + return this; + } + + @Override + public SqlRequests build() { + return new SqlRequests(requestsBuilder.build()); + } + + @Override + public <R extends AbstractSqlRequest> Builder addRequest(R request) { + requestsBuilder.add(request); + return this; + } + + @Override + public CreateSchemaRequestBuilder createSchemaBuilder() { + return new CreateSchemaRequestBuilder(this, initBuilder(CreateSchemaRequest.builder())); + } + + @Override + public DropSchemaRequestBuilder dropSchemaBuilder() { + return new DropSchemaRequestBuilder(this, initBuilder(DropSchemaRequest.builder())); + } + + @Override + public ReplicateTablesRequestBuilder replicateTablesBuilder() { + return new ReplicateTablesRequestBuilder(this, initBuilder(new ReplicateTablesRequest.Builder())); + } + + @Override + public UpdateTablesRequestBuilder updateTablesBuilder() { + return new UpdateTablesRequestBuilder(this, initBuilder(new UpdateTablesRequest.Builder())); + } + + @Override + public DeleteTablesRequestBuilder deleteTablesBuilder() { + return new DeleteTablesRequestBuilder(this, initBuilder(new DeleteTablesRequest.Builder())); + } + + @Override + public BuilderAddRequestStep addCreateSchema(CreateSchemaRequest request) { + return addRequest(request); + } + + @Override + public BuilderAddRequestStep addDropSchema(DropSchemaRequest request) { + return addRequest(request); + } + + @Override + public BuilderAddRequestStep addReplicateTables(ReplicateTablesRequest request) { + return addRequest(request); + } + + @Override + public BuilderAddRequestStep addUpdateTables(UpdateTablesRequest request) { + return addRequest(request); + } + + @Override + public BuilderAddRequestStep addDeleteTables(DeleteTablesRequest request) { + return addRequest(request); + } + + @Override + public Builder flush() { + return this; + } + + protected <B extends AbstractSqlRequest.AbstractSqlRequestBuilder<B, ?>> B initBuilder(B builder) { + return builder.from(sourceTopiaApplicationContext) + .to(targetTopiaApplicationContext) + .to(writer); + } + } + + public static class CreateSchemaRequestBuilder extends AbstractSchemaRequestBuilder<CreateSchemaRequest.Builder, CreateSchemaRequestBuilder> { + + public CreateSchemaRequestBuilder(BuilderImpl builder, CreateSchemaRequest.Builder delegate) { + super(builder, delegate); + } + + public CreateSchemaRequestBuilder setAddSchema(boolean addSchema) { + delegate.setAddSchema(addSchema); + return this; + } + + } + + public static class DropSchemaRequestBuilder extends AbstractSchemaRequestBuilder<DropSchemaRequest.Builder, DropSchemaRequestBuilder> { + + public DropSchemaRequestBuilder(BuilderImpl builder, DropSchemaRequest.Builder delegate) { + super(builder, delegate); + } + + public DropSchemaRequestBuilder setDropSchema(boolean dropSchema) { + delegate.setDropSchema(dropSchema); + return this; + } + + } + + public static class ReplicateTablesRequestBuilder extends AbstractTablesRequestBuilder<ReplicateTablesRequest.Builder, ReplicateTablesRequestBuilder> { + + public ReplicateTablesRequestBuilder(BuilderImpl builder, ReplicateTablesRequest.Builder delegate) { + super(builder, delegate); + } + + } + + public static class UpdateTablesRequestBuilder extends AbstractTablesRequestBuilder<UpdateTablesRequest.Builder, UpdateTablesRequestBuilder> { + + public UpdateTablesRequestBuilder(BuilderImpl builder, UpdateTablesRequest.Builder delegate) { + super(builder, delegate); + } + + } + + public static class DeleteTablesRequestBuilder extends AbstractTablesRequestBuilder<DeleteTablesRequest.Builder, DeleteTablesRequestBuilder> { + + public DeleteTablesRequestBuilder(BuilderImpl builder, DeleteTablesRequest.Builder delegate) { + super(builder, delegate); + } + + } + + protected static abstract class RequestBuilderImpl<R extends AbstractSqlRequest.AbstractSqlRequestBuilder, B extends RequestBuilderImpl> implements BuilderAddRequestStep { + + protected final Builder builder; + protected final R delegate; + + protected RequestBuilderImpl(BuilderImpl builder, R delegate) { + this.builder = builder; + this.delegate = delegate; + } + + @Override + public CreateSchemaRequestBuilder createSchemaBuilder() { + return flush().createSchemaBuilder(); + } + + @Override + public DropSchemaRequestBuilder dropSchemaBuilder() { + return flush().dropSchemaBuilder(); + } + + @Override + public ReplicateTablesRequestBuilder replicateTablesBuilder() { + return flush().replicateTablesBuilder(); + } + + @Override + public UpdateTablesRequestBuilder updateTablesBuilder() { + return flush().updateTablesBuilder(); + } + + @Override + public DeleteTablesRequestBuilder deleteTablesBuilder() { + return flush().deleteTablesBuilder(); + } + + @Override + public BuilderAddRequestStep addCreateSchema(CreateSchemaRequest request) { + return flush().addRequest(request); + } + + @Override + public BuilderAddRequestStep addDropSchema(DropSchemaRequest request) { + return flush().addRequest(request); + } + + @Override + public BuilderAddRequestStep addReplicateTables(ReplicateTablesRequest request) { + return flush().addRequest(request); + } + + @Override + public BuilderAddRequestStep addUpdateTables(UpdateTablesRequest request) { + return flush().addRequest(request); + } + + @Override + public BuilderAddRequestStep addDeleteTables(DeleteTablesRequest request) { + return addRequest(request); + } + + @Override + public <R extends AbstractSqlRequest> BuilderAddRequestStep addRequest(R request) { + return builder.addRequest(request); + } + + @Override + public SqlRequests build() { + return flush().build(); + } + + @Override + public Builder flush() { + addRequest(delegate.build()); + return builder; + } + +// protected BuilderAddRequestStep flushCurrentRequest() { +// return addRequest(delegate.build()); +// } + + protected B returnThis() { + return (B) this; + } + + } + + protected static class AbstractSchemaRequestBuilder<R extends AbstractSchemaRequest.AbstractSchemaRequestBuilder, B extends AbstractSchemaRequestBuilder> extends RequestBuilderImpl<R, B> { + + protected AbstractSchemaRequestBuilder(BuilderImpl builder, R delegate) { + super(builder, delegate); + } + + public B forH2() { + delegate.forH2(); + return returnThis(); + } + + public B forPostgres() { + delegate.forPostgres(); + return returnThis(); + } + + public B setDialect(Class<? extends Dialect> dialectType) { + delegate.setDialect(dialectType); + return returnThis(); + } + + public B setTemporaryPath(Path temporaryPath) { + delegate.setTemporaryPath(temporaryPath); + return returnThis(); + } + + } + + protected static class AbstractTablesRequestBuilder<R extends AbstractTablesRequest.AbstractTablesRequestBuilder, B extends AbstractTablesRequestBuilder> extends RequestBuilderImpl<R, B> { + + protected AbstractTablesRequestBuilder(BuilderImpl builder, R delegate) { + super(builder, delegate); + } + + public B setTables(TopiaSqlTables tables) { + delegate.setTables(tables); + return returnThis(); + } + + public B setFetchSize(int fetchSize) { + delegate.setFetchSize(fetchSize); + return returnThis(); + } + + public B setSelectArgument(TopiaSqlTableSelectArgument arg) { + delegate.setSelectArgument(arg); + return returnThis(); + } + + } + + +} diff --git a/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/TopiaSqlBatchService.java b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/TopiaSqlBatchService.java new file mode 100644 index 0000000..3f895ab --- /dev/null +++ b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/TopiaSqlBatchService.java @@ -0,0 +1,64 @@ +package org.nuiton.topia.service.sql.batch; + +/* + * #%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 org.nuiton.topia.persistence.TopiaService; +import org.nuiton.topia.service.sql.batch.actions.CreateSchemaRequest; +import org.nuiton.topia.service.sql.batch.actions.DeleteTablesRequest; +import org.nuiton.topia.service.sql.batch.actions.DropSchemaRequest; +import org.nuiton.topia.service.sql.batch.actions.ReplicateTablesRequest; +import org.nuiton.topia.service.sql.batch.actions.UpdateTablesRequest; + +/** + * Created on 04/01/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.0.1 + */ +public interface TopiaSqlBatchService extends TopiaService { + + SqlRequests.Builder requestBuilder(); + + CreateSchemaRequest.Builder createSchemaRequestBuilder(); + + DropSchemaRequest.Builder dropSchemaRequestBuilder(); + + ReplicateTablesRequest.Builder replicateTablesRequestBuilder(); + + UpdateTablesRequest.Builder updateTablesRequestBuilder(); + + DeleteTablesRequest.Builder deleteTablesRequestBuilder(); + + void execute(SqlRequests requests); + + void execute(CreateSchemaRequest request); + + void execute(DropSchemaRequest request); + + void execute(ReplicateTablesRequest request); + + void execute(UpdateTablesRequest request); + + void execute(DeleteTablesRequest request); + +} diff --git a/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/TopiaSqlBatchServiceImpl.java b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/TopiaSqlBatchServiceImpl.java new file mode 100644 index 0000000..1ea5fd2 --- /dev/null +++ b/topia-service-sql-batch/src/main/java/org/nuiton/topia/service/sql/batch/TopiaSqlBatchServiceImpl.java @@ -0,0 +1,175 @@ +package org.nuiton.topia.service.sql.batch; + +/* + * #%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.base.Preconditions; +import com.google.common.collect.ImmutableMap; +import org.nuiton.topia.persistence.TopiaApplicationContext; +import org.nuiton.topia.persistence.TopiaException; +import org.nuiton.topia.service.sql.batch.actions.AbstractSqlAction; +import org.nuiton.topia.service.sql.batch.actions.AbstractSqlRequest; +import org.nuiton.topia.service.sql.batch.actions.CreateSchemaAction; +import org.nuiton.topia.service.sql.batch.actions.CreateSchemaRequest; +import org.nuiton.topia.service.sql.batch.actions.DeleteTablesAction; +import org.nuiton.topia.service.sql.batch.actions.DeleteTablesRequest; +import org.nuiton.topia.service.sql.batch.actions.DropSchemaAction; +import org.nuiton.topia.service.sql.batch.actions.DropSchemaRequest; +import org.nuiton.topia.service.sql.batch.actions.ReplicateTablesAction; +import org.nuiton.topia.service.sql.batch.actions.ReplicateTablesRequest; +import org.nuiton.topia.service.sql.batch.actions.UpdateTablesAction; +import org.nuiton.topia.service.sql.batch.actions.UpdateTablesRequest; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.Iterator; +import java.util.Map; + +/** + * Created on 04/01/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.0.1 + */ +public class TopiaSqlBatchServiceImpl implements TopiaSqlBatchService { + + private static final ImmutableMap<Class, Class> ACTION_MAPPING = ImmutableMap + .<Class, Class>builder() + .put(CreateSchemaRequest.class, CreateSchemaAction.class) + .put(DropSchemaRequest.class, DropSchemaAction.class) + .put(ReplicateTablesRequest.class, ReplicateTablesAction.class) + .put(UpdateTablesRequest.class, UpdateTablesAction.class) + .put(DeleteTablesRequest.class, DeleteTablesAction.class) + .build(); + private TopiaApplicationContext topiaApplicationContext; + + @Override + public void initTopiaService(TopiaApplicationContext topiaApplicationContext, Map<String, String> serviceConfiguration) { + this.topiaApplicationContext = topiaApplicationContext; + } + + @Override + public void close() { + + } + + @Override + public SqlRequests.Builder requestBuilder() { + return SqlRequests.builder().from(topiaApplicationContext); + } + + @Override + public CreateSchemaRequest.Builder createSchemaRequestBuilder() { + return CreateSchemaRequest.builder().from(topiaApplicationContext); + } + + @Override + public DropSchemaRequest.Builder dropSchemaRequestBuilder() { + return DropSchemaRequest.builder().from(topiaApplicationContext); + } + + @Override + public ReplicateTablesRequest.Builder replicateTablesRequestBuilder() { + return ReplicateTablesRequest.builder().from(topiaApplicationContext); + } + + @Override + public UpdateTablesRequest.Builder updateTablesRequestBuilder() { + return UpdateTablesRequest.builder().from(topiaApplicationContext); + } + + @Override + public DeleteTablesRequest.Builder deleteTablesRequestBuilder() { + return DeleteTablesRequest.builder().from(topiaApplicationContext); + } + + @Override + public void execute(SqlRequests requests) { + + Iterator<AbstractSqlRequest> sqlRequestIterator = requests.iterator(); + while (sqlRequestIterator.hasNext()) { + + AbstractSqlRequest sqlRequest = sqlRequestIterator.next(); + AbstractSqlAction<?> action = createAction(sqlRequest); + + action.run(); + + //FIXME Review transaction management + + boolean needCommit = !sqlRequestIterator.hasNext(); + if (needCommit) { + action.commit(); + } + } + + } + + protected <R extends AbstractSqlRequest, A extends AbstractSqlAction<R>> A createAction(R request) { + + Preconditions.checkNotNull(request, "Request can't be null"); + Class<A> actionType = ACTION_MAPPING.get(request.getClass()); + Preconditions.checkNotNull(actionType, "Could not find action for request type: " + request.getClass().getName()); + + Constructor<A> constructor; + try { + constructor = actionType.getConstructor(request.getClass()); + } catch (NoSuchMethodException e) { + throw new TopiaException(e); + } + try { + return constructor.newInstance(request); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { + throw new TopiaException(e); + } + + } + + @Override + public void execute(CreateSchemaRequest request) { + executeOneRequest(request); + } + + @Override + public void execute(DropSchemaRequest request) { + executeOneRequest(request); + } + + @Override + public void execute(ReplicateTablesRequest request) { + executeOneRequest(request); + } + + @Override + public void execute(UpdateTablesRequest request) { + executeOneRequest(request); + } + + @Override + public void execute(DeleteTablesRequest request) { + executeOneRequest(request); + } + + protected void executeOneRequest(AbstractSqlRequest request) { + SqlRequests sqlRequests = SqlRequests.of(request); + execute(sqlRequests); + } +} -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
participants (1)
-
nuiton.org scm