Search in sources :

Example 6 with DocResult

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

the class CollectionAddTest method testGetGeneratedIds.

@Test
public void testGetGeneratedIds() throws Exception {
    assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.0")), "MySQL 8.0+ is required to run this test.");
    AddResult res = null;
    DbDoc doc = null;
    DocResult docs = null;
    int i = 0;
    // One record using String
    String json = "{\"FLD1\":\"Data1\"}";
    res = this.collection.add(json).execute();
    List<String> docIds = res.getGeneratedIds();
    assertTrue(docIds.get(0).matches("[a-f0-9]{28}"));
    assertEquals(1, this.collection.count());
    assertEquals(1, docIds.size());
    // More than One record using String
    json = "{\"FLD1\":\"Data2\"}";
    res = this.collection.add(json).add("{}").add("{\"_id\":\"id1\"}").add("{\"FLD1\":\"Data3\"}").execute();
    docIds = res.getGeneratedIds();
    assertEquals(5, this.collection.count());
    assertEquals(3, docIds.size());
    // More than One record using String, and single add()
    json = "{\"FLD1\":\"Data15\"}";
    res = this.collection.add(json, "{}", "{\"_id\":\"id2\"}", "{\"FLD1\":\"Data16\"}").execute();
    docIds = res.getGeneratedIds();
    assertEquals(9, this.collection.count());
    assertEquals(3, docIds.size());
    // One record using DbDoc
    DbDoc newDoc2 = new DbDocImpl();
    newDoc2.add("FLD1", new JsonString().setValue("Data4"));
    res = this.collection.add(newDoc2).execute();
    docIds = res.getGeneratedIds();
    assertEquals(10, this.collection.count());
    assertEquals(1, docIds.size());
    assertTrue(docIds.get(0).matches("[a-f0-9]{28}"));
    // More Than One record using DbDoc
    newDoc2.clear();
    newDoc2.add("FLD1", new JsonString().setValue("Data5"));
    DbDoc newDoc3 = new DbDocImpl();
    newDoc3.add("FLD1", new JsonString().setValue("Data6"));
    res = this.collection.add(newDoc2).add(newDoc3).execute();
    docIds = res.getGeneratedIds();
    assertEquals(12, this.collection.count());
    assertEquals(2, docIds.size());
    assertTrue(docIds.get(0).compareTo(docIds.get(1)) < 0);
    // One record using DbDoc[]
    DbDoc[] jsonlist1 = new DbDocImpl[1];
    newDoc2.clear();
    newDoc2.add("FLD1", new JsonString().setValue("Data7"));
    jsonlist1[0] = newDoc2;
    res = this.collection.add(jsonlist1).execute();
    docIds = res.getGeneratedIds();
    assertEquals(13, this.collection.count());
    assertEquals(1, docIds.size());
    assertTrue(docIds.get(0).matches("[a-f0-9]{28}"));
    // More Than One record using DbDoc[]
    DbDoc[] jsonlist = new DbDocImpl[5];
    for (i = 0; i < 5; i++) {
        DbDoc newDoc = new DbDocImpl();
        newDoc.add("FLD1", new JsonString().setValue("Data" + (i + 8)));
        if (i % 2 == 0) {
            newDoc.add("_id", new JsonString().setValue("id-" + (i + 8)));
        }
        jsonlist[i] = newDoc;
        newDoc = null;
    }
    res = this.collection.add(jsonlist).execute();
    docIds = res.getGeneratedIds();
    assertEquals(18, this.collection.count());
    assertEquals(2, docIds.size());
    json = "{}";
    res = this.collection.add(json).execute();
    docIds = res.getGeneratedIds();
    assertTrue(docIds.get(0).matches("[a-f0-9]{28}"));
    assertEquals(19, this.collection.count());
    assertEquals(1, docIds.size());
    // Verify that when _id is provided by client, getGeneratedIds() will return empty
    res = this.collection.add("{\"_id\":\"00001273834abcdfe\",\"FLD1\":\"Data1\",\"name\":\"name1\"}", "{\"_id\":\"000012738uyie98rjdeje\",\"FLD2\":\"Data2\",\"name\":\"name1\"}", "{\"_id\":\"00001273y834uhf489fe\",\"FLD3\":\"Data3\",\"name\":\"name1\"}").execute();
    docIds = res.getGeneratedIds();
    assertEquals(22, this.collection.count());
    assertEquals(0, docIds.size());
    res = this.collection.add("{\"_id\":null,\"FLD1\":\"nulldata\"}").execute();
    docIds = res.getGeneratedIds();
    assertEquals(23, this.collection.count());
    assertEquals(0, docIds.size());
    docs = this.collection.find("$.FLD1 == 'nulldata'").execute();
    doc = docs.next();
    assertEquals("null", ((JsonLiteral) doc.get("_id")).toString());
    // Try inserting duplicate _ids. User should get error
    assertThrows(XProtocolError.class, "ERROR 5116 \\(HY000\\) Document contains a field value that is not unique but required to be", () -> this.collection.add("{\"_id\":\"abcd1234\",\"FLD1\":\"Data1\"}").add("{\"_id\":\"abcd1234\",\"FLD1\":\"Data2\"}").execute());
}
Also used : DbDoc(com.mysql.cj.xdevapi.DbDoc) DbDocImpl(com.mysql.cj.xdevapi.DbDocImpl) AddResult(com.mysql.cj.xdevapi.AddResult) 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 7 with DocResult

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

