Search in sources :

Example 1 with JsonArray

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

the class CollectionFindTest method testCollectionFindInValidMix.

@Test
public void testCollectionFindInValidMix() throws Exception {
    assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.0")), "MySQL 8.0+ is required to run this test.");
    int i = 0, j = 0, maxrec = 8, minArraySize = 3;
    DbDoc doc = null;
    DocResult docs = null;
    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 < (minArraySize + i); j++) {
            jarray.addValue(new JsonString().setValue("Field-1-Data-" + i));
        }
        newDoc2.add("ARR1", jarray);
        this.collection.add(newDoc2).execute();
        newDoc2 = null;
        jarray = null;
    }
    assertEquals((maxrec), this.collection.count());
    /* add(DbDoc[] docs) */
    DbDoc[] jsonlist = new DbDocImpl[maxrec];
    for (i = 0; i < maxrec; i++) {
        DbDoc newDoc2 = new DbDocImpl();
        newDoc2.add("_id", new JsonString().setValue(String.valueOf(i + 1100)));
        newDoc2.add("ARR1", new JsonString().setValue("Field-1-Data-" + i));
        newDoc2.add("F2", new JsonString().setValue("10-15-201" + i));
        jsonlist[i] = newDoc2;
        newDoc2 = null;
    }
    this.collection.add(jsonlist).execute();
    /* find with IN for key having mixture of value and array */
    docs = this.collection.find("\"10-15-2017\" in $.F2").execute();
    doc = docs.next();
    assertEquals("1107", (((JsonString) doc.get("_id")).getString()));
    assertFalse(docs.hasNext());
    /* find with NULL IN in key having mix of array and string */
    docs = this.collection.find("NULL in $.ARR1").execute();
    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) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Example 2 with JsonArray

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

the class CollectionFindTest method testCollectionFindOverlaps.

