Search in sources :

Example 21 with Session

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

the class CollectionFindTest method testCollectionRowLockOptions.

@Test
public void testCollectionRowLockOptions() throws Exception {
    assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5")), "MySQL 8.0.5+ is required to run this test.");
    Function<DocResult, List<String>> asStringList = rr -> rr.fetchAll().stream().map(d -> ((JsonString) d.get("_id")).getString()).collect(Collectors.toList());
    this.collection.add("{\"_id\":\"1\", \"a\":1}").add("{\"_id\":\"2\", \"a\":1}").add("{\"_id\":\"3\", \"a\":1}").execute();
    Session session1 = null;
    Session session2 = null;
    try {
        session1 = new SessionFactory().getSession(this.testProperties);
        Collection col1 = session1.getDefaultSchema().getCollection(this.collectionName);
        session2 = new SessionFactory().getSession(this.testProperties);
        Collection col2 = session2.getDefaultSchema().getCollection(this.collectionName);
        DocResult res;
        CompletableFuture<DocResult> futRes;
        /*
             * 1. Shared Lock in both sessions.
             */
        // session2.lockShared() returns data immediately.
        session1.startTransaction();
        col1.find("_id = '1'").lockShared().execute();
        session2.startTransaction();
        res = col2.find("_id < '3'").lockShared().execute();
        assertEquals(2, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), hasItems("1", "2"));
        session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockShared().executeAsync();
        res = futRes.get(3, TimeUnit.SECONDS);
        assertTrue(futRes.isDone());
        assertEquals(2, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("1", "2"));
        session2.rollback();
        session1.rollback();
        // session2.lockShared(NOWAIT) returns data immediately.
        session1.startTransaction();
        col1.find("_id = '1'").lockShared().execute();
        session2.startTransaction();
        res = col2.find("_id < '3'").lockShared(Statement.LockContention.NOWAIT).execute();
        assertEquals(2, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("1", "2"));
        session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockShared(Statement.LockContention.NOWAIT).executeAsync();
        res = futRes.get(3, TimeUnit.SECONDS);
        assertTrue(futRes.isDone());
        assertEquals(2, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("1", "2"));
        session2.rollback();
        session1.rollback();
        // session2.lockShared(SKIP_LOCK) returns data immediately.
        session1.startTransaction();
        col1.find("_id = '1'").lockShared().execute();
        session2.startTransaction();
        res = col2.find("_id < '3'").lockShared(Statement.LockContention.SKIP_LOCKED).execute();
        assertEquals(2, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("1", "2"));
        session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockShared(Statement.LockContention.SKIP_LOCKED).executeAsync();
        res = futRes.get(3, TimeUnit.SECONDS);
        assertTrue(futRes.isDone());
        assertEquals(2, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("1", "2"));
        session2.rollback();
        session1.rollback();
        /*
             * 2. Shared Lock in first session and exclusive lock in second.
             */
        // session2.lockExclusive() blocks until session1 ends.
        session1.startTransaction();
        col1.find("_id = '1'").lockShared().execute();
        // session2.startTransaction();
        // res = col2.find("_id < '3'").lockExclusive().execute(); (Can't test)
        // session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockExclusive().executeAsync();
        final CompletableFuture<DocResult> fr1 = futRes;
        assertThrows(TimeoutException.class, () -> fr1.get(3, TimeUnit.SECONDS));
        // Unlocks session2.
        session1.rollback();
        res = futRes.get(3, TimeUnit.SECONDS);
        assertTrue(futRes.isDone());
        assertEquals(2, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("1", "2"));
        session2.rollback();
        // session2.lockExclusive(NOWAIT) should return locking error.
        session1.startTransaction();
        col1.find("_id = '1'").lockShared().execute();
        session2.startTransaction();
        assertThrows(XProtocolError.class, "ERROR 3572 \\(HY000\\) Statement aborted because lock\\(s\\) could not be acquired immediately and NOWAIT is set\\.", () -> col2.find("_id < '3'").lockExclusive(Statement.LockContention.NOWAIT).execute());
        session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockExclusive(Statement.LockContention.NOWAIT).executeAsync();
        final CompletableFuture<DocResult> fr2 = futRes;
        assertThrows(ExecutionException.class, ".*XProtocolError: ERROR 3572 \\(HY000\\) Statement aborted because lock\\(s\\) could not be acquired immediately and NOWAIT is set\\.", () -> fr2.get(3, TimeUnit.SECONDS));
        session2.rollback();
        session1.rollback();
        // session2.lockExclusive(SKIP_LOCK) should return (unlocked) data immediately.
        session1.startTransaction();
        col1.find("_id = '1'").lockShared().execute();
        session2.startTransaction();
        res = col2.find("_id < '3'").lockExclusive(Statement.LockContention.SKIP_LOCKED).execute();
        assertEquals(1, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("2"));
        session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockExclusive(Statement.LockContention.SKIP_LOCKED).executeAsync();
        res = futRes.get(3, TimeUnit.SECONDS);
        assertTrue(futRes.isDone());
        assertEquals(1, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("2"));
        session2.rollback();
        session1.rollback();
        /*
             * 3. Exclusive Lock in first session and shared lock in second.
             */
        // session2.lockShared() blocks until session1 ends.
        session1.startTransaction();
        col1.find("_id = '1'").lockExclusive().execute();
        // session2.startTransaction();
        // res = col2.find("_id < '3'").lockShared().execute(); (Can't test)
        // session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockShared().executeAsync();
        final CompletableFuture<DocResult> fr3 = futRes;
        assertThrows(TimeoutException.class, () -> fr3.get(3, TimeUnit.SECONDS));
        // Unlocks session2.
        session1.rollback();
        res = futRes.get(3, TimeUnit.SECONDS);
        assertTrue(futRes.isDone());
        assertEquals(2, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("1", "2"));
        session2.rollback();
        // session2.lockShared(NOWAIT) should return locking error.
        session1.startTransaction();
        col1.find("_id = '1'").lockExclusive().execute();
        session2.startTransaction();
        assertThrows(XProtocolError.class, "ERROR 3572 \\(HY000\\) Statement aborted because lock\\(s\\) could not be acquired immediately and NOWAIT is set\\.", () -> col2.find("_id < '3'").lockShared(Statement.LockContention.NOWAIT).execute());
        session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockShared(Statement.LockContention.NOWAIT).executeAsync();
        final CompletableFuture<DocResult> fr4 = futRes;
        assertThrows(ExecutionException.class, ".*XProtocolError: ERROR 3572 \\(HY000\\) Statement aborted because lock\\(s\\) could not be acquired immediately and NOWAIT is set\\.", () -> fr4.get(3, TimeUnit.SECONDS));
        session2.rollback();
        session1.rollback();
        // session2.lockShared(SKIP_LOCK) should return (unlocked) data immediately.
        session1.startTransaction();
        col1.find("_id = '1'").lockExclusive().execute();
        session2.startTransaction();
        res = col2.find("_id < '3'").lockShared(Statement.LockContention.SKIP_LOCKED).execute();
        assertEquals(1, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("2"));
        session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockShared(Statement.LockContention.SKIP_LOCKED).executeAsync();
        res = futRes.get(3, TimeUnit.SECONDS);
        assertTrue(futRes.isDone());
        assertEquals(1, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("2"));
        session2.rollback();
        session1.rollback();
        /*
             * 4. Exclusive Lock in both sessions.
             */
        // session2.lockExclusive() blocks until session1 ends.
        session1.startTransaction();
        col1.find("_id = '1'").lockExclusive().execute();
        // session2.startTransaction();
        // res = col2.find("_id < '3'").lockExclusive().execute(); (Can't test)
        // session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockExclusive().executeAsync();
        final CompletableFuture<DocResult> fr5 = futRes;
        assertThrows(TimeoutException.class, () -> fr5.get(3, TimeUnit.SECONDS));
        // Unlocks session2.
        session1.rollback();
        res = futRes.get(3, TimeUnit.SECONDS);
        assertTrue(futRes.isDone());
        assertEquals(2, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("1", "2"));
        session2.rollback();
        // session2.lockExclusive(NOWAIT) should return locking error.
        session1.startTransaction();
        col1.find("_id = '1'").lockExclusive().execute();
        session2.startTransaction();
        assertThrows(XProtocolError.class, "ERROR 3572 \\(HY000\\) Statement aborted because lock\\(s\\) could not be acquired immediately and NOWAIT is set\\.", () -> col2.find("_id < '3'").lockExclusive(Statement.LockContention.NOWAIT).execute());
        session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockExclusive(Statement.LockContention.NOWAIT).executeAsync();
        final CompletableFuture<DocResult> fr6 = futRes;
        assertThrows(ExecutionException.class, ".*XProtocolError: ERROR 3572 \\(HY000\\) Statement aborted because lock\\(s\\) could not be acquired immediately and NOWAIT is set\\.", () -> fr6.get(3, TimeUnit.SECONDS));
        session2.rollback();
        session1.rollback();
        // session2.lockExclusive(SKIP_LOCK) should return (unlocked) data immediately.
        session1.startTransaction();
        col1.find("_id = '1'").lockExclusive().execute();
        session2.startTransaction();
        res = col2.find("_id < '3'").lockExclusive(Statement.LockContention.SKIP_LOCKED).execute();
        assertEquals(1, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("2"));
        session2.rollback();
        session2.startTransaction();
        futRes = col2.find("_id < '3'").lockExclusive(Statement.LockContention.SKIP_LOCKED).executeAsync();
        res = futRes.get(3, TimeUnit.SECONDS);
        assertTrue(futRes.isDone());
        assertEquals(1, asStringList.apply(res).size());
        assertThat(asStringList.apply(res), CoreMatchers.hasItems("2"));
        session2.rollback();
        session1.rollback();
    } finally {
        if (session1 != null) {
            session1.close();
        }
        if (session2 != null) {
            session2.close();
        }
    }
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) RowResult(com.mysql.cj.xdevapi.RowResult) JsonNumber(com.mysql.cj.xdevapi.JsonNumber) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) TimeoutException(java.util.concurrent.TimeoutException) Collection(com.mysql.cj.xdevapi.Collection) Statement(com.mysql.cj.xdevapi.Statement) BigDecimal(java.math.BigDecimal) Expression.expr(com.mysql.cj.xdevapi.Expression.expr) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) FindStatementImpl(com.mysql.cj.xdevapi.FindStatementImpl) Map(java.util.Map) Table(com.mysql.cj.xdevapi.Table) ServerVersion(com.mysql.cj.ServerVersion) WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException) Warning(com.mysql.cj.xdevapi.Warning) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) FindStatement(com.mysql.cj.xdevapi.FindStatement) Assertions.fail(org.junit.jupiter.api.Assertions.fail) MysqlErrorNumbers(com.mysql.cj.exceptions.MysqlErrorNumbers) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Result(com.mysql.cj.xdevapi.Result) DbDocImpl(com.mysql.cj.xdevapi.DbDocImpl) JsonArray(com.mysql.cj.xdevapi.JsonArray) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) JsonString(com.mysql.cj.xdevapi.JsonString) Function(java.util.function.Function) ArrayList(java.util.ArrayList) AddResult(com.mysql.cj.xdevapi.AddResult) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SessionImpl(com.mysql.cj.xdevapi.SessionImpl) Session(com.mysql.cj.xdevapi.Session) Iterator(java.util.Iterator) CoreMatchers.hasItems(org.hamcrest.CoreMatchers.hasItems) DbDoc(com.mysql.cj.xdevapi.DbDoc) DocResult(com.mysql.cj.xdevapi.DocResult) XProtocolError(com.mysql.cj.protocol.x.XProtocolError) SessionFactory(com.mysql.cj.xdevapi.SessionFactory) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Row(com.mysql.cj.xdevapi.Row) SqlResult(com.mysql.cj.xdevapi.SqlResult) JsonLiteral(com.mysql.cj.xdevapi.JsonLiteral) SessionFactory(com.mysql.cj.xdevapi.SessionFactory) Collection(com.mysql.cj.xdevapi.Collection) List(java.util.List) ArrayList(java.util.ArrayList) JsonString(com.mysql.cj.xdevapi.JsonString) DocResult(com.mysql.cj.xdevapi.DocResult) Session(com.mysql.cj.xdevapi.Session) Test(org.junit.jupiter.api.Test)

