Search in sources :

Example 16 with DbDocImpl

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

the class CollectionModifyTest method testReplaceOne.

@Test
public void testReplaceOne() {
    assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.3")), "MySQL 8.0.3+ is required to run this test.");
    Result res = this.collection.replaceOne("someId", "{\"_id\":\"someId\",\"a\":3}");
    assertEquals(0, res.getAffectedItemsCount());
    this.collection.add("{\"_id\":\"existingId\",\"a\":1}").execute();
    res = this.collection.replaceOne("existingId", new DbDocImpl().add("a", new JsonNumber().setValue("2")));
    assertEquals(1, res.getAffectedItemsCount());
    DbDoc doc = this.collection.getOne("existingId");
    assertNotNull(doc);
    assertEquals(2, ((JsonNumber) doc.get("a")).getInteger());
    // Original behavior changed by Bug#32770013.
    assertThrows(XDevAPIError.class, "Replacement document has an _id that is different than the matched document\\.", new Callable<Void>() {

        public Void call() throws Exception {
            CollectionModifyTest.this.collection.replaceOne("nonExistingId", "{\"_id\":\"existingId\",\"a\":3}");
            return null;
        }
    });
    // Original behavior changed by Bug#32770013.
    assertThrows(XDevAPIError.class, "Replacement document has an _id that is different than the matched document\\.", new Callable<Void>() {

        public Void call() throws Exception {
            CollectionModifyTest.this.collection.replaceOne("", "{\"_id\":\"existingId\",\"a\":3}");
            return null;
        }
    });
    /*
         * FR5.2 The id of the document must remain immutable:
         * 
         * Use a collection with some documents
         * Fetch a document
         * Unset _id and modify any other field of the document
         * Call replaceOne() giving original ID and modified document: expect affected = 1
         * Fetch the document again, ensure other document modifications took place
         * Ensure the number of documents in the collection is unaltered
         */
    this.collection.remove("true").execute();
    assertEquals(0, this.collection.count());
    this.collection.add("{\"_id\":\"id1\",\"a\":1}").execute();
    doc = this.collection.getOne("id1");
    assertNotNull(doc);
    doc.remove("_id");
    ((JsonNumber) doc.get("a")).setValue("3");
    res = this.collection.replaceOne("id1", doc);
    assertEquals(1, res.getAffectedItemsCount());
    doc = this.collection.getOne("id1");
    assertNotNull(doc);
    assertEquals("id1", ((JsonString) doc.get("_id")).getString());
    assertEquals(new Integer(3), ((JsonNumber) doc.get("a")).getInteger());
    assertEquals(1, this.collection.count());
    // null document
    assertThrows(XDevAPIError.class, "Parameter 'doc' must not be null.", new Callable<Void>() {

        public Void call() throws Exception {
            CollectionModifyTest.this.collection.replaceOne("id1", (DbDoc) null);
            return null;
        }
    });
    assertThrows(XDevAPIError.class, "Parameter 'jsonString' must not be null.", new Callable<Void>() {

        public Void call() throws Exception {
            CollectionModifyTest.this.collection.replaceOne("id2", (String) null);
            return null;
        }
    });
    // null id parameter
    assertThrows(XDevAPIError.class, "Parameter 'id' must not be null.", new Callable<Void>() {

        public Void call() throws Exception {
            CollectionModifyTest.this.collection.replaceOne(null, new DbDocImpl().add("a", new JsonNumber().setValue("2")));
            return null;
        }
    });
    assertThrows(XDevAPIError.class, "Parameter 'id' must not be null.", new Callable<Void>() {

        public Void call() throws Exception {
            CollectionModifyTest.this.collection.replaceOne(null, "{\"_id\": \"id100\", \"a\": 100}");
            return null;
        }
    });
    assertNull(this.collection.getOne(null));
}
Also used : DbDoc(com.mysql.cj.xdevapi.DbDoc) DbDocImpl(com.mysql.cj.xdevapi.DbDocImpl) JsonNumber(com.mysql.cj.xdevapi.JsonNumber) JsonString(com.mysql.cj.xdevapi.JsonString) ExecutionException(java.util.concurrent.ExecutionException) Result(com.mysql.cj.xdevapi.Result) AddResult(com.mysql.cj.xdevapi.AddResult) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Example 17 with DbDocImpl

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

