Search in sources :

Example 6 with Row

use of com.mysql.cj.result.Row in project ABC by RuiPinto96274.

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 7 with Row

use of com.mysql.cj.result.Row in project ABC by RuiPinto96274.

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)

Example 8 with Row

use of com.mysql.cj.result.Row in project ABC by RuiPinto96274.

the class XProtocolTest method testAnotherBasicSqlQuery.

@Test
public void testAnotherBasicSqlQuery() {
    assumeTrue(this.isSetForXTests, PropertyDefinitions.SYSP_testsuite_url_mysqlx + " must be set to run this test.");
    this.protocol.send(this.messageBuilder.buildSqlStatement("select 'x' as a_string, 42 as a_long, 7.6 as a_decimal union select 'y' as a_string, 11 as a_long, .1111 as a_decimal"), 0);
    assertTrue(this.protocol.hasResults());
    ColumnDefinition metadata = this.protocol.readMetadata();
    assertEquals(3, metadata.getFields().length);
    assertEquals("a_string", metadata.getFields()[0].getColumnLabel());
    assertEquals("a_long", metadata.getFields()[1].getColumnLabel());
    assertEquals("a_decimal", metadata.getFields()[2].getColumnLabel());
    assertEquals(MysqlType.FIELD_TYPE_VARCHAR, metadata.getFields()[0].getMysqlTypeId());
    assertEquals(MysqlType.FIELD_TYPE_LONGLONG, metadata.getFields()[1].getMysqlTypeId());
    assertEquals(MysqlType.FIELD_TYPE_NEWDECIMAL, metadata.getFields()[2].getMysqlTypeId());
    Iterator<Row> rowInputStream = new XProtocolRowInputStream(metadata, this.protocol, null);
    // first row
    Row r = rowInputStream.next();
    String value = r.getValue(0, new StringValueFactory(this.protocol.getPropertySet()));
    assertEquals("x", value);
    value = r.getValue(1, new StringValueFactory(this.protocol.getPropertySet()));
    assertEquals("42", value);
    value = r.getValue(2, new StringValueFactory(this.protocol.getPropertySet()));
    // TODO: zeroes ok here??? scale is adjusted to 4 due to ".1111" value in RS? not happening in decimal test down below
    assertEquals("7.6000", value);
    // second row
    assertTrue(rowInputStream.hasNext());
    r = rowInputStream.next();
    value = r.getValue(0, new StringValueFactory(this.protocol.getPropertySet()));
    assertEquals("y", value);
    value = r.getValue(1, new StringValueFactory(this.protocol.getPropertySet()));
    assertEquals("11", value);
    value = r.getValue(2, new StringValueFactory(this.protocol.getPropertySet()));
    assertEquals("0.1111", value);
    assertFalse(rowInputStream.hasNext());
    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)

Example 9 with Row

use of com.mysql.cj.result.Row in project ABC by RuiPinto96274.

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)

Example 10 with Row

use of com.mysql.cj.result.Row in project ABC by RuiPinto96274.

the class CallableStatement method fakeParameterTypes.

/**
 * Used to fake up some metadata when we don't have access to
 * SHOW CREATE PROCEDURE or mysql.proc.
 *
 * @param isReallyProcedure
 *            is it a procedure or function
 *
 * @throws SQLException
 *             if we can't build the metadata.
 */
