Browse Source

移除 sharding 推荐动态数据源分片或者 mybatis-mate

hubin 3 years ago
parent
commit
cd092990a8

+ 0 - 36
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/handler/sharding/ShardingNode.java

@@ -1,36 +0,0 @@
-/*
- * Copyright (c) 2011-2021, baomidou (jobob@qq.com).
- *
- * Licensed 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 com.baomidou.mybatisplus.extension.plugins.handler.sharding;
-
-import lombok.AllArgsConstructor;
-import lombok.Getter;
-import lombok.Setter;
-
-import java.util.List;
-
-/**
- * @author zengzhihong
- * @since 2021-01-14
- */
-@AllArgsConstructor
-@Getter
-@Setter
-public class ShardingNode<T, C> {
-
-    private T node;
-
-    private List<C> list;
-}

+ 0 - 317
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/handler/sharding/ShardingNodeExtractor.java

@@ -1,317 +0,0 @@
-/*
- * Copyright (c) 2011-2021, baomidou (jobob@qq.com).
- *
- * Licensed 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 com.baomidou.mybatisplus.extension.plugins.handler.sharding;
-
-import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
-import lombok.Getter;
-import net.sf.jsqlparser.expression.*;
-import net.sf.jsqlparser.expression.operators.arithmetic.*;
-import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
-import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
-import net.sf.jsqlparser.expression.operators.relational.*;
-import net.sf.jsqlparser.schema.Column;
-import net.sf.jsqlparser.schema.Table;
-import net.sf.jsqlparser.statement.Statement;
-import net.sf.jsqlparser.statement.insert.Insert;
-import net.sf.jsqlparser.statement.select.Join;
-import net.sf.jsqlparser.statement.select.PlainSelect;
-import net.sf.jsqlparser.statement.select.SubJoin;
-import net.sf.jsqlparser.statement.select.SubSelect;
-import net.sf.jsqlparser.statement.update.Update;
-import net.sf.jsqlparser.util.TablesNamesFinder;
-
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * @author zengzhihong
- * @since 2021-01-14
- */
-public class ShardingNodeExtractor extends TablesNamesFinder {
-
-    @Getter
-    private final List<ShardingNode<Table, ShardingNode<String, Integer>>> nodes;
-
-    private Column currentColumn;
-
-    public ShardingNodeExtractor(Statement statement) {
-        this.nodes = new ArrayList<>();
-        super.getTableList(statement);
-    }
-
-
-    @Override
-    public void visit(Table table) {
-        nodes.add(new ShardingNode<>(table, new ArrayList<>()));
-    }
-
-
-    @Override
-    public void visit(Column column) {
-        this.currentColumn = column;
-        final ShardingNode<Table, ShardingNode<String, Integer>> tableNode =
-                obtainTableNode(column);
-        // SQL正确的前提下 tableNode一定不为null
-        if (null == tableNode) {
-            throw ExceptionUtils.mpe("please determine the alias on sql");
-        }
-        final ShardingNode<String, Integer> columnNode =
-                tableNode.getList().stream().filter(i -> i.getNode().equals(column.getColumnName())).findFirst().orElse(null);
-        if (null == columnNode) {
-            tableNode.getList().add(new ShardingNode<>(column.getColumnName(), new ArrayList<>()));
-        }
-    }
-
-    @Override
-    public void visit(Insert insert) {
-        visit(insert.getTable());
-        if (insert.getColumns() != null && insert.getItemsList() != null && insert.getItemsList() instanceof ExpressionList) {
-            final ExpressionList itemsList = (ExpressionList) insert.getItemsList();
-            if (null != itemsList.getExpressions() && insert.getColumns().size() == itemsList.getExpressions().size()) {
-                for (int i = 0; i < insert.getColumns().size(); i++) {
-                    final Expression expression = itemsList.getExpressions().get(i);
-                    if (!(expression instanceof JdbcParameter)) {
-                        continue;
-                    }
-                    visit(insert.getColumns().get(i));
-                    visit((JdbcParameter) expression);
-                }
-            }
-        }
-        if (insert.getSelect() != null) {
-            visit(insert.getSelect());
-        }
-    }
-
-    @Override
-    public void visit(Update update) {
-        visit(update.getTable());
-        if (update.getStartJoins() != null) {
-            for (Join join : update.getStartJoins()) {
-                join.getRightItem().accept(this);
-            }
-        }
-        /*if (update.getExpressions() != null) {
-            for (Expression expression : update.getExpressions()) {
-                expression.accept(this);
-            }
-        }*/
-
-        if (update.getFromItem() != null) {
-            update.getFromItem().accept(this);
-        }
-
-        if (update.getJoins() != null) {
-            for (Join join : update.getJoins()) {
-                join.getRightItem().accept(this);
-            }
-        }
-
-        if (update.getWhere() != null) {
-            update.getWhere().accept(this);
-        }
-    }
-
-    @Override
-    public void visit(JdbcParameter jdbcParameter) {
-        final ShardingNode<Table, ShardingNode<String, Integer>> tableNode =
-                obtainTableNode(this.currentColumn);
-        if (null == tableNode) {
-            throw ExceptionUtils.mpe("please determine the alias on sql");
-        }
-        final ShardingNode<String, Integer> columnNode =
-                tableNode.getList().stream().filter(i -> i.getNode().equals(this.currentColumn.getColumnName())).findFirst().orElse(null);
-        if (null == columnNode) {
-            throw ExceptionUtils.mpe("please determine the alias on sql");
-        }
-        columnNode.getList().add(jdbcParameter.getIndex());
-    }
-
-    @Override
-    public void visit(ExpressionList expressionList) {
-        for (Expression expression : expressionList.getExpressions()) {
-            expression.accept(this);
-        }
-    }
-
-    private ShardingNode<Table, ShardingNode<String, Integer>> obtainTableNode(Column column) {
-        return null == column || null == column.getTable() || null == column.getTable().getName() ? nodes.get(0) :
-                nodes.stream().filter(i -> i.getNode().getAlias().getName().equals(column.getTable().getName())).findFirst().orElse(null);
-    }
-
-
-    @Override
-    public void visit(JdbcNamedParameter jdbcNamedParameter) {
-
-    }
-
-
-    @Override
-    public void visit(Parenthesis parenthesis) {
-        parenthesis.getExpression().accept(this);
-    }
-
-
-    @Override
-    public void visit(Addition addition) {
-        visitBinaryExpression(addition);
-    }
-
-    @Override
-    public void visit(Division division) {
-        visitBinaryExpression(division);
-    }
-
-
-    @Override
-    public void visit(Subtraction subtraction) {
-        visitBinaryExpression(subtraction);
-    }
-
-    @Override
-    public void visit(AndExpression andExpression) {
-        visitBinaryExpression(andExpression);
-    }
-
-    @Override
-    public void visit(OrExpression orExpression) {
-        visitBinaryExpression(orExpression);
-    }
-
-    @Override
-    public void visit(Between between) {
-        between.getLeftExpression().accept(this);
-        between.getBetweenExpressionStart().accept(this);
-        between.getBetweenExpressionEnd().accept(this);
-    }
-
-    @Override
-    public void visit(EqualsTo equalsTo) {
-        visitBinaryExpression(equalsTo);
-    }
-
-    @Override
-    public void visit(GreaterThan greaterThan) {
-        visitBinaryExpression(greaterThan);
-    }
-
-    @Override
-    public void visit(GreaterThanEquals greaterThanEquals) {
-        visitBinaryExpression(greaterThanEquals);
-    }
-
-    @Override
-    public void visit(InExpression inExpression) {
-        inExpression.getLeftExpression().accept(this);
-        if (null != inExpression.getLeftExpression()) {
-            inExpression.getLeftExpression().accept(this);
-        }
-        if (null != inExpression.getRightItemsList()) {
-            inExpression.getRightItemsList().accept(this);
-        }
-    }
-
-    @Override
-    public void visit(LikeExpression likeExpression) {
-        visitBinaryExpression(likeExpression);
-    }
-
-    @Override
-    public void visit(MinorThan minorThan) {
-        visitBinaryExpression(minorThan);
-    }
-
-    @Override
-    public void visit(MinorThanEquals minorThanEquals) {
-        visitBinaryExpression(minorThanEquals);
-    }
-
-    @Override
-    public void visit(NotEqualsTo notEqualsTo) {
-        visitBinaryExpression(notEqualsTo);
-    }
-
-    @Override
-    public void visit(ExistsExpression existsExpression) {
-        existsExpression.getRightExpression().accept(this);
-    }
-
-    @Override
-    public void visit(AllComparisonExpression allComparisonExpression) {
-        allComparisonExpression.getSubSelect().getSelectBody().accept(this);
-    }
-
-    @Override
-    public void visit(AnyComparisonExpression anyComparisonExpression) {
-        anyComparisonExpression.getSubSelect().getSelectBody().accept(this);
-    }
-
-    @Override
-    public void visit(Concat concat) {
-        visitBinaryExpression(concat);
-    }
-
-    @Override
-    public void visit(Matches matches) {
-        visitBinaryExpression(matches);
-    }
-
-    @Override
-    public void visit(BitwiseAnd bitwiseAnd) {
-        visitBinaryExpression(bitwiseAnd);
-    }
-
-    @Override
-    public void visit(BitwiseOr bitwiseOr) {
-        visitBinaryExpression(bitwiseOr);
-    }
-
-    @Override
-    public void visit(BitwiseXor bitwiseXor) {
-        visitBinaryExpression(bitwiseXor);
-    }
-
-    @Override
-    public void visit(CastExpression cast) {
-    }
-
-    @Override
-    public void visit(SubSelect subSelect) {
-        subSelect.getSelectBody().accept(this);
-    }
-
-    @Override
-    public void visit(SubJoin subjoin) {
-        subjoin.getLeft().accept(this);
-        for (Join join : subjoin.getJoinList()) {
-            join.getRightItem().accept(this);
-        }
-    }
-
-
-    @Override
-    public void visit(PlainSelect plainSelect) {
-        plainSelect.getFromItem().accept(this);
-        if (plainSelect.getJoins() != null) {
-            for (Join join : plainSelect.getJoins()) {
-                join.getRightItem().accept(this);
-            }
-        }
-        if (plainSelect.getWhere() != null) {
-            plainSelect.getWhere().accept(this);
-        }
-    }
-}

