Search in sources :

Example 6 with DbDoc

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

the class CollectionAddTest method testBug92264.

/**
 * Test for Bug#92264 (28594434), JSONPARSER PUTS UNNECESSARY MAXIMUM LIMIT ON JSONNUMBER TO 10 DIGITS.
 *
 * @throws Exception
 */
@Test
public void testBug92264() throws Exception {
    this.collection.add("{\"_id\":\"1\",\"dataCreated\": 1546300800000}").execute();
    DocResult docs = this.collection.find("dataCreated = 1546300800000").execute();
    assertTrue(docs.hasNext());
    DbDoc doc = docs.next();
    assertEquals("1546300800000", doc.get("dataCreated").toString());
    assertEquals(new BigDecimal("1546300800000"), ((JsonNumber) doc.get("dataCreated")).getBigDecimal());
}
Also used : DbDoc(com.mysql.cj.xdevapi.DbDoc) DocResult(com.mysql.cj.xdevapi.DocResult) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Example 7 with DbDoc

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

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

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

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

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