Search in sources :

Example 56 with Supplier

use of io.reactivex.rxjava3.functions.Supplier in project Mycat2 by MyCATApache.

the class VertxExecuter method explainInsert.

@SneakyThrows
public static List<EachSQL> explainInsert(SQLInsertStatement statementArg, List<Object> paramArg) {
    final SQLInsertStatement statement = statementArg.clone();
    SQLInsertStatement template = statement.clone();
    template.getColumns().clear();
    template.getValuesList().clear();
    SQLExprTableSource tableSource = statement.getTableSource();
    String tableName = SQLUtils.normalize(tableSource.getTableName());
    String schemaName = SQLUtils.normalize(tableSource.getSchema());
    MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
    ShardingTable table = (ShardingTable) metadataManager.getTable(schemaName, tableName);
    SimpleColumnInfo autoIncrementColumn = table.getAutoIncrementColumn();
    // //////////////////////////////////////////////////////////////////////////////////////////////////////////////
    List<SQLName> columns = (List) statement.getColumns();
    if (columns.isEmpty()) {
        if (statement.getValues().getValues().size() == table.getColumns().size()) {
            for (SimpleColumnInfo column : table.getColumns()) {
                statement.addColumn(new SQLIdentifierExpr("`" + column.getColumnName() + "`"));
            }
        }
    }
    boolean fillAutoIncrement = needFillAutoIncrement(table, columns);
    if (fillAutoIncrement) {
        columns.add(new SQLIdentifierExpr(autoIncrementColumn.getColumnName()));
    }
    // //////////////////////////////////////////////////////////////////////////////////////////////////////////////
    Map<String, Integer> columnMap = new HashMap<>();
    int index = 0;
    for (SQLName column : columns) {
        columnMap.put(SQLUtils.normalize(column.getSimpleName()), index);
        ++index;
    }
    List<List> paramsList = (!paramArg.isEmpty() && paramArg.get(0) instanceof List) ? (List) paramArg : Collections.singletonList(paramArg);
    return paramsList.stream().flatMap(params -> {
        List<EachSQL> sqls = new LinkedList<>();
        for (SQLInsertStatement.ValuesClause valuesClause : statement.getValuesList()) {
            valuesClause = valuesClause.clone();
            SQLInsertStatement primaryStatement = template.clone();
            primaryStatement.getColumns().addAll(columns);
            primaryStatement.getValuesList().add(valuesClause);
            List<SQLExpr> values = primaryStatement.getValues().getValues();
            if (fillAutoIncrement) {
                Supplier<Number> stringSupplier = table.nextSequence();
                values.add(PreparedStatement.fromJavaObject(stringSupplier.get()));
            }
            Map<String, List<RangeVariable>> variables = compute(columns, values, params);
            Partition mPartition = table.getShardingFuntion().calculateOne((Map) variables);
            SQLExprTableSource exprTableSource = primaryStatement.getTableSource();
            exprTableSource.setSimpleName(mPartition.getTable());
            exprTableSource.setSchema(mPartition.getSchema());
            sqls.add(new EachSQL(mPartition.getTargetName(), primaryStatement.toString(), getNewParams(params, primaryStatement)));
            for (ShardingTable indexTable : table.getIndexTables()) {
                // fillIndexTableShardingKeys(variables, indexTable);
                Partition sPartition = indexTable.getShardingFuntion().calculateOne((Map) variables);
                SQLInsertStatement eachStatement = template.clone();
                eachStatement.getColumns().clear();
                fillIndexTableShardingKeys(columnMap, values, indexTable.getColumns(), eachStatement);
                SQLExprTableSource eachTableSource = eachStatement.getTableSource();
                eachTableSource.setSimpleName(sPartition.getTable());
                eachTableSource.setSchema(sPartition.getSchema());
                sqls.add(new EachSQL(sPartition.getTargetName(), eachStatement.toString(), getNewParams(params, eachStatement)));
            }
        }
        return sqls.stream();
    }).collect(Collectors.toList());
}
Also used : QueryPlanner(io.mycat.calcite.spm.QueryPlanner) io.mycat(io.mycat) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData) LoggerFactory(org.slf4j.LoggerFactory) SQLName(com.alibaba.druid.sql.ast.SQLName) Tuple(io.vertx.sqlclient.Tuple) MySQLColumnDef(io.mycat.api.collector.MySQLColumnDef) NewMycatConnection(io.mycat.newquery.NewMycatConnection) SQLReplaceable(com.alibaba.druid.sql.ast.SQLReplaceable) MycatView(io.mycat.calcite.logical.MycatView) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MysqlCollector(io.mycat.newquery.MysqlCollector) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) com.alibaba.druid.sql.ast.expr(com.alibaba.druid.sql.ast.expr) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) lombok(lombok) ArrayBindable(org.apache.calcite.runtime.ArrayBindable) NormalTable(io.mycat.calcite.table.NormalTable) MysqlObjectArrayRow(io.mycat.api.collector.MysqlObjectArrayRow) XaSqlConnection(cn.mycat.vertx.xa.XaSqlConnection) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) DbType(com.alibaba.druid.DbType) DrdsRunnerHelper(io.mycat.calcite.DrdsRunnerHelper) NotNull(org.jetbrains.annotations.NotNull) ShardingTable(io.mycat.calcite.table.ShardingTable) Iterables(com.google.common.collect.Iterables) java.util(java.util) MySQLClient(io.vertx.mysqlclient.MySQLClient) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Function(java.util.function.Function) Supplier(java.util.function.Supplier) CompositeFuture(io.vertx.core.CompositeFuture) MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) ImmutableList(com.google.common.collect.ImmutableList) SqlConnection(io.vertx.sqlclient.SqlConnection) Process(io.mycat.Process) Observable(io.reactivex.rxjava3.core.Observable) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) RowSet(io.vertx.sqlclient.RowSet) GlobalTable(io.mycat.calcite.table.GlobalTable) RelDataType(org.apache.calcite.rel.type.RelDataType) SQLUtils(com.alibaba.druid.sql.SQLUtils) Logger(org.slf4j.Logger) ExecutorProvider(io.mycat.calcite.ExecutorProvider) Enumerable(org.apache.calcite.linq4j.Enumerable) TimeUnit(java.util.concurrent.TimeUnit) MycatSQLEvalVisitorUtils(com.alibaba.druid.sql.visitor.MycatSQLEvalVisitorUtils) MysqlPayloadObject(io.mycat.api.collector.MysqlPayloadObject) CodeExecuterContext(io.mycat.calcite.CodeExecuterContext) Row(io.vertx.sqlclient.Row) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) com.alibaba.druid.sql.ast.statement(com.alibaba.druid.sql.ast.statement) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SQLName(com.alibaba.druid.sql.ast.SQLName) ImmutableList(com.google.common.collect.ImmutableList) Supplier(java.util.function.Supplier) ShardingTable(io.mycat.calcite.table.ShardingTable) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

Test (org.junit.Test)37 TestException (io.reactivex.rxjava3.exceptions.TestException)33 Observable (io.reactivex.rxjava3.core.Observable)13 IOException (java.io.IOException)9 InOrder (org.mockito.InOrder)8 java.util (java.util)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Disposable (io.reactivex.rxjava3.disposables.Disposable)5 Supplier (io.reactivex.rxjava3.functions.Supplier)5 ImmediateThinScheduler (io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler)5 TestObserver (io.reactivex.rxjava3.observers.TestObserver)5 Observer (io.reactivex.rxjava3.core.Observer)4 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)4 ImmutableList (com.google.common.collect.ImmutableList)3 io.reactivex.rxjava3.processors (io.reactivex.rxjava3.processors)3 TestHelper (io.reactivex.rxjava3.testsupport.TestHelper)3 java.util.function (java.util.function)3 java.util.stream (java.util.stream)3 Assert.assertFalse (org.junit.Assert.assertFalse)3 SQLUtils (com.alibaba.druid.sql.SQLUtils)2