Search in sources :

Example 1 with MycatRowMetaData

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

the class RowIteratorUtil method dumpColumnInfo.

public static String dumpColumnInfo(RowBaseIterator iterator) {
    StringBuilder sb = new StringBuilder();
    MycatRowMetaData mycatRowMetaData = iterator.getMetaData();
    int columnCount = mycatRowMetaData.getColumnCount();
    while (iterator.next()) {
        for (int i = 0; i < columnCount; i++) {
            sb.append(mycatRowMetaData.getColumnName(i)).append(":").append(Objects.toString(iterator.getObject(i))).append(" | ");
        }
        sb.append("\n");
    }
    return sb.toString();
}
Also used : MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData)

Example 2 with MycatRowMetaData

use of io.mycat.beans.mycat.MycatRowMetaData 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 3 with MycatRowMetaData

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

the class PrototypeService method getCreateTableSQLByJDBC.

public Optional<String> getCreateTableSQLByJDBC(String schemaName, String tableName, List<Partition> backends) {
    Optional<JdbcConnectionManager> jdbcConnectionManagerOptional = getPrototypeConnectionManager();
    if (!jdbcConnectionManagerOptional.isPresent()) {
        return Optional.empty();
    }
    JdbcConnectionManager jdbcConnectionManager = jdbcConnectionManagerOptional.get();
    backends = new ArrayList<>(backends);
    backends.add(new BackendTableInfo(PROTOTYPE, schemaName, tableName));
    if (backends == null || backends.isEmpty()) {
        return Optional.empty();
    }
    for (Partition backend : backends) {
        try {
            Partition backendTableInfo = backend;
            String targetName = backendTableInfo.getTargetName();
            String targetSchemaTable = backendTableInfo.getTargetSchemaTable();
            try (DefaultConnection connection = jdbcConnectionManager.getConnection(targetName)) {
                String sql = "SHOW CREATE TABLE " + targetSchemaTable;
                SQLStatement sqlStatement = null;
                try (RowBaseIterator rowBaseIterator = connection.executeQuery(sql)) {
                    rowBaseIterator.next();
                    String string = rowBaseIterator.getString(1);
                    sqlStatement = SQLUtils.parseSingleMysqlStatement(string);
                    if (sqlStatement instanceof MySqlCreateTableStatement) {
                        MySqlCreateTableStatement sqlStatement1 = (MySqlCreateTableStatement) sqlStatement;
                        sqlStatement1.setTableName(SQLUtils.normalize(tableName));
                        // 顺序不能颠倒
                        sqlStatement1.setSchema(SQLUtils.normalize(schemaName));
                        return Optional.of(sqlStatement1.toString());
                    }
                } catch (Exception e) {
                    LOGGER.error("", e);
                }
                if (sqlStatement == null || !(sqlStatement instanceof SQLCreateTableStatement)) {
                    try (RowBaseIterator rowBaseIterator = connection.executeQuery("select * from " + targetSchemaTable + " where 0 limit 0")) {
                        MycatRowMetaData metaData = rowBaseIterator.getMetaData();
                        String createTableSql = generateSql(schemaName, tableName, metaData.metaData());
                        return Optional.of(createTableSql);
                    }
                }
                continue;
            } catch (Exception e) {
                LOGGER.error("", e);
            }
            continue;
        } catch (Throwable e) {
            LOGGER.error("can not get create table sql from:" + backend.getTargetName() + backend.getTargetSchemaTable(), e);
            continue;
        }
    }
    return Optional.empty();
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData)

Example 4 with MycatRowMetaData

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

the class VisualTableHandler method createByMySQL.

public static VisualTableHandler createByMySQL(String sql, Supplier<Observable<Object[]>> rowsProvider) {
    SQLStatement sqlStatement1 = SQLUtils.parseSingleMysqlStatement(sql);
    MySqlCreateTableStatement sqlStatement = (MySqlCreateTableStatement) sqlStatement1;
    String schema = SQLUtils.normalize(sqlStatement.getSchema()).toLowerCase();
    String tableName = SQLUtils.normalize(sqlStatement.getTableName()).toLowerCase();
    MycatRowMetaData mycatRowMetaData = SQL2ResultSetUtil.getMycatRowMetaData((MySqlCreateTableStatement) sqlStatement);
    List<SimpleColumnInfo> columnInfo = CalciteConvertors.getColumnInfo(Objects.requireNonNull(mycatRowMetaData));
    LogicTable logicTable = new LogicTable(LogicTableType.NORMAL, schema, tableName, columnInfo, Collections.emptyMap(), sql);
    return new VisualTableHandler(logicTable) {

        @Override
        public Observable<Object[]> scanAll() {
            return rowsProvider.get();
        }
    };
}
Also used : SimpleColumnInfo(io.mycat.SimpleColumnInfo) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) LogicTable(io.mycat.calcite.table.LogicTable) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData)

Example 5 with MycatRowMetaData

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

the class BaseRowObservable method onRow.

@Override
public void onRow(Row row) {
    MycatRowMetaData metaData = this.metaData;
    int columnCount = this.metaData.getColumnCount();
    Object[] objects = getObjects(row, metaData);
    observer.onNext(objects);
}
Also used : MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData)

Aggregations

MycatRowMetaData (io.mycat.beans.mycat.MycatRowMetaData)22 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)7 RowBaseIterator (io.mycat.api.collector.RowBaseIterator)6 CopyMycatRowMetaData (io.mycat.beans.mycat.CopyMycatRowMetaData)5 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)5 MySqlASTVisitorAdapter (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter)4 ResultSetBuilder (io.mycat.beans.mycat.ResultSetBuilder)4 NotNull (org.jetbrains.annotations.NotNull)4 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)3 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)3 ImmutableList (com.google.common.collect.ImmutableList)3 MysqlPayloadObject (io.mycat.api.collector.MysqlPayloadObject)3 Observable (io.reactivex.rxjava3.core.Observable)3 Future (io.vertx.core.Future)3 Iterator (java.util.Iterator)3 SneakyThrows (lombok.SneakyThrows)3 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)2 SQLInsertStatement (com.alibaba.druid.sql.ast.statement.SQLInsertStatement)2 SQLUpdateStatement (com.alibaba.druid.sql.ast.statement.SQLUpdateStatement)2 MySqlCreateTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)2