private void fakeParameterTypes(boolean isReallyProcedure) throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        String encoding = this.connection.getSession().getServerSession().getCharsetSettings().getMetadataEncoding();
        int collationIndex = this.connection.getSession().getServerSession().getCharsetSettings().getMetadataCollationIndex();
        Field[] fields = new Field[13];
        fields[0] = new Field("", "PROCEDURE_CAT", collationIndex, encoding, MysqlType.CHAR, 0);
        fields[1] = new Field("", "PROCEDURE_SCHEM", collationIndex, encoding, MysqlType.CHAR, 0);
        fields[2] = new Field("", "PROCEDURE_NAME", collationIndex, encoding, MysqlType.CHAR, 0);
        fields[3] = new Field("", "COLUMN_NAME", collationIndex, encoding, MysqlType.CHAR, 0);
        fields[4] = new Field("", "COLUMN_TYPE", collationIndex, encoding, MysqlType.CHAR, 0);
        fields[5] = new Field("", "DATA_TYPE", collationIndex, encoding, MysqlType.SMALLINT, 0);
        fields[6] = new Field("", "TYPE_NAME", collationIndex, encoding, MysqlType.CHAR, 0);
        fields[7] = new Field("", "PRECISION", collationIndex, encoding, MysqlType.INT, 0);
        fields[8] = new Field("", "LENGTH", collationIndex, encoding, MysqlType.INT, 0);
        fields[9] = new Field("", "SCALE", collationIndex, encoding, MysqlType.SMALLINT, 0);
        fields[10] = new Field("", "RADIX", collationIndex, encoding, MysqlType.SMALLINT, 0);
        fields[11] = new Field("", "NULLABLE", collationIndex, encoding, MysqlType.SMALLINT, 0);
        fields[12] = new Field("", "REMARKS", collationIndex, encoding, MysqlType.CHAR, 0);
        String procName = isReallyProcedure ? extractProcedureName() : null;
        byte[] procNameAsBytes = null;
        procNameAsBytes = procName == null ? null : StringUtils.getBytes(procName, "UTF-8");
        ArrayList<Row> resultRows = new ArrayList<>();
        for (int i = 0; i < ((PreparedQuery<?>) this.query).getParameterCount(); i++) {
            byte[][] row = new byte[13][];
            // PROCEDURE_CAT
            row[0] = null;
            // PROCEDURE_SCHEM
            row[1] = null;
            // PROCEDURE/NAME
            row[2] = procNameAsBytes;
            // COLUMN_NAME
            row[3] = s2b(String.valueOf(i));
            row[4] = s2b(String.valueOf(java.sql.DatabaseMetaData.procedureColumnIn));
            // DATA_TYPE
            row[5] = s2b(String.valueOf(MysqlType.VARCHAR.getJdbcType()));
            // TYPE_NAME
            row[6] = s2b(MysqlType.VARCHAR.getName());
            // PRECISION
            row[7] = s2b(Integer.toString(65535));
            // LENGTH
            row[8] = s2b(Integer.toString(65535));
            // SCALE
            row[9] = s2b(Integer.toString(0));
            // RADIX
            row[10] = s2b(Integer.toString(10));
            // nullable
            row[11] = s2b(Integer.toString(java.sql.DatabaseMetaData.procedureNullableUnknown));
            row[12] = null;
            resultRows.add(new ByteArrayRow(row, getExceptionInterceptor()));
        }
        java.sql.ResultSet paramTypesRs = this.resultSetFactory.createFromResultsetRows(ResultSet.CONCUR_READ_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, new ResultsetRowsStatic(resultRows, new DefaultColumnDefinition(fields)));
        convertGetProcedureColumnsToInternalDescriptors(paramTypesRs);
    }
}
Also used : ArrayList(java.util.ArrayList) Field(com.mysql.cj.result.Field) ResultSet(java.sql.ResultSet) DefaultColumnDefinition(com.mysql.cj.result.DefaultColumnDefinition) ResultsetRowsStatic(com.mysql.cj.protocol.a.result.ResultsetRowsStatic) ByteArrayRow(com.mysql.cj.protocol.a.result.ByteArrayRow) Row(com.mysql.cj.result.Row) ByteArrayRow(com.mysql.cj.protocol.a.result.ByteArrayRow)

Aggregations

Row (com.mysql.cj.result.Row)144 ArrayList (java.util.ArrayList)81 ResultsetRow (com.mysql.cj.protocol.ResultsetRow)78 ByteArrayRow (com.mysql.cj.protocol.a.result.ByteArrayRow)72 DefaultColumnDefinition (com.mysql.cj.result.DefaultColumnDefinition)69 Field (com.mysql.cj.result.Field)69 ResultsetRowsStatic (com.mysql.cj.protocol.a.result.ResultsetRowsStatic)66 StringValueFactory (com.mysql.cj.result.StringValueFactory)48 ResultSet (java.sql.ResultSet)48 SQLException (java.sql.SQLException)45 PreparedStatement (java.sql.PreparedStatement)36 StatementExecuteOkBuilder (com.mysql.cj.protocol.x.StatementExecuteOkBuilder)33 Statement (java.sql.Statement)33 CJException (com.mysql.cj.exceptions.CJException)30 ColumnDefinition (com.mysql.cj.protocol.ColumnDefinition)30 AssertionFailedException (com.mysql.cj.exceptions.AssertionFailedException)27 XProtocolRowInputStream (com.mysql.cj.protocol.x.XProtocolRowInputStream)27 Test (org.junit.jupiter.api.Test)27 Resultset (com.mysql.cj.protocol.Resultset)21 IOException (java.io.IOException)21