Example 22 with Session

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

the class CollectionFindTest method testPreparedStatements.

@Test
public void testPreparedStatements() {
    assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.14")), "MySQL 8.0.14+ is required to run this test.");
    // Prepare test data.
    this.collection.add("{\"_id\":\"1\", \"ord\": 1}", "{\"_id\":\"2\", \"ord\": 2}", "{\"_id\":\"3\", \"ord\": 3}", "{\"_id\":\"4\", \"ord\": 4}", "{\"_id\":\"5\", \"ord\": 5}", "{\"_id\":\"6\", \"ord\": 6}", "{\"_id\":\"7\", \"ord\": 7}", "{\"_id\":\"8\", \"ord\": 8}").execute();
    SessionFactory sf = new SessionFactory();
    /*
         * Test common usage.
         */
    Session testSession = sf.getSession(this.testProperties);
    int sessionThreadId = getThreadId(testSession);
    assertPreparedStatementsCount(sessionThreadId, 0, 1);
    assertPreparedStatementsStatusCounts(testSession, 0, 0, 0);
    Collection testCol = testSession.getDefaultSchema().getCollection(this.collectionName);
    // Initialize several FindStatement objects.
    // Find all.
    FindStatement testFind1 = testCol.find();
    // Criteria with one placeholder.
    FindStatement testFind2 = testCol.find("$.ord >= :n");
    // Criteria with same placeholder repeated.
    FindStatement testFind3 = testCol.find("$.ord >= :n AND $.ord <= :n + 3");
    // Criteria with multiple placeholders.
    FindStatement testFind4 = testCol.find("$.ord >= :n AND $.ord <= :m");
    assertPreparedStatementsCountsAndId(testSession, 0, testFind1, 0, -1);
    assertPreparedStatementsCountsAndId(testSession, 0, testFind2, 0, -1);
    assertPreparedStatementsCountsAndId(testSession, 0, testFind3, 0, -1);
    assertPreparedStatementsCountsAndId(testSession, 0, testFind4, 0, -1);
    assertPreparedStatementsStatusCounts(testSession, 0, 0, 0);
    // A. Set binds: 1st execute -> non-prepared.
    assertTestPreparedStatementsResult(testFind1.execute(), 1, 8);
    assertPreparedStatementsCountsAndId(testSession, 0, testFind1, 0, -1);
    assertTestPreparedStatementsResult(testFind2.bind("n", 2).execute(), 2, 8);
    assertPreparedStatementsCountsAndId(testSession, 0, testFind2, 0, -1);
    assertTestPreparedStatementsResult(testFind3.bind("n", 2).execute(), 2, 5);
    assertPreparedStatementsCountsAndId(testSession, 0, testFind3, 0, -1);
    assertTestPreparedStatementsResult(testFind4.bind("n", 2).bind("m", 5).execute(), 2, 5);
    assertPreparedStatementsCountsAndId(testSession, 0, testFind4, 0, -1);
    assertPreparedStatementsStatusCounts(testSession, 0, 0, 0);
    // B. Set sort resets execution count: 1st execute -> non-prepared.
    assertTestPreparedStatementsResult(testFind1.sort("$._id").execute(), 1, 8);
    assertPreparedStatementsCountsAndId(testSession, 0, testFind1, 0, -1);
    assertTestPreparedStatementsResult(testFind2.sort("$._id").execute(), 2, 8);
    assertPreparedStatementsCountsAndId(testSession, 0, testFind2, 0, -1);
    assertTestPreparedStatementsResult(testFind3.sort("$._id").execute(), 2, 5);
    assertPreparedStatementsCountsAndId(testSession, 0, testFind3, 0, -1);
    assertTestPreparedStatementsResult(testFind4.sort("$._id").execute(), 2, 5);
    assertPreparedStatementsCountsAndId(testSession, 0, testFind4, 0, -1);
    assertPreparedStatementsStatusCounts(testSession, 0, 0, 0);
    // C. Set binds reuse statement: 2nd execute -> prepare + execute.
    assertTestPreparedStatementsResult(testFind1.execute(), 1, 8);
    assertPreparedStatementsCountsAndId(testSession, 1, testFind1, 1, 1);
    assertTestPreparedStatementsResult(testFind2.bind("n", 3).execute(), 3, 8);
    assertPreparedStatementsCountsAndId(testSession, 2, testFind2, 2, 1);
    assertTestPreparedStatementsResult(testFind3.bind("n", 3).execute(), 3, 6);
    assertPreparedStatementsCountsAndId(testSession, 3, testFind3, 3, 1);
    assertTestPreparedStatementsResult(testFind4.bind("m", 6).execute(), 2, 6);
    assertPreparedStatementsCountsAndId(testSession, 4, testFind4, 4, 1);
    assertPreparedStatementsStatusCounts(testSession, 4, 4, 0);
    // D. Set binds reuse statement: 3rd execute -> execute.
    assertTestPreparedStatementsResult(testFind1.execute(), 1, 8);
    assertPreparedStatementsCountsAndId(testSession, 4, testFind1, 1, 2);
    assertTestPreparedStatementsResult(testFind2.bind("n", 4).execute(), 4, 8);
    assertPreparedStatementsCountsAndId(testSession, 4, testFind2, 2, 2);
    assertTestPreparedStatementsResult(testFind3.bind("n", 4).execute(), 4, 7);
    assertPreparedStatementsCountsAndId(testSession, 4, testFind3, 3, 2);
    assertTestPreparedStatementsResult(testFind4.bind("n", 3).bind("m", 7).execute(), 3, 7);
    assertPreparedStatementsCountsAndId(testSession, 4, testFind4, 4, 2);
    assertPreparedStatementsStatusCounts(testSession, 4, 8, 0);
    // E. Set sort deallocates and resets execution count: 1st execute -> deallocate + non-prepared.
    assertTestPreparedStatementsResult(testFind1.sort("$._id").execute(), 1, 8);
    assertPreparedStatementsCountsAndId(testSession, 3, testFind1, 0, -1);
    assertTestPreparedStatementsResult(testFind2.sort("$._id").bind("n", 4).execute(), 4, 8);
    assertPreparedStatementsCountsAndId(testSession, 2, testFind2, 0, -1);
    assertTestPreparedStatementsResult(testFind3.sort("$._id").bind("n", 4).execute(), 4, 7);
    assertPreparedStatementsCountsAndId(testSession, 1, testFind3, 0, -1);
    assertTestPreparedStatementsResult(testFind4.sort("$._id").bind("n", 3).bind("m", 7).execute(), 3, 7);
    assertPreparedStatementsCountsAndId(testSession, 0, testFind4, 0, -1);
    assertPreparedStatementsStatusCounts(testSession, 4, 8, 4);
    // F. No Changes: 2nd execute -> prepare + execute.
    assertTestPreparedStatementsResult(testFind1.execute(), 1, 8);
    assertPreparedStatementsCountsAndId(testSession, 1, testFind1, 1, 1);
    assertTestPreparedStatementsResult(testFind2.bind("n", 4).execute(), 4, 8);
    assertPreparedStatementsCountsAndId(testSession, 2, testFind2, 2, 1);
    assertTestPreparedStatementsResult(testFind3.bind("n", 4).execute(), 4, 7);
    assertPreparedStatementsCountsAndId(testSession, 3, testFind3, 3, 1);
    assertTestPreparedStatementsResult(testFind4.bind("n", 3).bind("m", 7).execute(), 3, 7);
    assertPreparedStatementsCountsAndId(testSession, 4, testFind4, 4, 1);
    assertPreparedStatementsStatusCounts(testSession, 8, 12, 4);
    // G. Set limit for the first time deallocates and re-prepares: 1st execute -> re-prepare + execute.
    assertTestPreparedStatementsResult(testFind1.limit(2).execute(), 1, 2);
    assertPreparedStatementsCountsAndId(testSession, 4, testFind1, 1, 1);
    assertTestPreparedStatementsResult(testFind2.limit(2).execute(), 4, 5);
    assertPreparedStatementsCountsAndId(testSession, 4, testFind2, 2, 1);
    assertTestPreparedStatementsResult(testFind3.limit(2).execute(), 4, 5);
    assertPreparedStatementsCountsAndId(testSession, 4, testFind3, 3, 1);
    assertTestPreparedStatementsResult(testFind4.limit(2).execute(), 3, 4);
    assertPreparedStatementsCountsAndId(testSession, 4, testFind4, 4, 1);
    assertPreparedStatementsStatusCounts(testSession, 12, 16, 8);
    // H. Set limit and offset reuse prepared statement: 2nd execute -> execute.
    assertTestPreparedStatementsResult(testFind1.limit(1).offset(1).execute(), 2, 2);
    assertPreparedStatementsCountsAndId(testSession, 4, testFind1, 1, 2);
    assertTestPreparedStatementsResult(testFind2.limit(1).offset(1).execute(), 5, 5);
    assertPreparedStatementsCountsAndId(testSession, 4, testFind2, 2, 2);
    assertTestPreparedStatementsResult(testFind3.limit(1).offset(1).execute(), 5, 5);
    assertPreparedStatementsCountsAndId(testSession, 4, testFind3, 3, 2);
    assertTestPreparedStatementsResult(testFind4.limit(1).offset(1).execute(), 4, 4);
    assertPreparedStatementsCountsAndId(testSession, 4, testFind4, 4, 2);
    assertPreparedStatementsStatusCounts(testSession, 12, 20, 8);
    // I. Set sort deallocates and resets execution count, set limit and bind has no effect: 1st execute -> deallocate + non-prepared.
    assertTestPreparedStatementsResult(testFind1.sort("$._id").limit(2).execute(), 2, 3);
    assertPreparedStatementsCountsAndId(testSession, 3, testFind1, 0, -1);
    assertTestPreparedStatementsResult(testFind2.sort("$._id").limit(2).bind("n", 4).execute(), 5, 6);
    assertPreparedStatementsCountsAndId(testSession, 2, testFind2, 0, -1);
    assertTestPreparedStatementsResult(testFind3.sort("$._id").limit(2).bind("n", 4).execute(), 5, 6);
    assertPreparedStatementsCountsAndId(testSession, 1, testFind3, 0, -1);
    assertTestPreparedStatementsResult(testFind4.sort("$._id").limit(2).bind("n", 3).bind("m", 7).execute(), 4, 5);
    assertPreparedStatementsCountsAndId(testSession, 0, testFind4, 0, -1);
    assertPreparedStatementsStatusCounts(testSession, 12, 20, 12);
    // J. Set offset reuse statement: 2nd execute -> prepare + execute.
    assertTestPreparedStatementsResult(testFind1.offset(0).execute(), 1, 2);
    assertPreparedStatementsCountsAndId(testSession, 1, testFind1, 1, 1);
    assertTestPreparedStatementsResult(testFind2.offset(0).execute(), 4, 5);
    assertPreparedStatementsCountsAndId(testSession, 2, testFind2, 2, 1);
    assertTestPreparedStatementsResult(testFind3.offset(0).execute(), 4, 5);
    assertPreparedStatementsCountsAndId(testSession, 3, testFind3, 3, 1);
    assertTestPreparedStatementsResult(testFind4.offset(0).execute(), 3, 4);
    assertPreparedStatementsCountsAndId(testSession, 4, testFind4, 4, 1);
    assertPreparedStatementsStatusCounts(testSession, 16, 24, 12);
    testSession.close();
    // Prepared statements won't live past the closing of the session.
    assertPreparedStatementsCount(sessionThreadId, 0, 10);
    /*
         * Test falling back onto non-prepared statements.
         */
    testSession = sf.getSession(this.testProperties);
    int origMaxPrepStmtCount = this.session.sql("SELECT @@max_prepared_stmt_count").execute().fetchOne().getInt(0);
    try {
        // Allow preparing only one more statement.
        this.session.sql("SET GLOBAL max_prepared_stmt_count = ?").bind(getPreparedStatementsCount() + 1).execute();
        sessionThreadId = getThreadId(testSession);
        assertPreparedStatementsCount(sessionThreadId, 0, 1);
        assertPreparedStatementsStatusCounts(testSession, 0, 0, 0);
        testCol = testSession.getDefaultSchema().getCollection(this.collectionName);
        testFind1 = testCol.find();
        testFind2 = testCol.find();
        // 1st execute -> don't prepare.
        assertTestPreparedStatementsResult(testFind1.execute(), 1, 8);
        assertPreparedStatementsCountsAndId(testSession, 0, testFind1, 0, -1);
        assertTestPreparedStatementsResult(testFind2.execute(), 1, 8);
        assertPreparedStatementsCountsAndId(testSession, 0, testFind2, 0, -1);
        assertPreparedStatementsStatusCounts(testSession, 0, 0, 0);
        // 2nd execute -> prepare + execute.
        assertTestPreparedStatementsResult(testFind1.execute(), 1, 8);
        assertPreparedStatementsCountsAndId(testSession, 1, testFind1, 1, 1);
        // Fails preparing, execute as non-prepared.
        assertTestPreparedStatementsResult(testFind2.execute(), 1, 8);
        assertPreparedStatementsCountsAndId(testSession, 1, testFind2, 0, -1);
        // Failed prepare also counts.
        assertPreparedStatementsStatusCounts(testSession, 2, 1, 0);
        // 3rd execute -> execute.
        assertTestPreparedStatementsResult(testFind1.execute(), 1, 8);
        assertPreparedStatementsCountsAndId(testSession, 1, testFind1, 1, 2);
        // Execute as non-prepared.
        assertTestPreparedStatementsResult(testFind2.execute(), 1, 8);
        assertPreparedStatementsCountsAndId(testSession, 1, testFind2, 0, -1);
        assertPreparedStatementsStatusCounts(testSession, 2, 2, 0);
        testSession.close();
        // Prepared statements won't live past the closing of the session.
        assertPreparedStatementsCount(sessionThreadId, 0, 10);
    } finally {
        this.session.sql("SET GLOBAL max_prepared_stmt_count = ?").bind(origMaxPrepStmtCount).execute();
    }
}
Also used : SessionFactory(com.mysql.cj.xdevapi.SessionFactory) FindStatement(com.mysql.cj.xdevapi.FindStatement) Collection(com.mysql.cj.xdevapi.Collection) Session(com.mysql.cj.xdevapi.Session) Test(org.junit.jupiter.api.Test)

