Search in sources :

Example 1 with DocResult

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

the class DevApiSample method documentWalkthrough.

public static void documentWalkthrough(Schema schema) {
    // document walthrough
    Collection coll = schema.createCollection("myBooks", /* reuseExisting? */
    true);
    DbDoc newDoc = new DbDocImpl().add("isbn", new JsonString().setValue("12345"));
    newDoc.add("title", new JsonString().setValue("Effi Briest"));
    newDoc.add("author", new JsonString().setValue("Theodor Fontane"));
    newDoc.add("currentlyReadingPage", new JsonNumber().setValue(String.valueOf(42)));
    coll.add(newDoc).execute();
    // note: "$" prefix for document paths is optional. "$.title.somethingElse[0]" is the same as "title.somethingElse[0]" in document expressions
    DocResult docs = coll.find("$.title = 'Effi Briest' and $.currentlyReadingPage > 10").execute();
    DbDoc book = docs.next();
    System.err.println("Currently reading " + ((JsonString) book.get("title")).getString() + " on page " + ((JsonNumber) book.get("currentlyReadingPage")).getInteger());
    // increment the page number and fetch it again
    coll.modify("$.isbn = 12345").set("$.currentlyReadingPage", ((JsonNumber) book.get("currentlyReadingPage")).getInteger() + 1).execute();
    docs = coll.find("$.title = 'Effi Briest' and $.currentlyReadingPage > 10").execute();
    book = docs.next();
    System.err.println("Currently reading " + ((JsonString) book.get("title")).getString() + " on page " + ((JsonNumber) book.get("currentlyReadingPage")).getInteger());
    // remove the doc
    coll.remove("true").execute();
    System.err.println("Number of books in collection: " + coll.count());
    schema.dropCollection(coll.getName());
}
Also used : DbDoc(com.mysql.cj.xdevapi.DbDoc) DbDocImpl(com.mysql.cj.xdevapi.DbDocImpl) JsonNumber(com.mysql.cj.xdevapi.JsonNumber) Collection(com.mysql.cj.xdevapi.Collection) JsonString(com.mysql.cj.xdevapi.JsonString) DocResult(com.mysql.cj.xdevapi.DocResult)

Example 2 with DocResult

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

the class SessionTest method testBug97269.

/**
 * Test fix for Bug#97269 (30438500), POSSIBLE BUG IN COM.MYSQL.CJ.XDEVAPI.STREAMINGDOCRESULTBUILDER.
 *
 * @throws Exception
 */
@Test
public void testBug97269() throws Exception {
    Session sess = null;
    try {
        String message1 = "W1";
        String message2 = "W2";
        // create notice message buffers
        Frame.Builder notice1 = Frame.newBuilder().setScope(Frame.Scope.LOCAL).setType(Frame.Type.WARNING_VALUE).setPayload(com.mysql.cj.x.protobuf.MysqlxNotice.Warning.newBuilder().setCode(MysqlErrorNumbers.ER_BAD_DB_ERROR).setMsg(message1).build().toByteString());
        Frame.Builder notice2 = Frame.newBuilder().setScope(Frame.Scope.GLOBAL).setType(Frame.Type.WARNING_VALUE).setPayload(com.mysql.cj.x.protobuf.MysqlxNotice.Warning.newBuilder().setCode(MysqlErrorNumbers.ER_BAD_DB_ERROR).setMsg(message2).build().toByteString());
        byte[] notice1Bytes = makeNoticeBytes(notice1.build());
        byte[] notice2Bytes = makeNoticeBytes(notice2.build());
        int size = notice1Bytes.length + notice2Bytes.length;
        byte[] noticesBytes = new byte[size];
        System.arraycopy(notice1Bytes, 0, noticesBytes, 0, notice1Bytes.length);
        System.arraycopy(notice2Bytes, 0, noticesBytes, notice1Bytes.length, notice2Bytes.length);
        InjectedSocketFactory.flushAllStaticData();
        String url = this.baseUrl + (this.baseUrl.contains("?") ? "" : "?") + makeParam(PropertyKey.socketFactory, InjectedSocketFactory.class.getName(), !this.baseUrl.contains("?") || this.baseUrl.endsWith("?")) + makeParam(PropertyKey.xdevapiSslMode, XdevapiSslMode.DISABLED.toString()) + makeParam(PropertyKey.xdevapiCompression, Compression.DISABLED.toString()) + // to allow injection between result rows
        makeParam(PropertyKey.useReadAheadInput, "false");
        sess = this.fact.getSession(url);
        SocketFactory sf = ((SessionImpl) sess).getSession().getProtocol().getSocketConnection().getSocketFactory();
        assertTrue(InjectedSocketFactory.class.isAssignableFrom(sf.getClass()));
        Collection collection = sess.getDefaultSchema().createCollection("testBug97269");
        collection.add("{\"_id\":\"the_id\",\"g\":1}").execute();
        // StreamingDocResultBuilder
        InjectedSocketFactory.injectedBuffer = noticesBytes;
        DocResult docs = collection.find().fields("$._id as _id, $.g as g, 1 + 1 as q").execute();
        DbDoc doc = docs.next();
        assertEquals("the_id", ((JsonString) doc.get("_id")).getString());
        assertEquals(new Integer(1), ((JsonNumber) doc.get("g")).getInteger());
        assertEquals(new Integer(2), ((JsonNumber) doc.get("q")).getInteger());
        int cnt = 0;
        for (Iterator<Warning> warn = docs.getWarnings(); warn.hasNext(); ) {
            Warning w = warn.next();
            if (w.getMessage().equals(message1) || w.getMessage().equals(message2)) {
                cnt++;
            }
        }
        assertEquals(2, cnt);
        InjectedSocketFactory.flushAllStaticData();
        InjectedSocketFactory.injectedBuffer = noticesBytes;
        SqlResult rs1 = sess.sql("select 1").execute();
        assertEquals(1, rs1.fetchOne().getInt(0));
        cnt = 0;
        for (Iterator<Warning> warn = rs1.getWarnings(); warn.hasNext(); ) {
            Warning w = warn.next();
            if (w.getMessage().equals(message1) || w.getMessage().equals(message2)) {
                cnt++;
            }
        }
        assertEquals(2, cnt);
    } finally {
        InjectedSocketFactory.flushAllStaticData();
        dropCollection("testBug97269");
        if (sess != null) {
            sess.close();
        }
    }
}
Also used : Frame(com.mysql.cj.x.protobuf.MysqlxNotice.Frame) Warning(com.mysql.cj.xdevapi.Warning) SqlResult(com.mysql.cj.xdevapi.SqlResult) SocketFactory(com.mysql.cj.protocol.SocketFactory) InjectedSocketFactory(testsuite.InjectedSocketFactory) UnreliableSocketFactory(testsuite.UnreliableSocketFactory) JsonString(com.mysql.cj.xdevapi.JsonString) DbDoc(com.mysql.cj.xdevapi.DbDoc) InjectedSocketFactory(testsuite.InjectedSocketFactory) Collection(com.mysql.cj.xdevapi.Collection) DocResult(com.mysql.cj.xdevapi.DocResult) CoreSession(com.mysql.cj.CoreSession) Session(com.mysql.cj.xdevapi.Session) Test(org.junit.jupiter.api.Test)

