Search in sources :

Example 1 with SqlStatement

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

the class SessionTest method sqlArguments.

@Test
public void sqlArguments() {
    SqlStatement stmt = this.session.sql("select ? as a, 40 + ? as b, ? as c");
    SqlResult res = stmt.bind(1).bind(2).bind(3).execute();
    Row r = res.next();
    assertEquals("1", r.getString("a"));
    assertEquals("42", r.getString("b"));
    assertEquals("3", r.getString("c"));
}
Also used : SqlStatement(com.mysql.cj.xdevapi.SqlStatement) SqlResult(com.mysql.cj.xdevapi.SqlResult) Row(com.mysql.cj.xdevapi.Row) Test(org.junit.jupiter.api.Test)

Example 2 with SqlStatement

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

the class SessionTest method smartBufferMultipleResults.

@Test
public void smartBufferMultipleResults() {
    try {
        sqlUpdate("drop procedure if exists basicMultipleResults");
        sqlUpdate("create procedure basicMultipleResults() begin explain select 1; explain select 2; end");
        SqlStatement stmt = this.session.sql("call basicMultipleResults()");
        /* SqlResult res = */
        stmt.execute();
        // execute another statement, should work fine
        this.session.sql("call basicMultipleResults()");
        this.session.sql("call basicMultipleResults()");
        this.session.sql("call basicMultipleResults()");
    } finally {
        sqlUpdate("drop procedure if exists basicMultipleResults");
    }
}
Also used : SqlStatement(com.mysql.cj.xdevapi.SqlStatement) Test(org.junit.jupiter.api.Test)

Example 3 with SqlStatement

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

the class SessionTest method basicMultipleResults.

@Test
public void basicMultipleResults() {
    try {
        sqlUpdate("drop procedure if exists basicMultipleResults");
        sqlUpdate("create procedure basicMultipleResults() begin explain select 1; explain select 2; end");
        SqlStatement stmt = this.session.sql("call basicMultipleResults()");
        SqlResult res = stmt.execute();
        assertTrue(res.hasData());
        /* Row r = */
        res.next();
        assertFalse(res.hasNext());
        assertTrue(res.nextResult());
        assertTrue(res.hasData());
        assertFalse(res.nextResult());
        assertFalse(res.nextResult());
        assertFalse(res.nextResult());
    } finally {
        sqlUpdate("drop procedure if exists basicMultipleResults");
    }
}
Also used : SqlStatement(com.mysql.cj.xdevapi.SqlStatement) SqlResult(com.mysql.cj.xdevapi.SqlResult) Test(org.junit.jupiter.api.Test)

Example 4 with SqlStatement

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

the class SessionTest method sqlUpdate.

@Test
public void sqlUpdate() {
    SqlStatement stmt = this.session.sql("set @cjTestVar = 1");
    SqlResult res = stmt.execute();
    assertFalse(res.hasData());
    assertEquals(0, res.getAffectedItemsCount());
    assertEquals(null, res.getAutoIncrementValue());
    assertEquals(0, res.getWarningsCount());
    assertFalse(res.getWarnings().hasNext());
    // TODO SqlUpdateResult throws FeatureNotAvailableException("Not a multi-result");
    // res.nextResult();
    assertThrows(FeatureNotAvailableException.class, "No data", new Callable<Void>() {

        public Void call() throws Exception {
            res.fetchAll();
            return null;
        }
    });
    assertThrows(FeatureNotAvailableException.class, "No data", new Callable<Void>() {

        public Void call() throws Exception {
            res.next();
            return null;
        }
    });
    assertThrows(FeatureNotAvailableException.class, "No data", new Callable<Void>() {

        public Void call() throws Exception {
            res.hasNext();
            return null;
        }
    });
    assertThrows(FeatureNotAvailableException.class, "No data", new Callable<Void>() {

        public Void call() throws Exception {
            res.getColumnCount();
            return null;
        }
    });
    assertThrows(FeatureNotAvailableException.class, "No data", new Callable<Void>() {

        public Void call() throws Exception {
            res.getColumns();
            return null;
        }
    });
    assertThrows(FeatureNotAvailableException.class, "No data", new Callable<Void>() {

        public Void call() throws Exception {
            res.getColumnNames();
            return null;
        }
    });
    assertThrows(FeatureNotAvailableException.class, "No data", new Callable<Void>() {

        public Void call() throws Exception {
            res.count();
            return null;
        }
    });
}
Also used : SqlStatement(com.mysql.cj.xdevapi.SqlStatement) SqlResult(com.mysql.cj.xdevapi.SqlResult) WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException) CJCommunicationsException(com.mysql.cj.exceptions.CJCommunicationsException) FileNotFoundException(java.io.FileNotFoundException) FeatureNotAvailableException(com.mysql.cj.exceptions.FeatureNotAvailableException) ExecutionException(java.util.concurrent.ExecutionException) CJPacketTooBigException(com.mysql.cj.exceptions.CJPacketTooBigException) Test(org.junit.jupiter.api.Test)

Example 5 with SqlStatement

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

the class SessionTest method errorOnPacketTooBig.

/**
 * Test the client-side enforcing of server `mysqlx_max_allowed_packet'.
 */
@Test
public void errorOnPacketTooBig() {
    // to free the memory from previous tests artifacts
    System.gc();
    SqlStatement stmt = this.session.sql("select @@mysqlx_max_allowed_packet");
    SqlResult res = stmt.execute();
    Row r = res.next();
    long mysqlxMaxAllowedPacket = r.getLong(0);
    long size = 100 + mysqlxMaxAllowedPacket;
    StringBuilder b = new StringBuilder();
    for (int i = 0; i < size; ++i) {
        b.append('a');
    }
    String s = b.append("\"}").toString();
    assertThrows("Large packet should cause an exception", CJPacketTooBigException.class, () -> {
        this.session.dropSchema(s);
        return null;
    });
}
Also used : SqlStatement(com.mysql.cj.xdevapi.SqlStatement) SqlResult(com.mysql.cj.xdevapi.SqlResult) Row(com.mysql.cj.xdevapi.Row) JsonString(com.mysql.cj.xdevapi.JsonString) Test(org.junit.jupiter.api.Test)

Aggregations

SqlStatement (com.mysql.cj.xdevapi.SqlStatement)7 Test (org.junit.jupiter.api.Test)7 SqlResult (com.mysql.cj.xdevapi.SqlResult)6 Row (com.mysql.cj.xdevapi.Row)3 CJCommunicationsException (com.mysql.cj.exceptions.CJCommunicationsException)2 CJPacketTooBigException (com.mysql.cj.exceptions.CJPacketTooBigException)2 FeatureNotAvailableException (com.mysql.cj.exceptions.FeatureNotAvailableException)2 WrongArgumentException (com.mysql.cj.exceptions.WrongArgumentException)2 FileNotFoundException (java.io.FileNotFoundException)2 ExecutionException (java.util.concurrent.ExecutionException)2 JsonString (com.mysql.cj.xdevapi.JsonString)1