Search in sources :

Example 26 with DocResult

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

the class CollectionFindTest method testCollectionFindInSanity.

@Test
public void testCollectionFindInSanity() throws Exception {
    assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.0")), "MySQL 8.0+ is required to run this test.");
    int i = 0, maxrec = 10;
    DbDoc doc = null;
    DocResult docs = null;
    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) + 0.1234)));
        newDoc2.add("F3", new JsonNumber().setValue(String.valueOf(i + 1)));
        newDoc2.add("F4", new JsonNumber().setValue(String.valueOf(10000 - i)));
        jsonlist[i] = newDoc2;
        newDoc2 = null;
    }
    this.collection.add(jsonlist).execute();
    assertEquals((maxrec), this.collection.count());
    /* find without Condition */
    docs = this.collection.find().fields("$._id as _id, $.F1 as f1, $.F2 as f2, $.F3 as f3,$.F2/10 as tmp1,1/2 as tmp2").orderBy("$.F3").execute();
    i = 0;
    while (docs.hasNext()) {
        doc = docs.next();
        i++;
    }
    assertEquals((maxrec), i);
    /* find with single element IN which uses json_contains */
    docs = this.collection.find("'1001' in $._id").execute();
    doc = docs.next();
    assertEquals("1001", (((JsonString) doc.get("_id")).getString()));
    assertFalse(docs.hasNext());
    /* find with multiple IN which uses json_contains */
    String findCond = "";
    for (i = 1; i < maxrec; i++) {
        findCond = findCond + "'";
        findCond = findCond + String.valueOf(i + 1000) + "' not in $._id";
        if (i != maxrec - 1) {
            findCond = findCond + " and ";
        }
    }
    docs = this.collection.find(findCond).execute();
    doc = docs.next();
    assertEquals("1000", (((JsonString) doc.get("_id")).getString()));
    assertFalse(docs.hasNext());
    /* find with single IN for string with orderBy */
    docs = this.collection.find("'Field-1-Data-2' in $.F1").orderBy("CAST($.F4 as SIGNED)").execute();
    doc = docs.next();
    assertEquals(String.valueOf(1002), (((JsonString) doc.get("_id")).getString()));
    assertFalse(docs.hasNext());
    /* find with single IN for numeric with orderBy */
    docs = this.collection.find("10000 in $.F4").orderBy("CAST($.F4 as SIGNED)").execute();
    doc = docs.next();
    assertEquals(String.valueOf(1000), (((JsonString) doc.get("_id")).getString()));
    assertFalse(docs.hasNext());
    /* find with single IN for float with orderBy */
    docs = this.collection.find("20.1234 in $.F2").orderBy("CAST($.F4 as SIGNED)").execute();
    doc = docs.next();
    assertEquals(String.valueOf(1001), (((JsonString) doc.get("_id")).getString()));
    assertFalse(docs.hasNext());
    /* Testing with table */
    Table table = this.schema.getCollectionAsTable(this.collectionName);
    /* find with single IN for string */
    RowResult rows = table.select("doc->$._id as _id").where("'1001' in doc->$._id").execute();
    Row r = rows.next();
    assertEquals(r.getString("_id"), "\"1001\"");
    assertFalse(rows.hasNext());
    /* find with multiple IN in single select */
    findCond = "";
    for (i = 1; i < maxrec; i++) {
        findCond = findCond + "'";
        findCond = findCond + String.valueOf(i + 1000) + "' not in doc->$._id";
        if (i != maxrec - 1) {
            findCond = findCond + " and ";
        }
    }
    /* find with single IN for string */
    rows = table.select("doc->$._id as _id").where(findCond).execute();
    r = rows.next();
    assertEquals(r.getString("_id"), "\"1000\"");
    assertFalse(rows.hasNext());
    /* find with single IN for float */
    rows = table.select("doc->$._id as _id").where("20.1234 in doc->$.F2").execute();
    r = rows.next();
    assertEquals(r.getString("_id"), "\"1001\"");
    assertFalse(rows.hasNext());
    /* find with single IN for string */
    rows = table.select("doc->$._id as _id").where("'Field-1-Data-2' in doc->$.F1").execute();
    r = rows.next();
    assertEquals(r.getString("_id"), "\"1002\"");
    assertFalse(rows.hasNext());
    /* find with single IN for numeric */
    rows = table.select("doc->$._id as _id").where("10000 in doc->$.F4").execute();
    r = rows.next();
    assertEquals(r.getString("_id"), "\"1000\"");
    assertFalse(rows.hasNext());
}
Also used : DbDoc(com.mysql.cj.xdevapi.DbDoc) RowResult(com.mysql.cj.xdevapi.RowResult) DbDocImpl(com.mysql.cj.xdevapi.DbDocImpl) Table(com.mysql.cj.xdevapi.Table) JsonNumber(com.mysql.cj.xdevapi.JsonNumber) JsonString(com.mysql.cj.xdevapi.JsonString) 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 27 with DocResult

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

