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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use edn_derive::Serialize;
use transistor::client::Crux;
use transistor::types::Actions;
use transistor::types::{
error::CruxError,
{query::Query, CruxId},
};
#[cfg(not(feature = "async"))]
fn query() -> Result<(), CruxError> {
let crux = Database {
crux__db___id: CruxId::new("crux"),
name: "Crux Datalog".to_string(),
is_sql: false,
};
// edn_rs::to_string(crux) { :crux.db/id :crux, :name \"Crux Datalog\", :is-sql false, }
let psql = Database {
crux__db___id: CruxId::new("postgres"),
name: "Postgres".to_string(),
is_sql: true,
};
// edn_rs::to_string(psql) { :crux.db/id :postgres, :name \"Postgres\", :is-sql true, }
let mysql = Database {
crux__db___id: CruxId::new("mysql"),
name: "MySQL".to_string(),
is_sql: true,
};
// edn_rs::to_string(mysql) { :crux.db/id :mysql, :name \"MySQL\", :is-sql true, }
let client = Crux::new("localhost", "3000").http_client();
let actions = Actions::new()
.append_put(crux)
.append_put(psql)
.append_put(mysql);
// [[:crux.tx/put { :crux.db/id :crux, :name \"Crux Datalog\", :is-sql false, }],
// [:crux.tx/put { :crux.db/id :mysql, :name \"MySQL\", :is-sql true, }],
// [:crux.tx/put { :crux.db/id :postgres, :name \"Postgres\", :is-sql true, }]]
let _ = client.tx_log(actions)?;
let query_is_sql = Query::find(vec!["?p1", "?n"])?
.where_clause(vec!["?p1 :name ?n", "?p1 :is-sql true"])?
.build();
// Query:
// {:query
// {:find [?p1 ?n]
// :where [[?p1 :name ?n]
// [?p1 :is-sql true]]}}
let _ = client.query(query_is_sql?)?;
// BTreeSet{[":mysql", "MySQL"], [":postgres", "Postgres"]}
let query_is_no_sql = Query::find(vec!["?p1", "?n", "?s"])?
.where_clause(vec!["?p1 :name ?n", "?p1 :is-sql ?s", "?p1 :is-sql false"])?
.with_full_results()
.build();
// Query:
// {:query
// {:find [?p1]
// :where [[?p1 :name ?n]
// [?p1 :is-sql ?s]
// [?p1 :is-sql false]]}}
let _ = client.query(query_is_no_sql?)?;
// BTreeSet{
// ["{:crux.db/id: Key(\":cassandra\"), :is-sql: Bool(false), :name: Str(\"Cassandra\"), }", "Cassandra", "false"],
// ["{:crux.db/id: Key(\":crux\"), :is-sql: Bool(false), :name: Str(\"Crux Datalog\"), }", "Crux Datalog", "false"]}
Ok(())
}
#[cfg(not(feature = "async"))]
fn main() {
let _ = query();
}
#[test]
#[cfg(not(feature = "async"))]
fn test_query() {
query().unwrap()
}
#[derive(Debug, Clone, Serialize)]
#[allow(non_snake_case)]
pub struct Database {
crux__db___id: CruxId,
name: String,
is_sql: bool,
}