Search in sources :

Example 11 with DbDoc

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

Example 12 with DbDoc

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

the class CollectionFindTest method cast.

@Test
public void cast() {
    if (!mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
        this.collection.add("{\"_id\": \"1\", \"x\":100}").execute();
    } else {
        this.collection.add("{\"x\":100}").execute();
    }
    DbDoc d = this.collection.find().fields("CAST($.x as SIGNED) as x").execute().next();
    assertEquals(new Integer(100), ((JsonNumber) d.get("x")).getInteger());
}
Also used : DbDoc(com.mysql.cj.xdevapi.DbDoc) Test(org.junit.jupiter.api.Test)

Example 13 with DbDoc

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

the class CollectionFindTest method testDocumentProjection.

@Test
public void testDocumentProjection() {
    // use a document as a projection
    this.collection.add("{\"_id\":\"the_id\",\"g\":1}").execute();
    DocResult docs = this.collection.find().fields(expr("{'_id':$._id, 'q':1 + 1, 'g2':-20*$.g}")).execute();
    DbDoc doc = docs.next();
    assertEquals("the_id", ((JsonString) doc.get("_id")).getString());
    assertEquals(-20, ((JsonNumber) doc.get("g2")).getBigDecimal().intValue());
    assertEquals(new Integer(2), ((JsonNumber) doc.get("q")).getInteger());
}
Also used : DbDoc(com.mysql.cj.xdevapi.DbDoc) JsonNumber(com.mysql.cj.xdevapi.JsonNumber) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Example 14 with DbDoc

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

the class CollectionFindTest method testIterable.

/**
 * Test that {@link DocResult} implements {@link java.lang.Iterable}.
 */
@Test
public void testIterable() {
    if (!mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
        // Requires manual _id.
        this.collection.add("{\"_id\": \"1\"}").execute();
        this.collection.add("{\"_id\": \"2\"}").execute();
        this.collection.add("{\"_id\": \"3\"}").execute();
    } else {
        this.collection.add("{}").execute();
        this.collection.add("{}").execute();
        this.collection.add("{}").execute();
    }
    DocResult docs = this.collection.find().execute();
    int numDocs = 0;
    for (DbDoc d : docs) {
        if (d != null) {
            numDocs++;
        }
    }
    assertEquals(3, numDocs);
}
Also used : DbDoc(com.mysql.cj.xdevapi.DbDoc) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Example 15 with DbDoc

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

the class CollectionFindTest method testCollectionFindWithIntervalOperation.

