Is there a library, which is able to build SPARQL queries programmatically like the CriteriaBuilder
in JPA or to build the queries like with a PreparedStateme
I recently started to use Sesame query builder. It looks promising except it doesn't provide much documentation and I struggled to find examples. Here is simple sample which may help you to get started:
ParsedTupleQuery query = QueryBuilderFactory
.select("pubProperty", "pubPropertyValue")
.group()
.atom(cmResource(resourceId), LinkPublicationsTransformation.REFERENCE_URI, "pubUri")
.atom("pubUri", "pubProperty", "pubPropertyValue")
.filter(isLiteral("pubPropertyValue"))
.closeGroup()
.query();
Just note that isLiteral
and cmResource
are my own little static helper classes. isLiteral
stands for new IsLiteral(new Var("..."))
for example where the latter one create URI with my heavily used prefix.
You might be then also interested in SPARQLQueryRenderer
which can turn ParsedQuery
into String
which may be convenient for further usage.
If you end up using String(Builder)
approach what I discourage you to do have at least a look on RenderUtils
from sesame-queryrendered
which has all the convenient methods to add <
>
around URIs, escape special characters etc.