Search in sources :

Example 11 with NotSupportYetException

use of io.seata.common.exception.NotSupportYetException in project seata by seata.

the class MultiUpdateExecutor method beforeImage.

@Override
protected TableRecords beforeImage() throws SQLException {
    if (sqlRecognizers.size() == 1) {
        UpdateExecutor executor = new UpdateExecutor<>(statementProxy, statementCallback, sqlRecognizers.get(0));
        return executor.beforeImage();
    }
    final TableMeta tmeta = getTableMeta(sqlRecognizers.get(0).getTableName());
    final ArrayList<List<Object>> paramAppenderList = new ArrayList<>();
    Set<String> updateColumnsSet = new HashSet<>();
    StringBuilder whereCondition = new StringBuilder();
    boolean noWhereCondition = false;
    for (SQLRecognizer recognizer : sqlRecognizers) {
        sqlRecognizer = recognizer;
        SQLUpdateRecognizer sqlUpdateRecognizer = (SQLUpdateRecognizer) recognizer;
        ParametersHolder parametersHolder = statementProxy instanceof ParametersHolder ? (ParametersHolder) statementProxy : null;
        if (StringUtils.isNotBlank(sqlUpdateRecognizer.getLimit(parametersHolder, paramAppenderList))) {
            throw new NotSupportYetException("Multi update SQL with limit condition is not support yet !");
        }
        if (StringUtils.isNotBlank(sqlUpdateRecognizer.getOrderBy())) {
            throw new NotSupportYetException("Multi update SQL with orderBy condition is not support yet !");
        }
        List<String> updateColumns = sqlUpdateRecognizer.getUpdateColumns();
        updateColumnsSet.addAll(updateColumns);
        if (noWhereCondition) {
            continue;
        }
        String whereConditionStr = buildWhereCondition(sqlUpdateRecognizer, paramAppenderList);
        if (StringUtils.isBlank(whereConditionStr)) {
            noWhereCondition = true;
        } else {
            if (whereCondition.length() > 0) {
                whereCondition.append(" OR ");
            }
            whereCondition.append(whereConditionStr);
        }
    }
    StringBuilder prefix = new StringBuilder("SELECT ");
    final StringBuilder suffix = new StringBuilder(" FROM ").append(getFromTableInSQL());
    if (noWhereCondition) {
        // select all rows
        paramAppenderList.clear();
    } else {
        suffix.append(" WHERE ").append(whereCondition);
    }
    suffix.append(" FOR UPDATE");
    final StringJoiner selectSQLAppender = new StringJoiner(", ", prefix, suffix.toString());
    if (ONLY_CARE_UPDATE_COLUMNS) {
        if (!containsPK(new ArrayList<>(updateColumnsSet))) {
            selectSQLAppender.add(getColumnNamesInSQL(tmeta.getEscapePkNameList(getDbType())));
        }
        for (String updateCol : updateColumnsSet) {
            selectSQLAppender.add(updateCol);
        }
    } else {
        for (String columnName : tmeta.getAllColumns().keySet()) {
            selectSQLAppender.add(ColumnUtils.addEscape(columnName, getDbType()));
        }
    }
    return buildTableRecords(tmeta, selectSQLAppender.toString(), paramAppenderList);
}
Also used : ArrayList(java.util.ArrayList) SQLUpdateRecognizer(io.seata.sqlparser.SQLUpdateRecognizer) ParametersHolder(io.seata.sqlparser.ParametersHolder) SQLRecognizer(io.seata.sqlparser.SQLRecognizer) ArrayList(java.util.ArrayList) List(java.util.List) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) NotSupportYetException(io.seata.common.exception.NotSupportYetException) StringJoiner(java.util.StringJoiner) HashSet(java.util.HashSet)

Example 12 with NotSupportYetException

use of io.seata.common.exception.NotSupportYetException in project seata by seata.

the class AbstractDMLBaseExecutor method executeAutoCommitFalse.

/**
 * Execute auto commit false t.
 *
 * @param args the args
 * @return the t
 * @throws Exception the exception
 */
protected T executeAutoCommitFalse(Object[] args) throws Exception {
    if (!JdbcConstants.MYSQL.equalsIgnoreCase(getDbType()) && isMultiPk()) {
        throw new NotSupportYetException("multi pk only support mysql!");
    }
    TableRecords beforeImage = beforeImage();
    T result = statementCallback.execute(statementProxy.getTargetStatement(), args);
    TableRecords afterImage = afterImage(beforeImage);
    prepareUndoLog(beforeImage, afterImage);
    return result;
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) NotSupportYetException(io.seata.common.exception.NotSupportYetException)

