Skip to content

Commit 7f4d51c

Browse files
Improve multiline query support (#284)
## Usage and product changes We improve multi-line query support to allow copy-pasting queries and scripts containing empty newlines. In particular this makes pasting entire schema definitions from files. For example, pasting a console script opening a transaction, defining a schema containing newlines, and committing, is now possible: ``` >> transaction schema test define entity person; # newlines are allowed in pasted scripts: attribute name, value string; person owns name; commit ``` Empty newlines when written _interactively_ still cause queries to be submitted. However, an explicit query `end;` clause is a valid alternative now: ``` >> transaction schema test define entity person; # newlines are allowed in pasted scripts: attribute name, value string; person owns name; end; # <--- will submit immediately ``` Pasted query pipelines may now be ambiguous, such as the following example which by defaults executs a single "match-insert" query, even though there are newlines: ``` > transaction schema test match $x isa person; insert $y isa person; commit ``` To make this a "match" query and a separate "insert" query, we must use the `end;` markers: ``` > transaction schema test match $x isa person; end; insert $y isa person; end; commit ``` **Note that now `end` is a reserved keyword and cannot be used as a type!** ## Implementation Queries are now parsed using TypeQL when given input. A "maximum" query is parsed from the input at a time, so multi-query copy-paste and scripts need to take care to be include the right terminators between queries (eg "end;").
1 parent 8a8cd9c commit 7f4d51c

File tree

9 files changed

+300
-38
lines changed

9 files changed

+300
-38
lines changed

BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ rust_binary(
2222
srcs = glob(["src/**/*.rs"]),
2323
deps = [
2424
"@typedb_driver//rust:typedb_driver",
25+
"@typeql//rust:typeql",
2526

2627
# External dependencies
2728
"@crates//:clap",
@@ -143,6 +144,7 @@ release_validate_deps(
143144
refs = "@typedb_console_workspace_refs//:refs.json",
144145
tagged_deps = [
145146
"@typedb_driver",
147+
"@typeql",
146148
],
147149
tags = ["manual"], # in order for bazel test //... to not fail
148150
version_file = "VERSION",

Cargo.lock

Lines changed: 161 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ features = {}
3838
version = "0.3.2"
3939
default-features = false
4040

41+
[dependencies.typeql]
42+
features = []
43+
rev = "7cbb621200f10f2cb741b57e82494ff9659813c6"
44+
git = "https://github.com/typedb/typeql"
45+
default-features = false
46+
4147
[dependencies.typedb-driver]
4248
features = []
4349
git = "https://github.com/typedb/typedb-driver"

README.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ will also be autocompleted, while others, such as queries, will not.
9696

9797
### Transaction-level commands
9898

99-
- `<query>` : Once you're in the transaction REPL, the terminal immediately accepts a multi-line TypeQL query, and will execute it when you **hit enter twice**.
99+
- `<query>` : Once you're in the transaction REPL, the terminal immediately accepts a multi-line TypeQL query, and will execute it when you **hit enter twice (submit an empty newline) or use an explicit "end;" stage**.
100100
```
101101
my-typedb-database::schema>> define
102102
attribute name, value string;
@@ -105,7 +105,7 @@ will also be autocompleted, while others, such as queries, will not.
105105
Finished schema query.
106106
>>
107107
```
108-
- `source <file>` : Run TypeQL queries in a file (_not a general console script_), which you can refer to using relative or absolute path. Queries must be terminated by an empty newline.
108+
- `source <file>` : Run TypeQL queries in a file (_not a general console script_), which you can refer to using relative or absolute path. Queries must be terminated by an empty newline or an explicit "end;" stage.
109109
```
110110
my-typedb-database::schema> source ./schema.tql
111111
Successfully executed 1 queries.
@@ -183,6 +183,31 @@ Finished. Total answers: 1
183183
Transaction closed
184184
```
185185

186+
Queries will be parsed maximally and newlines **are** allowed.
187+
If you wish to split a query pipeline into two queries, or skip the usual empty newline after a query,
188+
use the query "end;" marker:
189+
```
190+
>> database create test
191+
transaction schema test
192+
define
193+
entity person; # newlines are allowed in pasted scripts:
194+
195+
attribute name, value string;
196+
197+
person owns name;
198+
199+
commit
200+
transaction write test
201+
insert
202+
$x isa person;
203+
204+
$x has name "bob";
205+
end; # this is required to avoid creating a single "insert-match" query pipeline!
206+
207+
match $x isa person, has name "bob"; # this is treated as a new query
208+
209+
commit
210+
```
186211

187212
### Non-interactive modes
188213

@@ -191,7 +216,9 @@ Transaction closed
191216
We can define a script file that contains the list of commands to run, then invoke console with `./typedb console --script=<path>`.
192217

193218
Script files take exactly the same format as scripts pasted directly in the REPL.
194-
Importantly, this means that the **end of a query is delimited by an empty newline**.
219+
Importantly, this means that:
220+
- **Ambiguous query pipelines may need to be split explicitly up with an "end;" marker**.
221+
- **A query is that is not ended with the "end;" marker must still be terminated with an empty newline**
195222

196223
For example, a script `commands.tqls`
197224
```

0 commit comments

Comments
 (0)