Search in sources :

Example 11 with StatementExecuteOkBuilder

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

the class XProtocolTest method testDecodingAllTypes.

/**
 * This tests that all types are decoded correctly. We retrieve them all as strings which happens after the decoding step. This is an exhaustive types of
 * type decoding and metadata from the server.
 */
@Test
public void testDecodingAllTypes() {
    assumeTrue(this.isSetForXTests, PropertyDefinitions.SYSP_testsuite_url_mysqlx + " must be set to run this test.");
    try {
        // some types depend on this table
        this.protocol.send(this.messageBuilder.buildSqlStatement("drop table if exists xprotocol_types_test"), 0);
        this.protocol.readQueryResult(new StatementExecuteOkBuilder());
        String testTable = "create table xprotocol_types_test (";
        testTable += " a_float float";
        testTable += ",a_set SET('abc', 'def', 'xyz')";
        testTable += ",an_enum ENUM('enum value a', 'enum value b')";
        testTable += ",an_unsigned_int bigint unsigned";
        this.protocol.send(this.messageBuilder.buildSqlStatement(testTable + ")"), 0);
        this.protocol.readQueryResult(new StatementExecuteOkBuilder());
        this.protocol.send(this.messageBuilder.buildSqlStatement("insert into xprotocol_types_test values ('2.42', 'xyz,def', 'enum value a', 9223372036854775808)"), 0);
        this.protocol.readQueryResult(new StatementExecuteOkBuilder());
        Map<String, BiConsumer<ColumnDefinition, Row>> tests = new HashMap<>();
        tests.put("'some string' as a_string", (metadata, row) -> {
            assertEquals("a_string", metadata.getFields()[0].getColumnLabel());
            assertEquals(MysqlType.FIELD_TYPE_VARCHAR, metadata.getFields()[0].getMysqlTypeId());
            assertEquals("some string", row.getValue(0, new StringValueFactory(this.protocol.getPropertySet())));
        });
        tests.put("date('2015-03-22') as a_date", (metadata, row) -> {
            assertEquals("a_date", metadata.getFields()[0].getColumnLabel());
            assertEquals(MysqlType.FIELD_TYPE_DATETIME, metadata.getFields()[0].getMysqlTypeId());
            assertEquals("2015-03-22", row.getValue(0, new StringValueFactory(this.protocol.getPropertySet())));
        });
        tests.put("curtime() as curtime, cast(curtime() as char(8)) as curtime_string", (metadata, row) -> {
            assertEquals("curtime", metadata.getFields()[0].getColumnLabel());
            assertEquals("curtime_string", metadata.getFields()[1].getColumnLabel());
            assertEquals(MysqlType.FIELD_TYPE_TIME, metadata.getFields()[0].getMysqlTypeId());
            String curtimeString = row.getValue(1, new StringValueFactory(this.protocol.getPropertySet()));
            assertEquals(curtimeString, row.getValue(0, new StringValueFactory(this.protocol.getPropertySet())));
        });
        tests.put("timestamp('2015-05-01 12:01:32') as a_datetime", (metadata, row) -> {
            assertEquals("a_datetime", metadata.getFields()[0].getColumnLabel());
            assertEquals(MysqlType.FIELD_TYPE_DATETIME, metadata.getFields()[0].getMysqlTypeId());
            assertEquals("2015-05-01 12:01:32", row.getValue(0, new StringValueFactory(this.protocol.getPropertySet())));
        });
        tests.put("cos(1) as a_double", (metadata, row) -> {
            assertEquals("a_double", metadata.getFields()[0].getColumnLabel());
            assertEquals(MysqlType.FIELD_TYPE_DOUBLE, metadata.getFields()[0].getMysqlTypeId());
            // value is 0.5403023058681398. Test most of it
            assertTrue(row.getValue(0, new StringValueFactory(this.protocol.getPropertySet())).startsWith("0.540302305868139"));
        });
        tests.put("2142 as an_int", (metadata, row) -> {
            assertEquals("an_int", metadata.getFields()[0].getColumnLabel());
            assertEquals(MysqlType.FIELD_TYPE_LONGLONG, metadata.getFields()[0].getMysqlTypeId());
            assertEquals("2142", row.getValue(0, new StringValueFactory(this.protocol.getPropertySet())));
        });
        tests.put("21.424 as decimal1, -1.0 as decimal2, -0.1 as decimal3, 1000.0 as decimal4", (metadata, row) -> {
            assertEquals("decimal1", metadata.getFields()[0].getColumnLabel());
            assertEquals(MysqlType.FIELD_TYPE_NEWDECIMAL, metadata.getFields()[0].getMysqlTypeId());
            assertEquals("21.424", row.getValue(0, new StringValueFactory(this.protocol.getPropertySet())));
            assertEquals("decimal2", metadata.getFields()[1].getColumnLabel());
            assertEquals(MysqlType.FIELD_TYPE_NEWDECIMAL, metadata.getFields()[1].getMysqlTypeId());
            assertEquals("-1.0", row.getValue(1, new StringValueFactory(this.protocol.getPropertySet())));
            assertEquals("decimal3", metadata.getFields()[2].getColumnLabel());
            assertEquals(MysqlType.FIELD_TYPE_NEWDECIMAL, metadata.getFields()[2].getMysqlTypeId());
            assertEquals("-0.1", row.getValue(2, new StringValueFactory(this.protocol.getPropertySet())));
            assertEquals("decimal4", metadata.getFields()[3].getColumnLabel());
            assertEquals(MysqlType.FIELD_TYPE_NEWDECIMAL, metadata.getFields()[3].getMysqlTypeId());
            assertEquals("1000.0", row.getValue(3, new StringValueFactory(this.protocol.getPropertySet())));
        });
        tests.put("9223372036854775807 as a_large_integer", (metadata, row) -> {
            // max signed 64bit integer
            assertEquals("a_large_integer", metadata.getFields()[0].getColumnLabel());
            assertEquals(MysqlType.FIELD_TYPE_LONGLONG, metadata.getFields()[0].getMysqlTypeId());
            assertEquals("9223372036854775807", row.getValue(0, new StringValueFactory(this.protocol.getPropertySet())));
        });
        tests.put("a_float, a_set, an_enum from xprotocol_types_test", (metadata, row) -> {
            assertEquals("a_float", metadata.getFields()[0].getColumnLabel());
            assertEquals("xprotocol_types_test", metadata.getFields()[0].getTableName());
            assertEquals("2.4200000762939453", row.getValue(0, new StringValueFactory(this.protocol.getPropertySet())));
            assertEquals("a_set", metadata.getFields()[1].getColumnLabel());
            assertEquals("xprotocol_types_test", metadata.getFields()[1].getTableName());
            assertEquals("def,xyz", row.getValue(1, new StringValueFactory(this.protocol.getPropertySet())));
            assertEquals("an_enum", metadata.getFields()[2].getColumnLabel());
            assertEquals("xprotocol_types_test", metadata.getFields()[2].getTableName());
            assertEquals("enum value a", row.getValue(2, new StringValueFactory(this.protocol.getPropertySet())));
        });
        tests.put("an_unsigned_int from xprotocol_types_test", (metadata, row) -> {
            assertEquals("an_unsigned_int", metadata.getFields()[0].getColumnLabel());
            assertEquals("9223372036854775808", row.getValue(0, new StringValueFactory(this.protocol.getPropertySet())));
        });
        // runner for above tests
        for (Map.Entry<String, BiConsumer<ColumnDefinition, Row>> t : tests.entrySet()) {
            this.protocol.send(this.messageBuilder.buildSqlStatement("select " + t.getKey()), 0);
            assertTrue(this.protocol.hasResults());
            ColumnDefinition metadata = this.protocol.readMetadata();
            Iterator<Row> rowInputStream = new XProtocolRowInputStream(metadata, this.protocol, null);
            t.getValue().accept(metadata, rowInputStream.next());
            this.protocol.readQueryResult(new StatementExecuteOkBuilder());
        }
    } finally {
        this.protocol.send(this.messageBuilder.buildSqlStatement("drop table if exists xprotocol_types_test"), 0);
        this.protocol.readQueryResult(new StatementExecuteOkBuilder());
    }
}
Also used : XProtocolRowInputStream(com.mysql.cj.protocol.x.XProtocolRowInputStream) StatementExecuteOkBuilder(com.mysql.cj.protocol.x.StatementExecuteOkBuilder) StringValueFactory(com.mysql.cj.result.StringValueFactory) HashMap(java.util.HashMap) Row(com.mysql.cj.result.Row) HashMap(java.util.HashMap) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) ColumnDefinition(com.mysql.cj.protocol.ColumnDefinition) Test(org.junit.jupiter.api.Test)