/* interval, In, NOT IN */
@Test
public void testCollectionFindWithIntervalOperation() throws Exception {
    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("dt", new JsonString().setValue((2000 + i) + "-02-" + (i * 2 + 10)));
        newDoc2.add("dtime", new JsonString().setValue((2000 + i) + "-02-01 12:" + (i + 10) + ":01"));
        newDoc2.add("str", new JsonString().setValue(buildString(SLen + i, 'q')));
        newDoc2.add("ival", new JsonNumber().setValue(String.valueOf((int) Math.pow(2, (i + 1)))));
        if (maxrec - 1 == i) {
            newDoc2.add("ival", new JsonNumber().setValue(String.valueOf(-2147483648)));
        }
        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($.ival as SIGNED)>1 ").fields("$._id as _id, $.dt as f1, $.dtime as f2, $.str as f3 , $.ival  as f4,$.dt - interval 25 day as tmp").execute();
    i = 0;
    while (docs.hasNext()) {
        doc = docs.next();
        assertEquals(String.valueOf(i + 1 + 1000), (((JsonString) doc.get("_id")).getString()));
        assertEquals(((2000 + i) + "-02-" + (i * 2 + 10)), (((JsonString) doc.get("f1")).getString()));
        assertEquals((2000 + i) + "-02-01 12:" + (i + 10) + ":01", (((JsonString) doc.get("f2")).getString()));
        assertEquals(buildString(SLen + i, 'q'), (((JsonString) doc.get("f3")).getString()));
        assertEquals((long) ((int) Math.pow(2, (i + 1))), (long) (((JsonNumber) doc.get("f4")).getInteger()));
        i++;
    }
    assertEquals((maxrec - 1), i);
    /* find With bitwise interval Condition */
    docs = this.collection.find("$.dt + interval 6 day = '2007-03-02' ").fields("$._id as _id, $.dt as f1 ").execute();
    doc = docs.next();
    assertEquals("2007-02-24", (((JsonString) doc.get("f1")).getString()));
    assertFalse(docs.hasNext());
    docs = this.collection.find("$.dt + interval 1 month = '2006-03-22' ").fields("$._id as _id, $.dt as f1 ").execute();
    doc = docs.next();
    assertEquals("2006-02-22", (((JsonString) doc.get("f1")).getString()));
    assertFalse(docs.hasNext());
    docs = this.collection.find("$.dt + interval 1 year = '2010-02-28' ").fields("$._id as _id, $.dt as f1 ").execute();
    doc = docs.next();
    assertEquals("2009-02-28", (((JsonString) doc.get("f1")).getString()));
    assertFalse(docs.hasNext());
    docs = this.collection.find("$.dt - interval 1 year = '2008-02-28' ").fields("$._id as _id, $.dt as f1 ").execute();
    doc = docs.next();
    assertEquals("2009-02-28", (((JsonString) doc.get("f1")).getString()));
    assertFalse(docs.hasNext());
    docs = this.collection.find("$.dt - interval 25 day = '2007-01-30' ").fields("$._id as _id, $.dt as f1 ").execute();
    doc = docs.next();
    assertEquals("2007-02-24", (((JsonString) doc.get("f1")).getString()));
    assertFalse(docs.hasNext());
    /* Between */
    docs = this.collection.find("CAST($.ival as SIGNED) between 65 and 128 ").fields("$._id as _id, $.ival as f1 ").execute();
    doc = docs.next();
    assertEquals((long) 128, (long) (((JsonNumber) doc.get("f1")).getInteger()));
    assertFalse(docs.hasNext());
    docs = this.collection.find("$.dt between '2006-01-28' and '2007-02-01' ").fields("$._id as _id, $.dt as f1 ").execute();
    doc = docs.next();
    assertEquals("2006-02-22", (((JsonString) doc.get("f1")).getString()));
    assertFalse(docs.hasNext());
    docs = this.collection.find("CAST($.ival as SIGNED) <0 ").fields("$._id as _id, $.ival as f1 ").execute();
    doc = docs.next();
    assertEquals((long) -2147483648, (long) (((JsonNumber) doc.get("f1")).getInteger()));
    assertFalse(docs.hasNext());
    docs = this.collection.find("(CAST($.ival as SIGNED) between 9 and 31) or (CAST($.ival as SIGNED) between 65 and 128)").fields("$._id as _id, $.ival as f1 ").orderBy("CAST($.ival as SIGNED) asc").execute();
    doc = docs.next();
    assertEquals((long) 16, (long) (((JsonNumber) doc.get("f1")).getInteger()));
    doc = docs.next();
    assertEquals((long) 128, (long) (((JsonNumber) doc.get("f1")).getInteger()));
    assertFalse(docs.hasNext());
    docs = this.collection.find("(CAST($.ival as SIGNED) between 9 and 31) and (CAST($.ival as SIGNED) between 65 and 128)").fields("$._id as _id, $.ival as f1 ").orderBy("CAST($.ival as SIGNED)").execute();
    assertFalse(docs.hasNext());
    docs = this.collection.find("CAST($.ival as SIGNED) in (20,NULL,31.5,'17',16,CAST($.ival as SIGNED)+1) ").fields("$._id as _id, $.ival as f1 ").execute();
    doc = docs.next();
    assertEquals((long) 16, (long) (((JsonNumber) doc.get("f1")).getInteger()));
    assertFalse(docs.hasNext());
    docs = this.collection.find("(CAST($.ival as SIGNED) in (16,32,4,256,512)) and ($.dt - interval 25 day = '2007-01-30' ) and  ($.ival not in(2,4))").fields("$._id as _id, $.ival as f1 ").execute();
    doc = docs.next();
    assertEquals((long) 256, (long) (((JsonNumber) doc.get("f1")).getInteger()));
    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

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