Search in sources :

Example 26 with DbDoc

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

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

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

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

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

DbDoc (com.mysql.cj.xdevapi.DbDoc)99 Test (org.junit.jupiter.api.Test)98 DocResult (com.mysql.cj.xdevapi.DocResult)87 JsonString (com.mysql.cj.xdevapi.JsonString)81 DbDocImpl (com.mysql.cj.xdevapi.DbDocImpl)56 JsonNumber (com.mysql.cj.xdevapi.JsonNumber)56 AddResult (com.mysql.cj.xdevapi.AddResult)36 Result (com.mysql.cj.xdevapi.Result)21 JsonArray (com.mysql.cj.xdevapi.JsonArray)18 RowResult (com.mysql.cj.xdevapi.RowResult)14 Table (com.mysql.cj.xdevapi.Table)12 Row (com.mysql.cj.xdevapi.Row)11 Collection (com.mysql.cj.xdevapi.Collection)10 Session (com.mysql.cj.xdevapi.Session)10 SqlResult (com.mysql.cj.xdevapi.SqlResult)9 BigDecimal (java.math.BigDecimal)9 ExecutionException (java.util.concurrent.ExecutionException)9 CompletableFuture (java.util.concurrent.CompletableFuture)6 SessionFactory (com.mysql.cj.xdevapi.SessionFactory)5 ArrayList (java.util.ArrayList)5