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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//! `SHOW DATABASES` and `SHOW`/`TERMINATE TRANSACTIONS` row producers.
use std::collections::BTreeMap;
use std::sync::Arc;
use relate_core::{Error, Result, Value};
use relate_parser::{DatabaseFilter, SchemaName};
use crate::ctx::QueryCtx;
use crate::eval::eval;
use crate::overlay::ExecGraph;
use crate::row::Row;
use super::super::catalog::catalog_row;
pub(crate) fn show_transactions_rows(
graph: &ExecGraph<'_>,
c: &relate_parser::ShowTransactionsClause,
rows: &[Row],
params: &BTreeMap<String, Value>,
qctx: QueryCtx<'_>,
) -> Result<Vec<Row>> {
Ok({
let txid = "neo4j-transaction-1".to_string();
let mut found = c.ids.is_empty();
for expr in &c.ids {
let expr = relate_front::lower_expr(expr)?;
for row in rows {
let value = eval(graph, row, &expr, params, qctx.cypher_version)?;
if value.is_null() && qctx.is_cypher5() {
return Err(Error::invalid_argument_type(
"transaction id must be STRING or LIST<STRING>",
));
}
found |= match value {
Value::String(ref s) => **s == *txid,
Value::List(ref xs) => xs
.iter()
.any(|v| matches!(v, Value::String(s) if **s == *txid)),
_ => false,
};
}
}
if found {
vec![catalog_row([
("transactionId", Value::String(txid.into())),
("currentQuery", Value::String(qctx.query_text.into())),
("database", Value::String("neo4j".into())),
])]
} else {
Vec::new()
}
})
}
pub(crate) fn terminate_transactions_rows(
graph: &ExecGraph<'_>,
c: &relate_parser::TerminateTransactionsClause,
rows: &[Row],
params: &BTreeMap<String, Value>,
qctx: QueryCtx<'_>,
) -> Result<Vec<Row>> {
Ok({
let mut out = Vec::new();
for expr in &c.ids {
let expr = relate_front::lower_expr(expr)?;
for outer in rows {
let values = match eval(graph, outer, &expr, params, qctx.cypher_version)? {
Value::Null if qctx.is_cypher5() => {
return Err(Error::invalid_argument_type(
"transaction id must be STRING or LIST<STRING>",
))
}
Value::List(v) => v,
v => Arc::new(vec![v]),
};
for value in values.iter() {
let id = match value {
Value::String(_) => value.clone(),
_ => Value::Null,
};
if !out
.iter()
.any(|r: &Row| r.get("transactionId") == Some(&id))
{
out.push(catalog_row([
("transactionId", id),
("username", Value::Null),
("message", Value::String("Transaction not found.".into())),
]));
}
}
}
}
out
})
}
/// Which fixed namespaces `SHOW DATABASES` may emit: every one, exactly
/// one, or none (a null name parameter off Cypher 5, or a name no fixed
/// namespace carries — `Namespace::parse` already matches
/// case-insensitively).
#[derive(Clone, Copy)]
enum NamespaceFilter {
All,
Only(relate_core::Namespace),
NoMatch,
}
impl NamespaceFilter {
/// The filter for a database name: `Only` when it names a fixed
/// namespace, `NoMatch` otherwise.
fn named(name: &str) -> Self {
match relate_core::Namespace::parse(name) {
Some(ns) => NamespaceFilter::Only(ns),
None => NamespaceFilter::NoMatch,
}
}
/// Whether a fixed namespace passes the filter.
fn admits(self, ns: relate_core::Namespace) -> bool {
match self {
NamespaceFilter::All => true,
NamespaceFilter::Only(wanted) => wanted == ns,
NamespaceFilter::NoMatch => false,
}
}
}
pub(crate) fn show_databases_rows(
graph: &ExecGraph<'_>,
c: &relate_parser::ShowDatabasesClause,
params: &BTreeMap<String, Value>,
qctx: QueryCtx<'_>,
) -> Result<Vec<Row>> {
Ok({
let state = graph.show_schema()?;
// The filter resolves to the typed model at the boundary; an absent
// parameter fails with `ParameterMissing` (ADR-0308).
let wanted = match &c.filter {
DatabaseFilter::All => NamespaceFilter::All,
DatabaseFilter::Default | DatabaseFilter::Home => {
NamespaceFilter::Only(relate_core::Namespace::Neo4j)
}
DatabaseFilter::Named(SchemaName::Literal(n)) => NamespaceFilter::named(n),
DatabaseFilter::Named(SchemaName::Parameter(p)) => match params.get(p) {
None => return Err(Error::parameters_missing([p.as_str()])),
Some(Value::String(name)) => NamespaceFilter::named(name),
Some(Value::Null) if qctx.is_cypher5() => {
return Err(Error::invalid_argument_type("database name must be STRING"))
}
Some(Value::Null) => NamespaceFilter::NoMatch,
_ => return Err(Error::invalid_argument_type("database name must be STRING")),
},
};
// ADR-0116 §1/§6: exactly the three fixed namespaces; an unknown
// name returns no rows. The stored `defaultLanguage` comes from the
// store-level databases catalog (ADR-0116 §8/§10; `CYPHER 25`
// default).
relate_core::Namespace::ALL
.iter()
.filter(|ns| wanted.admits(**ns))
.map(|ns| {
let name = ns.as_str();
// `DatabasesCatalog::default`/`sanitize` guarantee an
// entry for every fixed namespace, so indexing cannot miss.
let language = state.databases[ns].default_language;
// ADR-0142: the topology/status columns carry fixed
// Browser-compatible constants, not live status — Relate
// is a single-node embedded engine, so every namespace is
// always online/primary. `default`/`home` are true only for
// `neo4j` (ADR-0116). Column order matches relate-front's
// DEFAULT_DATABASE_COLUMNS so `YIELD *` exposes the Neo4j
// ordering. ADR-0142: `address` is the constant
// `localhost:7687` — Neo4j Desktop builds the instance id
// `name@address` and rejects an empty address; the value is
// a display placeholder correct for the default Bolt bind.
let is_neo4j = matches!(ns, relate_core::Namespace::Neo4j);
catalog_row([
("name", Value::String(name.into())),
(
"type",
Value::String(
if matches!(ns, relate_core::Namespace::System) {
"system"
} else {
"standard"
}
.into(),
),
),
("aliases", Value::List(Vec::new().into())),
(
"access",
Value::String(
if ns.is_read_only() {
"read-only"
} else {
"read-write"
}
.into(),
),
),
("address", Value::String("localhost:7687".into())),
("role", Value::String("primary".into())),
("writer", Value::Bool(true)),
("requestedStatus", Value::String("online".into())),
("currentStatus", Value::String("online".into())),
("statusMessage", Value::String("".into())),
("default", Value::Bool(is_neo4j)),
("home", Value::Bool(is_neo4j)),
("constituents", Value::List(Vec::new().into())),
(
"creationTime",
Value::Temporal(relate_core::Temporal::DateTime {
utc: chrono::Utc::now().naive_utc() - chrono::Duration::hours(1),
offset: 0,
zone: None,
}),
),
(
"lastStartTime",
Value::Temporal(relate_core::Temporal::DateTime {
utc: chrono::Utc::now().naive_utc() - chrono::Duration::minutes(30),
offset: 0,
zone: None,
}),
),
("lastStopTime", Value::Null),
("store", Value::String("relate-native-1.0".into())),
(
"defaultLanguage",
Value::String(format!("CYPHER {language}").into()),
),
])
})
.collect()
})
}