Search in sources :

Example 1 with VertxRowSetImpl

use of io.vertx.mysqlclient.impl.codec.VertxRowSetImpl in project Mycat2 by MyCATApache.

the class RowSetJdbcPreparedJdbcQuery method innerExecute.

private RowSet<Row> innerExecute(Tuple tuple) throws SQLException {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(sql);
    }
    try (PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
        setParams(tuple, preparedStatement);
        if (!preparedStatement.execute()) {
            VertxRowSetImpl vertxRowSet = new VertxRowSetImpl();
            vertxRowSet.setAffectRow(preparedStatement.getUpdateCount());
            ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
            if (generatedKeys != null) {
                if (generatedKeys.next()) {
                    Number object = (Number) generatedKeys.getObject(1);
                    if (object != null) {
                        vertxRowSet.setLastInsertId(object.longValue());
                    }
                }
            }
            return (vertxRowSet);
        }
        ResultSet resultSet = preparedStatement.getResultSet();
        if (resultSet == null) {
            return new VertxRowSetImpl();
        }
        JdbcRowMetaData metaData = new JdbcRowMetaData(resultSet.getMetaData());
        int columnCount = metaData.getColumnCount();
        List<ColumnDescriptor> columnDescriptors = new ArrayList<>();
        for (int i = 0; i < columnCount; i++) {
            int index = i;
            columnDescriptors.add(new ColumnDescriptor() {

                @Override
                public String name() {
                    return metaData.getColumnName(index);
                }

                @Override
                public boolean isArray() {
                    return false;
                }

                @Override
                public String typeName() {
                    return null;
                }

                @Override
                public JDBCType jdbcType() {
                    return JDBCType.valueOf(metaData.getColumnType(index));
                }
            });
        }
        VertxRowSetImpl vertxRowSet = new VertxRowSetImpl();
        RowDesc rowDesc = new RowDesc(metaData.getColumnList(), columnDescriptors);
        while (resultSet.next()) {
            JDBCRow jdbcRow = new MycatRow(rowDesc);
            for (int i = 0; i < columnCount; i++) {
                jdbcRow.addValue(resultSet.getObject(i + 1));
            }
            vertxRowSet.list.add(jdbcRow);
        }
        return (vertxRowSet);
    }
}
Also used : JdbcRowMetaData(io.mycat.beans.mycat.JdbcRowMetaData) ColumnDescriptor(io.vertx.sqlclient.desc.ColumnDescriptor) JDBCRow(io.vertx.jdbcclient.impl.JDBCRow) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) VertxRowSetImpl(io.vertx.mysqlclient.impl.codec.VertxRowSetImpl) MySQLRowDesc(io.vertx.mysqlclient.impl.MySQLRowDesc) RowDesc(io.vertx.sqlclient.impl.RowDesc)

Example 2 with VertxRowSetImpl

use of io.vertx.mysqlclient.impl.codec.VertxRowSetImpl in project Mycat2 by MyCATApache.

the class RowSetMySqlPreparedTextQuery method executeBatch.

@Override
public Future<RowSet<Row>> executeBatch(List<Tuple> batch) {
    Future<Void> future = Future.succeededFuture();
    List<long[]> list = new ArrayList<>();
    for (Tuple tuple : batch) {
        String eachSql = apply(sql, toObjects(tuple));
        future = future.flatMap(unused -> {
            Query<RowSet<Row>> query = connection.query(eachSql);
            return query.execute().map(rows -> {
                list.add(new long[] { rows.rowCount(), rows.property(MySQLClient.LAST_INSERTED_ID) });
                return null;
            });
        });
    }
    return future.map(unused -> {
        long[] reduce = list.stream().reduce(new long[] { 0, 0 }, (longs, longs2) -> new long[] { longs[0] + longs2[0], Math.max(longs[1], longs2[1]) });
        VertxRowSetImpl vertxRowSet = new VertxRowSetImpl();
        vertxRowSet.setAffectRow(reduce[0]);
        vertxRowSet.setLastInsertId(reduce[1]);
        return vertxRowSet;
    });
}
Also used : io.vertx.sqlclient(io.vertx.sqlclient) List(java.util.List) MySQLClient(io.vertx.mysqlclient.MySQLClient) VertxRowSetImpl(io.vertx.mysqlclient.impl.codec.VertxRowSetImpl) AbstractMySqlConnectionImpl.apply(io.mycat.vertxmycat.AbstractMySqlConnectionImpl.apply) AbstractMySqlConnectionImpl.toObjects(io.mycat.vertxmycat.AbstractMySqlConnectionImpl.toObjects) Collector(java.util.stream.Collector) Future(io.vertx.core.Future) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) VertxRowSetImpl(io.vertx.mysqlclient.impl.codec.VertxRowSetImpl)

Example 3 with VertxRowSetImpl

use of io.vertx.mysqlclient.impl.codec.VertxRowSetImpl in project Mycat2 by MyCATApache.

the class RowSetQuery method execute.

@Override
public Future<RowSet<Row>> execute() {
    VertxRowSetImpl vertxRowSet = new VertxRowSetImpl();
    StreamMysqlCollector streamMysqlCollector = new StreamMysqlCollector() {

        @Override
        public void onColumnDefinitions(MySQLRowDesc columnDefinitions) {
            vertxRowSet.setColumnDescriptor(columnDefinitions.columnDescriptor());
        }

        @Override
        public void onRow(Row row) {
            vertxRowSet.list.add(row);
        }
    };
    return runTextQuery(sql, mySQLClientSession.mySQLClientSession, streamMysqlCollector).map(voidSqlResult -> {
        vertxRowSet.setAffectRow(voidSqlResult.rowCount());
        vertxRowSet.setLastInsertId(voidSqlResult.property(MySQLClient.LAST_INSERTED_ID));
        return vertxRowSet;
    });
}
Also used : MySQLRowDesc(io.vertx.mysqlclient.impl.MySQLRowDesc) StreamMysqlCollector(io.vertx.mysqlclient.impl.codec.StreamMysqlCollector) VertxRowSetImpl(io.vertx.mysqlclient.impl.codec.VertxRowSetImpl) Row(io.vertx.sqlclient.Row)

Aggregations

VertxRowSetImpl (io.vertx.mysqlclient.impl.codec.VertxRowSetImpl)3 MySQLRowDesc (io.vertx.mysqlclient.impl.MySQLRowDesc)2 ArrayList (java.util.ArrayList)2 JdbcRowMetaData (io.mycat.beans.mycat.JdbcRowMetaData)1 AbstractMySqlConnectionImpl.apply (io.mycat.vertxmycat.AbstractMySqlConnectionImpl.apply)1 AbstractMySqlConnectionImpl.toObjects (io.mycat.vertxmycat.AbstractMySqlConnectionImpl.toObjects)1 Future (io.vertx.core.Future)1 JDBCRow (io.vertx.jdbcclient.impl.JDBCRow)1 MySQLClient (io.vertx.mysqlclient.MySQLClient)1 StreamMysqlCollector (io.vertx.mysqlclient.impl.codec.StreamMysqlCollector)1 io.vertx.sqlclient (io.vertx.sqlclient)1 Row (io.vertx.sqlclient.Row)1 ColumnDescriptor (io.vertx.sqlclient.desc.ColumnDescriptor)1 RowDesc (io.vertx.sqlclient.impl.RowDesc)1 PreparedStatement (java.sql.PreparedStatement)1 List (java.util.List)1 Function (java.util.function.Function)1 Collector (java.util.stream.Collector)1