Example 3 with DocResult

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

the class CollectionAddTest method testBasicAddMap.

@Test
@Disabled("Collection.add(Map<String, ?> doc) is not implemented yet.")
public void testBasicAddMap() {
    Map<String, Object> doc = new HashMap<>();
    doc.put("x", 1);
    doc.put("y", "this is y");
    doc.put("z", new BigDecimal("44.22"));
    AddResult res = this.collection.add(doc).execute();
    assertTrue(res.getGeneratedIds().get(0).matches("[a-f0-9]{28}"));
    DocResult docs = this.collection.find("z >= 44.22").execute();
    DbDoc d = docs.next();
    JsonString val = (JsonString) d.get("y");
    assertEquals("this is y", val.getString());
}
Also used : DbDoc(com.mysql.cj.xdevapi.DbDoc) HashMap(java.util.HashMap) AddResult(com.mysql.cj.xdevapi.AddResult) JsonString(com.mysql.cj.xdevapi.JsonString) JsonString(com.mysql.cj.xdevapi.JsonString) BigDecimal(java.math.BigDecimal) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 4 with DocResult

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

the class CollectionAddTest method testCollectionAddBigKeyData.

@Test
public void testCollectionAddBigKeyData() throws Exception {
    int i = 0, j = 0;
    int maxkey = 10;
    int maxrec = 5;
    int keylen = (10);
    int datalen = (1 * 5);
    String key_sub = buildString(keylen, 'X');
    String data_sub = buildString(datalen, 'X');
    /* Insert maxrec records with maxkey (key,value) pairs with key length=keylen and datalength=datalen */
    String key, data, query;
    for (i = 0; i < maxrec; i++) {
        DbDoc newDoc = new DbDocImpl();
        newDoc.add("_id", new JsonNumber().setValue(String.valueOf(i)));
        for (j = 0; j < maxkey; j++) {
            key = key_sub + j;
            data = data_sub + j;
            newDoc.add(key, new JsonString().setValue(data));
        }
        this.collection.add(newDoc).execute();
        newDoc = null;
    }
    assertEquals((maxrec), this.collection.count());
    /* Fetch all keys */
    query = "$._id as _id";
    for (j = 0; j < maxkey; j++) {
        key = key_sub + j;
        query = query + ",$." + key + " as " + key;
    }
    DocResult docs = this.collection.find().orderBy("$._id").fields(query).execute();
    DbDoc doc = null;
    i = 0;
    while (docs.hasNext()) {
        doc = docs.next();
        for (j = 0; j < maxkey; j++) {
            key = key_sub + j;
            data = data_sub + j;
            assertEquals((data), ((JsonString) doc.get(key)).getString());
        }
        i++;
    }
    assertEquals((maxrec), i);
}
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 5 with DocResult

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

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