Search in sources :

Example 6 with SqlStatement

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

the class SessionTest method basicSql.

@Test
public void basicSql() {
    SqlStatement stmt = this.session.sql("select 1,2,3 from dual");
    SqlResult res = stmt.execute();
    assertTrue(res.hasData());
    Row r = res.next();
    assertEquals("1", r.getString(0));
    assertEquals("2", r.getString(1));
    assertEquals("3", r.getString(2));
    assertEquals("1", r.getString("1"));
    assertEquals("2", r.getString("2"));
    assertEquals("3", r.getString("3"));
    assertFalse(res.hasNext());
    assertThrows(XDevAPIError.class, "Method getAutoIncrementValue\\(\\) is allowed only for insert statements.", new Callable<Void>() {

        public Void call() throws Exception {
            assertEquals(null, res.getAutoIncrementValue());
            return null;
        }
    });
}
Also used : SqlStatement(com.mysql.cj.xdevapi.SqlStatement) SqlResult(com.mysql.cj.xdevapi.SqlResult) Row(com.mysql.cj.xdevapi.Row) 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 7 with SqlStatement

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

the class SessionTest method sqlInsertAutoIncrementValue.

@Test
public void sqlInsertAutoIncrementValue() {
    try {
        sqlUpdate("drop table if exists lastInsertId");
        sqlUpdate("create table lastInsertId (id int not null primary key auto_increment, name varchar(20) not null)");
        SqlStatement stmt = this.session.sql("insert into lastInsertId values (null, 'a')");
        SqlResult res = stmt.execute();
        assertFalse(res.hasData());
        assertEquals(1, res.getAffectedItemsCount());
        assertEquals(0, res.getWarningsCount());
        assertFalse(res.getWarnings().hasNext());
        assertEquals(new Long(1), res.getAutoIncrementValue());
    } finally {
        sqlUpdate("drop table if exists lastInsertId");
    }
}
Also used : SqlStatement(com.mysql.cj.xdevapi.SqlStatement) SqlResult(com.mysql.cj.xdevapi.SqlResult) 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