Java query builder

The Java query builder library can be used to programmatically construct TypeQL queries with native Java code.

To use TypeQL, we first add it as a dependency to our pom.xml.

The latest version of typeql-lang can be found in the Vaticle’s public Maven repository.

Don’t forget to replace the {version} placeholder with an exact version of the library.

<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 we import TypeQL.

import com.vaticle.typeql.lang.TypeQL;

We are now ready to construct TypeQL queries, using the methods available on the TypeQL class.

Examples

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 we should get a TypeQL query in a getQuery variable that can be used for a get query like in the following line.

Since version 2.18.0 for concept variables we should use cVar(), and for value variables — vVar().

In version prior to 2.18.0 the var() was used instead of cVar() as there were no value variables.

readTransaction.query().get(getQuery)

The result should be the same as if we set getQuery variable as a TypeQL string.

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;";

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);

Example 3: A get query with value variables

TypeQLGet query = match(
        cVar("x").isa("triangle").has("base", cVar("b")).has("height", cVar("h")),
        vVar("area").assign(Expression.constant(0.5).mul(cVar("b").mul(cVar("h"))))))
);

Example 4: An insert query

The following example showcases the usage of insert query.

insertQuery = TypeQL.match(
        cVar("f").isa("file").has("path", filepath),
        cVar("vav").isa("action").has("name", "view_file")
)
.insert(cVar("pa").rel(cVar("vav")).rel(cVar("f")).isa("access"));

Provide Feedback