the class CollectionModifyTest method testCollectionModifyExpr.

/*
     * Update Using Expressions
     */
@Test
public void testCollectionModifyExpr() throws Exception {
    int i = 0, maxrec = 10;
    DbDoc doc = null;
    Result res = null;
    DocResult docs = null;
    DbDoc[] jsonlist = new DbDocImpl[maxrec];
    long l1 = Long.MAX_VALUE, l2 = Long.MIN_VALUE, l3 = 2147483647;
    double d1 = 100.4567;
    for (i = 0; i < maxrec; i++) {
        DbDoc newDoc2 = new DbDocImpl();
        newDoc2.add("_id", new JsonString().setValue(String.valueOf(i + 1000)));
        newDoc2.add("F1", new JsonString().setValue("Field-1-Data-" + i));
        newDoc2.add("F2", new JsonNumber().setValue(String.valueOf(d1 + i)));
        newDoc2.add("F3", new JsonNumber().setValue(String.valueOf(l1 - i)));
        newDoc2.add("F4", new JsonNumber().setValue(String.valueOf(l2 + i)));
        newDoc2.add("F5", new JsonNumber().setValue(String.valueOf(l3 + i)));
        newDoc2.add("F6", new JsonString().setValue((2000 + i) + "-02-" + (i * 2 + 10)));
        jsonlist[i] = newDoc2;
        newDoc2 = null;
    }
    this.collection.add(jsonlist).execute();
    assertEquals((maxrec), this.collection.count());
    /* condition on Double */
    res = this.collection.modify("CAST($.F2 as DECIMAL(10,4)) =" + d1).set("$.F1", expr("concat('data',$.F1,'UpdData1')")).sort("CAST($.F2 as DECIMAL(10,4))").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("$.F1 like 'data%UpdData1'").fields("$._id as _id, $.F2 as f2").execute();
    doc = docs.next();
    assertEquals(new BigDecimal(String.valueOf(d1)), (((JsonNumber) doc.get("f2")).getBigDecimal()));
    assertEquals(String.valueOf(1000), (((JsonString) doc.get("_id")).getString()));
    res = this.collection.modify("CAST($.F2 as DECIMAL(10,4)) =" + d1).set("$.F6", expr("$.F6 + interval 6 day")).sort("CAST($.F2 as DECIMAL(10,4))").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("$.F6 + interval 6 day = '2000-02-22'").fields("$._id as _id, $.F6 as f6").execute();
    doc = docs.next();
    assertEquals("2000-02-16", (((JsonString) doc.get("f6")).getString()));
    assertEquals(String.valueOf(1000), (((JsonString) doc.get("_id")).getString()));
    res = this.collection.modify("$.F6= '2004-02-18'").set("$.F6", expr("$.F6 + interval 11 day")).set("$.F1", "NewData").sort("CAST($.F2 as DECIMAL(10,4))").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("$.F1 = 'NewData'").fields("$._id as _id, $.F6 as f6").execute();
    doc = docs.next();
    assertEquals("2004-02-29", (((JsonString) doc.get("f6")).getString()));
    assertEquals(String.valueOf(1004), (((JsonString) doc.get("_id")).getString()));
    /* condition on Big Int */
    res = this.collection.modify("CAST($.F3 as SIGNED) =" + l1).set("$.F3", expr("CAST($.F3 as SIGNED)  -1")).sort("CAST($.F3 as SIGNED)").execute();
    assertEquals(1, res.getAffectedItemsCount());
    res = this.collection.modify("CAST($.F3 as SIGNED) + 1  =" + l1).set("$.F3", expr("CAST($.F3 as SIGNED) + 1")).sort("CAST($.F3 as SIGNED)").execute();
    assertEquals(2, res.getAffectedItemsCount());
    docs = this.collection.find("CAST($.F3 as SIGNED)=" + l1).fields("$._id as _id, $.F3 as f3").orderBy("$._id asc").execute();
    doc = docs.next();
    assertEquals(new BigDecimal(String.valueOf(l1)), (((JsonNumber) doc.get("f3")).getBigDecimal()));
    assertEquals(String.valueOf(1000), (((JsonString) doc.get("_id")).getString()));
    doc = docs.next();
    assertEquals(new BigDecimal(String.valueOf(l1)), (((JsonNumber) doc.get("f3")).getBigDecimal()));
    assertEquals(String.valueOf(1001), (((JsonString) doc.get("_id")).getString()));
    assertFalse(docs.hasNext());
    /* condition on Big Int.Compex Expression */
    res = this.collection.modify("CAST($.F4 as SIGNED) < 0").set("$.F1", "Abcd").set("$.F4", expr("((CAST($.F4 as SIGNED) + CAST($.F3 as SIGNED)) * 1)/1.1 + 1 ")).execute();
    assertEquals(maxrec, res.getAffectedItemsCount());
    res = this.collection.modify("true").set("$.F1", expr("concat('data',$.F1,'UpdData1')")).sort("CAST($.F2 as DECIMAL(10,4))").execute();
    assertEquals(10, res.getAffectedItemsCount());
    res = this.collection.modify("false").set("$.F1", "Abcd").set("$.F4", expr("((CAST($.F4 as SIGNED) + CAST($.F3 as SIGNED)) * 1)/1.1 + 1 ")).execute();
    assertEquals(0, res.getAffectedItemsCount());
}
Also used : DbDoc(com.mysql.cj.xdevapi.DbDoc) DbDocImpl(com.mysql.cj.xdevapi.DbDocImpl) JsonNumber(com.mysql.cj.xdevapi.JsonNumber) JsonString(com.mysql.cj.xdevapi.JsonString) DocResult(com.mysql.cj.xdevapi.DocResult) BigDecimal(java.math.BigDecimal) Result(com.mysql.cj.xdevapi.Result) AddResult(com.mysql.cj.xdevapi.AddResult) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Example 18 with DbDocImpl

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

