Search in sources :

Example 31 with SqlResult

use of com.mysql.cj.xdevapi.SqlResult 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 32 with SqlResult

use of com.mysql.cj.xdevapi.SqlResult 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)

Example 33 with SqlResult

use of com.mysql.cj.xdevapi.SqlResult 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 34 with SqlResult

use of com.mysql.cj.xdevapi.SqlResult 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)

Example 35 with SqlResult

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

the class SessionTest method testConnectionCloseNotification.

@Test
public void testConnectionCloseNotification() throws Exception {
    String shutdownMessage = "Server shutdown in progress";
    String ioReadErrorMessage = "IO Read error: read_timeout exceeded";
    String sessionWasKilledMessage = "Session was killed";
    // create notice message buffers
    Frame.Builder shutdownNotice = Frame.newBuilder().setScope(Frame.Scope.GLOBAL).setType(Frame.Type.WARNING_VALUE).setPayload(com.mysql.cj.x.protobuf.MysqlxNotice.Warning.newBuilder().setCode(MysqlErrorNumbers.ER_SERVER_SHUTDOWN).setMsg(shutdownMessage).build().toByteString());
    Frame.Builder ioReadErrorNotice = Frame.newBuilder().setScope(Frame.Scope.GLOBAL).setType(Frame.Type.WARNING_VALUE).setPayload(com.mysql.cj.x.protobuf.MysqlxNotice.Warning.newBuilder().setCode(MysqlErrorNumbers.ER_IO_READ_ERROR).setMsg(ioReadErrorMessage).build().toByteString());
    Frame.Builder sessionWasKilledNotice = Frame.newBuilder().setScope(Frame.Scope.GLOBAL).setType(Frame.Type.WARNING_VALUE).setPayload(com.mysql.cj.x.protobuf.MysqlxNotice.Warning.newBuilder().setCode(MysqlErrorNumbers.ER_SESSION_WAS_KILLED).setMsg(sessionWasKilledMessage).build().toByteString());
    byte[] shutdownNoticeBytes = makeNoticeBytes(shutdownNotice.build());
    byte[] ioReadErrorNoticeBytes = makeNoticeBytes(ioReadErrorNotice.build());
    byte[] sessionWasKilledNoticeBytes = makeNoticeBytes(sessionWasKilledNotice.build());
    InjectedSocketFactory.flushAllStaticData();
    String url = this.baseUrl + (this.baseUrl.contains("?") ? "" : "?") + makeParam(PropertyKey.socketFactory, InjectedSocketFactory.class.getName(), !this.baseUrl.contains("?") || this.baseUrl.endsWith("?")) + makeParam(PropertyKey.xdevapiSslMode, XdevapiSslMode.DISABLED.toString()) + makeParam(PropertyKey.xdevapiCompression, Compression.DISABLED.toString()) + // to allow injection between result rows
    makeParam(PropertyKey.useReadAheadInput, "false");
    // ER_SERVER_SHUTDOWN
    // 1.1. Shutdown during a sync query execution.
    Session sess11 = this.fact.getSession(url);
    SocketFactory sf = ((SessionImpl) sess11).getSession().getProtocol().getSocketConnection().getSocketFactory();
    assertTrue(InjectedSocketFactory.class.isAssignableFrom(sf.getClass()));
    InjectedSocketFactory.injectedBuffer = shutdownNoticeBytes;
    assertThrows(CJCommunicationsException.class, shutdownMessage, () -> sess11.sql("SELECT 1").execute());
    // 1.2. Shutdown during a sync result set consuming
    Session sess12 = this.fact.getSession(url);
    SqlResult res0 = sess12.sql("SELECT 1").execute();
    assertTrue(res0.hasNext());
    Row r = res0.next();
    assertEquals("1", r.getString(0));
    InjectedSocketFactory.injectedBuffer = shutdownNoticeBytes;
    assertThrows(CJCommunicationsException.class, shutdownMessage, () -> res0.next());
    // 1.3. Shutdown during an async query execution.
    Session sess13 = this.fact.getSession(url);
    InjectedSocketFactory.injectedBuffer = shutdownNoticeBytes;
    assertThrows(ExecutionException.class, CJCommunicationsException.class.getName() + ": " + shutdownMessage, () -> sess13.sql("SELECT 1").executeAsync().get());
    // 1.4. Shutdown during an async result set consuming
    Session sess14 = this.fact.getSession(url);
    SqlResult res1 = sess14.sql("SELECT 1").executeAsync().get();
    InjectedSocketFactory.injectedBuffer = shutdownNoticeBytes;
    assertTrue(res1.hasNext());
    r = res1.next();
    assertEquals("1", r.getString(0));
    assertFalse(res1.hasNext());
    assertThrows(CJCommunicationsException.class, shutdownMessage, () -> sess14.getSchemas());
    // ER_IO_READ_ERROR
    // 2.1. ER_IO_READ_ERROR during a sync query execution.
    Session sess21 = this.fact.getSession(url);
    InjectedSocketFactory.injectedBuffer = ioReadErrorNoticeBytes;
    assertThrows(CJCommunicationsException.class, ioReadErrorMessage, () -> sess21.sql("SELECT 1").execute());
    // 2.2. ER_IO_READ_ERROR during a sync result set consuming
    Session sess22 = this.fact.getSession(url);
    SqlResult res2 = sess22.sql("SELECT 1").execute();
    assertTrue(res2.hasNext());
    r = res2.next();
    assertEquals("1", r.getString(0));
    InjectedSocketFactory.injectedBuffer = ioReadErrorNoticeBytes;
    assertThrows(CJCommunicationsException.class, ioReadErrorMessage, () -> res2.next());
    // 2.3. ER_IO_READ_ERROR during an async query execution.
    Session sess23 = this.fact.getSession(url);
    InjectedSocketFactory.injectedBuffer = ioReadErrorNoticeBytes;
    assertThrows(ExecutionException.class, CJCommunicationsException.class.getName() + ": " + ioReadErrorMessage, () -> sess23.sql("SELECT 1").executeAsync().get());
    // 2.4. ER_IO_READ_ERROR during an async result set consuming
    Session sess24 = this.fact.getSession(url);
    res1 = sess24.sql("SELECT 1").executeAsync().get();
    InjectedSocketFactory.injectedBuffer = ioReadErrorNoticeBytes;
    assertTrue(res1.hasNext());
    r = res1.next();
    assertEquals("1", r.getString(0));
    assertFalse(res1.hasNext());
    assertThrows(CJCommunicationsException.class, ioReadErrorMessage, () -> sess24.getSchemas());
    // ER_SESSION_WAS_KILLED
    // 3.1. ER_SESSION_WAS_KILLED during a sync query execution.
    Session sess31 = this.fact.getSession(url);
    InjectedSocketFactory.injectedBuffer = sessionWasKilledNoticeBytes;
    assertThrows(CJCommunicationsException.class, sessionWasKilledMessage, () -> sess31.sql("SELECT 1").execute());
    // 3.2. ER_SESSION_WAS_KILLED during a sync result set consuming
    Session sess32 = this.fact.getSession(url);
    SqlResult res3 = sess32.sql("SELECT 1").execute();
    assertTrue(res3.hasNext());
    r = res3.next();
    assertEquals("1", r.getString(0));
    InjectedSocketFactory.injectedBuffer = sessionWasKilledNoticeBytes;
    assertThrows(CJCommunicationsException.class, sessionWasKilledMessage, () -> res3.next());
    // 3.3. ER_SESSION_WAS_KILLED during an async query execution.
    Session sess33 = this.fact.getSession(url);
    InjectedSocketFactory.injectedBuffer = sessionWasKilledNoticeBytes;
    assertThrows(ExecutionException.class, CJCommunicationsException.class.getName() + ": " + sessionWasKilledMessage, () -> sess33.sql("SELECT 1").executeAsync().get());
    // 3.4. ER_SESSION_WAS_KILLED during an async result set consuming
    Session sess34 = this.fact.getSession(url);
    res1 = sess34.sql("SELECT 1").executeAsync().get();
    InjectedSocketFactory.injectedBuffer = sessionWasKilledNoticeBytes;
    assertTrue(res1.hasNext());
    r = res1.next();
    assertEquals("1", r.getString(0));
    assertFalse(res1.hasNext());
    assertThrows(CJCommunicationsException.class, sessionWasKilledMessage, () -> sess34.getSchemas());
// TODO use a mock server instead of a real second server instance
// /*
// * With pooling.
// */
// 
// if (this.isSetForOpensslXTests) {
// 
// this.fact.getSession(this.baseOpensslUrl);
// 
// final String testUriPattern = "mysqlx://%s:%s@[%s]/%s?" + makeParam(PropertyKey.socketFactory, InjectedSocketFactory.class.getName())
// + makeParam(PropertyKey.xdevapiSslMode, XdevapiSslMode.DISABLED.toString()) + makeParam(PropertyKey.allowPublicKeyRetrieval, "true")
// + makeParam(PropertyKey.xdevapiCompression, Compression.DISABLED.toString())
// // to allow injection between result rows
// + makeParam(PropertyKey.useReadAheadInput, "false");
// 
// String testHosts = "(address=" + getTestHost() + ":" + getTestPort() + "),(address=" + getTestSslHost() + ":" + getTestSslPort() + ")";
// url = String.format(testUriPattern, getTestUser() == null ? "" : getTestUser(), getTestPassword() == null ? "" : getTestPassword(), testHosts,
// getTestDatabase());
// 
// Field fProtocol = CoreSession.class.getDeclaredField("protocol");
// fProtocol.setAccessible(true);
// 
// Field idleProtocols = ClientImpl.class.getDeclaredField("idleProtocols");
// idleProtocols.setAccessible(true);
// Field activeProtocols = ClientImpl.class.getDeclaredField("activeProtocols");
// activeProtocols.setAccessible(true);
// Field nonPooledSessions = ClientImpl.class.getDeclaredField("nonPooledSessions");
// nonPooledSessions.setAccessible(true);
// 
// String host1 = getTestHost();
// String host2 = getTestSslHost();
// int port1 = getTestPort();
// int port2 = getTestSslPort();
// 
// final ClientFactory cf = new ClientFactory();
// Client cli0 = cf.getClient(url, "{\"pooling\": {\"enabled\": true}}");
// 
// InjectedSocketFactory.downHost(getTestSslHost() + ":" + getTestSslPort());
// Session s1_1 = cli0.getSession();
// Session s1_2 = cli0.getSession();
// Session s1_3 = cli0.getSession();
// 
// assertEquals(host1, ((SessionImpl) s1_1).getSession().getProcessHost());
// assertEquals(host1, ((SessionImpl) s1_2).getSession().getProcessHost());
// assertEquals(host1, ((SessionImpl) s1_3).getSession().getProcessHost());
// assertEquals(port1, ((SessionImpl) s1_1).getSession().getPort());
// assertEquals(port1, ((SessionImpl) s1_2).getSession().getPort());
// assertEquals(port1, ((SessionImpl) s1_3).getSession().getPort());
// 
// InjectedSocketFactory.dontDownHost(getTestSslHost() + ":" + getTestSslPort());
// InjectedSocketFactory.downHost(getTestHost() + ":" + getTestPort());
// Session s2_1 = cli0.getSession();
// Session s2_2 = cli0.getSession();
// Session s2_3 = cli0.getSession();
// 
// assertEquals(host2, ((SessionImpl) s2_1).getSession().getProcessHost());
// assertEquals(host2, ((SessionImpl) s2_2).getSession().getProcessHost());
// assertEquals(host2, ((SessionImpl) s2_3).getSession().getProcessHost());
// assertEquals(port2, ((SessionImpl) s2_1).getSession().getPort());
// assertEquals(port2, ((SessionImpl) s2_2).getSession().getPort());
// assertEquals(port2, ((SessionImpl) s2_3).getSession().getPort());
// 
// InjectedSocketFactory.dontDownHost(getTestHost() + ":" + getTestPort());
// InjectedSocketFactory.downHost(getTestSslHost() + ":" + getTestSslPort());
// Session s1 = cli0.getSession();
// 
// assertEquals(host1, ((SessionImpl) s1).getSession().getProcessHost());
// assertEquals(port1, ((SessionImpl) s1).getSession().getPort());
// 
// s1_1.close();
// s1_2.close();
// s1_3.close();
// s2_1.close();
// s2_2.close();
// s2_3.close();
// 
// assertEquals(1, ((Set<WeakReference<PooledXProtocol>>) activeProtocols.get(cli0)).size());
// assertEquals(6, ((BlockingQueue<PooledXProtocol>) idleProtocols.get(cli0)).size());
// 
// // ER_IO_READ_ERROR
// 
// InjectedSocketFactory.injectedBuffer = ioReadErrorNoticeBytes;
// assertThrows(CJCommunicationsException.class, ioReadErrorMessage, () -> s1.sql("SELECT 1").execute());
// 
// assertEquals(0, ((Set<WeakReference<PooledXProtocol>>) activeProtocols.get(cli0)).size());
// assertEquals(6, ((BlockingQueue<PooledXProtocol>) idleProtocols.get(cli0)).size());
// assertThrows(CJCommunicationsException.class, "Unable to write message", () -> s1.sql("SELECT 1").execute());
// 
// // ER_SESSION_WAS_KILLED
// 
// Session s2 = cli0.getSession();
// assertEquals(host1, ((SessionImpl) s2).getSession().getProcessHost());
// assertEquals(port1, ((SessionImpl) s2).getSession().getPort());
// assertEquals(1, ((Set<WeakReference<PooledXProtocol>>) activeProtocols.get(cli0)).size());
// assertEquals(5, ((BlockingQueue<PooledXProtocol>) idleProtocols.get(cli0)).size());
// 
// InjectedSocketFactory.injectedBuffer = sessionWasKilledNoticeBytes;
// assertThrows(CJCommunicationsException.class, sessionWasKilledMessage, () -> s2.sql("SELECT 1").execute());
// 
// assertEquals(0, ((Set<WeakReference<PooledXProtocol>>) activeProtocols.get(cli0)).size());
// assertEquals(5, ((BlockingQueue<PooledXProtocol>) idleProtocols.get(cli0)).size());
// assertThrows(CJCommunicationsException.class, "Unable to write message", () -> s2.sql("SELECT 1").execute());
// 
// // ER_SERVER_SHUTDOWN
// 
// Session s3 = cli0.getSession();
// assertEquals(host1, ((SessionImpl) s3).getSession().getProcessHost());
// assertEquals(port1, ((SessionImpl) s3).getSession().getPort());
// assertEquals(1, ((Set<WeakReference<PooledXProtocol>>) activeProtocols.get(cli0)).size());
// assertEquals(4, ((BlockingQueue<PooledXProtocol>) idleProtocols.get(cli0)).size());
// 
// InjectedSocketFactory.injectedBuffer = shutdownNoticeBytes;
// assertThrows(CJCommunicationsException.class, shutdownMessage, () -> s3.sql("SELECT 1").execute());
// 
// assertEquals(0, ((Set<WeakReference<PooledXProtocol>>) activeProtocols.get(cli0)).size());
// assertEquals(3, ((BlockingQueue<PooledXProtocol>) idleProtocols.get(cli0)).size());
// }
}
Also used : Frame(com.mysql.cj.x.protobuf.MysqlxNotice.Frame) SqlResult(com.mysql.cj.xdevapi.SqlResult) InjectedSocketFactory(testsuite.InjectedSocketFactory) SocketFactory(com.mysql.cj.protocol.SocketFactory) InjectedSocketFactory(testsuite.InjectedSocketFactory) UnreliableSocketFactory(testsuite.UnreliableSocketFactory) JsonString(com.mysql.cj.xdevapi.JsonString) Row(com.mysql.cj.xdevapi.Row) CJCommunicationsException(com.mysql.cj.exceptions.CJCommunicationsException) CoreSession(com.mysql.cj.CoreSession) Session(com.mysql.cj.xdevapi.Session) Test(org.junit.jupiter.api.Test)

