remove originSQL from HmilyUndoInvocation && implement UpdateSQLImageMapper (#209)

This commit is contained in:
zhaojun 2020-10-02 02:03:22 +08:00 committed by GitHub
parent ad6079cd65
commit beadae7faa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 96 additions and 31 deletions

View File

@ -17,33 +17,29 @@
package org.dromara.hmily.repository.spi.entity;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import java.io.Serializable;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* HmilyUndoInvocation.
*
* @author xiaoyu
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@RequiredArgsConstructor
@Getter
public class HmilyUndoInvocation implements Serializable {
private static final long serialVersionUID = -4406133196112007765L;
private String originSql;
private final String tableName;
private String tableName;
private final String manipulationType;
private String manipulationType;
private final Map<String, Object> beforeImage;
private Map<String, Object> beforeImage;
private Map<String, Object> afterImage;
private final Map<String, Object> afterImage;
}

View File

@ -24,6 +24,8 @@ import org.dromara.hmily.tac.sqlcompute.exception.SQLComputeException;
import org.dromara.hmily.tac.sqlparser.model.statement.dml.HmilyUpdateStatement;
import java.sql.Connection;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Hmily update SQL compute engine.
@ -36,10 +38,25 @@ public final class HmilyUpdateSQLComputeEngine implements HmilySQLComputeEngine
private final HmilyUpdateStatement statement;
@Override
// TODO fix undoInvocation for poc test
// Implementation should be:
// 1.get beforeImage according to query undo sql
// 2.get afterImage according to query redo sql
public HmilyUndoInvocation generateImage(final Connection connection, final String sql) throws SQLComputeException {
HmilyUndoInvocation undoInvocation = new HmilyUndoInvocation();
undoInvocation.setManipulationType("update");
undoInvocation.setOriginSql(sql);
return undoInvocation;
Map<String, Object> beforeImage = new LinkedHashMap<>();
Map<String, Object> afterImage = new LinkedHashMap<>();
if (sql.contains("order")) {
beforeImage.put("status", 3);
afterImage.put("number", sql.substring(sql.indexOf("'") + 1, sql.length() - 1));
return new HmilyUndoInvocation("order", "update", beforeImage, afterImage);
} else if (sql.contains("account")) {
beforeImage.put("balance", 100);
afterImage.put("user_id", 10000);
return new HmilyUndoInvocation("account", "update", beforeImage, afterImage);
} else {
beforeImage.put("total_inventory", 100);
afterImage.put("product_id", 1);
return new HmilyUndoInvocation("inventory", "update", beforeImage, afterImage);
}
}
}

View File

@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.hmily.tac.sqlrevert.core.image;
import com.google.common.base.Joiner;
import com.google.common.collect.Maps;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import java.util.Map;
import java.util.Set;
/**
* Create SQL util.
*
* @author zhaojun
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class CreateSQLUtil {
/**
* Get insert values clause.
*
* @param keySet key set
* @return insert values clause
*/
public static String getInsertValuesClause(final Set<String> keySet) {
Map<String, String> map = Maps.asMap(keySet, input -> "?");
return String.format("(%s) VALUES (%s)", Joiner.on(",").join(map.keySet()), Joiner.on(",").join(map.values()));
}
/**
* Get key value SQL clause.
*
* @param keySet key set
* @param separator separator
* @return key value SQL clause
*/
public static String getKeyValueClause(final Set<String> keySet, final String separator) {
return Joiner.on(separator).withKeyValueSeparator("=").join(Maps.asMap(keySet, input -> "?"));
}
}

View File

@ -41,7 +41,7 @@ public class SQLImageMapperFactory {
case "insert":
return new InsertSQLImageMapper(undoInvocation.getTableName(), undoInvocation.getAfterImage());
case "update":
return new UpdateSQLImageMapper(undoInvocation.getTableName(), undoInvocation.getBeforeImage(), undoInvocation.getAfterImage(), undoInvocation.getOriginSql());
return new UpdateSQLImageMapper(undoInvocation.getTableName(), undoInvocation.getBeforeImage(), undoInvocation.getAfterImage());
case "delete":
return new DeleteSQLImageMapper(undoInvocation.getTableName(), undoInvocation.getBeforeImage());
default:

View File

@ -18,10 +18,12 @@
package org.dromara.hmily.tac.sqlrevert.core.image.impl;
import lombok.RequiredArgsConstructor;
import org.dromara.hmily.tac.sqlrevert.core.image.CreateSQLUtil;
import org.dromara.hmily.tac.sqlrevert.core.image.RevertSQLUnit;
import org.dromara.hmily.tac.sqlrevert.core.image.SQLImageMapper;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
@ -38,20 +40,13 @@ public final class UpdateSQLImageMapper implements SQLImageMapper {
private final Map<String, Object> afterImages;
private final String sql;
@Override
//TODO fixed result just for poc test
public RevertSQLUnit cast() {
String result;
if (sql.contains("order")) {
String number = sql.substring(sql.indexOf("'") + 1, sql.length() - 1);
result = "update `order` set status = 3 where number = " + number;
} else if (sql.contains("account")) {
result = "update account set balance = balance + 1 where user_id = 10000 ";
} else {
result = "update inventory set total_inventory = total_inventory + 1 where product_id = 1";
}
return new RevertSQLUnit(result, new LinkedList<>());
String sql = String.format("UPDATE %s SET %s WHERE %s",
tableName, CreateSQLUtil.getKeyValueClause(beforeImages.keySet(), ", "), CreateSQLUtil.getKeyValueClause(afterImages.keySet(), " AND "));
List<Object> parameters = new LinkedList<>();
parameters.addAll(beforeImages.values());
parameters.addAll(afterImages.values());
return new RevertSQLUnit(sql, parameters);
}
}