Search in sources :

Example 1 with SQLReplaceable

use of com.alibaba.druid.sql.ast.SQLReplaceable in project Mycat2 by MyCATApache.

the class PreparedStatement method getSQLStatementByBindValue.

public SQLStatement getSQLStatementByBindValue(BindValue[] values) {
    if (this.bindValues != values) {
        throw new AssertionError();
    }
    SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(this.statement.toString());
    boolean hasBlob = !(sqlStatement instanceof SQLSelectStatement);
    sqlStatement.accept(new MySqlASTVisitorAdapter() {

        int index;

        @Override
        public void endVisit(SQLVariantRefExpr x) {
            if ("?".equalsIgnoreCase(x.getName())) {
                Object o = null;
                if (index < bindValues.length) {
                    io.mycat.BindValue value = bindValues[index++];
                    if (!value.isNull) {
                        o = value.getJavaObject(hasBlob);
                    }
                }
                SQLReplaceable parent = (SQLReplaceable) x.getParent();
                parent.replace(x, PreparedStatement.fromJavaObject(o));
            }
            super.endVisit(x);
        }
    });
    return sqlStatement;
}
Also used : MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLReplaceable(com.alibaba.druid.sql.ast.SQLReplaceable)

Example 2 with SQLReplaceable

use of com.alibaba.druid.sql.ast.SQLReplaceable in project Mycat2 by MyCATApache.

the class NewMycatConnectionImpl method paramize.

public static String paramize(String sql, List<Object> params) {
    if (params.isEmpty())
        return sql;
    if (sql.startsWith("be")) {
        return sql;
    }
    SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql);
    sqlStatement.accept(new MySqlASTVisitorAdapter() {

        int index;

        @Override
        public void endVisit(SQLVariantRefExpr x) {
            if ("?".equalsIgnoreCase(x.getName())) {
                if (index < params.size()) {
                    Object value = params.get(index++);
                    SQLReplaceable parent = (SQLReplaceable) x.getParent();
                    parent.replace(x, io.mycat.PreparedStatement.fromJavaObject(value));
                }
            }
            super.endVisit(x);
        }
    });
    sql = sqlStatement.toString();
    return sql;
}
Also used : MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLReplaceable(com.alibaba.druid.sql.ast.SQLReplaceable)

Example 3 with SQLReplaceable

use of com.alibaba.druid.sql.ast.SQLReplaceable in project Mycat2 by MyCATApache.

the class NewVertxConnectionImpl method update.

@Override
public Future<SqlResult> update(String sql, List<Object> params) {
    LOGGER.debug("sql:{}", sql);
    if (!sql.startsWith("begin") && !sql.startsWith("XA")) {
        SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql);
        sqlStatement.accept(new MySqlASTVisitorAdapter() {

            int index;

            @Override
            public void endVisit(SQLVariantRefExpr x) {
                if ("?".equalsIgnoreCase(x.getName())) {
                    if (index < params.size()) {
                        Object value = params.get(index++);
                        SQLReplaceable parent = (SQLReplaceable) x.getParent();
                        parent.replace(x, io.mycat.PreparedStatement.fromJavaObject(value));
                    }
                }
                super.endVisit(x);
            }
        });
        sql = sqlStatement.toString();
    }
    Query<io.vertx.sqlclient.RowSet<Row>> preparedStatementFuture = this.mySQLConnection.query(sql);
    Future<SqlResult> sqlResultFuture = preparedStatementFuture.execute().map(rows -> {
        int affectRows = rows.rowCount();
        long insertId = Optional.ofNullable(rows.property(MySQLClient.LAST_INSERTED_ID)).orElse(0L);
        return SqlResult.of(affectRows, insertId);
    });
    return mapException(sqlResultFuture);
}
Also used : MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLReplaceable(com.alibaba.druid.sql.ast.SQLReplaceable) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)

Example 4 with SQLReplaceable

use of com.alibaba.druid.sql.ast.SQLReplaceable in project Mycat2 by MyCATApache.

the class VertxExecuter method rewriteInsertBatchedStatements.