the class CollectionAddTest method testCollectionAddBasic.

@Test
public void testCollectionAddBasic() throws Exception {
    assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.0")), "MySQL 8.0+ is required to run this test.");
    int i = 0, maxrec = 100;
    /* 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 JsonString().setValue("Field-2-Data-" + i));
        newDoc2.add("F3", new JsonNumber().setValue(String.valueOf(300 + i)));
        jsonlist[i] = newDoc2;
        newDoc2 = null;
    }
    this.collection.add(jsonlist).execute();
    /* add(DbDoc doc) */
    DbDoc newDoc = new DbDocImpl();
    newDoc.add("_id", new JsonString().setValue(String.valueOf(maxrec + 1000)));
    newDoc.add("F1", new JsonString().setValue("Field-1-Data-" + maxrec));
    newDoc.add("F2", new JsonString().setValue("Field-2-Data-" + maxrec));
    newDoc.add("F3", new JsonNumber().setValue(String.valueOf(300 + maxrec)));
    this.collection.add(newDoc).execute();
    /* add(String jsonString) */
    String json = "{'_id':'" + (maxrec + 1000 + 1) + "','F1':'Field-1-Data-" + (maxrec + 1) + "','F2':'Field-2-Data-" + (maxrec + 1) + "','F3':" + (300 + maxrec + 1) + "}";
    json = json.replaceAll("'", "\"");
    this.collection.add(json).execute();
    /* No _Id Field and chained add() */
    json = "{'F1': 'Field-1-Data-9999','F2': 'Field-2-Data-9999','F3': 'Field-3-Data-9999'}".replaceAll("'", "\"");
    this.collection.add(json).add(json.replaceAll("9", "8")).execute();
    assertEquals((maxrec + 4), this.collection.count());
    DocResult docs = this.collection.find("$._id = '1000'").fields("$._id as _id, $.F1 as f1, $.F2 as f2, $.F3 as f3").execute();
    DbDoc doc = null;
    doc = docs.next();
    assertEquals("1000", ((JsonString) doc.get("_id")).getString());
    System.out.println("ID :" + ((JsonString) doc.get("_id")).getString());
    System.out.println("F1 :" + ((JsonString) doc.get("f1")).getString());
    System.out.println("F2 :" + ((JsonString) doc.get("f2")).getString());
    System.out.println("F3 :" + ((JsonNumber) doc.get("f3")).getInteger());
}
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) Test(org.junit.jupiter.api.Test)

Example 8 with DocResult

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

the class CollectionAddTest method testAddLargeDocument.

@Test
public void testAddLargeDocument() {
    int docSize = 255 * 1024;
    StringBuilder b = new StringBuilder("{\"_id\": \"large_doc\", \"large_field\":\"");
    for (int i = 0; i < docSize; ++i) {
        b.append('.');
    }
    String s = b.append("\"}").toString();
    this.collection.add(s).execute();
    DocResult docs = this.collection.find().execute();
    DbDoc d = docs.next();
    assertEquals(docSize, ((JsonString) d.get("large_field")).getString().length());
}
Also used : DbDoc(com.mysql.cj.xdevapi.DbDoc) 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 9 with DocResult

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

the class CollectionAddTest method testBasicAddDoc.

@Test
public void testBasicAddDoc() {
    DbDoc doc = this.collection.newDoc().add("firstName", new JsonString().setValue("Georgia"));
    doc.add("middleName", new JsonString().setValue("Totto"));
    doc.add("lastName", new JsonString().setValue("O'Keeffe"));
    if (!mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
        // Inject an _id.
        doc.add("_id", new JsonString().setValue("1"));
    }
    AddResult res = this.collection.add(doc).execute();
    if (mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
        assertTrue(res.getGeneratedIds().get(0).matches("[a-f0-9]{28}"));
    } else {
        assertEquals(0, res.getGeneratedIds().size());
    }
    DocResult docs = this.collection.find("lastName like 'O\\'Kee%'").execute();
    DbDoc d = docs.next();
    JsonString val = (JsonString) d.get("lastName");
    assertEquals("O'Keeffe", val.getString());
}
Also used : DbDoc(com.mysql.cj.xdevapi.DbDoc) AddResult(com.mysql.cj.xdevapi.AddResult) JsonString(com.mysql.cj.xdevapi.JsonString) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Example 10 with DocResult

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

the class CollectionFindTest method testCollectionFindSkipWarning.

@SuppressWarnings("deprecation")
@Test
public void testCollectionFindSkipWarning() throws Exception {
    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))));
        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 with order by and condition */
    docs = this.collection.find("$.F3 > 1").orderBy("CAST($.F4 as SIGNED)").execute();
    i = 0;
    while (docs.hasNext()) {
        doc = docs.next();
        i++;
        assertEquals(String.valueOf(1000 + maxrec - i), (((JsonString) doc.get("_id")).getString()));
        assertEquals((long) (10000 - maxrec + i), (long) (((JsonNumber) doc.get("F4")).getInteger()));
    }
    assertEquals(i, (maxrec - 1));
    /* find with order by and limit with condition */
    docs = this.collection.find("$._id > 1001").orderBy("CAST($.F4 as SIGNED)").limit(1).skip(2).execute();
    doc = docs.next();
    assertEquals(String.valueOf(1000 + maxrec - 3), (((JsonString) doc.get("_id")).getString()));
    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)

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