Skip to content

Commit

Permalink
N1QL DSL WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ygree committed Jul 9, 2018
1 parent add5fb2 commit e9b43d8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import com.couchbase.client.java.document.json.JsonObject;
import com.couchbase.client.java.query.N1qlQuery;
import com.couchbase.client.java.query.ParameterizedN1qlQuery;
import com.couchbase.client.java.query.dsl.Expression;
import com.couchbase.client.java.query.dsl.functions.ConditionalFunctions;
import com.couchbase.client.java.query.dsl.path.UpdateSetPath;
import com.lightbend.couchbase.Couchbase;
import rx.Observable;
import utils.RxJava8Utils;
Expand All @@ -16,6 +19,12 @@
import java.util.Optional;
import java.util.concurrent.CompletionStage;

import static com.couchbase.client.java.query.Update.update;
import static com.couchbase.client.java.query.dsl.Expression.*;
import static com.couchbase.client.java.query.dsl.functions.ArrayFunctions.arrayIfNull;
import static com.couchbase.client.java.query.dsl.functions.ArrayFunctions.arrayPrepend;
import static com.couchbase.client.java.query.dsl.functions.ConditionalFunctions.*;

@Singleton
public class ReadSideRepository {

Expand All @@ -39,10 +48,19 @@ public CompletionStage<Done> updateMessage(String name, String message) {
String queryText = "UPDATE test USE KEYS $1 SET messages = ARRAY_PREPEND($2, IFNULL(messages, [])), message = $2;";
ParameterizedN1qlQuery query = N1qlQuery.parameterized(queryText, JsonArray.from(docId, message));

/* TODO: it doesn't translates into a correct query
UpdateSetPath updateSetPath =
update("test")
.useKeys(docId)
.set("message", message)
.set("messages", arrayPrepend(x(message), ifNull(x("messages"), x(JsonArray.empty()))));
*/

Observable<Done> result = bucket
.flatMap(b -> b.insert(doc).map(x -> b))
.onExceptionResumeNext(bucket)
.flatMap(b -> b.query(query))
// .flatMap(b -> b.query(updateSetPath))
.map(v -> Done.getInstance());

return RxJava8Utils.fromSingleObservable(result);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.lightbend.readside.impl;

import com.couchbase.client.java.document.json.JsonArray;
import com.couchbase.client.java.query.dsl.path.UpdateSetPath;
import org.junit.Assert;
import org.junit.Test;

import static com.couchbase.client.java.query.Update.update;
import static com.couchbase.client.java.query.dsl.Expression.x;
import static com.couchbase.client.java.query.dsl.functions.ArrayFunctions.arrayPrepend;
import static com.couchbase.client.java.query.dsl.functions.ConditionalFunctions.ifNull;

public class N1qlDslTests {

@Test
public void test() {

UpdateSetPath updateSetPath =
update("test")
.useKeys("$1")
.set("messages", arrayPrepend(ifNull(x("messages"), x(JsonArray.empty())), x("$2")))
.set("message", "$2");


Assert.assertEquals("UPDATE test USE KEYS $1 SET messages = ARRAY_PREPEND($2, IFNULL(messages, [])), message = $2;", updateSetPath.toString());

}
}

0 comments on commit e9b43d8

Please sign in to comment.