Search in sources :

Example 26 with JsonString

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

Example 27 with JsonString

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

the class CollectionModifyTest method testArrayModify.

@Test
public void testArrayModify() {
    JsonArray xArray = new JsonArray().addValue(new JsonString().setValue("a")).addValue(new JsonNumber().setValue("1"));
    DbDoc doc = new DbDocImpl().add("x", new JsonNumber().setValue("3")).add("y", xArray);
    if (!mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
        // Requires manual _id.
        this.collection.add("{\"_id\": \"1\", \"x\":1, \"y\":[\"b\", 2]}").execute();
        this.collection.add("{\"_id\": \"2\", \"x\":2, \"y\":22}").execute();
        // Inject an _id.
        this.collection.add(doc.add("_id", new JsonString().setValue("3"))).execute();
    } else {
        this.collection.add("{\"x\":1, \"y\":[\"b\", 2]}").execute();
        this.collection.add("{\"x\":2, \"y\":22}").execute();
        this.collection.add(doc).execute();
    }
    this.collection.modify("true").arrayInsert("$.y[1]", 44).execute();
    this.collection.modify("x = 2").change("$.y", xArray).execute();
    this.collection.modify("x = 3").set("y", xArray).execute();
    DocResult res = this.collection.find().execute();
    while (res.hasNext()) {
        DbDoc jd = res.next();
        if (((JsonNumber) jd.get("x")).getInteger() == 1) {
            assertEquals((new JsonArray().addValue(new JsonString().setValue("b")).addValue(new JsonNumber().setValue("44")).addValue(new JsonNumber().setValue("2"))).toString(), (jd.get("y")).toString());
        } else {
            assertEquals(xArray.toString(), jd.get("y").toString());
        }
    }
}
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 28 with JsonString

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

the class CollectionModifyTest method testCollectionModifyBasic.