Example 13 with NotSupportYetException

use of io.seata.common.exception.NotSupportYetException in project seata by seata.

the class BaseInsertExecutor method parsePkValuesFromStatement.

/**
 * parse primary key value from statement.
 * @return
 */
protected Map<String, List<Object>> parsePkValuesFromStatement() {
    // insert values including PK
    SQLInsertRecognizer recognizer = (SQLInsertRecognizer) sqlRecognizer;
    final Map<String, Integer> pkIndexMap = getPkIndex();
    if (pkIndexMap.isEmpty()) {
        throw new ShouldNeverHappenException("pkIndex is not found");
    }
    Map<String, List<Object>> pkValuesMap = new HashMap<>();
    boolean ps = true;
    if (statementProxy instanceof PreparedStatementProxy) {
        PreparedStatementProxy preparedStatementProxy = (PreparedStatementProxy) statementProxy;
        List<List<Object>> insertRows = recognizer.getInsertRows(pkIndexMap.values());
        if (insertRows != null && !insertRows.isEmpty()) {
            Map<Integer, ArrayList<Object>> parameters = preparedStatementProxy.getParameters();
            final int rowSize = insertRows.size();
            int totalPlaceholderNum = -1;
            for (List<Object> row : insertRows) {
                // insert parameter count will than the actual +1
                if (row.isEmpty()) {
                    continue;
                }
                int currentRowPlaceholderNum = -1;
                for (Object r : row) {
                    if (PLACEHOLDER.equals(r)) {
                        totalPlaceholderNum += 1;
                        currentRowPlaceholderNum += 1;
                    }
                }
                String pkKey;
                int pkIndex;
                List<Object> pkValues;
                for (Map.Entry<String, Integer> entry : pkIndexMap.entrySet()) {
                    pkKey = entry.getKey();
                    pkValues = pkValuesMap.get(pkKey);
                    if (Objects.isNull(pkValues)) {
                        pkValues = new ArrayList<>(rowSize);
                    }
                    pkIndex = entry.getValue();
                    Object pkValue = row.get(pkIndex);
                    if (PLACEHOLDER.equals(pkValue)) {
                        int currentRowNotPlaceholderNumBeforePkIndex = 0;
                        for (int n = 0, len = row.size(); n < len; n++) {
                            Object r = row.get(n);
                            if (n < pkIndex && !PLACEHOLDER.equals(r)) {
                                currentRowNotPlaceholderNumBeforePkIndex++;
                            }
                        }
                        int idx = totalPlaceholderNum - currentRowPlaceholderNum + pkIndex - currentRowNotPlaceholderNumBeforePkIndex;
                        ArrayList<Object> parameter = parameters.get(idx + 1);
                        pkValues.addAll(parameter);
                    } else {
                        pkValues.add(pkValue);
                    }
                    if (!pkValuesMap.containsKey(ColumnUtils.delEscape(pkKey, getDbType()))) {
                        pkValuesMap.put(ColumnUtils.delEscape(pkKey, getDbType()), pkValues);
                    }
                }
            }
        }
    } else {
        ps = false;
        List<List<Object>> insertRows = recognizer.getInsertRows(pkIndexMap.values());
        for (List<Object> row : insertRows) {
            pkIndexMap.forEach((pkKey, pkIndex) -> {
                List<Object> pkValues = pkValuesMap.get(pkKey);
                if (Objects.isNull(pkValues)) {
                    pkValuesMap.put(ColumnUtils.delEscape(pkKey, getDbType()), Lists.newArrayList(row.get(pkIndex)));
                } else {
                    pkValues.add(row.get(pkIndex));
                }
            });
        }
    }
    if (pkValuesMap.isEmpty()) {
        throw new ShouldNeverHappenException();
    }
    boolean b = this.checkPkValues(pkValuesMap, ps);
    if (!b) {
        throw new NotSupportYetException(String.format("not support sql [%s]", sqlRecognizer.getOriginalSQL()));
    }
    return pkValuesMap;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) ArrayList(java.util.ArrayList) List(java.util.List) PreparedStatementProxy(io.seata.rm.datasource.PreparedStatementProxy) HashMap(java.util.HashMap) Map(java.util.Map) NotSupportYetException(io.seata.common.exception.NotSupportYetException) SQLInsertRecognizer(io.seata.sqlparser.SQLInsertRecognizer)