the class CollectionModifyTest method testCollectionModifyAsyncMany.

@SuppressWarnings("unchecked")
@Test
public void testCollectionModifyAsyncMany() throws Exception {
    int i = 0, maxrec = 10;
    int NUMBER_OF_QUERIES = 1000;
    DbDoc doc = null;
    AddResult res = null;
    Result res2 = null;
    CompletableFuture<AddResult> asyncRes = null;
    CompletableFuture<DocResult> asyncDocs = null;
    DocResult docs = null;
    double d = 100.123;
    /* add().executeAsync() maxrec num of records */
    for (i = 0; i < maxrec; i++) {
        DbDoc newDoc2 = new DbDocImpl();
        newDoc2.add("_id", new JsonString().setValue(String.valueOf(i + 1000)));
        newDoc2.add("F1", new JsonString().setValue("Field-1-Data-" + i));
        newDoc2.add("F2", new JsonNumber().setValue(String.valueOf(d + i)));
        newDoc2.add("F3", new JsonNumber().setValue(String.valueOf(1000 + i)));
        newDoc2.add("T", new JsonNumber().setValue(String.valueOf(i)));
        asyncRes = this.collection.add(newDoc2).executeAsync();
        res = asyncRes.get();
        assertEquals(1, res.getAffectedItemsCount());
        newDoc2 = null;
    }
    assertEquals((maxrec), this.collection.count());
    List<Object> futures = new ArrayList<>();
    for (i = 0; i < NUMBER_OF_QUERIES; ++i) {
        if (i % 3 == 0) {
            futures.add(this.collection.modify("$.F3 % 2 = 0 ").change("$.T", expr("1000000+" + i)).sort("$.F3 desc").executeAsync());
        } else if (i % 3 == 1) {
            // Error
            futures.add(this.collection.modify("$.F3 = " + (1000 + i)).change("$.T", expr("NON_EXISTING_FUNCTION()")).sort("$.F3 desc").executeAsync());
        } else {
            futures.add(this.collection.modify("$.F3 % 2 = 1 ").change("$.T", expr("$.F3+" + i)).sort("$.F3 desc").executeAsync());
        }
    }
    for (i = 0; i < NUMBER_OF_QUERIES; ++i) {
        if (i % 3 == 0) {
            res2 = ((CompletableFuture<AddResult>) futures.get(i)).get();
            assertEquals((maxrec) / 2, res2.getAffectedItemsCount());
        } else if (i % 3 == 1) {
            int i1 = i;
            assertThrows(ExecutionException.class, ".*FUNCTION " + this.schema.getName() + ".NON_EXISTING_FUNCTION does not exist.*", () -> ((CompletableFuture<Result>) futures.get(i1)).get());
        } else {
            res2 = ((CompletableFuture<Result>) futures.get(i)).get();
            assertEquals((maxrec) / 2, res2.getAffectedItemsCount());
        }
    }
    asyncDocs = this.collection.find("$.T > :X ").bind("X", 1000000).fields(expr("{'cnt':count($.T)}")).executeAsync();
    docs = asyncDocs.get();
    doc = docs.next();
    assertEquals((long) (maxrec) / 2, (long) (((JsonNumber) doc.get("cnt")).getInteger()));
    asyncDocs = this.collection.find("$.T > :X and $.T < :Y").bind("X", 1000).bind("Y", 1000000).fields(expr("{'cnt':count($.T)}")).executeAsync();
    docs = asyncDocs.get();
    doc = docs.next();
    assertEquals((long) (maxrec) / 2, (long) (((JsonNumber) doc.get("cnt")).getInteger()));
}
Also used : ArrayList(java.util.ArrayList) AddResult(com.mysql.cj.xdevapi.AddResult) Result(com.mysql.cj.xdevapi.Result) AddResult(com.mysql.cj.xdevapi.AddResult) DocResult(com.mysql.cj.xdevapi.DocResult) DbDoc(com.mysql.cj.xdevapi.DbDoc) DbDocImpl(com.mysql.cj.xdevapi.DbDocImpl) CompletableFuture(java.util.concurrent.CompletableFuture) JsonNumber(com.mysql.cj.xdevapi.JsonNumber) JsonString(com.mysql.cj.xdevapi.JsonString) ExecutionException(java.util.concurrent.ExecutionException) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Example 19 with DbDocImpl

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

