Search in sources :

Example 1 with ColumnDefinition41Packet

use of io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet in project sharding-jdbc by shardingjdbc.

the class ComStmtPreparePacket method execute.

@Override
public List<DatabaseProtocolPacket> execute() {
    log.debug("COM_STMT_PREPARE received for Sharding-Proxy: {}", sql);
    List<DatabaseProtocolPacket> result = new LinkedList<>();
    int currentSequenceId = 0;
    SQLStatement sqlStatement = new SQLParsingEngine(DatabaseType.MySQL, sql, ShardingRuleRegistry.getInstance().getShardingRule()).parse();
    result.add(new ComStmtPrepareOKPacket(++currentSequenceId, PreparedStatementRegistry.getInstance().register(sql), getNumColumns(sqlStatement), sqlStatement.getParametersIndex(), 0));
    for (int i = 0; i < sqlStatement.getParametersIndex(); i++) {
        // TODO add column name
        result.add(new ColumnDefinition41Packet(++currentSequenceId, ShardingConstant.LOGIC_SCHEMA_NAME, sqlStatement.getTables().isSingleTable() ? sqlStatement.getTables().getSingleTableName() : "", "", "", "", 100, ColumnType.MYSQL_TYPE_VARCHAR, 0));
    }
    if (sqlStatement.getParametersIndex() > 0) {
        result.add(new EofPacket(++currentSequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue()));
    }
    // TODO add If numColumns > 0
    return result;
}
Also used : ColumnDefinition41Packet(io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet) DatabaseProtocolPacket(io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket) SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine) SQLStatement(io.shardingjdbc.core.parsing.parser.sql.SQLStatement) EofPacket(io.shardingjdbc.proxy.transport.mysql.packet.generic.EofPacket) LinkedList(java.util.LinkedList)

Example 2 with ColumnDefinition41Packet

use of io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet in project sharding-jdbc by shardingjdbc.

the class SQLExecuteBackendHandler method getDatabaseProtocolPackets.

private List<DatabaseProtocolPacket> getDatabaseProtocolPackets(final ResultSet resultSet) throws SQLException {
    List<DatabaseProtocolPacket> result = new LinkedList<>();
    int currentSequenceId = 0;
    ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
    int columnCount = resultSetMetaData.getColumnCount();
    if (0 == columnCount) {
        result.add(new OKPacket(++currentSequenceId, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, ""));
        return result;
    }
    result.add(new FieldCountPacket(++currentSequenceId, columnCount));
    for (int i = 1; i <= columnCount; i++) {
        result.add(new ColumnDefinition41Packet(++currentSequenceId, resultSetMetaData.getSchemaName(i), resultSetMetaData.getTableName(i), resultSetMetaData.getTableName(i), resultSetMetaData.getColumnLabel(i), resultSetMetaData.getColumnName(i), resultSetMetaData.getColumnDisplaySize(i), ColumnType.valueOfJDBCType(resultSetMetaData.getColumnType(i)), 0));
    }
    result.add(new EofPacket(++currentSequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue()));
    while (resultSet.next()) {
        List<Object> data = new ArrayList<>(columnCount);
        for (int i = 1; i <= columnCount; i++) {
            data.add(resultSet.getObject(i));
        }
        result.add(new TextResultSetRowPacket(++currentSequenceId, data));
    }
    result.add(new EofPacket(++currentSequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue()));
    return result;
}
Also used : ColumnDefinition41Packet(io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet) ArrayList(java.util.ArrayList) DatabaseProtocolPacket(io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket) LinkedList(java.util.LinkedList) ResultSetMetaData(java.sql.ResultSetMetaData) TextResultSetRowPacket(io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.TextResultSetRowPacket) FieldCountPacket(io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.FieldCountPacket) OKPacket(io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket) EofPacket(io.shardingjdbc.proxy.transport.mysql.packet.generic.EofPacket)

Aggregations

DatabaseProtocolPacket (io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket)2 ColumnDefinition41Packet (io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.ColumnDefinition41Packet)2 EofPacket (io.shardingjdbc.proxy.transport.mysql.packet.generic.EofPacket)2 LinkedList (java.util.LinkedList)2 SQLParsingEngine (io.shardingjdbc.core.parsing.SQLParsingEngine)1 SQLStatement (io.shardingjdbc.core.parsing.parser.sql.SQLStatement)1 FieldCountPacket (io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.FieldCountPacket)1 TextResultSetRowPacket (io.shardingjdbc.proxy.transport.mysql.packet.command.text.query.TextResultSetRowPacket)1 OKPacket (io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 ArrayList (java.util.ArrayList)1