Search in sources :

Example 16 with StatementExecuteOkBuilder

use of com.mysql.cj.protocol.x.StatementExecuteOkBuilder in project aws-mysql-jdbc by awslabs.

the class MysqlxSession method query.

public <M extends Message, R, RES> RES query(M message, Predicate<Row> rowFilter, Function<Row, R> rowMapper, Collector<R, ?, RES> collector) {
    this.protocol.send(message, 0);
    ColumnDefinition metadata = this.protocol.readMetadata();
    Iterator<Row> ris = new XProtocolRowInputStream(metadata, (XProtocol) this.protocol, null);
    Stream<Row> stream = StreamSupport.stream(Spliterators.spliteratorUnknownSize(ris, 0), false);
    if (rowFilter != null) {
        stream = stream.filter(rowFilter);
    }
    RES result = stream.map(rowMapper).collect(collector);
    this.protocol.readQueryResult(new StatementExecuteOkBuilder());
    return result;
}
Also used : XProtocolRowInputStream(com.mysql.cj.protocol.x.XProtocolRowInputStream) StatementExecuteOkBuilder(com.mysql.cj.protocol.x.StatementExecuteOkBuilder) Row(com.mysql.cj.result.Row) ColumnDefinition(com.mysql.cj.protocol.ColumnDefinition)

Example 17 with StatementExecuteOkBuilder

use of com.mysql.cj.protocol.x.StatementExecuteOkBuilder in project aws-mysql-jdbc by awslabs.

the class SqlResultBuilder method addProtocolEntity.

@Override
public boolean addProtocolEntity(ProtocolEntity entity) {
    if (entity instanceof Field) {
        this.fields.add((Field) entity);
        if (!this.isRowResult) {
            this.isRowResult = true;
        }
        this.prevEntity = entity;
        return false;
    } else if (entity instanceof Notice) {
        this.statementExecuteOkBuilder.addProtocolEntity(entity);
        return false;
    }
    if (this.isRowResult && this.metadata == null) {
        this.metadata = new DefaultColumnDefinition(this.fields.toArray(new Field[] {}));
    }
    if (entity instanceof Row) {
        this.rows.add(((Row) entity).setMetadata(this.metadata));
    } else if (entity instanceof FetchDoneMoreResults) {
        this.resultSets.add(new SqlSingleResult(this.metadata, this.defaultTimeZone, new BufferedRowList(this.rows), () -> this.statementExecuteOkBuilder.build(), this.pset));
        // clear variables to accept next result set
        this.fields = new ArrayList<>();
        this.metadata = null;
        this.rows = new ArrayList<>();
        this.statementExecuteOkBuilder = new StatementExecuteOkBuilder();
    } else if (entity instanceof FetchDoneEntity) {
        if (this.prevEntity instanceof FetchDoneMoreResults) {
        // no-op, possibly bug in xplugin sending FetchDone immediately following FetchDoneMoreResultsets
        } else {
            this.resultSets.add(new SqlSingleResult(this.metadata, this.defaultTimeZone, new BufferedRowList(this.rows), () -> this.statementExecuteOkBuilder.build(), this.pset));
        }
    } else if (entity instanceof StatementExecuteOk) {
        return true;
    }
    this.prevEntity = entity;
    return false;
}
Also used : BufferedRowList(com.mysql.cj.result.BufferedRowList) Field(com.mysql.cj.result.Field) Notice(com.mysql.cj.protocol.x.Notice) StatementExecuteOkBuilder(com.mysql.cj.protocol.x.StatementExecuteOkBuilder) DefaultColumnDefinition(com.mysql.cj.result.DefaultColumnDefinition) FetchDoneMoreResults(com.mysql.cj.protocol.x.FetchDoneMoreResults) ArrayList(java.util.ArrayList) Row(com.mysql.cj.result.Row) FetchDoneEntity(com.mysql.cj.protocol.x.FetchDoneEntity) StatementExecuteOk(com.mysql.cj.protocol.x.StatementExecuteOk)

Example 18 with StatementExecuteOkBuilder

use of com.mysql.cj.protocol.x.StatementExecuteOkBuilder in project aws-mysql-jdbc by awslabs.

the class StreamingSqlResultBuilder method addProtocolEntity.