Example 14 with NotSupportYetException

use of io.seata.common.exception.NotSupportYetException in project seata by seata.

the class RegistryFactory method getInstance.

public static Registry getInstance() {
    RegistryType registryType;
    String registryTypeName = ConfigurationFactory.getInstance().getConfig(ConfigurationKeys.METRICS_PREFIX + ConfigurationKeys.METRICS_REGISTRY_TYPE, null);
    if (!StringUtils.isNullOrEmpty(registryTypeName)) {
        try {
            registryType = RegistryType.getType(registryTypeName);
        } catch (Exception exx) {
            throw new NotSupportYetException("not support metrics registry type: " + registryTypeName);
        }
        return EnhancedServiceLoader.load(Registry.class, Objects.requireNonNull(registryType).getName());
    }
    return null;
}
Also used : NotSupportYetException(io.seata.common.exception.NotSupportYetException) NotSupportYetException(io.seata.common.exception.NotSupportYetException)

Example 15 with NotSupportYetException

use of io.seata.common.exception.NotSupportYetException in project seata by seata.

the class BeanUtils method mapToObject.

/**
 * map to object
 *
 * @param map the map
 * @param clazz the Object class
 * @return the object
 */
public static Object mapToObject(Map<String, String> map, Class<?> clazz) {
    if (CollectionUtils.isEmpty(map)) {
        return null;
    }
    try {
        Object instance = clazz.newInstance();
        Field[] fields = instance.getClass().getDeclaredFields();
        for (Field field : fields) {
            int modifiers = field.getModifiers();
            if (Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers)) {
                continue;
            }
            boolean accessible = field.isAccessible();
            field.setAccessible(true);
            Class<?> type = field.getType();
            if (type == Date.class) {
                if (!StringUtils.isEmpty(map.get(field.getName()))) {
                    field.set(instance, new Date(Long.valueOf(map.get(field.getName()))));
                }
            } else if (type == Long.class) {
                if (!StringUtils.isEmpty(map.get(field.getName()))) {
                    field.set(instance, Long.valueOf(map.get(field.getName())));
                }
            } else if (type == Integer.class) {
                if (!StringUtils.isEmpty(map.get(field.getName()))) {
                    field.set(instance, Integer.valueOf(map.get(field.getName())));
                }
            } else if (type == Double.class) {
                if (!StringUtils.isEmpty(map.get(field.getName()))) {
                    field.set(instance, Double.valueOf(map.get(field.getName())));
                }
            } else if (type == String.class) {
                if (!StringUtils.isEmpty(map.get(field.getName()))) {
                    field.set(instance, map.get(field.getName()));
                }
            }
            field.setAccessible(accessible);
        }
        return instance;
    } catch (IllegalAccessException e) {
        throw new NotSupportYetException("map to " + clazz.toString() + " failed:" + e.getMessage(), e);
    } catch (InstantiationException e) {
        throw new NotSupportYetException("map to " + clazz.toString() + " failed:" + e.getMessage(), e);
    }
}
Also used : Field(java.lang.reflect.Field) NotSupportYetException(io.seata.common.exception.NotSupportYetException) Date(java.util.Date)

Aggregations

NotSupportYetException (io.seata.common.exception.NotSupportYetException)16 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)6 SQLJoinTableSource (com.alibaba.druid.sql.ast.statement.SQLJoinTableSource)6 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)6 ArrayList (java.util.ArrayList)4 List (java.util.List)3 MySqlOutputVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor)2 OracleOutputVisitor (com.alibaba.druid.sql.dialect.oracle.visitor.OracleOutputVisitor)2 PGOutputVisitor (com.alibaba.druid.sql.dialect.postgresql.visitor.PGOutputVisitor)2 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)2 ParametersHolder (io.seata.sqlparser.ParametersHolder)2 SQLRecognizer (io.seata.sqlparser.SQLRecognizer)2 Field (java.lang.reflect.Field)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 StringJoiner (java.util.StringJoiner)2 ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)1 PreparedStatementProxy (io.seata.rm.datasource.PreparedStatementProxy)1