+ 0 - 35
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/handler/sharding/ShardingProcessor.java

@@ -1,35 +0,0 @@
-/*
- * Copyright (c) 2011-2021, baomidou (jobob@qq.com).
- *
- * Licensed 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 com.baomidou.mybatisplus.extension.plugins.handler.sharding;
-
-import java.util.List;
-import java.util.Map;
-
-/**
- * @author zengzhihong
- * @since 2021-01-14
- */
-public interface ShardingProcessor {
-
-    /**
-     * 分表执行
-     *
-     * @param strategy       策略
-     * @param shardingValues 分片字段和字段值
-     * @return 真实表名
-     */
-    String doSharding(ShardingStrategy strategy, Map<String, List<Object>> shardingValues);
-}

+ 0 - 28
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/handler/sharding/ShardingRuleEnum.java

@@ -1,28 +0,0 @@
-/*
- * Copyright (c) 2011-2021, baomidou (jobob@qq.com).
- *
- * Licensed 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 com.baomidou.mybatisplus.extension.plugins.handler.sharding;
-
-/**
- * @author zengzhihong
- * @since 2021-01-14
- */
-public enum ShardingRuleEnum {
-
-    /**
-     * = in 绝对定位
-     */
-    ABSOLUTE
-}

+ 0 - 77
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/handler/sharding/ShardingStrategy.java

