Search in sources :

Example 1 with JdbcRowMetaData

use of io.mycat.beans.mycat.JdbcRowMetaData in project Mycat2 by MyCATApache.

the class MycatResultSet method of.

@SneakyThrows
public static MycatResultSet of(List<ResultSet> resultSets) {
    ResultSet resultSet = resultSets.get(0);
    MycatRowMetaData mycatRowMetaData = new CopyMycatRowMetaData(new JdbcRowMetaData(resultSet.getMetaData()));
    int columnCount = mycatRowMetaData.getColumnCount();
    List<List<Object>> rows = new ArrayList<>();
    for (ResultSet set : resultSets) {
        while (set.next()) {
            List<Object> row = new ArrayList<>();
            for (int i = 0; i < columnCount; i++) {
                row.add(set.getObject(i + 1));
            }
            rows.add(row);
        }
    }
    return new MycatResultSet(mycatRowMetaData, rows);
}
Also used : JdbcRowMetaData(io.mycat.beans.mycat.JdbcRowMetaData) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData) SneakyThrows(lombok.SneakyThrows)

Example 2 with JdbcRowMetaData

use of io.mycat.beans.mycat.JdbcRowMetaData in project Mycat2 by MyCATApache.

the class PrototypeService method getMycatRowMetaDataPrepareSQLByTargetName.

private Optional<MycatRowMetaData> getMycatRowMetaDataPrepareSQLByTargetName(String targetName, String sql) {
    JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
    try (DefaultConnection connection = jdbcConnectionManager.getConnection(targetName)) {
        Connection rawConnection = connection.getRawConnection();
        PreparedStatement preparedStatement = rawConnection.prepareStatement(sql);
        ResultSetMetaData metaData = preparedStatement.getMetaData();
        if (metaData != null) {
            return Optional.of(new CopyMycatRowMetaData(new JdbcRowMetaData(metaData)));
        }
    } catch (Exception e) {
        LOGGER.warn("can not get meta from {} {}", targetName, e);
        return Optional.empty();
    }
    return Optional.empty();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcRowMetaData(io.mycat.beans.mycat.JdbcRowMetaData) Connection(java.sql.Connection) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) PreparedStatement(java.sql.PreparedStatement) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData)

Example 3 with JdbcRowMetaData

use of io.mycat.beans.mycat.JdbcRowMetaData 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 4 with JdbcRowMetaData

use of io.mycat.beans.mycat.JdbcRowMetaData in project Mycat2 by MyCATApache.

the class RowSetJdbcPreparedJdbcQuery method extracted.