@Test
public void testCollectionFindOverlaps() throws Exception {
    assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.0")), "MySQL 8.0+ is required to run this test.");
    try {
        int i = 0, j = 0, maxrec = 8, minArraySize = 3;
        DbDoc doc = null;
        DocResult docs = null;
        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 < (minArraySize + i); j++) {
                jarray.addValue(new JsonNumber().setValue(String.valueOf((l3 + j + i))));
            }
            newDoc2.add("ARR1", jarray);
            JsonArray karray = new JsonArray();
            for (j = 0; j < (minArraySize + i); j++) {
                karray.addValue(new JsonNumber().setValue(String.valueOf((d1 + j + i))));
            }
            newDoc2.add("ARR2", karray);
            JsonArray larray = new JsonArray();
            for (j = 0; j < (minArraySize + i); 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());
        /* find with single OVERLAPS in array */
        docs = this.collection.find("2147483647 overlaps $.ARR1").execute();
        doc = docs.next();
        assertEquals("1000", (((JsonString) doc.get("_id")).getString()));
        assertFalse(docs.hasNext());
        Table table = this.schema.getCollectionAsTable(this.collectionName);
        RowResult rows = table.select("doc->$._id as _id").where("2147483647 overlaps $.ARR1").execute();
        Row r = rows.next();
        assertEquals("\"1000\"", r.getString("_id"));
        assertFalse(rows.hasNext());
        /* find with array OVERLAPS array */
        docs = this.collection.find("[2147483647, 2147483648, 2147483649] overlaps $.ARR1").execute();
        doc = docs.next();
        assertEquals("1000", (((JsonString) doc.get("_id")).getString()));
        doc = docs.next();
        assertEquals("1001", (((JsonString) doc.get("_id")).getString()));
        doc = docs.next();
        assertEquals("1002", (((JsonString) doc.get("_id")).getString()));
        assertFalse(docs.hasNext());
        rows = table.select("doc->$._id as _id").where("[2147483647, 2147483648, 2147483649] overlaps $.ARR1").execute();
        r = rows.next();
        assertEquals("\"1000\"", r.getString("_id"));
        r = rows.next();
        assertEquals("\"1001\"", r.getString("_id"));
        r = rows.next();
        assertEquals("\"1002\"", r.getString("_id"));
        assertFalse(rows.hasNext());
        /* find with array OVERLAPS array with orderBy */
        docs = this.collection.find("[2147483648, 2147483648, 2147483649] overlaps $.ARR1").orderBy("_id").execute();
        doc = docs.next();
        assertEquals("1000", (((JsonString) doc.get("_id")).getString()));
        doc = docs.next();
        assertEquals("1001", (((JsonString) doc.get("_id")).getString()));
        doc = docs.next();
        assertEquals("1002", (((JsonString) doc.get("_id")).getString()));
        assertFalse(docs.hasNext());
        /* */
        docs = this.collection.find("[!false && true] OVERLAPS [true]").execute();
        assertEquals(maxrec, docs.count());
        /* Overlaps with NULL */
        docs = this.collection.find("NULL overlaps $.ARR1").execute();
        assertFalse(docs.hasNext());
        rows = table.select("doc->$._id as _id").where("NULL overlaps $.ARR1").execute();
        assertFalse(rows.hasNext());
        docs = this.collection.find("$.ARR1 overlaps null").execute();
        assertFalse(docs.hasNext());
        rows = table.select("doc->$._id as _id").where("$.ARR1 overlaps NULL").execute();
        assertFalse(rows.hasNext());
        /* Not Overlaps with NULL */
        docs = this.collection.find("NULL not overlaps $.ARR1").execute();
        assertTrue(docs.hasNext());
        assertEquals(maxrec, docs.count());
        rows = table.select("doc->$._id as _id").where("NULL not overlaps $.ARR1").execute();
        assertTrue(rows.hasNext());
        assertEquals(maxrec, docs.count());
        docs = this.collection.find("$.ARR1 not overlaps null").execute();
        assertTrue(docs.hasNext());
        assertEquals(maxrec, docs.count());
        rows = table.select("doc->$._id as _id").where("$.ARR1 not overlaps null").execute();
        assertTrue(rows.hasNext());
        assertEquals(maxrec, docs.count());
        /* Test OVERLAPS/NOT OVERLAPS with empty array - Expected to pass, though the array is empty but still valid */
        // checking the case insensitivity as well
        docs = this.collection.find("[] Overlaps $.ARR1").execute();
        assertFalse(docs.hasNext());
        rows = table.select().where("[] ovErlaps $.ARR1").execute();
        assertFalse(rows.hasNext());
        // checking the case insensitivity as well
        docs = this.collection.find("$.ARR1 overlapS []").execute();
        assertFalse(docs.hasNext());
        rows = table.select().where("$.ARR1 ovErlaps []").execute();
        assertFalse(rows.hasNext());
        docs = this.collection.find("[] not overlaps $.ARR1").execute();
        assertTrue(docs.hasNext());
        assertEquals(maxrec, docs.count());
        rows = table.select().where("[] not overlaps $.ARR1").execute();
        assertTrue(rows.hasNext());
        assertEquals(maxrec, docs.count());
        // checking the case insensitivity as well
        docs = this.collection.find("$.ARR1 not oveRlaps []").execute();
        assertTrue(docs.hasNext());
        assertEquals(maxrec, docs.count());
        rows = table.select().where("$.ARR1 not overlaps []").execute();
        assertTrue(rows.hasNext());
        assertEquals(maxrec, docs.count());
        /* When the right number of operands are not provided - error should be thrown */
        assertThrows(WrongArgumentException.class, "Cannot find atomic expression at token pos: 0", () -> this.collection.find("overlaps $.ARR1").execute());
        assertThrows(WrongArgumentException.class, "No more tokens when expecting one at token pos 4", () -> this.collection.find("$.ARR1 OVERLAPS").execute());
        assertThrows(WrongArgumentException.class, "Cannot find atomic expression at token pos: 0", () -> this.collection.find("OVERLAPS").execute());
        assertThrows(WrongArgumentException.class, "Cannot find atomic expression at token pos: 1", () -> this.collection.find("not overlaps $.ARR1").execute());
        assertThrows(WrongArgumentException.class, "No more tokens when expecting one at token pos 5", () -> this.collection.find("$.ARR1 NOT OVERLAPS").execute());
        assertThrows(WrongArgumentException.class, "Cannot find atomic expression at token pos: 1", () -> this.collection.find("not OVERLAPS").execute());
        final Table table1 = this.schema.getCollectionAsTable(this.collectionName);
        assertThrows(WrongArgumentException.class, "Cannot find atomic expression at token pos: 0", () -> table1.select().where("overlaps $.ARR1").execute());
        assertThrows(WrongArgumentException.class, "No more tokens when expecting one at token pos 4", () -> table1.select().where("$.ARR1 OVERLAPS").execute());
        assertThrows(WrongArgumentException.class, "Cannot find atomic expression at token pos: 0", () -> table1.select().where("OVERLAPS").execute());
        assertThrows(WrongArgumentException.class, "Cannot find atomic expression at token pos: 1", () -> table1.select().where("not overlaps $.ARR1").execute());
        assertThrows(WrongArgumentException.class, "No more tokens when expecting one at token pos 5", () -> table1.select().where("$.ARR1 NOT OVERLAPS").execute());
        assertThrows(WrongArgumentException.class, "Cannot find atomic expression at token pos: 1", () -> table1.select().where("not OVERLAPS").execute());
        /* invalid criteria, e.g. .find("[1, 2, 3] OVERLAPS $.age") . where $.age is atomic value */
        dropCollection("coll2");
        Collection coll2 = this.schema.createCollection("coll2", true);
        coll2.add("{ \"_id\": \"1\", \"name\": \"nonjson\", \"age\": \"50\",\"arrayField\":[1,[7]]}").execute();
        // The below command should give exception, but X-plugin doesn't return any error
        docs = coll2.find("[1,2,3] overlaps $.age").execute();
        assertEquals(0, docs.count());
        docs = coll2.find("arrayField OVERLAPS [7]").execute();
        assertEquals(0, docs.count());
        docs = coll2.find("arrayField[1] OVERLAPS [7]").execute();
        assertEquals(1, docs.count());
        table = this.schema.getCollectionAsTable("coll2");
        rows = table.select().where("[1,2,3] overlaps $.age").execute();
        assertEquals(0, rows.count());
        /* Test with empty spaces */
        dropCollection("coll3");
        Collection coll3 = this.schema.createCollection("coll3", true);
        // List contains an array without any space
        coll3.add("{ \"_id\":1, \"name\": \"Record1\",\"list\":[\"\"], \"age\":15, \"intList\":[1,2,3] }").execute();
        // List contains an array with space
        coll3.add("{ \"_id\":2, \"name\": \"overlaps\",\"list\":[\" \"],\"age\":24}").execute();
        coll3.add("{ \"_id\":3, \"overlaps\": \"overlaps\",\"age\":30}").execute();
        docs = coll3.find("[''] OVERLAPS $.list").execute();
        assertEquals(1, docs.count());
        assertEquals(new Integer(1), ((JsonNumber) docs.next().get("_id")).getInteger());
        table = this.schema.getCollectionAsTable("coll3");
        rows = table.select("doc->$._id as _id").where("[''] overlaps $.list").execute();
        r = rows.next();
        assertEquals("1", r.getString("_id"));
        docs = coll3.find("[' '] OVERLAPS $.list").execute();
        assertEquals(1, docs.count());
        assertEquals(new Integer(2), ((JsonNumber) docs.next().get("_id")).getInteger());
        rows = table.select("doc->$._id as _id").where("[' '] overlaps $.list").execute();
        r = rows.next();
        assertEquals("2", r.getString("_id"));
        docs = coll3.find("'overlaps' OVERLAPS $.name").execute();
        assertEquals(1, docs.count());
        assertEquals(new Integer(2), ((JsonNumber) docs.next().get("_id")).getInteger());
        rows = table.select("doc->$._id as _id").where("'overlaps' overlaps $.name").execute();
        r = rows.next();
        assertEquals(1, docs.count());
        assertEquals("2", r.getString("_id"));
        docs = coll3.find("[3] OVERLAPS $.intList").execute();
        assertEquals(1, docs.count());
        rows = table.select().where("[3] overlaps $.intList").execute();
        assertEquals(1, rows.count());
        /* Escape the keyword, to use it as identifier */
        docs = coll3.find("`overlaps` OVERLAPS $.`overlaps`").execute();
        assertEquals(1, docs.count());
        rows = table.select().where("'overlaps' overlaps $.`overlaps`").execute();
        assertEquals(1, rows.count());
        docs = coll3.find("$.`overlaps` OVERLAPS `overlaps`").execute();
        assertEquals(1, docs.count());
        rows = table.select().where("$.`overlaps` overlaps 'overlaps'").execute();
        assertEquals(1, rows.count());
        dropCollection("coll4");
        Collection coll4 = this.schema.createCollection("coll4", true);
        coll4.add("{\"overlaps\":{\"one\":1, \"two\":2, \"three\":3},\"list\":{\"one\":1, \"two\":2, \"three\":3},\"name\":\"one\"}").execute();
        coll4.add("{\"overlaps\":{\"one\":1, \"two\":2, \"three\":3},\"list\":{\"four\":4, \"five\":5, \"six\":6},\"name\":\"two\"}").execute();
        coll4.add("{\"overlaps\":{\"one\":1, \"three\":3, \"five\":5},\"list\":{\"two\":2, \"four\":4, \"six\":6},\"name\":\"three\"}").execute();
        coll4.add("{\"overlaps\":{\"one\":1, \"three\":3, \"five\":5},\"list\":{\"three\":3, \"six\":9, \"nine\":9},\"name\":\"four\"}").execute();
        coll4.add("{\"overlaps\":{\"one\":1, \"three\":3, \"five\":5},\"list\":{\"three\":6, \"six\":12, \"nine\":18},\"name\":\"five\"}").execute();
        coll4.add("{\"overlaps\":{\"one\":[1,2,3]}, \"list\":{\"one\":[3,4,5]}, \"name\":\"six\"}").execute();
        coll4.add("{\"overlaps\":{\"one\":[1,2,3]}, \"list\":{\"one\":[1,2,3]}, \"name\":\"seven\"}").execute();
        docs = coll4.find("`overlaps` OVERLAPS `list`").execute();
        assertEquals(3, docs.count());
        doc = docs.fetchOne();
        assertEquals("one", (((JsonString) doc.get("name")).getString()));
        doc = docs.fetchOne();
        assertEquals("four", (((JsonString) doc.get("name")).getString()));
        doc = docs.fetchOne();
        assertEquals("seven", (((JsonString) doc.get("name")).getString()));
        table = this.schema.getCollectionAsTable("coll4");
        rows = table.select("doc->$.name as name").where("$.`overlaps` OVERLAPS $.`list`").execute();
        assertEquals(3, rows.count());
        r = rows.next();
        assertEquals("\"one\"", r.getString("name"));
    } finally {
        dropCollection("coll4");
        dropCollection("coll3");
        dropCollection("coll2");
    }
}
Also used : Table(com.mysql.cj.xdevapi.Table) JsonArray(com.mysql.cj.xdevapi.JsonArray) DbDoc(com.mysql.cj.xdevapi.DbDoc) RowResult(com.mysql.cj.xdevapi.RowResult) DbDocImpl(com.mysql.cj.xdevapi.DbDocImpl) JsonNumber(com.mysql.cj.xdevapi.JsonNumber) Collection(com.mysql.cj.xdevapi.Collection) JsonString(com.mysql.cj.xdevapi.JsonString) Row(com.mysql.cj.xdevapi.Row) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Example 3 with JsonArray

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