@Test
public void testCollectionModifyBasic() throws Exception {
    int i = 0, maxrec = 30, recCnt = 0;
    DbDoc doc = null;
    Result res = null;
    String s1 = buildString((10), 'X');
    /* 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 + 1000)));
        newDoc2.add("F1", new JsonString().setValue("Field-1-Data-" + i));
        newDoc2.add("F2", new JsonNumber().setValue(String.valueOf(10 * (i + 1))));
        if (i % 3 == 2) {
            newDoc2.add("F3", JsonLiteral.TRUE);
        } else if (i % 3 == 1) {
            newDoc2.add("F3", JsonLiteral.NULL);
        } else {
            newDoc2.add("F3", JsonLiteral.FALSE);
        }
        newDoc2.add("tmp1", new JsonString().setValue("tempdata-" + i));
        newDoc2.add("tmp2", new JsonString().setValue("tempForChange-" + i));
        jsonlist[i] = newDoc2;
        newDoc2 = null;
    }
    this.collection.add(jsonlist).execute();
    assertEquals((maxrec), this.collection.count());
    /* fetch all */
    DocResult docs = this.collection.find("CAST($.F2 as SIGNED)> 0").fields("$._id as _id, $.F1 as f1, $.F2 as f2, $.F3 as f3").execute();
    i = 0;
    while (docs.hasNext()) {
        doc = docs.next();
        assertEquals(String.valueOf(i + 1000), (((JsonString) doc.get("_id")).getString()));
        assertEquals((long) (10 * (i + 1)), (long) (((JsonNumber) doc.get("f2")).getInteger()));
        i++;
    }
    assertEquals((maxrec), i);
    /* Modify using empty Condition */
    assertThrows(XDevAPIError.class, "Parameter 'criteria' must not be null or empty.", () -> CollectionModifyTest.this.collection.modify("").set("$.F1", "Data_New").execute());
    /* Modify using null Condition */
    assertThrows(XDevAPIError.class, "Parameter 'criteria' must not be null or empty.", () -> CollectionModifyTest.this.collection.modify(null).set("$.F1", "Data_New").execute());
    /* Modify with true Condition using Set */
    res = this.collection.modify("true").set("$.F1", "Data_True").execute();
    assertEquals(maxrec, res.getAffectedItemsCount());
    docs = this.collection.find("$.F1 Like '%True'").fields("$._id as _id, $.F1 as f1, $.F2 as f2, $.F3 as f3").execute();
    recCnt = count_data(docs);
    assertEquals(maxrec, recCnt);
    res = this.collection.modify("1 == 1").set("$.F1", "Data_New").execute();
    assertEquals(maxrec, res.getAffectedItemsCount());
    docs = this.collection.find("$.F1 Like '%New'").fields("$._id as _id, $.F1 as f1, $.F2 as f2, $.F3 as f3").execute();
    recCnt = count_data(docs);
    assertEquals(maxrec, recCnt);
    /* Modify with a false Condition */
    res = this.collection.modify("false").set("$.F1", "False_Data").execute();
    assertEquals(0, res.getAffectedItemsCount());
    // Modify with a Condition which results to false
    res = this.collection.modify("0 == 1").set("$.F1", "False_Data").execute();
    assertEquals(0, res.getAffectedItemsCount());
    /* Un Set */
    // Test UnSet with condition
    docs = this.collection.find("$.tmp1 Like 'tempdata%'").fields("$._id as _id, $.tmp1 as tp").execute();
    recCnt = count_data(docs);
    // Total Rec with $.tmp1 Like 'tempdata%'
    assertEquals(maxrec, recCnt);
    res = this.collection.modify("CAST($._id as SIGNED) % 2").unset("$.tmp1").execute();
    assertEquals(maxrec / 2, res.getAffectedItemsCount());
    docs = this.collection.find("$.tmp1 Like 'tempdata%'").fields("$._id as _id, $.tmp1 as tp").execute();
    recCnt = count_data(docs);
    // Total records after Unset(with condition)
    assertEquals(maxrec / 2, recCnt);
    // Test true condition with unset
    res = this.collection.modify("1 == 1").unset("$.tmp1").execute();
    assertEquals(maxrec / 2, res.getAffectedItemsCount());
    docs = this.collection.find("$.tmp1 Like 'tempdata%'").fields("$._id as _id, $.tmp1 as tp").execute();
    recCnt = count_data(docs);
    // Total records after Unset(without condition)
    assertEquals(0, recCnt);
    /* Test for Change().unset tmp2 for half of the total records.Call change without condition. */
    res = this.collection.modify("CAST($._id as SIGNED) % 2").unset("$.tmp2").execute();
    assertEquals(maxrec / 2, res.getAffectedItemsCount());
    docs = this.collection.find("$.tmp2 Like 'tempForChange%'").fields("$._id as _id, $.tmp2 as tp").execute();
    recCnt = count_data(docs);
    // Total records after Unset(with condition)
    assertEquals((maxrec / 2), recCnt);
    // Test for Change()
    res = this.collection.modify("true").change("$.tmp2", "Changedata").execute();
    assertEquals(maxrec / 2, res.getAffectedItemsCount());
    docs = this.collection.find("$.tmp2 Like 'Changedata'").fields("$._id as _id, $.tmp2 as tp").execute();
    recCnt = count_data(docs);
    // Total records Changed after modify().change(without condition)
    assertEquals((maxrec / 2), recCnt);
    // Test for set () after unset
    res = this.collection.modify("true").set("$.tmp2", "Changedata1").execute();
    assertEquals(maxrec, res.getAffectedItemsCount());
    docs = this.collection.find("$.tmp2 Like 'Changedata1'").fields("$._id as _id, $.tmp2 as tp").execute();
    recCnt = count_data(docs);
    // Total Records Set when Half of the records were unset
    assertEquals(maxrec, recCnt);
    // Test for set () after unset All
    res = this.collection.modify("true").unset("$.tmp2").execute();
    assertEquals(maxrec, res.getAffectedItemsCount());
    docs = this.collection.find("$.tmp2 IS Not NULL").fields("$._id as _id, $.tmp2 as tp").execute();
    recCnt = count_data(docs);
    // Total Records After unsetting all
    assertEquals(0, recCnt);
    res = this.collection.modify("1 == 1").set("$.tmp2", "Changedata3").execute();
    assertEquals(maxrec, res.getAffectedItemsCount());
    docs = this.collection.find("$.tmp2 Like 'Changedata3'").fields("$._id as _id, $.tmp2 as tp").execute();
    recCnt = count_data(docs);
    // Total Records Set when All the records were unset
    assertEquals(maxrec, recCnt);
    // Modify with Condition using Set
    res = this.collection.modify("$._id = '1001' and  $.F1 Like '%New' ").set("$.F1", s1).execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("$.F1 = '" + s1 + "'").fields("$._id as _id, $.F1 as f1, $.F2 as f2, $.F3 as f3").execute();
    recCnt = count_data(docs);
    assertEquals(1, recCnt);
}
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) 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)