Example 23 with Session

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

the class CollectionRemoveTest method testPreparedStatements.

@Test
public void testPreparedStatements() {
    assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.14")), "MySQL 8.0.14+ is required to run this test.");
    try {
        // Prepare test data.
        testPreparedStatementsResetData();
        SessionFactory sf = new SessionFactory();
        /*
             * Test common usage.
             */
        Session testSession = sf.getSession(this.testProperties);
        int sessionThreadId = getThreadId(testSession);
        assertPreparedStatementsCount(sessionThreadId, 0, 1);
        assertPreparedStatementsStatusCounts(testSession, 0, 0, 0);
        Collection testCol1 = testSession.getDefaultSchema().getCollection(this.collectionName + "_1");
        Collection testCol2 = testSession.getDefaultSchema().getCollection(this.collectionName + "_2");
        Collection testCol3 = testSession.getDefaultSchema().getCollection(this.collectionName + "_3");
        Collection testCol4 = testSession.getDefaultSchema().getCollection(this.collectionName + "_4");
        // Initialize several RemoveStatement objects.
        // Remove all.
        RemoveStatement testRemove1 = testCol1.remove("true");
        // Criteria with one placeholder.
        RemoveStatement testRemove2 = testCol2.remove("$.ord >= :n");
        // Criteria with same placeholder repeated.
        RemoveStatement testRemove3 = testCol3.remove("$.ord >= :n AND $.ord <= :n + 1");
        // Criteria with multiple placeholders.
        RemoveStatement testRemove4 = testCol4.remove("$.ord >= :n AND $.ord <= :m");
        assertPreparedStatementsCountsAndId(testSession, 0, testRemove1, 0, -1);
        assertPreparedStatementsCountsAndId(testSession, 0, testRemove2, 0, -1);
        assertPreparedStatementsCountsAndId(testSession, 0, testRemove3, 0, -1);
        assertPreparedStatementsCountsAndId(testSession, 0, testRemove4, 0, -1);
        // A. Set binds: 1st execute -> non-prepared.
        assertTestPreparedStatementsResult(testRemove1.execute(), 4, testCol1.getName());
        assertPreparedStatementsCountsAndId(testSession, 0, testRemove1, 0, -1);
        assertTestPreparedStatementsResult(testRemove2.bind("n", 2).execute(), 3, testCol2.getName(), 1);
        assertPreparedStatementsCountsAndId(testSession, 0, testRemove2, 0, -1);
        assertTestPreparedStatementsResult(testRemove3.bind("n", 2).execute(), 2, testCol3.getName(), 1, 4);
        assertPreparedStatementsCountsAndId(testSession, 0, testRemove3, 0, -1);
        assertTestPreparedStatementsResult(testRemove4.bind("n", 2).bind("m", 3).execute(), 2, testCol4.getName(), 1, 4);
        assertPreparedStatementsCountsAndId(testSession, 0, testRemove4, 0, -1);
        assertPreparedStatementsStatusCounts(testSession, 0, 0, 0);
        testPreparedStatementsResetData();
        // B. Set sort resets execution count: 1st execute -> non-prepared.
        assertTestPreparedStatementsResult(testRemove1.sort("$._id").execute(), 4, testCol1.getName());
        assertPreparedStatementsCountsAndId(testSession, 0, testRemove1, 0, -1);
        assertTestPreparedStatementsResult(testRemove2.sort("$._id").execute(), 3, testCol2.getName(), 1);
        assertPreparedStatementsCountsAndId(testSession, 0, testRemove2, 0, -1);
        assertTestPreparedStatementsResult(testRemove3.sort("$._id").execute(), 2, testCol3.getName(), 1, 4);
        assertPreparedStatementsCountsAndId(testSession, 0, testRemove3, 0, -1);
        assertTestPreparedStatementsResult(testRemove4.sort("$._id").execute(), 2, testCol4.getName(), 1, 4);
        assertPreparedStatementsCountsAndId(testSession, 0, testRemove4, 0, -1);
        assertPreparedStatementsStatusCounts(testSession, 0, 0, 0);
        testPreparedStatementsResetData();
        // C. Set binds reuse statement: 2nd execute -> prepare + execute.
        assertTestPreparedStatementsResult(testRemove1.execute(), 4, testCol1.getName());
        assertPreparedStatementsCountsAndId(testSession, 1, testRemove1, 1, 1);
        assertTestPreparedStatementsResult(testRemove2.bind("n", 3).execute(), 2, testCol2.getName(), 1, 2);
        assertPreparedStatementsCountsAndId(testSession, 2, testRemove2, 2, 1);
        assertTestPreparedStatementsResult(testRemove3.bind("n", 3).execute(), 2, testCol3.getName(), 1, 2);
        assertPreparedStatementsCountsAndId(testSession, 3, testRemove3, 3, 1);
        assertTestPreparedStatementsResult(testRemove4.bind("m", 4).execute(), 3, testCol4.getName(), 1);
        assertPreparedStatementsCountsAndId(testSession, 4, testRemove4, 4, 1);
        assertPreparedStatementsStatusCounts(testSession, 4, 4, 0);
        testPreparedStatementsResetData();
        // D. Set binds reuse statement: 3rd execute -> execute.
        assertTestPreparedStatementsResult(testRemove1.execute(), 4, testCol1.getName());
        assertPreparedStatementsCountsAndId(testSession, 4, testRemove1, 1, 2);
        assertTestPreparedStatementsResult(testRemove2.bind("n", 4).execute(), 1, testCol2.getName(), 1, 2, 3);
        assertPreparedStatementsCountsAndId(testSession, 4, testRemove2, 2, 2);
        assertTestPreparedStatementsResult(testRemove3.bind("n", 1).execute(), 2, testCol3.getName(), 3, 4);
        assertPreparedStatementsCountsAndId(testSession, 4, testRemove3, 3, 2);
        assertTestPreparedStatementsResult(testRemove4.bind("m", 2).execute(), 1, testCol4.getName(), 1, 3, 4);
        assertPreparedStatementsCountsAndId(testSession, 4, testRemove4, 4, 2);
        assertPreparedStatementsStatusCounts(testSession, 4, 8, 0);
        testPreparedStatementsResetData();
        // E. Set sort deallocates and resets execution count: 1st execute -> deallocate + non-prepared.
        assertTestPreparedStatementsResult(testRemove1.sort("$._id").execute(), 4, testCol1.getName());
        assertPreparedStatementsCountsAndId(testSession, 3, testRemove1, 0, -1);
        assertTestPreparedStatementsResult(testRemove2.sort("$._id").execute(), 1, testCol2.getName(), 1, 2, 3);
        assertPreparedStatementsCountsAndId(testSession, 2, testRemove2, 0, -1);
        assertTestPreparedStatementsResult(testRemove3.sort("$._id").execute(), 2, testCol3.getName(), 3, 4);
        assertPreparedStatementsCountsAndId(testSession, 1, testRemove3, 0, -1);
        assertTestPreparedStatementsResult(testRemove4.sort("$._id").execute(), 1, testCol4.getName(), 1, 3, 4);
        assertPreparedStatementsCountsAndId(testSession, 0, testRemove4, 0, -1);
        assertPreparedStatementsStatusCounts(testSession, 4, 8, 4);
        testPreparedStatementsResetData();
        // F. No Changes: 2nd execute -> prepare + execute.
        assertTestPreparedStatementsResult(testRemove1.execute(), 4, testCol1.getName());
        assertPreparedStatementsCountsAndId(testSession, 1, testRemove1, 1, 1);
        assertTestPreparedStatementsResult(testRemove2.execute(), 1, testCol2.getName(), 1, 2, 3);
        assertPreparedStatementsCountsAndId(testSession, 2, testRemove2, 2, 1);
        assertTestPreparedStatementsResult(testRemove3.execute(), 2, testCol3.getName(), 3, 4);
        assertPreparedStatementsCountsAndId(testSession, 3, testRemove3, 3, 1);
        assertTestPreparedStatementsResult(testRemove4.execute(), 1, testCol4.getName(), 1, 3, 4);
        assertPreparedStatementsCountsAndId(testSession, 4, testRemove4, 4, 1);
        assertPreparedStatementsStatusCounts(testSession, 8, 12, 4);
        testPreparedStatementsResetData();
        // G. Set limit for the first time deallocates and re-prepares: 1st execute -> re-prepare + execute.
        assertTestPreparedStatementsResult(testRemove1.limit(1).execute(), 1, testCol1.getName(), 2, 3, 4);
        assertPreparedStatementsCountsAndId(testSession, 4, testRemove1, 1, 1);
        assertTestPreparedStatementsResult(testRemove2.limit(1).execute(), 1, testCol2.getName(), 1, 2, 3);
        assertPreparedStatementsCountsAndId(testSession, 4, testRemove2, 2, 1);
        assertTestPreparedStatementsResult(testRemove3.limit(1).execute(), 1, testCol3.getName(), 2, 3, 4);
        assertPreparedStatementsCountsAndId(testSession, 4, testRemove3, 3, 1);
        assertTestPreparedStatementsResult(testRemove4.limit(1).execute(), 1, testCol4.getName(), 1, 3, 4);
        assertPreparedStatementsCountsAndId(testSession, 4, testRemove4, 4, 1);
        assertPreparedStatementsStatusCounts(testSession, 12, 16, 8);
        testPreparedStatementsResetData();
        // H. Set limit reuse prepared statement: 2nd execute -> execute.
        assertTestPreparedStatementsResult(testRemove1.limit(2).execute(), 2, testCol1.getName(), 3, 4);
        assertPreparedStatementsCountsAndId(testSession, 4, testRemove1, 1, 2);
        assertTestPreparedStatementsResult(testRemove2.limit(2).execute(), 1, testCol2.getName(), 1, 2, 3);
        assertPreparedStatementsCountsAndId(testSession, 4, testRemove2, 2, 2);
        assertTestPreparedStatementsResult(testRemove3.limit(2).execute(), 2, testCol3.getName(), 3, 4);
        assertPreparedStatementsCountsAndId(testSession, 4, testRemove3, 3, 2);
        assertTestPreparedStatementsResult(testRemove4.limit(2).execute(), 1, testCol4.getName(), 1, 3, 4);
        assertPreparedStatementsCountsAndId(testSession, 4, testRemove4, 4, 2);
        assertPreparedStatementsStatusCounts(testSession, 12, 20, 8);
        testPreparedStatementsResetData();
        // I. Set sort deallocates and resets execution count, set limit has no effect: 1st execute -> deallocate + non-prepared.
        assertTestPreparedStatementsResult(testRemove1.sort("$._id").limit(1).execute(), 1, testCol1.getName(), 2, 3, 4);
        assertPreparedStatementsCountsAndId(testSession, 3, testRemove1, 0, -1);
        assertTestPreparedStatementsResult(testRemove2.sort("$._id").limit(1).execute(), 1, testCol2.getName(), 1, 2, 3);
        assertPreparedStatementsCountsAndId(testSession, 2, testRemove2, 0, -1);
        assertTestPreparedStatementsResult(testRemove3.sort("$._id").limit(1).execute(), 1, testCol3.getName(), 2, 3, 4);
        assertPreparedStatementsCountsAndId(testSession, 1, testRemove3, 0, -1);
        assertTestPreparedStatementsResult(testRemove4.sort("$._id").limit(1).execute(), 1, testCol4.getName(), 1, 3, 4);
        assertPreparedStatementsCountsAndId(testSession, 0, testRemove4, 0, -1);
        assertPreparedStatementsStatusCounts(testSession, 12, 20, 12);
        testPreparedStatementsResetData();
        // J. Set limit reuse statement: 2nd execute -> prepare + execute.
        assertTestPreparedStatementsResult(testRemove1.limit(2).execute(), 2, testCol1.getName(), 3, 4);
        assertPreparedStatementsCountsAndId(testSession, 1, testRemove1, 1, 1);
        assertTestPreparedStatementsResult(testRemove2.limit(2).execute(), 1, testCol2.getName(), 1, 2, 3);
        assertPreparedStatementsCountsAndId(testSession, 2, testRemove2, 2, 1);
        assertTestPreparedStatementsResult(testRemove3.limit(2).execute(), 2, testCol3.getName(), 3, 4);
        assertPreparedStatementsCountsAndId(testSession, 3, testRemove3, 3, 1);
        assertTestPreparedStatementsResult(testRemove4.limit(2).execute(), 1, testCol4.getName(), 1, 3, 4);
        assertPreparedStatementsCountsAndId(testSession, 4, testRemove4, 4, 1);
        assertPreparedStatementsStatusCounts(testSession, 16, 24, 12);
        testPreparedStatementsResetData();
        testSession.close();
        // Prepared statements won't live past the closing of the session.
        assertPreparedStatementsCount(sessionThreadId, 0, 10);
        /*
             * Test falling back onto non-prepared statements.
             */
        testSession = sf.getSession(this.testProperties);
        int origMaxPrepStmtCount = this.session.sql("SELECT @@max_prepared_stmt_count").execute().fetchOne().getInt(0);
        try {
            // Allow preparing only one more statement.
            this.session.sql("SET GLOBAL max_prepared_stmt_count = ?").bind(getPreparedStatementsCount() + 1).execute();
            sessionThreadId = getThreadId(testSession);
            assertPreparedStatementsCount(sessionThreadId, 0, 1);
            assertPreparedStatementsStatusCounts(testSession, 0, 0, 0);
            testCol1 = testSession.getDefaultSchema().getCollection(this.collectionName + "_1");
            testCol2 = testSession.getDefaultSchema().getCollection(this.collectionName + "_2");
            testRemove1 = testCol1.remove("true");
            testRemove2 = testCol2.remove("true");
            // 1st execute -> don't prepare.
            assertTestPreparedStatementsResult(testRemove1.execute(), 4, testCol1.getName());
            assertPreparedStatementsCountsAndId(testSession, 0, testRemove1, 0, -1);
            assertTestPreparedStatementsResult(testRemove2.execute(), 4, testCol2.getName());
            assertPreparedStatementsCountsAndId(testSession, 0, testRemove2, 0, -1);
            assertPreparedStatementsStatusCounts(testSession, 0, 0, 0);
            testPreparedStatementsResetData();
            // 2nd execute -> prepare + execute.
            assertTestPreparedStatementsResult(testRemove1.execute(), 4, testCol1.getName());
            assertPreparedStatementsCountsAndId(testSession, 1, testRemove1, 1, 1);
            // Fails preparing, execute as non-prepared.
            assertTestPreparedStatementsResult(testRemove2.execute(), 4, testCol2.getName());
            assertPreparedStatementsCountsAndId(testSession, 1, testRemove2, 0, -1);
            // Failed prepare also counts.
            assertPreparedStatementsStatusCounts(testSession, 2, 1, 0);
            testPreparedStatementsResetData();
            // 3rd execute -> execute.
            assertTestPreparedStatementsResult(testRemove1.execute(), 4, testCol1.getName());
            assertPreparedStatementsCountsAndId(testSession, 1, testRemove1, 1, 2);
            // Execute as non-prepared.
            assertTestPreparedStatementsResult(testRemove2.execute(), 4, testCol2.getName());
            assertPreparedStatementsCountsAndId(testSession, 1, testRemove2, 0, -1);
            assertPreparedStatementsStatusCounts(testSession, 2, 2, 0);
            testPreparedStatementsResetData();
            testSession.close();
            // Prepared statements won't live past the closing of the session.
            assertPreparedStatementsCount(sessionThreadId, 0, 10);
        } finally {
            this.session.sql("SET GLOBAL max_prepared_stmt_count = ?").bind(origMaxPrepStmtCount).execute();
        }
    } finally {
        for (int i = 0; i < 4; i++) {
            dropCollection(this.collectionName + "_" + (i + 1));
        }
    }
}
Also used : SessionFactory(com.mysql.cj.xdevapi.SessionFactory) Collection(com.mysql.cj.xdevapi.Collection) RemoveStatement(com.mysql.cj.xdevapi.RemoveStatement) Session(com.mysql.cj.xdevapi.Session) Test(org.junit.jupiter.api.Test)

