use of com.mysql.cj.xdevapi.DocResult in project aws-mysql-jdbc by awslabs.
the class CollectionModifyTest method testBug24471057.
/**
* Tests fix for BUG#24471057, UPDATE FAILS WHEN THE NEW VALUE IS OF TYPE DBDOC WHICH HAS ARRAY IN IT.
*
* @throws Exception
* if the test fails.
*/
@Test
public void testBug24471057() throws Exception {
String docStr = "{\"B\" : 2, \"ID\" : 1, \"KEY\" : [1]}";
if (!mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
// Inject an _id.
docStr = docStr.replace("{", "{\"_id\": \"1\", ");
}
DbDoc doc1 = JsonParser.parseDoc(new StringReader(docStr));
AddResult res = this.collection.add(doc1).execute();
this.collection.modify("ID=1").set("$.B", doc1).execute();
// expected doc
DbDoc doc2 = JsonParser.parseDoc(new StringReader(docStr));
doc2.put("B", doc1);
if (mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
doc2.put("_id", new JsonString().setValue(res.getGeneratedIds().get(0)));
}
DocResult docs = this.collection.find().execute();
DbDoc doc = docs.next();
assertEquals(doc2.toString(), doc.toString());
// DbDoc as an array member
DbDoc doc3 = JsonParser.parseDoc(new StringReader(docStr));
((JsonArray) doc1.get("KEY")).add(doc3);
this.collection.modify("ID=1").set("$.B", doc1).execute();
// expected doc
doc2.put("B", doc1);
docs = this.collection.find().execute();
doc = docs.next();
assertEquals(doc2.toString(), doc.toString());
}
use of com.mysql.cj.xdevapi.DocResult in project aws-mysql-jdbc by awslabs.
the class CollectionModifyTest method testMergePatch.
@Test
public void testMergePatch() throws Exception {
assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.3")), "MySQL 8.0.3+ is required to run this test.");
// 1. Update the name and zip code of match
this.collection.add("{\"_id\": \"1\", \"name\": \"Alice\", \"address\": {\"zip\": \"12345\", \"street\": \"32 Main str\"}}").execute();
this.collection.add("{\"_id\": \"2\", \"name\": \"Bob\", \"address\": {\"zip\": \"325226\", \"city\": \"San Francisco\", \"street\": \"42 2nd str\"}}").execute();
this.collection.modify("_id = :id").patch(JsonParser.parseDoc("{\"name\": \"Joe\", \"address\": {\"zip\":\"91234\"}}")).bind("id", "1").execute();
DocResult docs = this.collection.find().orderBy("$._id").execute();
assertTrue(docs.hasNext());
assertEquals(JsonParser.parseDoc("{\"_id\": \"1\", \"name\": \"Joe\", \"address\": {\"zip\": \"91234\", \"street\": \"32 Main str\"}}").toString(), docs.next().toString());
assertTrue(docs.hasNext());
assertEquals(JsonParser.parseDoc("{\"_id\": \"2\", \"name\": \"Bob\", \"address\": {\"zip\": \"325226\", \"city\": \"San Francisco\", \"street\": \"42 2nd str\"}}").toString(), docs.next().toString());
assertFalse(docs.hasNext());
// Delete the address field of match
this.collection.modify("_id = :id").patch("{\"address\": null}").bind("id", "1").execute();
docs = this.collection.find().orderBy("$._id").execute();
assertTrue(docs.hasNext());
assertEquals(JsonParser.parseDoc("{\"_id\": \"1\", \"name\": \"Joe\"}").toString(), docs.next().toString());
assertTrue(docs.hasNext());
assertEquals(JsonParser.parseDoc("{\"_id\": \"2\", \"name\": \"Bob\", \"address\": {\"zip\": \"325226\", \"city\": \"San Francisco\", \"street\": \"42 2nd str\"}}").toString(), docs.next().toString());
assertFalse(docs.hasNext());
String id = "a6f4b93e1a264a108393524f29546a8c";
this.collection.add(//
"{\"_id\" : \"" + id + "\"," + //
"\"title\" : \"AFRICAN EGG\"," + //
"\"description\" : \"A Fast-Paced Documentary of a Pastry Chef And a Dentist who must Pursue a Forensic Psychologist in The Gulf of Mexico\"," + //
"\"releaseyear\" : 2006," + //
"\"language\" : \"English\"," + //
"\"duration\" : 130," + //
"\"rating\" : \"G\"," + //
"\"genre\" : \"Science fiction\"," + //
"\"actors\" : [" + //
" {\"name\" : \"MILLA PECK\"," + //
" \"country\" : \"Mexico\"," + //
" \"birthdate\": \"12 Jan 1984\"}," + //
" {\"name\" : \"VAL BOLGER\"," + //
" \"country\" : \"Botswana\"," + //
" \"birthdate\": \"26 Jul 1975\" }," + //
" {\"name\" : \"SCARLETT BENING\"," + //
" \"country\" : \"Syria\"," + //
" \"birthdate\": \"16 Mar 1978\" }" + //
" ]," + //
"\"additionalinfo\" : {" + //
" \"director\" : {" + //
" \"name\": \"Sharice Legaspi\"," + //
" \"age\":57," + //
" \"awards\": [" + //
" {\"award\": \"Best Movie\"," + //
" \"movie\": \"THE EGG\"," + //
" \"year\": 2002}," + //
" {\"award\": \"Best Special Effects\"," + //
" \"movie\": \"AFRICAN EGG\"," + //
" \"year\": 2006}" + //
" ]" + //
" }," + //
" \"writers\" : [\"Rusty Couturier\", \"Angelic Orduno\", \"Carin Postell\"]," + //
" \"productioncompanies\" : [\"Qvodrill\", \"Indigoholdings\"]" + //
" }" + "}").execute();
// Adding a new field to multiple documents
this.collection.modify("language = :lang").patch("{\"translations\": [\"Spanish\"]}").bind("lang", "English").execute();
docs = this.collection.find("_id = :id").bind("id", id).limit(1).execute();
assertTrue(docs.hasNext());
DbDoc doc = docs.next();
assertNotNull(doc.get("translations"));
JsonArray arr = (JsonArray) doc.get("translations");
assertEquals(1, arr.size());
assertEquals("Spanish", ((JsonString) arr.get(0)).getString());
this.collection.modify("additionalinfo.director.name = :director").patch("{\"additionalinfo\": {\"musicby\": \"Sakila D\" }}").bind("director", "Sharice Legaspi").execute();
docs = this.collection.find("_id = :id").bind("id", id).limit(1).execute();
assertTrue(docs.hasNext());
doc = docs.next();
assertNotNull(doc.get("additionalinfo"));
DbDoc doc2 = (DbDoc) doc.get("additionalinfo");
assertNotNull(doc2.get("musicby"));
assertEquals("Sakila D", ((JsonString) doc2.get("musicby")).getString());
this.collection.modify("additionalinfo.director.name = :director").patch("{\"additionalinfo\": {\"director\": {\"country\": \"France\"}}}").bind("director", "Sharice Legaspi").execute();
docs = this.collection.find("_id = :id").bind("id", id).limit(1).execute();
assertTrue(docs.hasNext());
doc = docs.next();
assertNotNull(doc.get("additionalinfo"));
assertNotNull(((DbDoc) doc.get("additionalinfo")).get("director"));
doc2 = (DbDoc) ((DbDoc) doc.get("additionalinfo")).get("director");
assertNotNull(doc2.get("country"));
assertEquals("France", ((JsonString) doc2.get("country")).getString());
// Replacing/Updating a field's value in multiple documents
this.collection.modify("language = :lang").patch("{\"translations\": [\"Spanish\", \"Italian\"]}").bind("lang", "English").execute();
docs = this.collection.find("_id = :id").bind("id", id).limit(1).execute();
assertTrue(docs.hasNext());
doc = docs.next();
assertNotNull(doc.get("translations"));
arr = (JsonArray) doc.get("translations");
assertEquals(2, arr.size());
assertEquals("Spanish", ((JsonString) arr.get(0)).getString());
assertEquals("Italian", ((JsonString) arr.get(1)).getString());
this.collection.modify("additionalinfo.director.name = :director").patch("{\"additionalinfo\": {\"musicby\": \"The Sakila\" }}").bind("director", "Sharice Legaspi").execute();
docs = this.collection.find("_id = :id").bind("id", id).limit(1).execute();
assertTrue(docs.hasNext());
doc = docs.next();
assertNotNull(doc.get("additionalinfo"));
doc2 = (DbDoc) doc.get("additionalinfo");
assertNotNull(doc2.get("musicby"));
assertEquals("The Sakila", ((JsonString) doc2.get("musicby")).getString());
this.collection.modify("additionalinfo.director.name = :director").patch("{\"additionalinfo\": {\"director\": {\"country\": \"Canada\"}}}").bind("director", "Sharice Legaspi").execute();
docs = this.collection.find("_id = :id").bind("id", id).limit(1).execute();
assertTrue(docs.hasNext());
doc = docs.next();
assertNotNull(doc.get("additionalinfo"));
assertNotNull(((DbDoc) doc.get("additionalinfo")).get("director"));
doc2 = (DbDoc) ((DbDoc) doc.get("additionalinfo")).get("director");
assertNotNull(doc2.get("country"));
assertEquals("Canada", ((JsonString) doc2.get("country")).getString());
// Removing a field from multiple documents:
this.collection.modify("language = :lang").patch("{\"translations\": null}").bind("lang", "English").execute();
docs = this.collection.find("_id = :id").bind("id", id).limit(1).execute();
assertTrue(docs.hasNext());
doc = docs.next();
assertNull(doc.get("translations"));
this.collection.modify("additionalinfo.director.name = :director").patch("{\"additionalinfo\": {\"musicby\": null }}").bind("director", "Sharice Legaspi").execute();
docs = this.collection.find("_id = :id").bind("id", id).limit(1).execute();
assertTrue(docs.hasNext());
doc = docs.next();
assertNotNull(doc.get("additionalinfo"));
doc2 = (DbDoc) doc.get("additionalinfo");
assertNull(doc2.get("musicby"));
this.collection.modify("additionalinfo.director.name = :director").patch("{\"additionalinfo\": {\"director\": {\"country\": null}}}").bind("director", "Sharice Legaspi").execute();
docs = this.collection.find("_id = :id").bind("id", id).limit(1).execute();
assertTrue(docs.hasNext());
doc = docs.next();
assertNotNull(doc.get("additionalinfo"));
assertNotNull(((DbDoc) doc.get("additionalinfo")).get("director"));
doc2 = (DbDoc) ((DbDoc) doc.get("additionalinfo")).get("director");
assertNull(doc2.get("country"));
// Using expressions
this.collection.modify("_id = :id").patch("{\"zip\": address.zip-300000, \"street\": CONCAT($.name, '''s street: ', $.address.street)}").bind("id", "2").execute();
this.collection.modify("_id = :id").patch("{\"city\": UPPER($.address.city)}").bind("id", "2").execute();
docs = this.collection.find("_id = :id").bind("id", "2").limit(1).execute();
assertTrue(docs.hasNext());
doc = docs.next();
assertEquals(25226, ((JsonNumber) doc.get("zip")).getBigDecimal().intValue());
assertEquals("Bob's street: 42 2nd str", ((JsonString) doc.get("street")).getString());
assertEquals("SAN FRANCISCO", ((JsonString) doc.get("city")).getString());
doc2 = (DbDoc) doc.get("address");
assertNotNull(doc2);
assertEquals("325226", ((JsonString) doc2.get("zip")).getString());
assertEquals("42 2nd str", ((JsonString) doc2.get("street")).getString());
assertEquals("San Francisco", ((JsonString) doc2.get("city")).getString());
}
use of com.mysql.cj.xdevapi.DocResult in project aws-mysql-jdbc by awslabs.
the class CollectionModifyTest method testCollectionModifyBind.
@Test
@Disabled("$.F4 = 9223372036854775807 condition without quotes bind() not supported with modify.")
public void testCollectionModifyBind() throws Exception {
int i = 0, maxrec = 10, recCnt = 0;
Result res = null;
/* add(DbDoc[] docs) */
DbDoc[] jsonlist = new DbDocImpl[maxrec];
long l1 = Long.MAX_VALUE, l2 = Long.MIN_VALUE, l3 = 2147483647;
System.out.println("l = ===" + l1);
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());
/* find */
DocResult docs = this.collection.find("CAST($.F3 as SIGNED)=2147483649").fields("$._id as _id, $.F1 as f1, $.F2 as f2, $.F3+0 as f3").execute();
recCnt = count_data(docs);
assertEquals(maxrec, recCnt);
// /*
res = this.collection.modify("$.F4 = ?").set("$.F4", 1).bind(new Object[] { l2 }).sort("$.F1 asc").execute();
assertEquals(1, res.getAffectedItemsCount());
// */
}
use of com.mysql.cj.xdevapi.DocResult in project aws-mysql-jdbc by awslabs.
the class AsyncQueryTest method manyFutures.
/**
* This test addresses the "correlation" of messages to their proper async listeners.
*
* @throws Exception
*/
@Test
public void manyFutures() throws Exception {
// 100000;
int MANY = 10;
Collection coll = this.collection;
List<CompletableFuture<DocResult>> futures = new ArrayList<>();
for (int i = 0; i < MANY; ++i) {
// System.out.println("++++ Write " + i + " set " + i % 3 + " +++++");
if (i % 3 == 0) {
futures.add(coll.find("F1 like '%Field%-5'").fields("$._id as _id, $.F1 as F1, $.F2 as F2, $.F3 as F3").executeAsync());
} else if (i % 3 == 1) {
// Expecting Error
futures.add(coll.find("NON_EXISTING_FUNCTION()").fields("$._id as _id, $.F1 as F1, $.F2 as F2, $.F3 as F3").executeAsync());
} else {
futures.add(coll.find("F3 = ?").bind(106).executeAsync());
}
}
DocResult docs;
for (int i = 0; i < MANY; ++i) {
// System.out.println("++++ Read " + i + " set " + i % 3 + " +++++");
if (i % 3 == 0) {
// Expect Success and check F1 is like %Field%-5
System.out.println("\nExpect Success and check F1 is like %Field%-5");
docs = futures.get(i).get();
assertFalse(docs.hasNext());
System.out.println(docs.fetchOne());
} else if (i % 3 == 1) {
try {
// Expecting Error FUNCTION test.NON_EXISTING_FUNCTION does not exist
docs = futures.get(i).get();
fail("Expected error");
} catch (ExecutionException ex) {
XProtocolError err = (XProtocolError) ex.getCause();
assertEquals(MysqlErrorNumbers.ER_SP_DOES_NOT_EXIST, err.getErrorCode());
}
} else {
// Expect Success and check F3 is 106
System.out.println("\nExpect Success and check F3 is 106");
docs = futures.get(i).get();
assertFalse(docs.hasNext());
System.out.println(docs.fetchOne());
}
}
System.out.println("Done.");
}
use of com.mysql.cj.xdevapi.DocResult in project aws-mysql-jdbc by awslabs.
the class AsyncQueryTest method overlappedAsyncQueries.
@Test
public void overlappedAsyncQueries() throws Exception {
final int NUMBER_OF_QUERIES = 1000;
Session sess = null;
try {
sess = new SessionFactory().getSession(this.baseUrl);
Collection coll = sess.getSchema(this.schema.getName()).getCollection(this.collection.getName());
String json1 = "{'mode': 'sync'}".replaceAll("'", "\"");
String json2 = "{'mode': 'async'}".replaceAll("'", "\"");
if (!mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
// Inject an _id.
json1 = json1.replace("{", "{\"_id\": \"1\", ");
json2 = json2.replace("{", "{\"_id\": \"2\", ");
}
AddResult res = coll.add(json1).add(json2).execute();
if (mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
assertTrue(res.getGeneratedIds().get(0).matches("[a-f0-9]{28}"));
assertTrue(res.getGeneratedIds().get(1).matches("[a-f0-9]{28}"));
} else {
assertEquals(0, res.getGeneratedIds().size());
}
List<CompletableFuture<DocResult>> futures = new ArrayList<>();
for (int i = 0; i < NUMBER_OF_QUERIES; ++i) {
if (i % 5 == 0) {
futures.add(CompletableFuture.completedFuture(coll.find("mode = 'sync'").execute()));
} else {
futures.add(coll.find("mode = 'async'").executeAsync());
}
}
for (int i = 0; i < NUMBER_OF_QUERIES; ++i) {
try {
DocResult docs = futures.get(i).get();
DbDoc d = docs.next();
JsonString mode = (JsonString) d.get("mode");
if (i % 5 == 0) {
assertEquals("sync", mode.getString(), "i = " + i);
} else {
assertEquals("async", mode.getString(), "i = " + i);
}
} catch (Throwable t) {
throw new Exception("Error on i = " + i, t);
}
}
} finally {
if (sess != null) {
sess.close();
sess = null;
}
}
}
Aggregations