Example 29 with JsonString

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

the class CollectionRemoveTest method testCollectionRemoveBasic.

@SuppressWarnings("deprecation")
@Test
public void testCollectionRemoveBasic() throws Exception {
    int i = 0, j = 0, maxrec = 100, recCnt = 0, arraySize = 30;
    Result res = null;
    DocResult docs = null;
    /* add(DbDoc[] docs) */
    DbDoc[] jsonlist = new DbDocImpl[maxrec];
    long l1 = Long.MAX_VALUE, l2 = Long.MIN_VALUE;
    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(1 + i)));
        newDoc2.add("F6", new JsonString().setValue((2000 + i) + "-02-" + (i * 2 + 10)));
        JsonArray jarray = new JsonArray();
        for (j = 0; j < (arraySize); j++) {
            jarray.addValue(new JsonString().setValue("String-" + i + "-" + j));
        }
        newDoc2.add("ARR1", jarray);
        jsonlist[i] = newDoc2;
        newDoc2 = null;
    }
    this.collection.add(jsonlist).execute();
    assertEquals((maxrec), this.collection.count());
    /* find without Condition */
    docs = this.collection.find("$.F4<0").fields("$._id as _id, $.F1 as f1, $.F2 as f2").execute();
    recCnt = count_data(docs);
    assertEquals(maxrec, recCnt);
    /* remove with condition */
    res = this.collection.remove("CAST($.F5 as SIGNED) = 1").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("CAST($.F5 as SIGNED) = 1 ").fields("$._id as _id, $.F1 as f1, $.F2 as f2").execute();
    assertFalse(docs.hasNext());
    /* remove with condition, limit and orderBy */
    res = this.collection.remove("CAST($.F5 as SIGNED) < 10").limit(1).orderBy("CAST($.F5 as SIGNED)").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("CAST($.F5 as SIGNED) = 2 ").fields("$._id as _id, $.F1 as f1, $.F2 as f2, $.F3+0 as f3").execute();
    assertFalse(docs.hasNext());
    /* remove with condition on string */
    res = this.collection.remove("$.F1 = 'Field-1-Data-2'").limit(10).orderBy("CAST($.F5 as SIGNED)").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("$.F1 = 'Field-1-Data-2'").fields("$._id as _id, $.F1 as f1, $.F2 as f2").execute();
    assertFalse(docs.hasNext());
    /* remove with condition on BigInt */
    res = this.collection.remove("CAST($.F3 as SIGNED) = " + (l1 - 3)).limit(10).orderBy("CAST($.F5 as SIGNED)").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("CAST($.F3 as SIGNED) = " + (l1 - 3)).fields("$._id as _id, $.F1 as f1, $.F2 as f2").execute();
    assertFalse(docs.hasNext());
    /* remove with condition on Double */
    res = this.collection.remove("CAST($.F2 as DECIMAL(10,5)) = " + (d1 + 4)).limit(10).orderBy("CAST($.F5 as SIGNED)").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("CAST($.F2 as SIGNED) = " + (d1 + 4)).fields("$._id as _id, $.F1 as f1, $.F2 as f2").execute();
    assertFalse(docs.hasNext());
    /* remove with condition on Array */
    res = this.collection.remove("$.ARR1[1]  like 'String-5-1' OR $.ARR1[0]  like 'String-5-0' AND $.ARR1[2]  like 'String-5-2'").limit(10).orderBy("CAST($.F5 as SIGNED)").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("$.ARR1[1]  like 'String-5-%'").fields("$._id as _id, $.F1 as f1, $.F2 as f2").execute();
    assertFalse(docs.hasNext());
    /* Try to remove non-existing row with condition on Array */
    res = this.collection.remove("$.ARR1[1]  like 'String-5-1' OR $.ARR1[0]  like 'String-5-0' AND $.ARR1[2]  like 'String-5-2'").limit(10).orderBy("CAST($.F5 as SIGNED)").execute();
    assertEquals(0, res.getAffectedItemsCount());
    /* remove with condition on Array */
    res = this.collection.remove("$.ARR1[1] like concat(substr($.ARR1[0],1,7),'6','-1')").limit(10).orderBy("CAST($.F5 as SIGNED)").execute();
    assertEquals(1, res.getAffectedItemsCount());
    docs = this.collection.find("$.ARR1[1]  like 'String-6-%'").fields("$._id as _id, $.F1 as f1, $.F2 as f2").execute();
    assertFalse(docs.hasNext());
    /* remove with null condition */
    i = (int) this.collection.count();
    assertThrows(XDevAPIError.class, "Parameter 'criteria' must not be null or empty.", () -> this.collection.remove(null).execute());
    /* remove with empty condition */
    i = (int) this.collection.count();
    assertThrows(XDevAPIError.class, "Parameter 'criteria' must not be null or empty.", () -> this.collection.remove(" ").execute());
    /* remove All with a true condition */
    i = (int) this.collection.count();
    res = this.collection.remove("true").limit((maxrec * 10)).orderBy("CAST($.F5 as SIGNED)").execute();
    assertEquals(i, res.getAffectedItemsCount());
    docs = this.collection.find("$.ARR1[1]  like 'S%'").fields("$._id as _id, $.F1 as f1, $.F2 as f2").execute();
    assertFalse(docs.hasNext());
    /* remove with a false condition */
    i = (int) this.collection.count();
    res = this.collection.remove("false").limit((maxrec * 10)).orderBy("CAST($.F5 as SIGNED)").execute();
    assertEquals(0, res.getAffectedItemsCount());
}
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) Result(com.mysql.cj.xdevapi.Result) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Example 30 with JsonString

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