the class CollectionFindTest method testCollectionFindInInvalid.

@Test
public void testCollectionFindInInvalid() throws Exception {
    assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.0")), "MySQL 8.0+ is required to run this test.");
    int i = 0, j = 0, maxrec = 8, minArraySize = 3;
    DocResult docs = null;
    String json = "";
    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 < (minArraySize + i); j++) {
            jarray.addValue(new JsonString().setValue("Field-1-Data-" + i));
        }
        newDoc2.add("ARR1", jarray);
        this.collection.add(newDoc2).execute();
        newDoc2 = null;
        jarray = null;
    }
    assertEquals((maxrec), this.collection.count());
    /* add(DbDoc[] docs) */
    DbDoc[] jsonlist = new DbDocImpl[maxrec];
    for (i = 0; i < maxrec; i++) {
        DbDoc newDoc2 = new DbDocImpl();
        newDoc2.add("_id", new JsonString().setValue(String.valueOf(i + 1100)));
        newDoc2.add("ARR1", new JsonString().setValue("Field-1-Data-" + i));
        newDoc2.add("F2", new JsonString().setValue("10-15-201" + i));
        jsonlist[i] = newDoc2;
        newDoc2 = null;
    }
    this.collection.add(jsonlist).execute();
    json = "{\"_id\":\"1201\",\"XYZ\":2222, \"DATAX\":{\"D1\":1, \"D2\":2, \"D3\":3}}";
    this.collection.add(json).execute();
    /* find with invalid IN in document */
    try {
        docs = this.collection.find("{\"D1\":3, \"D2\":2, \"D3\":3} in $.DATAX").execute();
        assertFalse(docs.hasNext());
    } catch (XProtocolError Ex) {
        Ex.printStackTrace();
        if (Ex.getErrorCode() != MysqlErrorNumbers.ER_BAD_NULL_ERROR) {
            throw Ex;
        }
    }
    /* find with IN that does not match */
    docs = this.collection.find("\"2222\" in $.XYZ").execute();
    assertFalse(docs.hasNext());
    /* find with NULL IN */
    docs = this.collection.find("NULL in $.ARR1").execute();
    assertFalse(docs.hasNext());
    /* find with NULL IN */
    docs = this.collection.find("NULL in $.DATAX").execute();
    assertFalse(docs.hasNext());
    /* find with IN for non existant key */
    docs = this.collection.find("\"ABC\" in $.nonexistant").execute();
    assertFalse(docs.hasNext());
}
Also used : JsonArray(com.mysql.cj.xdevapi.JsonArray) DbDoc(com.mysql.cj.xdevapi.DbDoc) DbDocImpl(com.mysql.cj.xdevapi.DbDocImpl) XProtocolError(com.mysql.cj.protocol.x.XProtocolError) JsonNumber(com.mysql.cj.xdevapi.JsonNumber) JsonString(com.mysql.cj.xdevapi.JsonString) JsonString(com.mysql.cj.xdevapi.JsonString) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Example 4 with JsonArray

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