@Override
public boolean addProtocolEntity(ProtocolEntity entity) {
    if (entity instanceof Notice) {
        this.statementExecuteOkBuilder.addProtocolEntity(entity);
    } else {
        this.lastEntity = entity;
    }
    AtomicBoolean readLastResult = new AtomicBoolean(false);
    Supplier<ProtocolEntity> okReader = () -> {
        if (readLastResult.get()) {
            throw new CJCommunicationsException("Invalid state attempting to read ok packet");
        }
        if (this.protocol.hasMoreResults()) {
            StatementExecuteOk res = this.statementExecuteOkBuilder.build();
            this.statementExecuteOkBuilder = new StatementExecuteOkBuilder();
            return res;
        }
        readLastResult.set(true);
        return this.protocol.readQueryResult(this.statementExecuteOkBuilder);
    };
    Supplier<SqlResult> resultStream = () -> {
        if (readLastResult.get()) {
            return null;
        } else if (this.lastEntity != null && this.lastEntity instanceof Field || this.protocol.isSqlResultPending()) {
            ColumnDefinition cd;
            if (this.lastEntity != null && this.lastEntity instanceof Field) {
                cd = this.protocol.readMetadata((Field) this.lastEntity, (n) -> {
                    this.statementExecuteOkBuilder.addProtocolEntity(n);
                });
                this.lastEntity = null;
            } else {
                cd = this.protocol.readMetadata(this.statementExecuteOkBuilder::addProtocolEntity);
            }
            return new SqlSingleResult(cd, this.protocol.getServerSession().getDefaultTimeZone(), new XProtocolRowInputStream(cd, this.protocol, (n) -> {
                this.statementExecuteOkBuilder.addProtocolEntity(n);
            }), okReader, this.pset);
        } else {
            readLastResult.set(true);
            SqlResultBuilder rb = new SqlResultBuilder(this.defaultTimeZone, this.pset);
            rb.addProtocolEntity(entity);
            return this.protocol.readQueryResult(rb);
        }
    };
    this.result = new SqlMultiResult(resultStream);
    return true;
}
Also used : PropertySet(com.mysql.cj.conf.PropertySet) CJCommunicationsException(com.mysql.cj.exceptions.CJCommunicationsException) ResultBuilder(com.mysql.cj.protocol.ResultBuilder) ColumnDefinition(com.mysql.cj.protocol.ColumnDefinition) ProtocolEntity(com.mysql.cj.protocol.ProtocolEntity) XProtocolRowInputStream(com.mysql.cj.protocol.x.XProtocolRowInputStream) TimeZone(java.util.TimeZone) Field(com.mysql.cj.result.Field) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Supplier(java.util.function.Supplier) MysqlxSession(com.mysql.cj.MysqlxSession) ArrayList(java.util.ArrayList) StatementExecuteOkBuilder(com.mysql.cj.protocol.x.StatementExecuteOkBuilder) List(java.util.List) Notice(com.mysql.cj.protocol.x.Notice) XProtocol(com.mysql.cj.protocol.x.XProtocol) StatementExecuteOk(com.mysql.cj.protocol.x.StatementExecuteOk) StatementExecuteOkBuilder(com.mysql.cj.protocol.x.StatementExecuteOkBuilder) CJCommunicationsException(com.mysql.cj.exceptions.CJCommunicationsException) StatementExecuteOk(com.mysql.cj.protocol.x.StatementExecuteOk) ColumnDefinition(com.mysql.cj.protocol.ColumnDefinition) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Field(com.mysql.cj.result.Field) XProtocolRowInputStream(com.mysql.cj.protocol.x.XProtocolRowInputStream) Notice(com.mysql.cj.protocol.x.Notice) ProtocolEntity(com.mysql.cj.protocol.ProtocolEntity)

Aggregations

StatementExecuteOkBuilder (com.mysql.cj.protocol.x.StatementExecuteOkBuilder)18 Test (org.junit.jupiter.api.Test)12 ColumnDefinition (com.mysql.cj.protocol.ColumnDefinition)11 XProtocolRowInputStream (com.mysql.cj.protocol.x.XProtocolRowInputStream)11 Row (com.mysql.cj.result.Row)11 StringValueFactory (com.mysql.cj.result.StringValueFactory)8 DocFilterParams (com.mysql.cj.xdevapi.DocFilterParams)5 ArrayList (java.util.ArrayList)5 XProtocolError (com.mysql.cj.protocol.x.XProtocolError)4 Field (com.mysql.cj.result.Field)4 StatementExecuteOk (com.mysql.cj.protocol.x.StatementExecuteOk)3 XMessageBuilder (com.mysql.cj.protocol.x.XMessageBuilder)3 FilterParams (com.mysql.cj.xdevapi.FilterParams)3 SqlResult (com.mysql.cj.xdevapi.SqlResult)3 SqlResultBuilder (com.mysql.cj.xdevapi.SqlResultBuilder)3 TableFilterParams (com.mysql.cj.xdevapi.TableFilterParams)3 ProtocolEntity (com.mysql.cj.protocol.ProtocolEntity)2 Notice (com.mysql.cj.protocol.x.Notice)2 DefaultColumnDefinition (com.mysql.cj.result.DefaultColumnDefinition)2 MysqlxSession (com.mysql.cj.MysqlxSession)1