alisson-gomesc / jkniv

Powering your database/repositories with Java code agnostic from JDBC and SQL
http://jkniv.sf.net
GNU Lesser General Public License v2.1
4 stars 0 forks source link

Question: escaping double quotes #3

Closed julianobrasil closed 3 years ago

julianobrasil commented 5 years ago

Is there a way I can send an entire mango query to the xml file? I was thinking of doing this:

<select id="generic-query" returnType="my.domain.MyEntity">
 :query
</select>

and then, in java code, send the entire query through the query key. But I couldn't figure out how to right double quotes to the query variable without messing with the final generated mango json query.

If I do something like:

  QueryFactory.of("generic-query","query","selector: {" + .... + "}");

I end up with

{
  selector: {  // <= no mandatory double quotes :(
    ...
 }

Because the above json attributes are written without any double quotes (malformed), couchdb doesn't execute the query.

If I do something like (trying to escape the double quotes in Java):

  QueryFactory.of("generic-query", "query", "{\"selector\": {" + .... + "}");

I end up with the following json:

{
  \"selector\": {  // <= the backslash wasn't supposed to be written here
    ...
 }

Actually in the project I was just trying out jkniv possibilities, as it could open my way to build a fluent QueryBuilder, for exemple. (In the real project, I can work it around by writing specific queries in the xml file).

alisson-gomesc commented 5 years ago

One from main philosophy whinstone is keep SQL away from java, but for whinstone-couchdb there is one way to do that.

I didn't try but you need create a Selector Class that represents Request JSON Object

public class SelectorSample {

@JsonProperty("selector")
private Selector selector;
@JsonProperty("fields")
private List<String> fields = null;
@JsonProperty("sort")
private List<Sort> sort = null;
@JsonProperty("limit")
private Integer limit;
@JsonProperty("skip")
private Integer skip;
@JsonProperty("execution_stats")
private Boolean executionStats;

Your XML

    <select id="generic-query" returnType="my.domain.MyEntity">
     :query
    </select>

Your java:

SelectorSample selector = new SelectorSample();
QueryFactory.of("generic-query","query", selector);

whinstone-couchdb parse your object parameters using jackson

String jsonBody = new ObjectMapper().readValue(selector);

The jsonBody beccome HTTP body. So your SelectorSample class must be very smart to represent the attribute selector couchdb request JSON object with your operators and combination of operators like $eq, $gt, $gte, $lt, $lte, $elemMatch, $allMatch etc.

julianobrasil commented 5 years ago

Thanks. I'll test it and let you know the result.