the class CollectionModifyTest method testArrayAppend.

@Test
public void testArrayAppend() {
    if (!mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
        // Requires manual _id.
        this.collection.add("{\"_id\": \"1\", \"x\":[8,16,32]}").execute();
    } else {
        this.collection.add("{\"x\":[8,16,32]}").execute();
    }
    this.collection.modify("true").arrayAppend("$.x", "64").execute();
    DocResult res = this.collection.find().execute();
    DbDoc jd = res.next();
    JsonArray xArray = (JsonArray) jd.get("x");
    assertEquals(new Integer(8), ((JsonNumber) xArray.get(0)).getInteger());
    assertEquals(new Integer(16), ((JsonNumber) xArray.get(1)).getInteger());
    assertEquals(new Integer(32), ((JsonNumber) xArray.get(2)).getInteger());
    // TODO: better arrayAppend() overloads?
    assertEquals("64", ((JsonString) xArray.get(3)).getString());
    assertEquals(4, xArray.size());
}
Also used : JsonArray(com.mysql.cj.xdevapi.JsonArray) DbDoc(com.mysql.cj.xdevapi.DbDoc) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Example 5 with JsonArray

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

Aggregations

JsonArray (com.mysql.cj.xdevapi.JsonArray)19 Test (org.junit.jupiter.api.Test)19 DbDoc (com.mysql.cj.xdevapi.DbDoc)18 DocResult (com.mysql.cj.xdevapi.DocResult)17 JsonString (com.mysql.cj.xdevapi.JsonString)17 DbDocImpl (com.mysql.cj.xdevapi.DbDocImpl)15 JsonNumber (com.mysql.cj.xdevapi.JsonNumber)14 Result (com.mysql.cj.xdevapi.Result)5 AddResult (com.mysql.cj.xdevapi.AddResult)4 BigDecimal (java.math.BigDecimal)3 Row (com.mysql.cj.xdevapi.Row)2 RowResult (com.mysql.cj.xdevapi.RowResult)2 ExecutionException (java.util.concurrent.ExecutionException)2 WrongArgumentException (com.mysql.cj.exceptions.WrongArgumentException)1 XProtocolError (com.mysql.cj.protocol.x.XProtocolError)1 Collection (com.mysql.cj.xdevapi.Collection)1 Table (com.mysql.cj.xdevapi.Table)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1