use of com.mysql.cj.xdevapi.Result in project aws-mysql-jdbc by awslabs.
the class CollectionFindTest method testCollectionFindInDelete.
@SuppressWarnings("deprecation")
@Test
public void testCollectionFindInDelete() throws Exception {
assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.0")), "MySQL 8.0+ is required to run this test.");
int i = 0, maxrec = 10;
DocResult docs = null;
Result res = null;
/* 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 JsonNumber().setValue(String.valueOf(10 * (i + 1) + 0.1234)));
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 without Condition */
docs = this.collection.find().fields("$._id as _id, $.F1 as f1, $.F2 as f2, $.F3 as f3,$.F2/10 as tmp1,1/2 as tmp2").orderBy("$.F3").execute();
i = 0;
while (docs.hasNext()) {
docs.next();
i++;
}
assertEquals((maxrec), i);
/* remove with single IN */
res = this.collection.remove("'1001' in $._id").execute();
assertEquals(1, res.getAffectedItemsCount());
docs = this.collection.find("'1001' in $._id").execute();
assertFalse(docs.hasNext());
/* remove with mulltiple IN */
String findCond = "";
for (i = 1; i < maxrec; i++) {
findCond = findCond + "'";
findCond = findCond + String.valueOf(i + 1000) + "' not in $._id";
if (i != maxrec - 1) {
findCond = findCond + " and ";
}
}
res = this.collection.remove(findCond).execute();
assertEquals(1, res.getAffectedItemsCount());
docs = this.collection.find(findCond).execute();
assertFalse(docs.hasNext());
/* remove with single IN */
res = this.collection.remove("10004 in $.F4").orderBy("CAST($.F4 as SIGNED)").execute();
assertEquals(1, res.getAffectedItemsCount());
docs = this.collection.find("10004 in $.F4").orderBy("CAST($.F4 as SIGNED)").execute();
assertFalse(docs.hasNext());
/* remove with single IN for float */
res = this.collection.remove("30.1234 in $.F2").orderBy("CAST($.F4 as SIGNED)").execute();
assertEquals(1, res.getAffectedItemsCount());
docs = this.collection.find("30.1234 in $.F2").orderBy("CAST($.F4 as SIGNED)").execute();
assertFalse(docs.hasNext());
res = this.collection.remove("true").execute();
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) + 0.1234)));
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());
Table table = this.schema.getCollectionAsTable(this.collectionName);
/* delete with single IN */
res = table.delete().where("'1001' in doc->$._id").execute();
assertEquals(res.getAffectedItemsCount(), 1);
RowResult rows = table.select("doc->$.F1 as _id").where("'1001' in doc->$._id").execute();
assertFalse(rows.hasNext());
/* delete with multiple IN */
findCond = "";
for (i = 1; i < maxrec; i++) {
findCond = findCond + "'";
findCond = findCond + String.valueOf(i + 1000) + "' not in doc->$._id";
if (i != maxrec - 1) {
findCond = findCond + " and ";
}
}
res = table.delete().where(findCond).execute();
assertEquals(res.getAffectedItemsCount(), 1);
rows = table.select("doc->$.F1 as _id").where(findCond).execute();
assertFalse(rows.hasNext());
/* delete with single IN for float */
res = table.delete().where("30.1234 in doc->$.F2").execute();
assertEquals(res.getAffectedItemsCount(), 1);
rows = table.select("doc->$.F1 as F1").where("30.1234 in doc->$.F2").execute();
assertFalse(rows.hasNext());
/* delete with single IN for int */
res = table.delete().where("10004 in doc->$.F4").execute();
assertEquals(res.getAffectedItemsCount(), 1);
rows = table.select("doc->$.F1 as F1").where("10004 in doc->$.F4").execute();
assertFalse(rows.hasNext());
}
use of com.mysql.cj.xdevapi.Result in project aws-mysql-jdbc by awslabs.
the class CollectionFindTest method testCollRemoveTabDeleteWithOverlaps.
@SuppressWarnings("deprecation")
@Test
public void testCollRemoveTabDeleteWithOverlaps() throws Exception {
assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.0")), "MySQL 8.0+ is required to run this test.");
int i = 0, maxrec = 10;
DocResult docs = null;
Result res = null;
/* 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 JsonNumber().setValue(String.valueOf(10 * (i + 1) + 0.1234)));
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 without Condition */
docs = this.collection.find().fields("$._id as _id, $.F1 as f1, $.F2 as f2, $.F3 as f3,$.F2/10 as tmp1,1/2 as tmp2").orderBy("$.F3").execute();
i = 0;
while (docs.hasNext()) {
docs.next();
i++;
}
assertEquals((maxrec), i);
/* remove with single OVERLAPS */
res = this.collection.remove("'1001' overlaps $._id").execute();
assertEquals(1, res.getAffectedItemsCount());
docs = this.collection.find("'1001' overlaps $._id").execute();
assertFalse(docs.hasNext());
/* remove with mulltiple OVERLAPS */
String findCond = "";
for (i = 1; i < maxrec; i++) {
findCond = findCond + "'";
findCond = findCond + String.valueOf(i + 1000) + "' not overlaps $._id";
if (i != maxrec - 1) {
findCond = findCond + " and ";
}
}
res = this.collection.remove(findCond).execute();
assertEquals(1, res.getAffectedItemsCount());
docs = this.collection.find(findCond).execute();
assertFalse(docs.hasNext());
/* remove with single OVERLAPS */
res = this.collection.remove("10004 overlaps $.F4").orderBy("CAST($.F4 as SIGNED)").execute();
assertEquals(1, res.getAffectedItemsCount());
docs = this.collection.find("10004 overlaps $.F4").orderBy("CAST($.F4 as SIGNED)").execute();
assertFalse(docs.hasNext());
/* remove with single OVERLAPS for float */
res = this.collection.remove("30.1234 overlaps $.F2").orderBy("CAST($.F4 as SIGNED)").execute();
assertEquals(1, res.getAffectedItemsCount());
docs = this.collection.find("30.1234 overlaps $.F2").orderBy("CAST($.F4 as SIGNED)").execute();
assertFalse(docs.hasNext());
res = this.collection.remove("true").execute();
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) + 0.1234)));
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());
Table table = this.schema.getCollectionAsTable(this.collectionName);
/* delete with single OVERLAPS */
res = table.delete().where("'1001' overlaps doc->$._id").execute();
assertEquals(res.getAffectedItemsCount(), 1);
RowResult rows = table.select("doc->$.F1 as _id").where("'1001' overlaps doc->$._id").execute();
assertFalse(rows.hasNext());
/* delete with multiple OVERLAPS */
findCond = "";
for (i = 1; i < maxrec; i++) {
findCond = findCond + "'";
findCond = findCond + String.valueOf(i + 1000) + "' not overlaps doc->$._id";
if (i != maxrec - 1) {
findCond = findCond + " and ";
}
}
res = table.delete().where(findCond).execute();
assertEquals(res.getAffectedItemsCount(), 1);
rows = table.select("doc->$.F1 as _id").where(findCond).execute();
assertFalse(rows.hasNext());
/* delete with single OVERLAPS for float */
res = table.delete().where("30.1234 overlaps doc->$.F2").execute();
assertEquals(res.getAffectedItemsCount(), 1);
rows = table.select("doc->$.F1 as F1").where("30.1234 overlaps doc->$.F2").execute();
assertFalse(rows.hasNext());
/* delete with single OVERLAPS for int */
res = table.delete().where("10004 overlaps doc->$.F4").execute();
assertEquals(res.getAffectedItemsCount(), 1);
rows = table.select("doc->$.F1 as F1").where("10004 overlaps doc->$.F4").execute();
assertFalse(rows.hasNext());
}
use of com.mysql.cj.xdevapi.Result 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));
}
use of com.mysql.cj.xdevapi.Result 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());
}
use of com.mysql.cj.xdevapi.Result in project aws-mysql-jdbc by awslabs.
the class CollectionModifyTest method testCollectionModifyAsyncMany.
@SuppressWarnings("unchecked")
@Test
public void testCollectionModifyAsyncMany() throws Exception {
int i = 0, maxrec = 10;
int NUMBER_OF_QUERIES = 1000;
DbDoc doc = null;
AddResult res = null;
Result res2 = null;
CompletableFuture<AddResult> asyncRes = null;
CompletableFuture<DocResult> asyncDocs = null;
DocResult docs = null;
double d = 100.123;
/* add().executeAsync() maxrec num of records */
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(d + i)));
newDoc2.add("F3", new JsonNumber().setValue(String.valueOf(1000 + i)));
newDoc2.add("T", new JsonNumber().setValue(String.valueOf(i)));
asyncRes = this.collection.add(newDoc2).executeAsync();
res = asyncRes.get();
assertEquals(1, res.getAffectedItemsCount());
newDoc2 = null;
}
assertEquals((maxrec), this.collection.count());
List<Object> futures = new ArrayList<>();
for (i = 0; i < NUMBER_OF_QUERIES; ++i) {
if (i % 3 == 0) {
futures.add(this.collection.modify("$.F3 % 2 = 0 ").change("$.T", expr("1000000+" + i)).sort("$.F3 desc").executeAsync());
} else if (i % 3 == 1) {
// Error
futures.add(this.collection.modify("$.F3 = " + (1000 + i)).change("$.T", expr("NON_EXISTING_FUNCTION()")).sort("$.F3 desc").executeAsync());
} else {
futures.add(this.collection.modify("$.F3 % 2 = 1 ").change("$.T", expr("$.F3+" + i)).sort("$.F3 desc").executeAsync());
}
}
for (i = 0; i < NUMBER_OF_QUERIES; ++i) {
if (i % 3 == 0) {
res2 = ((CompletableFuture<AddResult>) futures.get(i)).get();
assertEquals((maxrec) / 2, res2.getAffectedItemsCount());
} else if (i % 3 == 1) {
int i1 = i;
assertThrows(ExecutionException.class, ".*FUNCTION " + this.schema.getName() + ".NON_EXISTING_FUNCTION does not exist.*", () -> ((CompletableFuture<Result>) futures.get(i1)).get());
} else {
res2 = ((CompletableFuture<Result>) futures.get(i)).get();
assertEquals((maxrec) / 2, res2.getAffectedItemsCount());
}
}
asyncDocs = this.collection.find("$.T > :X ").bind("X", 1000000).fields(expr("{'cnt':count($.T)}")).executeAsync();
docs = asyncDocs.get();
doc = docs.next();
assertEquals((long) (maxrec) / 2, (long) (((JsonNumber) doc.get("cnt")).getInteger()));
asyncDocs = this.collection.find("$.T > :X and $.T < :Y").bind("X", 1000).bind("Y", 1000000).fields(expr("{'cnt':count($.T)}")).executeAsync();
docs = asyncDocs.get();
doc = docs.next();
assertEquals((long) (maxrec) / 2, (long) (((JsonNumber) doc.get("cnt")).getInteger()));
}
Aggregations