Search in sources :

Example 6 with StatementExecuteOkBuilder

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

the class XProtocolTest method testSqlDml.

/**
 * Test DML that is executed with <i>StmtExecute</i> and does not return a result set.
 */
@Test
public void testSqlDml() {
    assumeTrue(this.isSetForXTests, PropertyDefinitions.SYSP_testsuite_url_mysqlx + " must be set to run this test.");
    this.protocol.send(this.messageBuilder.buildSqlStatement("drop table if exists mysqlx_sqlDmlTest"), 0);
    assertFalse(this.protocol.hasResults());
    SqlResult res = this.protocol.readQueryResult(new SqlResultBuilder(this.protocol.getServerSession().getDefaultTimeZone(), this.protocol.getPropertySet()));
    assertEquals(0, res.getAffectedItemsCount());
    this.protocol.send(this.messageBuilder.buildSqlStatement("create table mysqlx_sqlDmlTest (w int primary key auto_increment, x int) auto_increment = 7"), 0);
    assertFalse(this.protocol.hasResults());
    res = this.protocol.readQueryResult(new SqlResultBuilder(this.protocol.getServerSession().getDefaultTimeZone(), this.protocol.getPropertySet()));
    assertEquals(0, res.getAffectedItemsCount());
    this.protocol.send(this.messageBuilder.buildSqlStatement("insert into mysqlx_sqlDmlTest (x) values (44),(29)"), 0);
    assertFalse(this.protocol.hasResults());
    res = this.protocol.readQueryResult(new SqlResultBuilder(this.protocol.getServerSession().getDefaultTimeZone(), this.protocol.getPropertySet()));
    assertEquals(2, res.getAffectedItemsCount());
    assertEquals(new Long(7), res.getAutoIncrementValue());
    this.protocol.send(this.messageBuilder.buildSqlStatement("drop table mysqlx_sqlDmlTest"), 0);
    assertFalse(this.protocol.hasResults());
    this.protocol.readQueryResult(new StatementExecuteOkBuilder());
}
Also used : SqlResult(com.mysql.cj.xdevapi.SqlResult) SqlResultBuilder(com.mysql.cj.xdevapi.SqlResultBuilder) StatementExecuteOkBuilder(com.mysql.cj.protocol.x.StatementExecuteOkBuilder) Test(org.junit.jupiter.api.Test)

Example 7 with StatementExecuteOkBuilder

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

the class InternalXBaseTestCase method dropTempTestCollection.

public void dropTempTestCollection(XProtocol protocol) {
    String collName = "protocol_test_collection";
    XMessageBuilder messageBuilder = (XMessageBuilder) protocol.getMessageBuilder();
    try {
        protocol.send(messageBuilder.buildDropCollection(getTestDatabase(), collName), 0);
        protocol.readQueryResult(new StatementExecuteOkBuilder());
    } catch (XProtocolError err) {
    // ignore
    }
}
Also used : XMessageBuilder(com.mysql.cj.protocol.x.XMessageBuilder) StatementExecuteOkBuilder(com.mysql.cj.protocol.x.StatementExecuteOkBuilder) XProtocolError(com.mysql.cj.protocol.x.XProtocolError)

Example 8 with StatementExecuteOkBuilder

use of com.mysql.cj.protocol.x.StatementExecuteOkBuilder 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 9 with StatementExecuteOkBuilder

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

the class InternalXBaseTestCase method createTempTestCollection.

/**
 * Create a temporary collection for testing.
 *
 * @param protocol
 *
 * @return the temporary collection name
 */
public String createTempTestCollection(XProtocol protocol) {
    String collName = "protocol_test_collection";
    XMessageBuilder messageBuilder = (XMessageBuilder) protocol.getMessageBuilder();
    try {
        protocol.send(messageBuilder.buildDropCollection(getTestDatabase(), collName), 0);
        protocol.readQueryResult(new StatementExecuteOkBuilder());
    } catch (XProtocolError err) {
    // ignore
    }
    protocol.send(messageBuilder.buildCreateCollection(getTestDatabase(), collName), 0);
    protocol.readQueryResult(new StatementExecuteOkBuilder());
    return collName;
}
Also used : XMessageBuilder(com.mysql.cj.protocol.x.XMessageBuilder) StatementExecuteOkBuilder(com.mysql.cj.protocol.x.StatementExecuteOkBuilder) XProtocolError(com.mysql.cj.protocol.x.XProtocolError)

Example 10 with StatementExecuteOkBuilder

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

the class InternalXBaseTestCase method createTestSchema.

public void createTestSchema(XProtocol protocol, String schemaName) {
    XMessageBuilder messageBuilder = (XMessageBuilder) protocol.getMessageBuilder();
    try {
        StringBuilder stmtString = new StringBuilder("CREATE DATABASE ");
        stmtString.append(StringUtils.quoteIdentifier(schemaName, true));
        protocol.send(messageBuilder.buildSqlStatement(stmtString.toString()), 0);
        protocol.readQueryResult(new StatementExecuteOkBuilder());
    } catch (XProtocolError ex) {
        if (ex.getErrorCode() != MysqlErrorNumbers.ER_DB_CREATE_EXISTS) {
            throw ex;
        }
    }
}
Also used : XMessageBuilder(com.mysql.cj.protocol.x.XMessageBuilder) StatementExecuteOkBuilder(com.mysql.cj.protocol.x.StatementExecuteOkBuilder) XProtocolError(com.mysql.cj.protocol.x.XProtocolError)

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