Aggregations

SqlResult (com.mysql.cj.xdevapi.SqlResult)39 Test (org.junit.jupiter.api.Test)27 Row (com.mysql.cj.xdevapi.Row)17 JsonString (com.mysql.cj.xdevapi.JsonString)11 Session (com.mysql.cj.xdevapi.Session)8 ExecutionException (java.util.concurrent.ExecutionException)7 WrongArgumentException (com.mysql.cj.exceptions.WrongArgumentException)6 SqlStatement (com.mysql.cj.xdevapi.SqlStatement)6 CoreSession (com.mysql.cj.CoreSession)5 CJCommunicationsException (com.mysql.cj.exceptions.CJCommunicationsException)4 StatementExecuteOkBuilder (com.mysql.cj.protocol.x.StatementExecuteOkBuilder)4 SqlResultBuilder (com.mysql.cj.xdevapi.SqlResultBuilder)4 ArrayList (java.util.ArrayList)4 CJPacketTooBigException (com.mysql.cj.exceptions.CJPacketTooBigException)3 FeatureNotAvailableException (com.mysql.cj.exceptions.FeatureNotAvailableException)3 XProtocolRowInputStream (com.mysql.cj.protocol.x.XProtocolRowInputStream)3 DbDoc (com.mysql.cj.xdevapi.DbDoc)3 DbDocImpl (com.mysql.cj.xdevapi.DbDocImpl)3 DocResult (com.mysql.cj.xdevapi.DocResult)3 SessionFactory (com.mysql.cj.xdevapi.SessionFactory)3