Search in sources :

Example 1 with RelationInfo

use of io.crate.metadata.RelationInfo in project crate by crate.

the class Messages method sendRowDescription.

/**
 * RowDescription (B)
 * <p>
 * | 'T' | int32 len | int16 numCols
 * <p>
 * For each field:
 * <p>
 * | string name | int32 table_oid | int16 attr_num | int32 oid | int16 typlen | int32 type_modifier | int16 format_code
 * <p>
 * See https://www.postgresql.org/docs/current/static/protocol-message-formats.html
 */
static void sendRowDescription(Channel channel, Collection<Symbol> columns, @Nullable FormatCodes.FormatCode[] formatCodes, @Nullable RelationInfo relation) {
    int length = 4 + 2;
    int columnSize = 4 + 2 + 4 + 2 + 4 + 2;
    ByteBuf buffer = channel.alloc().buffer(// use 10 as an estimate for columnName length
    length + (columns.size() * (10 + columnSize)));
    buffer.writeByte('T');
    // will be set at the end
    buffer.writeInt(0);
    buffer.writeShort(columns.size());
    int tableOid = 0;
    if (relation != null && columns.stream().allMatch(Messages::isRefWithPosition)) {
        tableOid = OidHash.relationOid(relation);
    }
    int idx = 0;
    for (Symbol column : columns) {
        byte[] nameBytes = Symbols.pathFromSymbol(column).sqlFqn().getBytes(StandardCharsets.UTF_8);
        length += nameBytes.length + 1;
        length += columnSize;
        writeCString(buffer, nameBytes);
        buffer.writeInt(tableOid);
        // attr_num
        if (column instanceof Reference) {
            Integer position = ((Reference) column).position();
            buffer.writeShort(position == null ? 0 : position);
        } else {
            buffer.writeShort(0);
        }
        PGType<?> pgType = PGTypes.get(column.valueType());
        buffer.writeInt(pgType.oid());
        buffer.writeShort(pgType.typeLen());
        buffer.writeInt(pgType.typeMod());
        buffer.writeShort(FormatCodes.getFormatCode(formatCodes, idx).ordinal());
        idx++;
    }
    buffer.setInt(1, length);
    ChannelFuture channelFuture = channel.write(buffer);
    if (LOGGER.isTraceEnabled()) {
        channelFuture.addListener((ChannelFutureListener) future -> LOGGER.trace("sentRowDescription"));
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SortedSet(java.util.SortedSet) RelationInfo(io.crate.metadata.RelationInfo) AccessControl(io.crate.auth.AccessControl) PGType(io.crate.protocols.postgres.types.PGType) PGTypes(io.crate.protocols.postgres.types.PGTypes) Collection(java.util.Collection) Reference(io.crate.metadata.Reference) DataType(io.crate.types.DataType) StandardCharsets(java.nio.charset.StandardCharsets) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) OidHash(io.crate.metadata.pgcatalog.OidHash) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Row(io.crate.data.Row) ByteBuf(io.netty.buffer.ByteBuf) Symbol(io.crate.expression.symbol.Symbol) Symbols(io.crate.expression.symbol.Symbols) Locale(java.util.Locale) ChannelFutureListener(io.netty.channel.ChannelFutureListener) SQLExceptions(io.crate.exceptions.SQLExceptions) LogManager(org.apache.logging.log4j.LogManager) Nullable(javax.annotation.Nullable) Symbol(io.crate.expression.symbol.Symbol) Reference(io.crate.metadata.Reference) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

AccessControl (io.crate.auth.AccessControl)1 Row (io.crate.data.Row)1 SQLExceptions (io.crate.exceptions.SQLExceptions)1 Symbol (io.crate.expression.symbol.Symbol)1 Symbols (io.crate.expression.symbol.Symbols)1 Reference (io.crate.metadata.Reference)1 RelationInfo (io.crate.metadata.RelationInfo)1 OidHash (io.crate.metadata.pgcatalog.OidHash)1 PGType (io.crate.protocols.postgres.types.PGType)1 PGTypes (io.crate.protocols.postgres.types.PGTypes)1 DataType (io.crate.types.DataType)1 ByteBuf (io.netty.buffer.ByteBuf)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Collection (java.util.Collection)1 List (java.util.List)1 Locale (java.util.Locale)1 SortedSet (java.util.SortedSet)1