the class CollectionTest method testArrayIndex014.

@Test
public void testArrayIndex014() throws Exception {
    assumeTrue(mysqlVersionMeetsMinimum(this.baseUrl, ServerVersion.parseVersion("8.0.17")), "MySQL 8.0.17+ is required to run this test.");
    String collname = "coll1";
    DbDoc doc = null;
    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.remove("true").execute();
        coll.add("{\"intField\" : [1,2,3], \"uintField\" : [51,52,53], \"dateField\" : [\"2019-1-1\", \"2019-2-1\", \"2019-3-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],\"dateFieldWOI\" : \"2019-1-1\"}").execute();
        coll.add("{\"intField\" : [11,12,3], \"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],\"dateFieldWOI\" : \"2019-1-1\"}").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\", \"7.30\", \"12.30\"], \"floatField\" : [51.2,52.7,53.6],\"dateFieldWOI\" : \"2019-2-1\"}").execute();
        try {
            coll.add("{\"intField\" : \"[1,2,3]\", \"uintField\" : [51,52,53], \"dateField\" : [\"2019-1-1\", \"2019-2-1\", \"2019-3-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("functional index"));
        }
        DocResult docs = coll.find(":intField in $.intField").bind("intField", 12).execute();
        doc = null;
        int i = 0;
        while (docs.hasNext()) {
            doc = docs.next();
            i++;
        }
        assertTrue(i == 2);
        docs = coll.find(":uintField in $.uintField").bind("uintField", 52).execute();
        doc = null;
        i = 0;
        while (docs.hasNext()) {
            doc = docs.next();
            i++;
        }
        assertTrue(i == 3);
        docs = coll.find(":charField in $.charField").bind("charField", "abcd1").execute();
        doc = null;
        i = 0;
        while (docs.hasNext()) {
            doc = docs.next();
            i++;
        }
        assertTrue(i == 3);
        docs = coll.find(":binaryField in $.binaryField").bind("binaryField", "abcd1").execute();
        doc = null;
        i = 0;
        while (docs.hasNext()) {
            doc = docs.next();
            i++;
        }
        assertTrue(i == 3);
        docs = coll.find(":floatField in $.floatField").bind("floatField", 51.2).execute();
        doc = null;
        i = 0;
        while (docs.hasNext()) {
            doc = docs.next();
            i++;
        }
        System.out.println("Count = " + i);
        assertTrue(i == 2);
        docs = coll.find("CAST(CAST('2019-2-1' as DATE) as JSON) in $.dateField").execute();
        doc = null;
        i = 0;
        while (docs.hasNext()) {
            doc = docs.next();
            i++;
        }
        System.out.println("Count = " + i);
        assertTrue(i == 3);
        docs = coll.find("CAST(CAST('2019-2-1' as DATE) as JSON) not in $.dateField").execute();
        doc = null;
        i = 0;
        while (docs.hasNext()) {
            doc = docs.next();
            i++;
        }
        System.out.println("Using NOT IN");
        System.out.println("Count = " + i);
        // assertTrue(i == 0);
        docs = coll.find("'2019-1-1' not in $.dateFieldWOI").execute();
        doc = null;
        i = 0;
        while (docs.hasNext()) {
            doc = docs.next();
            System.out.println((((JsonString) doc.get("dateFieldWOI")).getString()));
            i++;
        }
        System.out.println("Using NOT IN Without Index");
        System.out.println("Count = " + i);
        docs = coll.find("CAST(CAST('2019-2-1' as DATE) as JSON) overlaps $.dateField").execute();
        doc = null;
        i = 0;
        while (docs.hasNext()) {
            doc = docs.next();
            i++;
        }
        System.out.println("Count = " + i);
        assertTrue(i == 3);
        docs = coll.find("CAST(CAST('2019-2-1' as DATE) as JSON) not overlaps $.dateField").execute();
        doc = null;
        i = 0;
        while (docs.hasNext()) {
            doc = docs.next();
            i++;
        }
        System.out.println("Using NOT OVERLAPS");
        System.out.println("Count = " + i);
        // assertTrue(i == 0);
        docs = coll.find("CAST(CAST(:datetimeField as DATETIME) as JSON) in $.datetimeField").bind("datetimeField", "9999-12-30 23:59:59").execute();
        doc = null;
        i = 0;
        while (docs.hasNext()) {
            doc = docs.next();
            i++;
        }
        System.out.println("Count = " + i);
        assertTrue(i == 2);
        docs = coll.find("CAST(CAST(:timeField as TIME) as JSON) in $.timeField").bind("timeField", "7.30").execute();
        doc = null;
        i = 0;
        while (docs.hasNext()) {
            doc = docs.next();
            i++;
        }
        System.out.println("Count = " + i);
        assertTrue(i == 1);
        // Integration scenarios between Index and Overlaps. Explicit casting added due to server Bug#29752056. NOT IN and NOT OVERLAPS doesn't require explicit casting
        docs = coll.find("CAST(CAST('2019-2-1' as DATE) as JSON) in $.dateField").execute();
        // System.out.println("Number of rows using IN with indexed array = "+docs.count());
        assertTrue(docs.count() == 3);
        docs = coll.find("'2019-2-1' not in $.dateField").execute();
        // System.out.println("Number of rows using NOT IN without casting = "+docs.count());
        assertTrue(docs.count() == 0);
        docs = coll.find("CAST(CAST('2019-2-1' as DATE) as JSON) overlaps $.dateField").execute();
        // System.out.println("Number of rows using OVERLAPS with indexed array = "+docs.count());
        assertTrue(docs.count() == 3);
        docs = coll.find("'2019-2-1' not overlaps $.dateField").execute();
        // System.out.println("Number of rows using NOT OVERLAPS without casting = "+docs.count());
        assertTrue(docs.count() == 0);
        // Integration scenarios for time
        docs = coll.find("CAST(CAST(:timeField as TIME) as JSON) in $.timeField").bind("timeField", "7.30").execute();
        assertTrue(docs.count() == 1);
        docs = coll.find(":timeField not in $.timeField").bind("timeField", "7.30").execute();
        assertTrue(docs.count() == 2);
        docs = coll.find("CAST(CAST(:timeField as TIME) as JSON) overlaps $.timeField").bind("timeField", "7.30").execute();
        assertTrue(docs.count() == 1);
        docs = coll.find(":timeField not overlaps $.timeField").bind("timeField", "7.30").execute();
        assertTrue(docs.count() == 2);
        // Integration scenarios for datetime
        docs = coll.find("CAST(CAST(:datetimeField as DATETIME) as JSON) in $.datetimeField").bind("datetimeField", "9999-12-30 23:59:59").execute();
        assertTrue(docs.count() == 2);
        docs = coll.find(":datetimeField NOT IN $.datetimeField").bind("datetimeField", "9999-12-30 23:59:59").execute();
        assertTrue(docs.count() == 1);
        docs = coll.find("CAST(CAST(:datetimeField as DATETIME) as JSON) OVERLAPS $.datetimeField").bind("datetimeField", "9999-12-30 23:59:59").execute();
        assertTrue(docs.count() == 2);
        docs = coll.find(":datetimeField NOT OVERLAPS $.datetimeField").bind("datetimeField", "9999-12-30 23:59:59").execute();
        assertTrue(docs.count() == 1);
        // Integration scenaris of Integer
        docs = coll.find(":intField not in $.intField").bind("intField", 12).execute();
        assertTrue(docs.count() == 1);
        docs = coll.find(":intField overlaps $.intField").bind("intField", 12).execute();
        assertTrue(docs.count() == 2);
        docs = coll.find(":intField not overlaps $.intField").bind("intField", 12).execute();
        assertTrue(docs.count() == 1);
        // Integration scenaris of unsigned integer
        docs = coll.find(":uintField not in $.uintField").bind("uintField", 52).execute();
        assertTrue(docs.count() == 0);
        docs = coll.find(":uintField overlaps $.uintField").bind("uintField", 52).execute();
        assertTrue(docs.count() == 3);
        docs = coll.find(":uintField not overlaps $.uintField").bind("uintField", 52).execute();
        assertTrue(docs.count() == 0);
        // Integration scenaris of character type
        docs = coll.find(":charField not in $.charField").bind("charField", "abcd1").execute();
        assertTrue(docs.count() == 0);
        docs = coll.find(":charField overlaps $.charField").bind("charField", "abcd1").execute();
        assertTrue(docs.count() == 3);
        docs = coll.find(":charField not overlaps $.charField").bind("charField", "abcd1").execute();
        assertTrue(docs.count() == 0);
        // Integration scenarios of binary type
        docs = coll.find(":binaryField not in $.binaryField").bind("binaryField", "abcd1").execute();
        assertTrue(docs.count() == 0);
        docs = coll.find(":binaryField overlaps $.binaryField").bind("binaryField", "abcd1").execute();
        assertTrue(docs.count() == 3);
        docs = coll.find(":binaryField not overlaps $.binaryField").bind("binaryField", "abcd1").execute();
        assertTrue(docs.count() == 0);
        sch.dropCollection(collname);
    } finally {
        if (sess != null) {
            sess.close();
            sess = null;
        }
    }
}
Also used : SessionFactory(com.mysql.cj.xdevapi.SessionFactory) DbDoc(com.mysql.cj.xdevapi.DbDoc) Schema(com.mysql.cj.xdevapi.Schema) Collection(com.mysql.cj.xdevapi.Collection) JsonString(com.mysql.cj.xdevapi.JsonString) JsonString(com.mysql.cj.xdevapi.JsonString) WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException) ExecutionException(java.util.concurrent.ExecutionException) DocResult(com.mysql.cj.xdevapi.DocResult) Session(com.mysql.cj.xdevapi.Session) Test(org.junit.jupiter.api.Test)

Aggregations

JsonString (com.mysql.cj.xdevapi.JsonString)80 Test (org.junit.jupiter.api.Test)79 DbDoc (com.mysql.cj.xdevapi.DbDoc)78 DocResult (com.mysql.cj.xdevapi.DocResult)74 DbDocImpl (com.mysql.cj.xdevapi.DbDocImpl)58 JsonNumber (com.mysql.cj.xdevapi.JsonNumber)53 AddResult (com.mysql.cj.xdevapi.AddResult)33 Result (com.mysql.cj.xdevapi.Result)21 JsonArray (com.mysql.cj.xdevapi.JsonArray)18 RowResult (com.mysql.cj.xdevapi.RowResult)15 Table (com.mysql.cj.xdevapi.Table)12 Row (com.mysql.cj.xdevapi.Row)11 SqlResult (com.mysql.cj.xdevapi.SqlResult)10 ExecutionException (java.util.concurrent.ExecutionException)10 BigDecimal (java.math.BigDecimal)9 Collection (com.mysql.cj.xdevapi.Collection)8 Session (com.mysql.cj.xdevapi.Session)8 SessionFactory (com.mysql.cj.xdevapi.SessionFactory)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6