public static List<EachSQL> rewriteInsertBatchedStatements(Iterable<EachSQL> eachSQLs, int batchSize) {
    @Data
    @AllArgsConstructor
    @EqualsAndHashCode
    class key {

        String target;

        String sql;
    }
    Map<key, SQLInsertStatement> map = new ConcurrentHashMap<>();
    final Function<Map.Entry<key, SQLInsertStatement>, EachSQL> finalFunction = i -> {
        key key = i.getKey();
        SQLInsertStatement value = i.getValue();
        return new EachSQL(key.getTarget(), value.toString(), Collections.emptyList());
    };
    LinkedList<EachSQL> res = new LinkedList<>();
    for (EachSQL eachSQL : eachSQLs) {
        String target = eachSQL.getTarget();
        String sql = eachSQL.getSql();
        SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql);
        List<Object> sqlParams = eachSQL.getParams();
        if (sqlStatement instanceof SQLInsertStatement) {
            SQLInsertStatement insertStatement = (SQLInsertStatement) sqlStatement;
            key key = new key(target, sql);
            final SQLInsertStatement nowInsertStatement = map.computeIfAbsent(key, key1 -> {
                SQLInsertStatement clone = insertStatement.clone();
                clone.getValuesList().clear();
                return clone;
            });
            MySqlASTVisitorAdapter mySqlASTVisitorAdapter = new MySqlASTVisitorAdapter() {

                @Override
                public boolean visit(SQLVariantRefExpr x) {
                    SQLReplaceable parent = (SQLReplaceable) x.getParent();
                    parent.replace(x, PreparedStatement.fromJavaObject(sqlParams.get(x.getIndex())));
                    return false;
                }
            };
            List<EachSQL> list = new LinkedList<>();
            for (SQLInsertStatement.ValuesClause valuesClause : insertStatement.getValuesList()) {
                valuesClause = valuesClause.clone();
                nowInsertStatement.addValueCause(valuesClause);
                valuesClause.accept(mySqlASTVisitorAdapter);
                if (nowInsertStatement.getValuesList().size() >= batchSize) {
                    EachSQL e = finalFunction.apply(new AbstractMap.SimpleEntry(key, nowInsertStatement.clone()));
                    list.add(e);
                    nowInsertStatement.getValuesList().clear();
                } else {
                    continue;
                }
            }
            res.addAll(list);
        } else {
            res.add(eachSQL);
        }
    }
    res.addAll(map.entrySet().stream().map(finalFunction).collect(Collectors.toList()));
    return res;
}
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) MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLReplaceable(com.alibaba.druid.sql.ast.SQLReplaceable) MysqlPayloadObject(io.mycat.api.collector.MysqlPayloadObject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 5 with SQLReplaceable

use of com.alibaba.druid.sql.ast.SQLReplaceable in project Mycat2 by MyCATApache.

the class AbstractMySqlConnectionImpl method apply.

public static String apply(String parameterizedSql, List<Object> parameters) {
    SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(parameterizedSql);
    sqlStatement.accept(new MySqlASTVisitorAdapter() {

        @Override
        public void endVisit(SQLVariantRefExpr x) {
            SQLReplaceable parent = (SQLReplaceable) x.getParent();
            parent.replace(x, fromJavaObject(parameters.get(x.getIndex())));
        }
    });
    return sqlStatement.toString();
}
Also used : MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLReplaceable(com.alibaba.druid.sql.ast.SQLReplaceable)

Aggregations

SQLReplaceable (com.alibaba.druid.sql.ast.SQLReplaceable)7 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)6 MySqlASTVisitorAdapter (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter)6 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)3 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 XaSqlConnection (cn.mycat.vertx.xa.XaSqlConnection)1 DbType (com.alibaba.druid.DbType)1 SQLUtils (com.alibaba.druid.sql.SQLUtils)1 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLObject (com.alibaba.druid.sql.ast.SQLObject)1 com.alibaba.druid.sql.ast.expr (com.alibaba.druid.sql.ast.expr)1 com.alibaba.druid.sql.ast.statement (com.alibaba.druid.sql.ast.statement)1 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)1 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)1 MycatSQLEvalVisitorUtils (com.alibaba.druid.sql.visitor.MycatSQLEvalVisitorUtils)1 ImmutableList (com.google.common.collect.ImmutableList)1 Iterables (com.google.common.collect.Iterables)1 io.mycat (io.mycat)1 Process (io.mycat.Process)1 MySQLColumnDef (io.mycat.api.collector.MySQLColumnDef)1