Example 24 with Session

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

the class CollectionTest method testArrayIndex011.

@Test
public void testArrayIndex011() throws Exception {
    assumeTrue(mysqlVersionMeetsMinimum(this.baseUrl, ServerVersion.parseVersion("8.0.17")), "MySQL 8.0.17+ is required to run this test.");
    String collname = "coll1";
    Session sess = null;
    try {
        sess = new SessionFactory().getSession(this.baseUrl);
        Schema sch = sess.getDefaultSchema();
        sch.dropCollection(collname);
        Collection coll = sch.createCollection(collname, true);
        /* create basic index */
        coll.createIndex("intArrayIndex", "{\"fields\": [{\"field\": \"$.intField\", \"type\": \"SIGNED INTEGER\", \"array\": true}]}");
        coll.createIndex("uintArrayIndex", "{\"fields\": [{\"field\": \"$.uintField\", \"type\": \"UNSIGNED INTEGER\", \"array\": true}]}");
        coll.createIndex("floatArrayIndex", "{\"fields\": [{\"field\": \"$.floatField\", \"type\": \"DECIMAL(10,2)\", \"array\": true}]}");
        coll.createIndex("dateArrayIndex", "{\"fields\": [{\"field\": \"$.dateField\", \"type\": \"DATE\", \"array\": true}]}");
        coll.createIndex("datetimeArrayIndex", "{\"fields\": [{\"field\": \"$.datetimeField\", \"type\": \"DATETIME\", \"array\": true}]}");
        coll.createIndex("timeArrayIndex", "{\"fields\": [{\"field\": \"$.timeField\", \"type\": \"TIME\", \"array\": true}]}");
        coll.createIndex("charArrayIndex", "{\"fields\": [{\"field\": \"$.charField\", \"type\": \"CHAR(256)\", \"array\": true}]}");
        coll.createIndex("binaryArrayIndex", "{\"fields\": [{\"field\": \"$.binaryField\", \"type\": \"BINARY(256)\", \"array\": true}]}");
        validateArrayIndex("intArrayIndex", "coll1", 1);
        validateArrayIndex("uintArrayIndex", "coll1", 1);
        validateArrayIndex("floatArrayIndex", "coll1", 1);
        validateArrayIndex("dateArrayIndex", "coll1", 1);
        validateArrayIndex("datetimeArrayIndex", "coll1", 1);
        validateArrayIndex("timeArrayIndex", "coll1", 1);
        validateArrayIndex("charArrayIndex", "coll1", 1);
        validateArrayIndex("binaryArrayIndex", "coll1", 1);
        coll.add("{\"intField\" : [], \"uintField\" : [], \"dateField\" : [], \"datetimeField\" : [], \"charField\" : [], \"binaryField\" : [],\"timeField\" : [], \"floatField\" : []}").execute();
        coll.add("{\"intField\" : [], \"uintField\" : [51,52,53], \"dateField\" : [\"2019-1-1\", \"2019-2-1\", \"2019-3-1\"], \"datetimeField\" : [\"9999-12-30 23:59:59\", \"9999-12-29 23:59:59\", \"9999-12-31 23:59:59\"], \"charField\" : [\"abcd1\", \"abcd1\", \"abcd2\", \"abcd4\"], \"binaryField\" : [\"abcd1\", \"abcd1\", \"abcd2\", \"abcd4\"], \"timeField\" : [\"10.30\", \"11.30\", \"12.30\"], \"floatField\" : [51.1,52.9,53.0]}").execute();
        coll.add("{\"intField\" : [12,23,34], \"uintField\" : [], \"dateField\" : [\"2019-1-1\", \"2019-2-1\", \"2019-3-1\"], \"datetimeField\" : [\"9999-12-31 23:59:59\", \"9999-12-31 23:59:59\", \"9999-12-7 23:59:59\"], \"charField\" : [\"abcd1\", \"abcd1\", \"abcd2\", \"abcd4\"], \"binaryField\" : [\"abcd1\", \"abcd1\", \"abcd2\", \"abcd4\"], \"timeField\" : [\"10.30\", \"11.30\", \"12.30\"], \"floatField\" : [51.2,52.7,53.6]}").execute();
        coll.add("{\"intField\" : [12,23,34], \"uintField\" : [51,52,53], \"dateField\" : [], \"datetimeField\" : [\"9999-12-31 23:59:59\", \"9999-12-31 23:59:59\", \"9999-12-7 23:59:59\"], \"charField\" : [\"abcd1\", \"abcd1\", \"abcd2\", \"abcd4\"], \"binaryField\" : [\"abcd1\", \"abcd1\", \"abcd2\", \"abcd4\"], \"timeField\" : [\"10.30\", \"11.30\", \"12.30\"], \"floatField\" : [51.2,52.7,53.6]}").execute();
        coll.add("{\"intField\" : [12,23,34], \"uintField\" : [51,52,53], \"dateField\" : [\"2019-1-1\", \"2019-2-1\", \"2019-3-1\"], \"datetimeField\" : [], \"charField\" : [\"abcd1\", \"abcd1\", \"abcd2\", \"abcd4\"], \"binaryField\" : [\"abcd1\", \"abcd1\", \"abcd2\", \"abcd4\"], \"timeField\" : [\"10.30\", \"11.30\", \"12.30\"], \"floatField\" : [51.2,52.7,53.6]}").execute();
        coll.add("{\"intField\" : [12,23,34], \"uintField\" : [51,52,53], \"dateField\" : [\"2019-1-1\", \"2019-2-1\", \"2019-3-1\"], \"datetimeField\" : [\"9999-12-31 23:59:59\", \"9999-12-31 23:59:59\", \"9999-12-7 23:59:59\"], \"charField\" : [], \"binaryField\" : [\"abcd1\", \"abcd1\", \"abcd2\", \"abcd4\"], \"timeField\" : [\"10.30\", \"11.30\", \"12.30\"], \"floatField\" : [51.2,52.7,53.6]}").execute();
        coll.add("{\"intField\" : [12,23,34], \"uintField\" : [51,52,53], \"dateField\" : [\"2019-1-1\", \"2019-2-1\", \"2019-3-1\"], \"datetimeField\" : [\"9999-12-31 23:59:59\", \"9999-12-31 23:59:59\", \"9999-12-7 23:59:59\"], \"charField\" : [\"abcd1\", \"abcd1\", \"abcd2\", \"abcd4\"], \"binaryField\" : [], \"timeField\" : [\"10.30\", \"11.30\", \"12.30\"], \"floatField\" : [51.2,52.7,53.6]}").execute();
        coll.add("{\"intField\" : [12,23,34], \"uintField\" : [51,52,53], \"dateField\" : [\"2019-1-1\", \"2019-2-1\", \"2019-3-1\"], \"datetimeField\" : [\"9999-12-31 23:59:59\", \"9999-12-31 23:59:59\", \"9999-12-7 23:59:59\"], \"charField\" : [\"abcd1\", \"abcd1\", \"abcd2\", \"abcd4\"], \"binaryField\" : [\"abcd1\", \"abcd1\", \"abcd2\", \"abcd4\"], \"timeField\" : [], \"floatField\" : [51.2,52.7,53.6]}").execute();
        coll.add("{\"intField\" : [12,23,34], \"uintField\" : [51,52,53], \"dateField\" : [\"2019-1-1\", \"2019-2-1\", \"2019-3-1\"], \"datetimeField\" : [\"9999-12-31 23:59:59\", \"9999-12-31 23:59:59\", \"9999-12-7 23:59:59\"], \"charField\" : [\"abcd1\", \"abcd1\", \"abcd2\", \"abcd4\"], \"binaryField\" : [\"abcd1\", \"abcd1\", \"abcd2\", \"abcd4\"], \"timeField\" : [\"10.30\", \"11.30\", \"12.30\"], \"floatField\" : []}").execute();
        try {
            coll.add("{\"intField\" : [1,[2, 3],4], \"uintField\" : [51,[52, 53],54], \"dateField\" : [\"2019-1-1\", [\"2019-2-1\", \"2019-3-1\"], \"2019-4-1\"], \"datetimeField\" : [\"9999-12-30 23:59:59\", [\"9999-12-31 23:59:59\"], \"9999-12-31 23:59:59\"], \"charField\" : [\"abcd1\", \"abcd1\", \"abcd2\", \"abcd4\"], \"binaryField\" : [\"abcd1\", [\"abcd1\", \"abcd2\"], \"abcd4\"],\"timeField\" : [\"10.30\", \"11.30\", \"12.30\"], \"floatField\" : [51.2,[52.4],53.6]}").execute();
            assertTrue(false);
        } catch (Exception e) {
            System.out.println("ERROR : " + e.getMessage());
            assertTrue(e.getMessage().contains("Cannot store an array or an object in a scalar key part of the index"));
        }
        try {
            coll.add("{\"intField\" : \"\", \"uintField\" : [51,[52, 53],54], \"dateField\" : [\"2019-1-1\", [\"2019-2-1\", \"2019-3-1\"], \"2019-4-1\"], \"datetimeField\" : [\"9999-12-30 23:59:59\", \"9999-12-31 23:59:59\", \"9999-12-31 23:59:59\"], \"charField\" : [\"abcd1\", \"abcd1\", \"abcd2\", \"abcd4\"], \"binaryField\" : [\"abcd1\", \"abcd1\", \"abcd2\", \"abcd4\"],\"timeField\" : [\"10.30\", \"11.30\", \"12.30\"], \"floatField\" : [51.2,52.4,53.6]}").execute();
            assertTrue(false);
        } catch (Exception e) {
            System.out.println("ERROR : " + e.getMessage());
            if (mysqlVersionMeetsMinimum(this.baseUrl, ServerVersion.parseVersion("8.0.18"))) {
                assertTrue(e.getMessage().contains("Cannot store an array or an object in a scalar key part of the index"));
            } else {
                assertTrue(e.getMessage().contains("functional index"));
            }
        }
        try {
            coll.add("{\"intField\" : [1,5,4], \"dateField\" : \"\", \"datetimeField\" : \"\", \"charField\" : [\"abcd1\", \"abcd1\", \"abcd2\", \"abcd4\"], \"binaryField\" : [\"abcd1\", \"abcd1\", \"abcd2\", \"abcd4\"],\"timeField\" : \"\", \"floatField\" : [51.2,52.4,53.6]}").execute();
            assertTrue(false);
        } catch (Exception e) {
            System.out.println("ERROR : " + e.getMessage());
            assertTrue(e.getMessage().contains("functional index"));
        }
        try {
            coll.add("{\"intField\" : [1,5,4], \"dateField\" : [\"2019-1-1\", \"2019-2-1\", \"2019-3-1\", \"2019-4-1\"], \"datetimeField\" : [\"9999-12-30 23:59:59\", \"9999-12-31 23:59:59\", \"9999-12-31 23:59:59\"], \"charField\" : \"\", \"binaryField\" : \"\", \"timeField\" : [\"10.30\", \"11.30\", \"12.30\"], \"floatField\" : [51.2,52.4,53.6]}").execute();
        } catch (Exception e) {
            System.out.println("ERROR : " + e.getMessage());
            assertTrue(e.getMessage().contains("functional index"));
        }
        sch.dropCollection(collname);
    } finally {
        if (sess != null) {
            sess.close();
            sess = null;
        }
    }
}
Also used : SessionFactory(com.mysql.cj.xdevapi.SessionFactory) Schema(com.mysql.cj.xdevapi.Schema) Collection(com.mysql.cj.xdevapi.Collection) JsonString(com.mysql.cj.xdevapi.JsonString) WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException) ExecutionException(java.util.concurrent.ExecutionException) Session(com.mysql.cj.xdevapi.Session) Test(org.junit.jupiter.api.Test)