@@ -1,77 +0,0 @@
-/*
- * Copyright (c) 2011-2021, baomidou (jobob@qq.com).
- *
- * Licensed 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 com.baomidou.mybatisplus.extension.plugins.handler.sharding;
-
-import com.baomidou.mybatisplus.core.toolkit.StringPool;
-import lombok.EqualsAndHashCode;
-import lombok.Getter;
-import lombok.Setter;
-
-import java.util.Arrays;
-import java.util.List;
-
-/**
- * @author zengzhihong
- * @since 2021-01-14
- */
-@EqualsAndHashCode
-public class ShardingStrategy {
-
-    /**
-     * 逻辑表名 如:order_info
-     */
-    @Getter
-    @Setter
-    private String logicTable;
-
-    /**
-     * 分片字段 , 隔开 如: id,create_time
-     */
-    private String column;
-
-    /**
-     * 分片规则 {@link ShardingRuleEnum}
-     */
-    @Getter
-    @Setter
-    private ShardingRuleEnum rule;
-
-    @Getter
-    @Setter
-    private Class<? extends ShardingProcessor> processor;
-
-    private List<String> shardingColumnList;
-
-    public ShardingStrategy(String logicTable, String column, Class<? extends ShardingProcessor> processor) {
-        this(logicTable, column, ShardingRuleEnum.ABSOLUTE, processor);
-    }
-
-    public ShardingStrategy(String logicTable, String column, ShardingRuleEnum rule, Class<? extends ShardingProcessor> processor) {
-        this.logicTable = logicTable;
-        this.rule = rule;
-        this.processor = processor;
-        this.setColumn(column);
-    }
-
-    public void setColumn(String column) {
-        this.column = column;
-        this.shardingColumnList = Arrays.asList(this.column.split(StringPool.COMMA));
-    }
-
-    public boolean containsColumn(String column) {
-        return shardingColumnList.contains(column);
-    }
-}

