Search in sources :

Example 1 with UpdateSpec

use of com.mysql.cj.xdevapi.UpdateSpec in project aws-mysql-jdbc by awslabs.

the class XProtocolTest method testDocUpdate.

@Test
public void testDocUpdate() {
    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());
        List<UpdateSpec> updates = new ArrayList<>();
        updates.add(new UpdateSpec(UpdateType.ITEM_SET, "$.a").setValue("lemon"));
        updates.add(new UpdateSpec(UpdateType.ITEM_REMOVE, "$.insertedBy"));
        this.protocol.send(this.messageBuilder.buildDocUpdate(new DocFilterParams(getTestDatabase(), collName), updates), 0);
        this.protocol.readQueryResult(new StatementExecuteOkBuilder());
        // verify
        this.protocol.send(this.messageBuilder.buildFind(new DocFilterParams(getTestDatabase(), collName)), 0);
        ColumnDefinition metadata = this.protocol.readMetadata();
        Iterator<Row> ris = new XProtocolRowInputStream(metadata, this.protocol, null);
        Row r = ris.next();
        assertEquals("{\"a\": \"lemon\", \"_id\": \"85983efc2a9a11e5b345feff819cdc9f\", \"testVal\": \"1\"}", r.getValue(0, new StringValueFactory(this.protocol.getPropertySet())));
        this.protocol.readQueryResult(new StatementExecuteOkBuilder());
    } finally {
        dropTempTestCollection(this.protocol);
    }
}
Also used : XProtocolRowInputStream(com.mysql.cj.protocol.x.XProtocolRowInputStream) StatementExecuteOkBuilder(com.mysql.cj.protocol.x.StatementExecuteOkBuilder) StringValueFactory(com.mysql.cj.result.StringValueFactory) ArrayList(java.util.ArrayList) UpdateSpec(com.mysql.cj.xdevapi.UpdateSpec) Row(com.mysql.cj.result.Row) DocFilterParams(com.mysql.cj.xdevapi.DocFilterParams) ColumnDefinition(com.mysql.cj.protocol.ColumnDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

ColumnDefinition (com.mysql.cj.protocol.ColumnDefinition)1 StatementExecuteOkBuilder (com.mysql.cj.protocol.x.StatementExecuteOkBuilder)1 XProtocolRowInputStream (com.mysql.cj.protocol.x.XProtocolRowInputStream)1 Row (com.mysql.cj.result.Row)1 StringValueFactory (com.mysql.cj.result.StringValueFactory)1 DocFilterParams (com.mysql.cj.xdevapi.DocFilterParams)1 UpdateSpec (com.mysql.cj.xdevapi.UpdateSpec)1 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1