public static <R> void extracted(Promise<SqlResult<R>> promise, Statement statement, ResultSet resultSet, Collector<Row, ?, R> collector) throws SQLException {
    try {
        if (resultSet == null) {
            Object o = collector.supplier().get();
            Function<Object, Object> finisher = (Function) collector.finisher();
            promise.complete(new MySqlResult<>(0, 0, 0, (R) finisher.apply(o), Collections.emptyList()));
            return;
        }
        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));
                }
            });
        }
        RowDesc rowDesc = new RowDesc(metaData.getColumnList(), columnDescriptors);
        ColumnDefPacket[] columnDefPackets = new ColumnDefPacket[columnCount];
        for (int i = 0; i < columnCount; i++) {
            columnDefPackets[i] = new ColumnDefPacketImpl(metaData, i);
        }
        if (collector instanceof StreamMysqlCollector) {
            MySQLRowDesc mySQLRowDesc = new MySQLRowDesc(Arrays.asList(columnDefPackets).stream().map(packet -> {
                String catalog = new String(packet.getColumnCatalog());
                String schema = new String(packet.getColumnSchema());
                String table = new String(packet.getColumnTable());
                String orgTable = new String(packet.getColumnOrgTable());
                String name = new String(packet.getColumnName());
                String orgName = new String(packet.getColumnOrgName());
                int characterSet = packet.getColumnCharsetSet();
                long columnLength = packet.getColumnLength();
                DataType type = DataType.valueOf(packet.getColumnType() == 15 ? 253 : packet.getColumnType());
                int flags = packet.getColumnFlags();
                byte decimals = packet.getColumnDecimals();
                ColumnDefinition columnDefinition = new ColumnDefinition(catalog, schema, table, orgTable, name, orgName, characterSet, columnLength, type, flags, decimals);
                return columnDefinition;
            }).toArray(n -> new ColumnDefinition[n]), DataFormat.TEXT);
            ((StreamMysqlCollector) collector).onColumnDefinitions(mySQLRowDesc);
        }
        {
            Object supplier = collector.supplier().get();
            BiConsumer<Object, Row> accumulator = (BiConsumer) collector.accumulator();
            Function<Object, Object> finisher = (Function) collector.finisher();
            int count = 0;
            while (resultSet.next()) {
                JDBCRow jdbcRow = new MycatRow(rowDesc);
                for (int i = 0; i < columnCount; i++) {
                    jdbcRow.addValue(resultSet.getObject(i + 1));
                }
                count++;
                accumulator.accept(supplier, jdbcRow);
            }
            finisher.apply(supplier);
            resultSet.close();
            statement.close();
            promise.complete(new MySqlResult<>(count, 0, 0, (R) supplier, columnDescriptors));
        }
    } catch (Throwable throwable) {
        promise.tryFail(throwable);
    }
}
Also used : DataFormat(io.vertx.mysqlclient.impl.datatype.DataFormat) java.sql(java.sql) Arrays(java.util.Arrays) MetaClusterCurrent(io.mycat.MetaClusterCurrent) AbstractMySqlConnectionImpl.adaptType(io.mycat.vertxmycat.AbstractMySqlConnectionImpl.adaptType) LoggerFactory(org.slf4j.LoggerFactory) MySQLRowDesc(io.vertx.mysqlclient.impl.MySQLRowDesc) Function(java.util.function.Function) IOExecutor(io.mycat.IOExecutor) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) JDBCRow(io.vertx.jdbcclient.impl.JDBCRow) StreamMysqlCollector(io.vertx.mysqlclient.impl.codec.StreamMysqlCollector) BiConsumer(java.util.function.BiConsumer) AbstractMySqlConnectionImpl.toObjects(io.mycat.vertxmycat.AbstractMySqlConnectionImpl.toObjects) DataType(io.vertx.mysqlclient.impl.datatype.DataType) ColumnDefinition(io.vertx.mysqlclient.impl.protocol.ColumnDefinition) Collector(java.util.stream.Collector) io.vertx.sqlclient(io.vertx.sqlclient) JdbcRowMetaData(io.mycat.beans.mycat.JdbcRowMetaData) Logger(org.slf4j.Logger) ColumnDefPacket(io.mycat.beans.mysql.packet.ColumnDefPacket) Promise(io.vertx.core.Promise) PreparedStatement(java.sql.PreparedStatement) Future(io.vertx.core.Future) List(java.util.List) ColumnDescriptor(io.vertx.sqlclient.desc.ColumnDescriptor) ColumnDefPacketImpl(io.mycat.beans.mysql.packet.ColumnDefPacketImpl) MycatPreparedStatementUtil(io.mycat.calcite.executor.MycatPreparedStatementUtil) VertxRowSetImpl(io.vertx.mysqlclient.impl.codec.VertxRowSetImpl) RowDesc(io.vertx.sqlclient.impl.RowDesc) Handler(io.vertx.core.Handler) Collections(java.util.Collections) JDBCRow(io.vertx.jdbcclient.impl.JDBCRow) ArrayList(java.util.ArrayList) Function(java.util.function.Function) ColumnDefPacket(io.mycat.beans.mysql.packet.ColumnDefPacket) DataType(io.vertx.mysqlclient.impl.datatype.DataType) JdbcRowMetaData(io.mycat.beans.mycat.JdbcRowMetaData) MySQLRowDesc(io.vertx.mysqlclient.impl.MySQLRowDesc) StreamMysqlCollector(io.vertx.mysqlclient.impl.codec.StreamMysqlCollector) ColumnDescriptor(io.vertx.sqlclient.desc.ColumnDescriptor) ColumnDefPacketImpl(io.mycat.beans.mysql.packet.ColumnDefPacketImpl) ColumnDefinition(io.vertx.mysqlclient.impl.protocol.ColumnDefinition) MySQLRowDesc(io.vertx.mysqlclient.impl.MySQLRowDesc) RowDesc(io.vertx.sqlclient.impl.RowDesc) BiConsumer(java.util.function.BiConsumer)

Example 5 with JdbcRowMetaData

use of io.mycat.beans.mycat.JdbcRowMetaData in project Mycat2 by MyCATApache.

the class SQL2ResultSetUtil method getMycatRowMetaData.

@SneakyThrows
public static MycatRowMetaData getMycatRowMetaData(JdbcConnectionManager jdbcConnectionManager, String prototypeServer, String schema, String table) {
    try (DefaultConnection connection = jdbcConnectionManager.getConnection(prototypeServer)) {
        Connection rawConnection = connection.getRawConnection();
        try (Statement statement = rawConnection.createStatement()) {
            statement.setMaxRows(0);
            ResultSet resultSet = statement.executeQuery("select * from " + schema + "." + table + " where 0");
            resultSet.next();
            return new CopyMycatRowMetaData(new JdbcRowMetaData(resultSet.getMetaData()));
        }
    }
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcRowMetaData(io.mycat.beans.mycat.JdbcRowMetaData) SQLCreateViewStatement(com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) ResultSet(java.sql.ResultSet) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData) SneakyThrows(lombok.SneakyThrows)

Aggregations

JdbcRowMetaData (io.mycat.beans.mycat.JdbcRowMetaData)5 CopyMycatRowMetaData (io.mycat.beans.mycat.CopyMycatRowMetaData)3 PreparedStatement (java.sql.PreparedStatement)3 ArrayList (java.util.ArrayList)3 DefaultConnection (io.mycat.datasource.jdbc.datasource.DefaultConnection)2 JDBCRow (io.vertx.jdbcclient.impl.JDBCRow)2 MySQLRowDesc (io.vertx.mysqlclient.impl.MySQLRowDesc)2 VertxRowSetImpl (io.vertx.mysqlclient.impl.codec.VertxRowSetImpl)2 ColumnDescriptor (io.vertx.sqlclient.desc.ColumnDescriptor)2 RowDesc (io.vertx.sqlclient.impl.RowDesc)2 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 List (java.util.List)2 SneakyThrows (lombok.SneakyThrows)2 SQLCreateViewStatement (com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement)1 MySqlCreateTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)1 IOExecutor (io.mycat.IOExecutor)1 MetaClusterCurrent (io.mycat.MetaClusterCurrent)1 MycatRowMetaData (io.mycat.beans.mycat.MycatRowMetaData)1 ColumnDefPacket (io.mycat.beans.mysql.packet.ColumnDefPacket)1