+ 0 - 172
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/ShardingInnerInterceptor.java

@@ -1,172 +0,0 @@
-/*
- * Copyright (c) 2011-2021, baomidou (jobob@qq.com).
- *
- * Licensed 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 com.baomidou.mybatisplus.extension.plugins.inner;
-
-import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
-import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
-import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
-import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
-import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
-import com.baomidou.mybatisplus.extension.parser.JsqlParserSupport;
-import com.baomidou.mybatisplus.extension.plugins.handler.sharding.ShardingNode;
-import com.baomidou.mybatisplus.extension.plugins.handler.sharding.ShardingNodeExtractor;
-import com.baomidou.mybatisplus.extension.plugins.handler.sharding.ShardingProcessor;
-import com.baomidou.mybatisplus.extension.plugins.handler.sharding.ShardingStrategy;
-import lombok.AllArgsConstructor;
-import lombok.Getter;
-import net.sf.jsqlparser.schema.Table;
-import net.sf.jsqlparser.statement.Statement;
-import net.sf.jsqlparser.statement.delete.Delete;
-import net.sf.jsqlparser.statement.insert.Insert;
-import net.sf.jsqlparser.statement.select.Select;
-import net.sf.jsqlparser.statement.update.Update;
-import org.apache.ibatis.executor.BatchExecutor;
-import org.apache.ibatis.executor.Executor;
-import org.apache.ibatis.executor.ReuseExecutor;
-import org.apache.ibatis.executor.statement.StatementHandler;
-import org.apache.ibatis.mapping.ParameterMapping;
-import org.apache.ibatis.mapping.ParameterMode;
-import org.apache.ibatis.reflection.MetaObject;
-
-import java.sql.Connection;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.stream.Collectors;
-
-/**
- * 分表拦截器
- * <p>不支持带参数子查询 不支持复杂SQL 单个Statement只能返回一个真实表名</p>
- *
- * @author zengzhihong
- * @since 2021-01-14
- */
-public class ShardingInnerInterceptor extends JsqlParserSupport implements InnerInterceptor {
-
-    private final Map<String, ShardingStrategyProcessor> shardingMap;
-
-    public ShardingInnerInterceptor(ShardingStrategy... shardingStrategies) {
-        shardingMap = Arrays.stream(shardingStrategies).collect(Collectors.toMap(ShardingStrategy::getLogicTable, i -> new ShardingStrategyProcessor(i, ClassUtils.newInstance(i.getProcessor()))));
-    }
-
-    @Override
-    public void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) {
-        PluginUtils.MPStatementHandler mpSh = PluginUtils.mpStatementHandler(sh);
-        final Executor executor = mpSh.executor();
-        // BatchExecutor或者ReuseExecutor 由beforeGetBoundSql方法已经处理过了
-        if (executor instanceof BatchExecutor || executor instanceof ReuseExecutor) {
-            return;
-        }
-        doParse(mpSh);
-    }
-
-    @Override
-    public void beforeGetBoundSql(StatementHandler sh) {
-        doParse(PluginUtils.mpStatementHandler(sh));
-    }
-
-    private void doParse(PluginUtils.MPStatementHandler mpSh) {
-        if (InterceptorIgnoreHelper.willIgnoreSharding(mpSh.mappedStatement().getId())) {
-            return;
-        }
-        PluginUtils.MPBoundSql mpBs = mpSh.mPBoundSql();
-        mpBs.sql(parserMulti(mpBs.sql(), mpSh));
-    }
-
-    @Override
-    protected void processSelect(Select select, int index, String sql, Object obj) {
-        process(select, (PluginUtils.MPStatementHandler) obj);
-    }
-
-    @Override
-    protected void processInsert(Insert insert, int index, String sql, Object obj) {
-        process(insert, (PluginUtils.MPStatementHandler) obj);
-    }
-
-    @Override
-    protected void processUpdate(Update update, int index, String sql, Object obj) {
-        process(update, (PluginUtils.MPStatementHandler) obj);
-    }
-
-    @Override
-    protected void processDelete(Delete delete, int index, String sql, Object obj) {
-        process(delete, (PluginUtils.MPStatementHandler) obj);
-    }
-
-    private void process(Statement statement, PluginUtils.MPStatementHandler mpSh) {
-        final ShardingNodeExtractor shardingNodeExtractor = new ShardingNodeExtractor(statement);
-        if (CollectionUtils.isEmpty(shardingNodeExtractor.getNodes())) {
-            return;
-        }
-        final List<Object> parameterValues = handleParameter(mpSh);
-        for (ShardingNode<Table, ShardingNode<String, Integer>> tableNode : shardingNodeExtractor.getNodes()) {
-            final ShardingStrategyProcessor strategyProcessor = shardingMap.get(tableNode.getNode().getName());
-            if (null == strategyProcessor) {
-                continue;
-            }
-            Map<String, List<Object>> shardingValues = new HashMap<>(tableNode.getList().size());
-            for (ShardingNode<String, Integer> columnNode : tableNode.getList()) {
-                if (CollectionUtils.isEmpty(columnNode.getList()) || !strategyProcessor.getStrategy().containsColumn(columnNode.getNode())) {
-                    continue;
-                }
-                shardingValues.put(columnNode.getNode(),
-                        columnNode.getList().stream().map(i -> parameterValues.get(i - 1)).collect(Collectors.toList()));
-            }
-            if (CollectionUtils.isEmpty(shardingValues)) {
-                throw ExceptionUtils.mpe("no fragment sharding column found");
-            }
-            tableNode.getNode().setName(strategyProcessor.getProcessor().doSharding(strategyProcessor.getStrategy(), shardingValues));
-        }
-    }
-
-    private List<Object> handleParameter(PluginUtils.MPStatementHandler mpSh) {
-        List<Object> values = new ArrayList<>();
-        final Object parameterObject = mpSh.boundSql().getParameterObject();
-        List<ParameterMapping> parameterMappings = mpSh.boundSql().getParameterMappings();
-        if (parameterMappings != null) {
-            for (ParameterMapping parameterMapping : parameterMappings) {
-                if (parameterMapping.getMode() != ParameterMode.OUT) {
-                    Object value;
-                    String propertyName = parameterMapping.getProperty();
-                    if (mpSh.boundSql().hasAdditionalParameter(propertyName)) { // issue #448 ask first for
-                        // additional params
-                        value = mpSh.boundSql().getAdditionalParameter(propertyName);
-                    } else if (parameterObject == null) {
-                        value = null;
-                    } else if (mpSh.configuration().getTypeHandlerRegistry().hasTypeHandler(parameterObject.getClass())) {
-                        value = parameterObject;
-                    } else {
-                        MetaObject metaObject = mpSh.configuration().newMetaObject(parameterObject);
-                        value = metaObject.getValue(propertyName);
-                    }
-                    values.add(value);
-                }
-            }
-        }
-        return values;
-    }
-
-    @AllArgsConstructor
-    @Getter
-    static class ShardingStrategyProcessor {
-
-        private final ShardingStrategy strategy;
-
-        private final ShardingProcessor processor;
-    }
-}