the class CollectionFindTest method testCollectionFindWithBitOperation.

/* |,&,^,<<,>>,~ */
@Test
public void testCollectionFindWithBitOperation() throws Exception {
    assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.0")), "MySQL 8.0+ is required to run this test.");
    int i = 0, maxrec = 10;
    int SLen = 1;
    DbDoc doc = null;
    DbDoc[] jsonlist = new DbDocImpl[maxrec];
    for (i = 0; i < maxrec; i++) {
        DbDoc newDoc2 = new DbDocImpl();
        newDoc2.add("_id", new JsonString().setValue(String.valueOf(i + 1 + 1000)));
        newDoc2.add("F1", new JsonNumber().setValue(String.valueOf(i + 1)));
        newDoc2.add("F2", new JsonNumber().setValue(String.valueOf((int) Math.pow(2, (i + 1)))));
        newDoc2.add("F3", new JsonString().setValue(buildString(SLen + i, 'q')));
        // newDoc2.add("F3", new JsonString().setValue("?????"));
        jsonlist[i] = newDoc2;
        newDoc2 = null;
    }
    this.collection.add(jsonlist).execute();
    assertEquals((maxrec), this.collection.count());
    /* find With bitwise | Condition */
    DocResult docs = this.collection.find("CAST($.F2 as SIGNED) | pow(2,$.F1) = $.F2 ").fields("$._id as _id, $.F1 as f1, $.F2 as f2, $.F3 as f3 , $.F2 | pow(2,$.F1) as tmp").execute();
    i = 0;
    while (docs.hasNext()) {
        doc = docs.next();
        assertEquals(String.valueOf(i + 1 + 1000), (((JsonString) doc.get("_id")).getString()));
        assertEquals((long) (i + 1), (long) (((JsonNumber) doc.get("f1")).getInteger()));
        assertEquals((long) ((int) Math.pow(2, (i + 1))), (long) (((JsonNumber) doc.get("f2")).getInteger()));
        assertEquals(buildString(SLen + i, 'q'), (((JsonString) doc.get("f3")).getString()));
        assertEquals((long) Math.pow(2, (i + 1)), (long) (((JsonNumber) doc.get("tmp")).getInteger()));
        i++;
    }
    assertEquals((maxrec), i);
    /* find With bitwise & Condition */
    docs = this.collection.find("CAST($.F2 as SIGNED) & 64 ").fields("$._id as _id, $.F1 as f1, $.F2 as f2, $.F3 as f3 , $.F2 & 64 as tmp").execute();
    doc = docs.next();
    assertEquals((long) 64, (long) (((JsonNumber) doc.get("f2")).getInteger()));
    assertEquals((long) 64, (long) (((JsonNumber) doc.get("tmp")).getInteger()));
    assertFalse(docs.hasNext());
    /* find With bitwise | Condition */
    docs = this.collection.find("CAST($.F2 as SIGNED) | $.F1 = 37 ").fields("$._id as _id, $.F1 as f1, $.F2 as f2, $.F3 as f3 ,$.F2 | $.F1  as tmp").execute();
    doc = docs.next();
    assertEquals((long) 32, (long) (((JsonNumber) doc.get("f2")).getInteger()));
    assertEquals((long) 37, (long) (((JsonNumber) doc.get("tmp")).getInteger()));
    assertFalse(docs.hasNext());
    /* find With bitwise << Condition */
    docs = this.collection.find("CAST($.F2 as SIGNED) = 1<<4 ").fields("$._id as _id, $.F1 as f1, $.F2 as f2, $.F3 as f3 ,  1<<4  as tmp").execute();
    doc = docs.next();
    assertEquals((long) 16, (long) (((JsonNumber) doc.get("f2")).getInteger()));
    assertEquals((long) 16, (long) (((JsonNumber) doc.get("tmp")).getInteger()));
    assertFalse(docs.hasNext());
    /* find With bitwise >> Condition */
    docs = this.collection.find("CAST($.F2 as SIGNED) = 32>>4 ").fields("$._id as _id, $.F1 as f1, $.F2 as f2, $.F3 as f3 , 32>>4  as tmp").execute();
    doc = docs.next();
    assertEquals((long) 2, (long) (((JsonNumber) doc.get("f2")).getInteger()));
    assertEquals((long) 2, (long) (((JsonNumber) doc.get("tmp")).getInteger()));
    assertFalse(docs.hasNext());
    /* find With bitwise ^ Condition */
    docs = this.collection.find("CAST($.F2 as SIGNED) ^ 1 = 17").fields("$._id as _id,$.F2 as f2, $.F2 ^ 1 as tmp").execute();
    i = 0;
    while (docs.hasNext()) {
        doc = docs.next();
        assertEquals((long) 16, (long) (((JsonNumber) doc.get("f2")).getInteger()));
        assertEquals((long) 17, (long) (((JsonNumber) doc.get("tmp")).getInteger()));
        i++;
    }
    assertFalse(docs.hasNext());
    this.collection.add("{\"x1\":\"31\", \"x2\":\"13\", \"x3\":\"8\", \"x4\":\"18446744073709551614\"}").execute();
    /* find With bitwise ~ Condition **********FAILING************ */
    docs = this.collection.find("~16 = ~CAST($.F2 as SIGNED)").fields("$._id as _id,$.F2 as f2, ~1 as tmp").execute();
    i = 0;
    while (docs.hasNext()) {
        doc = docs.next();
        assertEquals((long) 16, (long) (((JsonNumber) doc.get("f2")).getInteger()));
        // assertEquals(17, (((JsonNumber) doc.get("tmp")).getInteger()));
        i++;
    }
    assertFalse(docs.hasNext());
}
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) Test(org.junit.jupiter.api.Test)

