0

I want to clear the where clause values in a selectBuilder object, but I cannot get it work.

import org.apache.jena.arq.querybuilder.SelectBuilder;

public class TestClearWhere {
    public static void main(String[] args) {
        SelectBuilder sb = new SelectBuilder();
        sb.addVar("r").addVar("unit").addVar("time").addVar("value").addVar("interest");
        sb.addWhere("?r", "a", "sao:Point");
        System.out.println("Before clear\n" + sb.toString());
        sb.clearWhereValues(); // ?
        sb.clearValues(); // ? clearValues neither works
        System.out.println("After clear\n" + sb.buildString());
    }
}

Output

Before clear
SELECT  ?r ?unit ?time ?value ?interest
WHERE
  { ?r  a                     "sao:Point"}

After clear
SELECT  ?r ?unit ?time ?value ?interest
WHERE
  { ?r  a                     "sao:Point"}

Please let me know what I did wrong.

soscler
  • 227
  • 4
  • 4
  • 1
    You're not doing anything wrong but misunderstanding that the clear methods only work on `VALUES` clause, e.g. if you do `sb..addValueVar("?v", "val1")` you'll add inline data to the query `SELECT ... WHERE {} VALUES ?v { "val1" }` and this will be remove with the `clear()` (one is for having `VALUES` inside the other for outside the `WHERE` clause) methods. What you want is just create a new builder as there is no reset method. – UninformedUser Nov 29 '19 at 07:55
  • Thank you! yeah I misunderstood the method. So I solved my problem by creating new selectBuilder object whenever I want to use one. It really would be nice to have an actual clearWhereStatement method that clear the where statement. It will make it easier to use it as a singleton and also for Dependency Injection. Or a better option will be to automatically reset the request statement and its parameters (so you don't end up creating an object for every request). – soscler Dec 07 '19 at 23:43
  • well, you can at least use the `clone()` method which copies the current state. So initially, you could add some prefixes and all the common stuff, then clone it, then add the WHERE part each time. It's also recommended here for template usage: https://jena.apache.org/documentation/extras/querybuilder/index.html – UninformedUser Dec 08 '19 at 04:14

0 Answers0