Search in sources :

Example 1 with ProtocolEntity

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

the class XProtocolAsyncTest method simpleSuccessfulQuery.

@Test
public void simpleSuccessfulQuery() throws Exception {
    assumeTrue(this.isSetForXTests, PropertyDefinitions.SYSP_testsuite_url_mysqlx + " must be set to run this test.");
    try {
        String collName = createTempTestCollection(this.protocol);
        String json = "{'_id': '85983efc2a9a11e5b345feff819cdc9f', 'testVal': 1, 'insertedBy': 'Jess'}".replaceAll("'", "\"");
        this.protocol.send(this.messageBuilder.buildDocInsert(getTestDatabase(), collName, Arrays.asList(new String[] { json }), false), 0);
        this.protocol.readQueryResult(new StatementExecuteOkBuilder());
        final ValueHolder<ColumnDefinition> metadataHolder = new ValueHolder<>();
        final ValueHolder<ArrayList<Row>> rowHolder = new ValueHolder<>();
        rowHolder.accept(new ArrayList<>());
        final ValueHolder<StatementExecuteOk> okHolder = new ValueHolder<>();
        final ValueHolder<Throwable> excHolder = new ValueHolder<>();
        this.protocol.queryAsync(this.messageBuilder.buildFind(new DocFilterParams(getTestDatabase(), collName)), new ResultBuilder<RowResult>() {

            private ArrayList<Field> fields = new ArrayList<>();

            private ColumnDefinition metadata;

            @Override
            public boolean addProtocolEntity(ProtocolEntity entity) {
                if (entity instanceof Field) {
                    this.fields.add((Field) entity);
                } else if (entity instanceof ColumnDefinition) {
                    this.metadata = (ColumnDefinition) entity;
                    metadataHolder.accept(this.metadata);
                } else if (entity instanceof Row) {
                    if (this.metadata == null) {
                        this.metadata = new DefaultColumnDefinition(this.fields.toArray(new Field[] {}));
                        metadataHolder.accept(this.metadata);
                    }
                    rowHolder.get().add((Row) entity);
                } else if (entity instanceof StatementExecuteOk) {
                    okHolder.accept((StatementExecuteOk) entity);
                    synchronized (XProtocolAsyncTest.this) {
                        XProtocolAsyncTest.this.notify();
                    }
                    return true;
                }
                return false;
            }

            @Override
            public RowResult build() {
                return null;
            }
        });
        synchronized (this) {
            // timeout in case we get stuck
            this.wait(5000);
        }
        assertEquals(1, metadataHolder.get().getFields().length);
        assertEquals(1, rowHolder.get().size());
        assertNotNull(okHolder.get());
        assertNull(excHolder.get());
    } finally {
        dropTempTestCollection(this.protocol);
    }
}
Also used : StatementExecuteOkBuilder(com.mysql.cj.protocol.x.StatementExecuteOkBuilder) ArrayList(java.util.ArrayList) DefaultColumnDefinition(com.mysql.cj.result.DefaultColumnDefinition) ColumnDefinition(com.mysql.cj.protocol.ColumnDefinition) StatementExecuteOk(com.mysql.cj.protocol.x.StatementExecuteOk) RowResult(com.mysql.cj.xdevapi.RowResult) Field(com.mysql.cj.result.Field) DefaultColumnDefinition(com.mysql.cj.result.DefaultColumnDefinition) Row(com.mysql.cj.result.Row) DocFilterParams(com.mysql.cj.xdevapi.DocFilterParams) ProtocolEntity(com.mysql.cj.protocol.ProtocolEntity) Test(org.junit.jupiter.api.Test)

Example 2 with ProtocolEntity

use of com.mysql.cj.protocol.ProtocolEntity 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

ColumnDefinition (com.mysql.cj.protocol.ColumnDefinition)2 ProtocolEntity (com.mysql.cj.protocol.ProtocolEntity)2 StatementExecuteOk (com.mysql.cj.protocol.x.StatementExecuteOk)2 StatementExecuteOkBuilder (com.mysql.cj.protocol.x.StatementExecuteOkBuilder)2 Field (com.mysql.cj.result.Field)2 ArrayList (java.util.ArrayList)2 MysqlxSession (com.mysql.cj.MysqlxSession)1 PropertySet (com.mysql.cj.conf.PropertySet)1 CJCommunicationsException (com.mysql.cj.exceptions.CJCommunicationsException)1 ResultBuilder (com.mysql.cj.protocol.ResultBuilder)1 Notice (com.mysql.cj.protocol.x.Notice)1 XProtocol (com.mysql.cj.protocol.x.XProtocol)1 XProtocolRowInputStream (com.mysql.cj.protocol.x.XProtocolRowInputStream)1 DefaultColumnDefinition (com.mysql.cj.result.DefaultColumnDefinition)1 Row (com.mysql.cj.result.Row)1 DocFilterParams (com.mysql.cj.xdevapi.DocFilterParams)1 RowResult (com.mysql.cj.xdevapi.RowResult)1 List (java.util.List)1 TimeZone (java.util.TimeZone)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1