Example 12 with StatementExecuteOkBuilder

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

the class XProtocolTest method testMultiInsert.

@Test
public void testMultiInsert() {
    assumeTrue(this.isSetForXTests, PropertyDefinitions.SYSP_testsuite_url_mysqlx + " must be set to run this test.");
    try {
        String collName = createTempTestCollection(this.protocol);
        List<String> stringDocs = new ArrayList<>();
        stringDocs.add("{'a': 'A', 'a1': 'A1', '_id': 'a'}");
        stringDocs.add("{'b': 'B', 'b2': 'B2', '_id': 'b'}");
        stringDocs.add("{'c': 'C', 'c3': 'C3', '_id': 'c'}");
        stringDocs = stringDocs.stream().map(s -> s.replaceAll("'", "\"")).collect(Collectors.toList());
        this.protocol.send(this.messageBuilder.buildDocInsert(getTestDatabase(), collName, stringDocs, false), 0);
        this.protocol.readQueryResult(new StatementExecuteOkBuilder());
        FilterParams filterParams = new DocFilterParams(getTestDatabase(), collName);
        filterParams.setOrder("_id");
        this.protocol.send(this.messageBuilder.buildFind(filterParams), 0);
        ColumnDefinition metadata = this.protocol.readMetadata();
        Iterator<Row> ris = new XProtocolRowInputStream(metadata, this.protocol, null);
        Row r = ris.next();
        assertEquals(stringDocs.get(0), r.getValue(0, new StringValueFactory(this.protocol.getPropertySet())));
        r = ris.next();
        assertEquals(stringDocs.get(1), r.getValue(0, new StringValueFactory(this.protocol.getPropertySet())));
        r = ris.next();
        assertEquals(stringDocs.get(2), 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) DocFilterParams(com.mysql.cj.xdevapi.DocFilterParams) TableFilterParams(com.mysql.cj.xdevapi.TableFilterParams) FilterParams(com.mysql.cj.xdevapi.FilterParams) ArrayList(java.util.ArrayList) Row(com.mysql.cj.result.Row) DocFilterParams(com.mysql.cj.xdevapi.DocFilterParams) ColumnDefinition(com.mysql.cj.protocol.ColumnDefinition) Test(org.junit.jupiter.api.Test)

Example 13 with StatementExecuteOkBuilder

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

the class XProtocolTest method testTrivialSqlQuery.

@Test
public void testTrivialSqlQuery() {
    assumeTrue(this.isSetForXTests, PropertyDefinitions.SYSP_testsuite_url_mysqlx + " must be set to run this test.");
    this.protocol.send(this.messageBuilder.buildSqlStatement("select 'x' as y"), 0);
    assertTrue(this.protocol.hasResults());
    ColumnDefinition metadata = this.protocol.readMetadata();
    assertEquals(1, metadata.getFields().length);
    Field f = metadata.getFields()[0];
    // not an exhaustive metadata test
    assertEquals("y", f.getColumnLabel());
    assertEquals(MysqlType.FIELD_TYPE_VARCHAR, f.getMysqlTypeId());
    Iterator<Row> rowInputStream = new XProtocolRowInputStream(metadata, this.protocol, null);
    Row r = rowInputStream.next();
    String value = r.getValue(0, new StringValueFactory(this.protocol.getPropertySet()));
    assertEquals("x", value);
    this.protocol.readQueryResult(new StatementExecuteOkBuilder());
}
Also used : Field(com.mysql.cj.result.Field) XProtocolRowInputStream(com.mysql.cj.protocol.x.XProtocolRowInputStream) StringValueFactory(com.mysql.cj.result.StringValueFactory) StatementExecuteOkBuilder(com.mysql.cj.protocol.x.StatementExecuteOkBuilder) Row(com.mysql.cj.result.Row) ColumnDefinition(com.mysql.cj.protocol.ColumnDefinition) Test(org.junit.jupiter.api.Test)

Example 14 with StatementExecuteOkBuilder

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

the class XProtocolTest method testBasicCrudInsertFind.

@Test
public void testBasicCrudInsertFind() {
    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());
        FilterParams filterParams = new DocFilterParams(getTestDatabase(), collName);
        filterParams.setCriteria("$.testVal = 2-1");
        this.protocol.send(this.messageBuilder.buildFind(filterParams), 0);
        ColumnDefinition metadata = this.protocol.readMetadata();
        Iterator<Row> ris = new XProtocolRowInputStream(metadata, this.protocol, null);
        Row r = ris.next();
        assertEquals(json, 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) DocFilterParams(com.mysql.cj.xdevapi.DocFilterParams) TableFilterParams(com.mysql.cj.xdevapi.TableFilterParams) FilterParams(com.mysql.cj.xdevapi.FilterParams) Row(com.mysql.cj.result.Row) DocFilterParams(com.mysql.cj.xdevapi.DocFilterParams) ColumnDefinition(com.mysql.cj.protocol.ColumnDefinition) Test(org.junit.jupiter.api.Test)

Example 15 with StatementExecuteOkBuilder

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

the class XProtocolTest method testResultSet.

/**
 * This is a development method that will print a detailed result set for any command sent.
 */
@Test
public void testResultSet() {
    assumeTrue(this.isSetForXTests, PropertyDefinitions.SYSP_testsuite_url_mysqlx + " must be set to run this test.");
    // begin "send" stage, change this as necessary
    this.protocol.send(this.messageBuilder.buildListNotices(), 0);
    // this will read the metadata and result and print all data
    ColumnDefinition metadata = this.protocol.readMetadata();
    Arrays.stream(metadata.getFields()).forEach(f -> {
        System.err.println("***************** field ****************");
        System.err.println("Field: " + f.getColumnLabel());
        System.err.println("Type: " + f.getMysqlTypeId());
        System.err.println("Encoding: " + f.getEncoding());
    });
    Iterator<Row> ris = new XProtocolRowInputStream(metadata, this.protocol, null);
    ris.forEachRemaining(r -> {
        System.err.println("***************** row ****************");
        for (int i = 0; i < metadata.getFields().length; ++i) {
            System.err.println(metadata.getFields()[i].getColumnLabel() + ": " + r.getValue(i, new StringValueFactory(this.protocol.getPropertySet())));
        }
    });
    this.protocol.readQueryResult(new StatementExecuteOkBuilder());
}
Also used : XProtocolRowInputStream(com.mysql.cj.protocol.x.XProtocolRowInputStream) StringValueFactory(com.mysql.cj.result.StringValueFactory) StatementExecuteOkBuilder(com.mysql.cj.protocol.x.StatementExecuteOkBuilder) Row(com.mysql.cj.result.Row) ColumnDefinition(com.mysql.cj.protocol.ColumnDefinition) Test(org.junit.jupiter.api.Test)

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