Repository navigation
Expand file tree
/
Copy pathQueryAsData.java
More file actions
58 lines (45 loc) · 2.37 KB
/
Copy pathQueryAsData.java
File metadata and controls
58 lines (45 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package datomic.samples;
import datomic.Connection;
import datomic.Database;
import datomic.Peer;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.net.URL;
import java.util.List;
import static datomic.Peer.q;
import static datomic.Util.*;
import static datomic.samples.IO.*;
import static datomic.samples.Fns.scratchConnection;
public class QueryAsData {
public static Object tempid() {
return Peer.tempid(":db.part/user");
}
public static void main(String[] args) throws IOException {
Connection conn = scratchConnection();
URL url = resource("datomic-java-examples/social-news.edn");
transactAll(conn, new InputStreamReader(url.openStream()));
// tx data is plain lists and maps
conn.transact(
list(map(":db/id", tempid(), ":user/firstName", "Stewart", ":user/lastName", "Brand"),
map(":db/id", tempid(), ":user/firstName", "Stuart", ":user/lastName", "Smalley"),
map(":db/id", tempid(), ":user/firstName", "John", ":user/lastName", "Stewart")));
Database db = conn.db();
// Find all Stewart first names (should return 1 tuple)
System.out.println(q("[:find ?e :in $ ?name :where [?e :user/firstName ?name]]", db, "Stewart"));
// Find all Stewart or Stuart first names (should return 2 tuples)
List names = list("Stewart", "Stuart");
System.out.println(q("[:find ?e :in $ [?name ...] :where [?e :user/firstName ?name]]", db, names));
// Find all [Stewart|Stuart] [first|last] names (should return 3 tuples)
List nameAttributes = list(read(":user/firstName"), read(":user/lastName"));
System.out.println(q("[:find ?e :in $ [?name ...] [?attr ...] :where [?e ?attr ?name]]", db, names, nameAttributes));
// Build a query out of data. You might need this if e.g. writing a query optimizer.
// Do *not* do this if parameterizing inputs (as shown above) is sufficient!
List firstNameQuery = list(read(":find"), read("?e"),
read(":in"), read("$"), read("?name"),
read(":where"), list(read("?e"), read(":user/firstName"), read("?name")));
// Find all Stewart first names (should return 1 tuple)
System.out.println(q(firstNameQuery, db, "Stewart"));
}
}