the class CollectionModifyTest method testCollectionModifyArrayInsert.

/* ArrayInsert() for int , double and string */
@Test
public void testCollectionModifyArrayInsert() throws Exception {
    int i = 0, j = 0, maxrec = 8, arraySize = 30;
    int lStr = 10;
    JsonArray yArray = null;
    DbDoc doc = null;
    DocResult docs = null;
    Result res = null;
    String s1 = buildString((lStr), '.');
    long l3 = 2147483647;
    double d1 = 1000.1234;
    for (i = 0; i < maxrec; i++) {
        DbDoc newDoc2 = new DbDocImpl();
        newDoc2.add("_id", new JsonString().setValue(String.valueOf(i + 1000)));
        newDoc2.add("F1", new JsonNumber().setValue(String.valueOf(i + 1)));
        JsonArray jarray = new JsonArray();
        for (j = 0; j < (arraySize); j++) {
            jarray.addValue(new JsonNumber().setValue(String.valueOf((l3 + j + i))));
        }
        newDoc2.add("ARR1", jarray);
        JsonArray karray = new JsonArray();
        for (j = 0; j < (arraySize); j++) {
            karray.addValue(new JsonNumber().setValue(String.valueOf((d1 + j + i))));
        }
        newDoc2.add("ARR2", karray);
        JsonArray larray = new JsonArray();
        for (j = 0; j < (arraySize); j++) {
            larray.addValue(new JsonString().setValue("St_" + i + "_" + j));
        }
        newDoc2.add("ARR3", larray);
        this.collection.add(newDoc2).execute();
        newDoc2 = null;
        jarray = null;
    }
    assertEquals((maxrec), this.collection.count());
    // Insert to a aposistion > arraySize Shld Work same as Append
    // Insert 1 number in the array (ARR1) after position arraySize where $.F1 = 1
    res = this.collection.modify("CAST($.F1 as SIGNED) = 1").arrayInsert("$.ARR1[" + (arraySize * 2) + "]", -1).sort("$._id").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("CAST($.ARR1[" + (arraySize) + "] as SIGNED) = -1").orderBy("$._id").execute();
    doc = docs.next();
    yArray = (JsonArray) doc.get("ARR1");
    assertEquals(arraySize + 1, yArray.size());
    assertEquals((long) (1), (long) (((JsonNumber) doc.get("F1")).getInteger()));
    assertFalse(docs.hasNext());
    // Insert 3 numbers in the array (ARR1) where $.F1 = 2
    res = this.collection.modify("CAST($.F1 as SIGNED) = 2").arrayInsert("$.ARR1[0]", -2).arrayInsert("$.ARR1[1]", -3).arrayInsert("$.ARR1[2]", -4).sort("$._id").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("CAST($.ARR1[0] as SIGNED) = -2").orderBy("$._id").execute();
    doc = docs.next();
    yArray = (JsonArray) doc.get("ARR1");
    assertEquals((long) (arraySize + 3), (long) yArray.size());
    assertEquals((long) (2), (long) (((JsonNumber) doc.get("F1")).getInteger()));
    assertEquals((long) (-2), (long) (((JsonNumber) yArray.get(0)).getInteger()));
    assertEquals((long) (-3), (long) (((JsonNumber) yArray.get(1)).getInteger()));
    assertEquals((long) (-4), (long) (((JsonNumber) yArray.get(2)).getInteger()));
    assertFalse(docs.hasNext());
    // Insert 3 numbers in the array (ARR1) where $.F1 = 3
    res = this.collection.modify("CAST($.F1 as SIGNED) = 3").arrayInsert("$.ARR1[2]", -4).arrayInsert("$.ARR1[1]", -3).arrayInsert("$.ARR1[0]", -2).sort("$._id").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("CAST($.ARR1[2] as SIGNED) = -3").orderBy("$._id").execute();
    doc = docs.next();
    yArray = (JsonArray) doc.get("ARR1");
    assertEquals(arraySize + 3, yArray.size());
    assertEquals((3), (long) (((JsonNumber) doc.get("F1")).getInteger()));
    assertEquals((long) (-2), (long) (((JsonNumber) yArray.get(0)).getInteger()));
    assertEquals((long) (-3), (long) (((JsonNumber) yArray.get(2)).getInteger()));
    assertEquals((long) (-4), (long) (((JsonNumber) yArray.get(4)).getInteger()));
    assertFalse(docs.hasNext());
    // Insert 1 number in the array (ARR2) where $.F1 = 1
    res = this.collection.modify("CAST($.F1 as SIGNED) = 1").arrayInsert("$.ARR2[1]", -4321.4321).sort("$._id").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("CAST($.ARR2[1] as DECIMAL(10,4)) = -4321.4321").orderBy("$._id").execute();
    doc = docs.next();
    yArray = (JsonArray) doc.get("ARR2");
    assertEquals((long) (arraySize + 1), (long) yArray.size());
    assertEquals((long) (1), (long) (((JsonNumber) doc.get("F1")).getInteger()));
    assertFalse(docs.hasNext());
    // Insert 3 number in the array (ARR2) where $.F1 = 2
    res = this.collection.modify("CAST($.F1 as SIGNED) = 2").arrayInsert("$.ARR2[2]", 4321.1234).arrayInsert("$.ARR2[0]", 4321.9847).arrayInsert("$.ARR2[1]", -4321.9888).sort("$._id").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("CAST($.ARR2[0] as  DECIMAL(10,4)) =  4321.9847").orderBy("$._id").execute();
    doc = docs.next();
    yArray = (JsonArray) doc.get("ARR2");
    assertEquals(arraySize + 3, yArray.size());
    assertEquals((long) (2), (long) (((JsonNumber) doc.get("F1")).getInteger()));
    assertEquals(new BigDecimal(String.valueOf("4321.1234")), (((JsonNumber) yArray.get(4)).getBigDecimal()));
    assertEquals(new BigDecimal(String.valueOf("4321.9847")), (((JsonNumber) yArray.get(0)).getBigDecimal()));
    assertEquals(new BigDecimal(String.valueOf("-4321.9888")), (((JsonNumber) yArray.get(1)).getBigDecimal()));
    assertFalse(docs.hasNext());
    // Insert 1 String in the array (ARR3) where $.F1 = 1
    res = this.collection.modify("CAST($.F1 as SIGNED) = 1").arrayInsert("$.ARR3[1]", s1).sort("$._id").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("$.ARR3[1]  = '" + s1 + "'").orderBy("$._id").execute();
    doc = docs.next();
    yArray = (JsonArray) doc.get("ARR3");
    assertEquals(arraySize + 1, yArray.size());
    assertEquals((long) (1), (long) (((JsonNumber) doc.get("F1")).getInteger()));
    assertFalse(docs.hasNext());
    // Insert 3 Strings in the array (ARR3) where $.F1 = 2
    res = this.collection.modify("CAST($.F1 as SIGNED) = 2").arrayInsert("$.ARR3[1]", s1).arrayInsert("$.ARR3[2]", s1).arrayInsert("$.ARR3[0]", "").sort("$._id").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("$.ARR3[0]  = ''").orderBy("$._id").execute();
    doc = docs.next();
    yArray = (JsonArray) doc.get("ARR3");
    assertEquals(arraySize + 3, yArray.size());
    assertEquals((long) (2), (long) (((JsonNumber) doc.get("F1")).getInteger()));
    assertFalse(docs.hasNext());
    // Insert 3 Strings in the array (ARR3) using an empty condition
    assertThrows(XDevAPIError.class, "Parameter 'criteria' must not be null or empty.", new Callable<Void>() {

        public Void call() throws Exception {
            CollectionModifyTest.this.collection.modify(" ").arrayInsert("$.ARR3[1]", s1).arrayInsert("$.ARR3[2]", s1).arrayInsert("$.ARR3[0]", "").sort("$._id").execute();
            return null;
        }
    });
    // Insert 3 Strings in the array (ARR3) using null condition
    assertThrows(XDevAPIError.class, "Parameter 'criteria' must not be null or empty.", new Callable<Void>() {

        public Void call() throws Exception {
            CollectionModifyTest.this.collection.modify(null).arrayInsert("$.ARR3[1]", s1).arrayInsert("$.ARR3[2]", s1).arrayInsert("$.ARR3[0]", "").sort("$._id").execute();
            return null;
        }
    });
}
Also used : JsonString(com.mysql.cj.xdevapi.JsonString) BigDecimal(java.math.BigDecimal) ExecutionException(java.util.concurrent.ExecutionException) Result(com.mysql.cj.xdevapi.Result) AddResult(com.mysql.cj.xdevapi.AddResult) DocResult(com.mysql.cj.xdevapi.DocResult) JsonArray(com.mysql.cj.xdevapi.JsonArray) DbDoc(com.mysql.cj.xdevapi.DbDoc) DbDocImpl(com.mysql.cj.xdevapi.DbDocImpl) JsonNumber(com.mysql.cj.xdevapi.JsonNumber) JsonString(com.mysql.cj.xdevapi.JsonString) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Example 20 with DbDocImpl

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