Example 25 with Session

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

the class CollectionTest method testArrayIndex010.

@Test
public void testArrayIndex010() throws Exception {
    assumeTrue(mysqlVersionMeetsMinimum(this.baseUrl, ServerVersion.parseVersion("8.0.17")), "MySQL 8.0.17+ is required to run this test.");
    String collname = "coll1";
    Session sess = null;
    try {
        sess = new SessionFactory().getSession(this.baseUrl);
        Schema sch = sess.getDefaultSchema();
        sch.dropCollection(collname);
        Collection coll = sch.createCollection(collname, true);
        try {
            coll.createIndex("multiArrayIndex1", "{\"fields\": [{\"field\": \"$.charField\", \"type\":\"CHAR(128)\", \"array\": true}," + "{\"field\": \"$.binaryField\", \"type\":\"BINARY(128)\"}," + "{\"field\": \"$.intField\", \"type\":\"SIGNED INTEGER\"}," + "{\"field\": \"$.intField2\", \"type\":\"SIGNED\"}," + "{\"field\": \"$.uintField\", \"type\":\"UNSIGNED\"}," + "{\"field\": \"$.uintField2\", \"type\":\"UNSIGNED INTEGER\"}," + "{\"field\": \"$.dateField\", \"type\":\"DATE\"}," + "{\"field\": \"$.datetimeField\", \"type\":\"DATETIME\"}," + "{\"field\": \"$.decimalField\", \"type\":\"DECIMAL(20,9)\"}" + "]}");
        } catch (Exception e) {
            System.out.println("ERROR : " + e.getMessage());
            assertTrue(e.getMessage().contains("Invalid or unsupported type specification 'BINARY(128)'"));
        }
        coll.add("{\"intField\" : 1, \"dateField\" : \"2019-3-1\", \"charField\" : \"abcd1\", \"timeField\" : [\"10.30\", \"11.30\", \"12.30\"]}").execute();
        coll.add("{\"intField\" : 2, \"dateField\" : \"2019-5-1\", \"charField\" : \"abcd2\", \"timeField\" : [\"10.30\", \"11.30\", \"12.30\"]}").execute();
        coll.add("{\"intField\" : 3, \"dateField\" : \"2019-7-1\", \"charField\" : \"abcd3\", \"timeField\" : [\"10.30\", \"11.30\", \"12.30\"]}").execute();
        sch.dropCollection(collname);
    } finally {
        if (sess != null) {
            sess.close();
            sess = null;
        }
    }
}
Also used : SessionFactory(com.mysql.cj.xdevapi.SessionFactory) Schema(com.mysql.cj.xdevapi.Schema) Collection(com.mysql.cj.xdevapi.Collection) JsonString(com.mysql.cj.xdevapi.JsonString) WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException) ExecutionException(java.util.concurrent.ExecutionException) Session(com.mysql.cj.xdevapi.Session) Test(org.junit.jupiter.api.Test)

