Java query builder
The Java query builder library can be used to programmatically construct TypeQL queries with native Java code.
Import
To use TypeQL, add it as a dependency to the pom.xml
file.
The latest version of the typeql-lang
can be found in the
Vaticle’s public Maven repository.
Replace the |
<repositories>
<repository>
<id>repo.typedb.com</id>
<url>https://repo.typedb.com/public/public-release/maven/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.vaticle.typeql</groupId>
<artifactId>typeql-lang</artifactId>
<version>{version}</version>
</dependency>
</dependencies>
Then, import TypeQL
in the source code:
import com.vaticle.typeql.lang.TypeQL;
You are now ready to construct TypeQL queries, using the methods available for the TypeQL
class.
Usage
Using the Java query builder library is quite simple as it produces a TypeQL string. See the examples below.
Example 1: A get query
TypeQLGet getQuery = TypeQL.match(
cVar("u").isa("user").has("full-name", "Kevin Morrison"),
cVar("p").rel(cVar("u")).rel(cVar("pa")).isa("permission"),
cVar("o").isa("object").has("path", cVar("fp")),
cVar("pa").rel(cVar("o")).rel(cVar("va")).isa("access")
).get(cVar("fp"));
As the result of the above example you should get a TypeQL query string in a getQuery
variable that can be used for a
get query.
The resulted string should be similar to the following:
String getQuery = "match $u isa user, has full-name 'Kevin Morrison'; $p($u, $pa) isa permission; " +
"$o isa object, has path $fp; $pa($o, $va) isa access; get $fp;";
In versions prior to |
Send the query:
readTransaction.query().get(getQuery);
Example 2: A get query with additional parameters
The following example showcases the usage of sorting, offsetting and limiting a get query.
TypeQLGet getQuery = TypeQL.match(
cVar("u").isa("user").has("full-name", "Kevin Morrison"),
cVar("p").rel(cVar("u")).rel(cVar("pa")).isa("permission"),
cVar("o").isa("object").has("path", cVar("fp")),
cVar("pa").rel(cVar("o")).rel(cVar("va")).isa("access"),
cVar("va").isa("action").has("name", "view_file")
).get(cVar("fp")).sort(cVar("fp")).offset(0).limit(5);