Example 28 with DocResult

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

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

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

the class CollectionModifyTest method testUnset.

@Test
public void testUnset() {
    if (!mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
        // Requires manual _id.
        this.collection.add("{\"_id\": \"1\", \"x\":\"100\", \"y\":\"200\", \"z\":1}").execute();
        this.collection.add("{\"_id\": \"2\", \"a\":\"100\", \"b\":\"200\", \"c\":1}").execute();
    } else {
        this.collection.add("{\"x\":\"100\", \"y\":\"200\", \"z\":1}").execute();
        this.collection.add("{\"a\":\"100\", \"b\":\"200\", \"c\":1}").execute();
    }
    this.collection.modify("true").unset("$.x").unset("$.y").execute();
    this.collection.modify("true").unset("$.a", "$.b").execute();
    DocResult res = this.collection.find().execute();
    DbDoc jd = res.next();
    assertNull(jd.get("x"));
    assertNull(jd.get("y"));
    assertNull(jd.get("a"));
    assertNull(jd.get("b"));
}
Also used : DbDoc(com.mysql.cj.xdevapi.DbDoc) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Aggregations

DocResult (com.mysql.cj.xdevapi.DocResult)102 Test (org.junit.jupiter.api.Test)99 DbDoc (com.mysql.cj.xdevapi.DbDoc)85 JsonString (com.mysql.cj.xdevapi.JsonString)76 JsonNumber (com.mysql.cj.xdevapi.JsonNumber)55 DbDocImpl (com.mysql.cj.xdevapi.DbDocImpl)52 AddResult (com.mysql.cj.xdevapi.AddResult)34 Result (com.mysql.cj.xdevapi.Result)19 JsonArray (com.mysql.cj.xdevapi.JsonArray)18 Collection (com.mysql.cj.xdevapi.Collection)14 RowResult (com.mysql.cj.xdevapi.RowResult)13 Session (com.mysql.cj.xdevapi.Session)12 Table (com.mysql.cj.xdevapi.Table)11 ExecutionException (java.util.concurrent.ExecutionException)11 SqlResult (com.mysql.cj.xdevapi.SqlResult)10 BigDecimal (java.math.BigDecimal)10 Row (com.mysql.cj.xdevapi.Row)9 SessionFactory (com.mysql.cj.xdevapi.SessionFactory)8 ArrayList (java.util.ArrayList)8 CompletableFuture (java.util.concurrent.CompletableFuture)7