Aggregations

Session (com.mysql.cj.xdevapi.Session)85 Test (org.junit.jupiter.api.Test)79 JsonString (com.mysql.cj.xdevapi.JsonString)40 SessionFactory (com.mysql.cj.xdevapi.SessionFactory)39 CoreSession (com.mysql.cj.CoreSession)38 Collection (com.mysql.cj.xdevapi.Collection)33 Properties (java.util.Properties)29 ExecutionException (java.util.concurrent.ExecutionException)26 WrongArgumentException (com.mysql.cj.exceptions.WrongArgumentException)23 Schema (com.mysql.cj.xdevapi.Schema)22 DocResult (com.mysql.cj.xdevapi.DocResult)16 DbDoc (com.mysql.cj.xdevapi.DbDoc)15 SessionImpl (com.mysql.cj.xdevapi.SessionImpl)15 SqlResult (com.mysql.cj.xdevapi.SqlResult)15 Row (com.mysql.cj.xdevapi.Row)14 Client (com.mysql.cj.xdevapi.Client)11 ClientFactory (com.mysql.cj.xdevapi.ClientFactory)11 RowResult (com.mysql.cj.xdevapi.RowResult)10 Field (java.lang.reflect.Field)9 CJCommunicationsException (com.mysql.cj.exceptions.CJCommunicationsException)8