the class CollectionModifyTest method testCollectionModifyArrayAppend.

/* ArrayAppend() for int double and string */
@Test
public void testCollectionModifyArrayAppend() throws Exception {
    int i = 0, j = 0, maxrec = 8, arraySize = 30;
    int lStr = 10;
    JsonArray yArray = null;
    DbDoc doc = null;
    DocResult docs = null;
    Result res = null;
    String s1 = buildString((lStr), '.');
    long l3 = 2147483647;
    double d1 = 1000.1234;
    for (i = 0; i < maxrec; i++) {
        DbDoc newDoc2 = new DbDocImpl();
        newDoc2.add("_id", new JsonString().setValue(String.valueOf(i + 1000)));
        newDoc2.add("F1", new JsonNumber().setValue(String.valueOf(i + 1)));
        JsonArray jarray = new JsonArray();
        for (j = 0; j < (arraySize); j++) {
            jarray.addValue(new JsonNumber().setValue(String.valueOf((l3 + j + i))));
        }
        newDoc2.add("ARR1", jarray);
        JsonArray karray = new JsonArray();
        for (j = 0; j < (arraySize); j++) {
            karray.addValue(new JsonNumber().setValue(String.valueOf((d1 + j + i))));
        }
        newDoc2.add("ARR2", karray);
        JsonArray larray = new JsonArray();
        for (j = 0; j < (arraySize); j++) {
            larray.addValue(new JsonString().setValue("St_" + i + "_" + j));
        }
        newDoc2.add("ARR3", larray);
        this.collection.add(newDoc2).execute();
        newDoc2 = null;
        jarray = null;
    }
    assertEquals((maxrec), this.collection.count());
    // Append 1 number in the array (ARR1) where $.F1 = 1
    res = this.collection.modify("$.F1 = 1").arrayAppend("$.ARR1", -1).sort("$._id").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("CAST($.ARR1[" + (arraySize) + "] as SIGNED) = -1").orderBy("$._id").execute();
    doc = docs.next();
    yArray = (JsonArray) doc.get("ARR1");
    assertEquals(arraySize + 1, yArray.size());
    assertEquals((long) (1), (long) (((JsonNumber) doc.get("F1")).getInteger()));
    assertFalse(docs.hasNext());
    // Append 3 numbers in the array (ARR1) where $.F1 = 1
    res = this.collection.modify("CAST($.F1 as SIGNED) = 1").arrayAppend("$.ARR1", -2).arrayAppend("$.ARR1", -3).arrayAppend("$.ARR1", -4).sort("$._id").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("CAST($.ARR1[" + (arraySize) + "] as SIGNED) = -1").orderBy("$._id").execute();
    doc = docs.next();
    yArray = (JsonArray) doc.get("ARR1");
    assertEquals(arraySize + 4, yArray.size());
    assertEquals((long) (1), (long) (((JsonNumber) doc.get("F1")).getInteger()));
    assertFalse(docs.hasNext());
    // Append 1 number in the array (ARR2) where $.F1 = 1
    res = this.collection.modify("CAST($.F1 as SIGNED) = 1").arrayAppend("$.ARR2", -4321.4321).sort("$._id").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("CAST($.ARR2[" + (arraySize) + "] as DECIMAL(10,4)) = -4321.4321").orderBy("$._id").execute();
    doc = docs.next();
    yArray = (JsonArray) doc.get("ARR2");
    assertEquals(arraySize + 1, yArray.size());
    assertEquals((long) (1), (long) (((JsonNumber) doc.get("F1")).getInteger()));
    assertFalse(docs.hasNext());
    // Append 3 number in the array (ARR2) where $.F1 = 1
    res = this.collection.modify("CAST($.F1 as SIGNED) = 1").arrayAppend("$.ARR2", 4321.1234).arrayAppend("$.ARR2", 4321.9847).arrayAppend("$.ARR2", -4321.9888).sort("$._id").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("CAST($.ARR2[" + (arraySize) + "] as  DECIMAL(10,4)) =  -4321.4321").orderBy("$._id").execute();
    doc = docs.next();
    yArray = (JsonArray) doc.get("ARR2");
    assertEquals(arraySize + 4, yArray.size());
    assertEquals((long) (1), (long) (((JsonNumber) doc.get("F1")).getInteger()));
    assertFalse(docs.hasNext());
    // Append 1 String in the array (ARR3) where $.F1 = 1
    res = this.collection.modify("CAST($.F1 as SIGNED) = 1").arrayAppend("$.ARR3", s1).sort("$._id").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("$.ARR3[" + (arraySize) + "] = '" + s1 + "'").orderBy("$._id").execute();
    doc = docs.next();
    yArray = (JsonArray) doc.get("ARR3");
    assertEquals(arraySize + 1, yArray.size());
    assertEquals((long) (1), (long) (((JsonNumber) doc.get("F1")).getInteger()));
    assertFalse(docs.hasNext());
    // Append 5 Strings in the array (ARR3) where $.F1 = 1
    res = this.collection.modify("CAST($.F1 as SIGNED) = 1").arrayAppend("$.ARR3", s1 + "1").arrayAppend("$.ARR3", s1 + "2").arrayAppend("$.ARR3", s1 + "3").arrayAppend("$.ARR3", s1 + "4").arrayAppend("$.ARR3", s1 + "5").sort("$._id").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("$.ARR3[" + (arraySize) + "] = '" + s1 + "'").orderBy("$._id").execute();
    doc = docs.next();
    yArray = (JsonArray) doc.get("ARR3");
    assertEquals(arraySize + 6, yArray.size());
    assertEquals((long) (1), (long) (((JsonNumber) doc.get("F1")).getInteger()));
    assertFalse(docs.hasNext());
}
Also used : JsonArray(com.mysql.cj.xdevapi.JsonArray) DbDoc(com.mysql.cj.xdevapi.DbDoc) DbDocImpl(com.mysql.cj.xdevapi.DbDocImpl) JsonNumber(com.mysql.cj.xdevapi.JsonNumber) JsonString(com.mysql.cj.xdevapi.JsonString) JsonString(com.mysql.cj.xdevapi.JsonString) DocResult(com.mysql.cj.xdevapi.DocResult) Result(com.mysql.cj.xdevapi.Result) AddResult(com.mysql.cj.xdevapi.AddResult) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Aggregations

DbDocImpl (com.mysql.cj.xdevapi.DbDocImpl)57 JsonString (com.mysql.cj.xdevapi.JsonString)57 DbDoc (com.mysql.cj.xdevapi.DbDoc)56 Test (org.junit.jupiter.api.Test)56 DocResult (com.mysql.cj.xdevapi.DocResult)52 JsonNumber (com.mysql.cj.xdevapi.JsonNumber)50 AddResult (com.mysql.cj.xdevapi.AddResult)21 Result (com.mysql.cj.xdevapi.Result)19 JsonArray (com.mysql.cj.xdevapi.JsonArray)15 RowResult (com.mysql.cj.xdevapi.RowResult)11 Row (com.mysql.cj.xdevapi.Row)9 SqlResult (com.mysql.cj.xdevapi.SqlResult)8 Table (com.mysql.cj.xdevapi.Table)8 BigDecimal (java.math.BigDecimal)7 ExecutionException (java.util.concurrent.ExecutionException)7 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 Collection (com.mysql.cj.xdevapi.Collection)2 Session (com.mysql.cj.xdevapi.Session)2