From 9e2fa3b1dcdad25ae94449b1acf569c5c88d5e8a Mon Sep 17 00:00:00 2001 From: jasper blues Date: Sat, 5 Dec 2026 15:27:20 +1000 Subject: [PATCH 1/6] Name a $-named CaseInsensitive property's lowercased copy for the user's name on SQLite and Oracle, read it where the node holds it in each fill, and fill it in again on each ensure (drivine-cypher-spec#355) --- docs/06-fragments-and-views.md | 12 ++- drivine/backend/oracle/oracle_save_builder.py | 14 ++-- .../backend/oracle/oracle_schema_grammar.py | 5 +- .../postgres/postgres_schema_grammar.py | 10 ++- drivine/backend/sql/value_layout.py | 10 +++ drivine/backend/sqlite/sqlite_functions.py | 8 +- drivine/backend/sqlite/sqlite_plan_writer.py | 14 ++-- drivine/schema/lower_copy_catalog.py | 29 +++++-- drivine/sync/schema/lower_copy_catalog.py | 29 +++++-- tests/golden/statements.json | 10 +-- tests/integration/sql/test_value_layout.py | 80 ++++++++++++++++++- .../sync_integration/sql/test_value_layout.py | 80 ++++++++++++++++++- tests/unit/test_case_insensitive.py | 69 ++++++++++++++++ tests/unit/test_sqlite_plan_writer.py | 24 ++++++ tests/unit/test_value_layout.py | 19 ++++- 15 files changed, 373 insertions(+), 40 deletions(-) diff --git a/docs/06-fragments-and-views.md b/docs/06-fragments-and-views.md index 8946a7d0..b104d2e1 100644 --- a/docs/06-fragments-and-views.md +++ b/docs/06-fragments-and-views.md @@ -138,10 +138,11 @@ await gom.load_all(Account, where=refs(Account).email.eq_ignore_case("Ada@Exampl - **The copy.** Every `save`, `create`, `update`, `save_all`, `patch` and `patch_all`, and the save of a view's root and related nodes, writes a lowercased copy of the property beside it, under `__drivine.lower.` (the name it - is stored under, after `GraphProperty`). The database lowercases it, as it lowercases the value a lookup compares - with, so drivine-python, drivine4j and TypeScript's Drivine store and match the same thing. A property with no value - has no copy. A load doesn't read it, and a flat `PropertyBag` never holds it. The copy is no part of whether a save - changed the node, so a save that only fills one in keeps the node's stamp. Cypher you send yourself doesn't: see + is stored under, after `GraphProperty`); on a SQL store, which holds a property named `$mail` as `$$mail`, the copy + is still `__drivine.lower.$mail`. The database lowercases it, as it lowercases the value a lookup compares with, so + drivine-python, drivine4j and TypeScript's Drivine store and match the same thing. A property with no value has no + copy. A load doesn't read it, and a flat `PropertyBag` never holds it. The copy is no part of whether a save changed + the node, so a save that only fills one in keeps the node's stamp. Cypher you send yourself doesn't: see [below](#cypher-of-your-own). - **The index.** `RangeIndex(ignore_case=True)`, `Unique(ignore_case=True)`, `range_index(label, property, ignore_case=True)` and `uniqueness_constraint(label, property, ignore_case=True)` are made over the copy, under their @@ -152,6 +153,9 @@ await gom.load_all(Account, where=refs(Account).email.eq_ignore_case("Ada@Exampl - **Filling it in.** Ensuring an item that ignores case first fills the copy in on every node of the label that lacks it, 10,000 nodes a statement, and records that every node has it: on the store's `DrivineStore` node on the graph engines, in the graph's catalog on Oracle and Postgres. An item made anew, or recreated, fills the copies in again. + On Oracle, Postgres and SQLite every ensure also fills in again the copy of a property named with a `$`, though + recorded: an earlier version wrote none, or wrote it under the held name, `__drivine.lower.$$mail`, which no lookup + reads, so a node it saved is missed by a lookup until an ensure has run. - **The lookup.** Once the copy is recorded, `eq_ignore_case` and `contains_ignore_case` on the field compare with the copy: in a load, a count, a `delete_all`, a grouped read and a patch's condition, on the root's own fields. Until then, and for an empty value or a related node's field, they lowercase the property, as for any field: the same diff --git a/drivine/backend/oracle/oracle_save_builder.py b/drivine/backend/oracle/oracle_save_builder.py index 75ca2a3b..c0161fa2 100644 --- a/drivine/backend/oracle/oracle_save_builder.py +++ b/drivine/backend/oracle/oracle_save_builder.py @@ -604,9 +604,10 @@ def prelude(graph: OracleGraph, declarations: Sequence[str], lowers: bool = Fals def _lower_node(nodes: str) -> str: """ - `lower_node`: writes on the node the lowercased copy of each property `p_keys`, a JSON array, names, from what it - holds, and removes the copy of one that holds nothing, which an empty text is on Oracle. Oracle lowercases it, as it - lowercases what a lookup compares the copy with: see lower_copies. It is called once a write's change is told, so + `lower_node`: writes on the node the lowercased copy of each property `p_keys`, a JSON array, names as the node + holds it (see held_key), from what it holds, under the user's name (`__drivine.lower.$mail` for `$$mail`), and + removes the copy of one that holds nothing, which an empty text is on Oracle. Oracle lowercases it, as it lowercases + what a lookup compares the copy with: see lower_copies. It is called once a write's change is told, so the copies are no part of it. The copy is read and written as a CLOB, so a text past what a VARCHAR2 holds has its copy too. """ @@ -617,17 +618,20 @@ def _lower_node(nodes: str) -> str: o JSON_OBJECT_T; lowered JSON_ARRAY_T := JSON_ARRAY_T.parse(p_keys); k VARCHAR2(4000); + c VARCHAR2(4000); v CLOB; BEGIN SELECT props INTO held FROM {nodes} WHERE nid = p; o := JSON_OBJECT_T(held); FOR i IN 0 .. lowered.get_size - 1 LOOP k := lowered.get_string(i); + -- k is the property as the node holds it; its copy is named for the user's name, $mail for $$mail. + c := '{lower_copies.PREFIX}' || CASE WHEN SUBSTR(k, 1, 2) = '$$' THEN SUBSTR(k, 2) ELSE k END; v := NULL; IF o.has(k) AND NOT o.get(k).is_null THEN v := LOWER(o.get_clob(k)); END IF; -- Oracle keeps an empty text as null, so an empty property has no copy. - IF v IS NULL OR LENGTH(v) = 0 THEN o.remove('{lower_copies.PREFIX}' || k); - ELSE o.put('{lower_copies.PREFIX}' || k, v); END IF; + IF v IS NULL OR LENGTH(v) = 0 THEN o.remove(c); + ELSE o.put(c, v); END IF; END LOOP; -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573). held := o.to_json; diff --git a/drivine/backend/oracle/oracle_schema_grammar.py b/drivine/backend/oracle/oracle_schema_grammar.py index f6388c2b..6607ec95 100644 --- a/drivine/backend/oracle/oracle_schema_grammar.py +++ b/drivine/backend/oracle/oracle_schema_grammar.py @@ -23,6 +23,7 @@ catalog_properties, catalog_record, ) +from drivine.backend.sql.value_layout import held_key from drivine.model import lower_copies from drivine.schema.full_text_index_spec import FullTextIndexSpec from drivine.schema.lower_copy_statements import LOWER_COPY_KIND, LowerCopyStatements, Recording, catalog_entries @@ -165,8 +166,10 @@ def _lower_copies(graph: OracleGraph) -> LowerCopyStatements: """ def fill(label: str, property: str, batch: int) -> str: + # The copy is named for the user's name, and the value read where the node holds it, $$mail for $mail + # (drivine-cypher-spec#355). path = f"'$.{drivine4j_step(lower_copies.of(property))}'" - value = f"JSON_VALUE(n.props, '$.{drivine4j_step(property)}' RETURNING CLOB)" + value = f"JSON_VALUE(n.props, '$.{drivine4j_step(held_key(property))}' RETURNING CLOB)" copy = f"JSON_VALUE(n.props, {path} RETURNING CLOB)" return f"""DECLARE v_written NUMBER; diff --git a/drivine/backend/postgres/postgres_schema_grammar.py b/drivine/backend/postgres/postgres_schema_grammar.py index fc1b9228..6136e283 100644 --- a/drivine/backend/postgres/postgres_schema_grammar.py +++ b/drivine/backend/postgres/postgres_schema_grammar.py @@ -17,6 +17,7 @@ catalog_record, ) from drivine.backend.sql.sql_graph import sql_text +from drivine.backend.sql.value_layout import held_key from drivine.model import lower_copies from drivine.schema.full_text_index_spec import FullTextIndexSpec from drivine.schema.lower_copy_statements import LOWER_COPY_KIND, LowerCopyStatements, Recording, catalog_entries @@ -80,18 +81,21 @@ def _lower_copies(graph: PostgresGraph) -> LowerCopyStatements: """ def fill(label: str, property: str, batch: int) -> str: + # The copy is named for the user's name, and the value read where the node holds it, $$mail for $mail + # (drivine-cypher-spec#355). copy_key = sql_text(lower_copies.of(property)) + held = held_key(property) return "\n".join( [ "WITH written AS (", f" UPDATE {graph.nodes} n", - f" SET props = CASE WHEN n.props ->> {sql_text(property)} IS NULL THEN n.props - {copy_key}", + f" SET props = CASE WHEN n.props ->> {sql_text(held)} IS NULL THEN n.props - {copy_key}", f" ELSE n.props || jsonb_build_object({copy_key}, lower(n.props ->> " - f"{sql_text(property)})) END", + f"{sql_text(held)})) END", " WHERE n.nid IN (", f" SELECT m.nid FROM {graph.nodes} m", f" WHERE {has_labels('m', [label])} AND {text_of('m', lower_copies.of(property))} IS DISTINCT " - f"FROM lower({text_of('m', property)})", + f"FROM lower({text_of('m', held)})", f" LIMIT {batch})", " RETURNING 1)", 'SELECT count(*) AS "written" FROM written', diff --git a/drivine/backend/sql/value_layout.py b/drivine/backend/sql/value_layout.py index 428defba..632abd03 100644 --- a/drivine/backend/sql/value_layout.py +++ b/drivine/backend/sql/value_layout.py @@ -59,6 +59,16 @@ def held_key(name: str) -> str: return f"${name}" if name.startswith("$") else name +def user_key(held: str) -> str: + """ + The user's name of a property the store holds as `held`, undoing held_key: `$mail` for `$$mail`. A name held with a + `$` begins with `$$`; one that begins with a single `$` is no held name, and is given back as it is. The lowercased + copy of a property is named for the user's name (lower_copies.of), as each lookup and the ignore-case index read it + (liberation-data/drivine-cypher-spec#355). + """ + return held[1:] if held.startswith("$$") else held + + def write(value: Any, store: SqlStore) -> Any: """`value` as a save writes it on `store` (see the module): as it is, or the module's text of it, `Held`.""" made = prepared(value) diff --git a/drivine/backend/sqlite/sqlite_functions.py b/drivine/backend/sqlite/sqlite_functions.py index b70346c1..52abbf0f 100644 --- a/drivine/backend/sqlite/sqlite_functions.py +++ b/drivine/backend/sqlite/sqlite_functions.py @@ -31,6 +31,7 @@ import apsw +from drivine.backend.sql.value_layout import held_key from drivine.backend.sqlite import sqlite_cypher_functions, sqlite_extension, sqlite_graph from drivine.backend.sqlite.sqlite_arguments import text as text_of_argument from drivine.backend.sqlite.sqlite_graph import SqliteGraph, has_label, path, text_of @@ -136,14 +137,17 @@ def _lower_copies( if graph is None or label is None or property is None: raise ValueError("drivine_lower_copies names a graph, a label and a property.") nodes = SqliteGraph(graph).nodes + # The copy is named for the user's name, and the value read where the node holds it, $$mail for $mail + # (drivine-cypher-spec#355). + held = held_key(property) copy = lower_copies.of(property) def lowered(alias: str) -> str: - return f"{sqlite_graph.LOWER}({text_of(alias, property)})" + return f"{sqlite_graph.LOWER}({text_of(alias, held)})" connection.execute( f"UPDATE {nodes} AS n\n" - f"SET props = CASE WHEN {text_of('n', property)} IS NULL THEN json_remove(n.props, {path(copy)}) " + f"SET props = CASE WHEN {text_of('n', held)} IS NULL THEN json_remove(n.props, {path(copy)}) " f"ELSE json_set(n.props, {path(copy)}, {lowered('n')}) END\n" "WHERE n.nid IN (\n" f" SELECT m.nid FROM {nodes} m\n" diff --git a/drivine/backend/sqlite/sqlite_plan_writer.py b/drivine/backend/sqlite/sqlite_plan_writer.py index c2adfbc7..f24ac789 100644 --- a/drivine/backend/sqlite/sqlite_plan_writer.py +++ b/drivine/backend/sqlite/sqlite_plan_writer.py @@ -8,6 +8,7 @@ import apsw +from drivine.backend.sql.value_layout import user_key from drivine.backend.sql.write_plan import WriteReturns from drivine.backend.sqlite import exact_json from drivine.backend.sqlite.sqlite_graph import SqliteGraph, has_labels @@ -584,20 +585,23 @@ def _set_and_unset(o: dict[str, Any], step: dict[str, Any]) -> None: def _lowered(o: dict[str, Any], keys: Any) -> dict[str, Any]: """ - `o` with the lowercased copy of each property `keys` names, from what `o` holds, and without the copy of one that - holds nothing: see lower_copies. It is lowercased as the lower function lowercases what a lookup compares the copy - with. `o` itself where `keys` names none. + `o` with the lowercased copy of each property `keys` names as the node holds it (see held_key), from what `o` + holds, and without the copy of one that holds nothing: see lower_copies. The copy is named for the user's name, + `__drivine.lower.$mail` for `$$mail` (see user_key). It is lowercased as the lower function lowercases what a + lookup compares the copy with. `o` itself where `keys` names none. """ names = _strings(keys) if not names: return o copied = dict(o) for key in names: + # Each key is the property as the node holds it; its copy is named for the user's name + # (drivine-cypher-spec#355). value = copied.get(key) if value is None: - copied.pop(lower_copies.of(key), None) + copied.pop(lower_copies.of(user_key(key)), None) else: - copied[lower_copies.of(key)] = _as_text(value).lower() + copied[lower_copies.of(user_key(key))] = _as_text(value).lower() return copied diff --git a/drivine/schema/lower_copy_catalog.py b/drivine/schema/lower_copy_catalog.py index 5ef24074..3bfaf5d1 100644 --- a/drivine/schema/lower_copy_catalog.py +++ b/drivine/schema/lower_copy_catalog.py @@ -9,7 +9,12 @@ from drivine.drivine_error import DrivineError from drivine.schema.full_text_index_spec import FullTextIndexSpec -from drivine.schema.lower_copy_statements import LOWER_COPY_BATCH, LowerCopyStatements, column_of +from drivine.schema.lower_copy_statements import ( + CYPHER_LOWER_COPIES, + LOWER_COPY_BATCH, + LowerCopyStatements, + column_of, +) from drivine.schema.range_index_spec import RangeIndexSpec from drivine.schema.schema_specs import item_name, lowercased from drivine.schema.uniqueness_constraint_spec import UniquenessConstraintSpec @@ -84,7 +89,7 @@ async def fill_in(self, label: str, properties: Sequence[str], again: bool) -> N if not properties: return stored: set[tuple[str, str]] = set() if again else await self._stored() - for property in [it for it in properties if again or (label, it) not in stored]: + for property in [it for it in properties if again or self._healed(it) or (label, it) not in stored]: filled = 0 while True: rows = await self._run(self._statements.fill(label, property, self._batch), None) @@ -98,7 +103,9 @@ async def fill_in(self, label: str, properties: Sequence[str], again: bool) -> N recording = self._statements.record(label, property) await self._run(recording.statement, recording.bindings) self._recorded.add((label, property)) - logger.info( + # A $-named property is filled in again on every ensure (see _healed): one that wrote nothing is no news. + logger.log( + logging.DEBUG if filled == 0 and self._healed(property) else logging.INFO, "Filled in the lowercased copy of %s.%s on %d node(s), and recorded that every node has it", label, property, @@ -106,8 +113,20 @@ async def fill_in(self, label: str, properties: Sequence[str], again: bool) -> N ) async def stored(self) -> set[tuple[str, str]]: - """The lowercased copies the store records as filled in, each `(label, property)`, read now.""" - return await self._stored() + """ + The lowercased copies the store records as filled in, each `(label, property)`, read now, but for those to fill + in again though recorded (see _healed): the schema planner's `ReadLowerCopies`, which fills in only what this + leaves out. A lookup reads the whole record (see filled_in). + """ + return {it for it in await self._stored() if not self._healed(it[1])} + + def _healed(self, property: str) -> bool: + """ + Whether `property` is filled in again though recorded: on a SQL store, one named with a `$`. One saved before + drivine-cypher-spec#355 held its copy under the escaped name, `__drivine.lower.$$mail`, which no lookup reads, + or held none; the fill writes only the nodes whose copy is out of step. The graph engines always named it right. + """ + return self._statements is not CYPHER_LOWER_COPIES and property.startswith("$") async def _stored(self) -> set[tuple[str, str]]: """What the store records, read now.""" diff --git a/drivine/sync/schema/lower_copy_catalog.py b/drivine/sync/schema/lower_copy_catalog.py index 13a78144..0782fdcf 100644 --- a/drivine/sync/schema/lower_copy_catalog.py +++ b/drivine/sync/schema/lower_copy_catalog.py @@ -11,7 +11,12 @@ from drivine.drivine_error import DrivineError from drivine.schema.full_text_index_spec import FullTextIndexSpec -from drivine.schema.lower_copy_statements import LOWER_COPY_BATCH, LowerCopyStatements, column_of +from drivine.schema.lower_copy_statements import ( + CYPHER_LOWER_COPIES, + LOWER_COPY_BATCH, + LowerCopyStatements, + column_of, +) from drivine.schema.range_index_spec import RangeIndexSpec from drivine.schema.schema_specs import item_name, lowercased from drivine.schema.uniqueness_constraint_spec import UniquenessConstraintSpec @@ -86,7 +91,7 @@ def fill_in(self, label: str, properties: Sequence[str], again: bool) -> None: if not properties: return stored: set[tuple[str, str]] = set() if again else self._stored() - for property in [it for it in properties if again or (label, it) not in stored]: + for property in [it for it in properties if again or self._healed(it) or (label, it) not in stored]: filled = 0 while True: rows = self._run(self._statements.fill(label, property, self._batch), None) @@ -100,7 +105,9 @@ def fill_in(self, label: str, properties: Sequence[str], again: bool) -> None: recording = self._statements.record(label, property) self._run(recording.statement, recording.bindings) self._recorded.add((label, property)) - logger.info( + # A $-named property is filled in again on every ensure (see _healed): one that wrote nothing is no news. + logger.log( + logging.DEBUG if filled == 0 and self._healed(property) else logging.INFO, "Filled in the lowercased copy of %s.%s on %d node(s), and recorded that every node has it", label, property, @@ -108,8 +115,20 @@ def fill_in(self, label: str, properties: Sequence[str], again: bool) -> None: ) def stored(self) -> set[tuple[str, str]]: - """The lowercased copies the store records as filled in, each `(label, property)`, read now.""" - return self._stored() + """ + The lowercased copies the store records as filled in, each `(label, property)`, read now, but for those to fill + in again though recorded (see _healed): the schema planner's `ReadLowerCopies`, which fills in only what this + leaves out. A lookup reads the whole record (see filled_in). + """ + return {it for it in self._stored() if not self._healed(it[1])} + + def _healed(self, property: str) -> bool: + """ + Whether `property` is filled in again though recorded: on a SQL store, one named with a `$`. One saved before + drivine-cypher-spec#355 held its copy under the escaped name, `__drivine.lower.$$mail`, which no lookup reads, + or held none; the fill writes only the nodes whose copy is out of step. The graph engines always named it right. + """ + return self._statements is not CYPHER_LOWER_COPIES and property.startswith("$") def _stored(self) -> set[tuple[str, str]]: """What the store records, read now.""" diff --git a/tests/golden/statements.json b/tests/golden/statements.json index 6a6ec070..f03da555 100644 --- a/tests/golden/statements.json +++ b/tests/golden/statements.json @@ -324,11 +324,11 @@ "Person.heldProperties": "SELECT JSON_TRANSFORM(n.props, KEEP '$') AS \"props\"\nFROM drivine_node n\nWHERE INSTR(',' || LTRIM(RTRIM(JSON_SERIALIZE(n.labels), ']'), '[') || ',\"', ',\"Person\",\"') > 0 AND (CASE WHEN CASE JSON_VALUE(n.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(n.props, '$.uuid.s'), JSON_VALUE(n.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(n.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(n.props, '$.uuid') END IS NOT NULL THEN CASE JSON_VALUE(n.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(n.props, '$.uuid.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(n.props, '$.uuid.type()' RETURNING VARCHAR2(16)) END END = CASE WHEN CASE JSON_VALUE(JSON($id EXTENDED), '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(JSON($id EXTENDED), '$.s'), JSON_VALUE(JSON($id EXTENDED), '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(JSON($id EXTENDED), '$') END IS NOT NULL THEN CASE JSON_VALUE(JSON($id EXTENDED), '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(JSON($id EXTENDED), '$.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(JSON($id EXTENDED), '$.type()' RETURNING VARCHAR2(16)) END END AND CASE JSON_VALUE(n.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(n.props, '$.uuid.s'), JSON_VALUE(n.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(n.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(n.props, '$.uuid') END = CASE JSON_VALUE(JSON($id EXTENDED), '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(JSON($id EXTENDED), '$.s'), JSON_VALUE(JSON($id EXTENDED), '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(JSON($id EXTENDED), '$') END)", "Person.foundStamp": "SELECT NVL(n.stamp, '-') AS \"found\"\nFROM drivine_node n\nWHERE INSTR(',' || LTRIM(RTRIM(JSON_SERIALIZE(n.labels), ']'), '[') || ',\"', ',\"Person\",\"') > 0 AND (CASE WHEN CASE JSON_VALUE(n.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(n.props, '$.uuid.s'), JSON_VALUE(n.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(n.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(n.props, '$.uuid') END IS NOT NULL THEN CASE JSON_VALUE(n.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(n.props, '$.uuid.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(n.props, '$.uuid.type()' RETURNING VARCHAR2(16)) END END = CASE WHEN CASE JSON_VALUE(JSON($id EXTENDED), '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(JSON($id EXTENDED), '$.s'), JSON_VALUE(JSON($id EXTENDED), '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(JSON($id EXTENDED), '$') END IS NOT NULL THEN CASE JSON_VALUE(JSON($id EXTENDED), '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(JSON($id EXTENDED), '$.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(JSON($id EXTENDED), '$.type()' RETURNING VARCHAR2(16)) END END AND CASE JSON_VALUE(n.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(n.props, '$.uuid.s'), JSON_VALUE(n.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(n.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(n.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(n.props, '$.uuid') END = CASE JSON_VALUE(JSON($id EXTENDED), '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(JSON($id EXTENDED), '$.s'), JSON_VALUE(JSON($id EXTENDED), '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(JSON($id EXTENDED), '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(JSON($id EXTENDED), '$') END)", "saveAll.fragments": "DECLARE\n v_offered VARCHAR2(40) := $x_stamp;\n v_root NUMBER;\n v_found VARCHAR2(40);\n v_made BOOLEAN := FALSE;\n v_ok BOOLEAN := TRUE;\n c SYS_REFCURSOR;\n v_before0 CLOB;\n v_after0 CLOB;\n v_before1 CLOB;\n v_after1 CLOB;\n\n FUNCTION two_tokens(s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n RETURN s IS NOT NULL AND LENGTH(s) = 33 AND SUBSTR(s, 17, 1) = ':';\n END;\n\n PROCEDURE restamp(p NUMBER, p_changed BOOLEAN) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF p_changed OR NOT two_tokens(s) THEN\n IF two_tokens(s) THEN s := SUBSTR(v_offered, 1, 16) || SUBSTR(s, 17);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END IF;\n END;\n\n PROCEDURE relink(p NUMBER) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF two_tokens(s) THEN s := SUBSTR(s, 1, 17) || SUBSTR(v_offered, 18);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END;\n\n FUNCTION find_node(p_match VARCHAR2, p_id VARCHAR2) RETURN NUMBER IS\n ids SYS.ODCINUMBERLIST;\n BEGIN\n EXECUTE IMMEDIATE 'SELECT nid FROM drivine_node n WHERE ' || p_match BULK COLLECT INTO ids USING p_id;\n IF ids.COUNT > 1 THEN\n RAISE_APPLICATION_ERROR(-20001, ids.COUNT || ' nodes have the id ' || p_id || '. An id is one node''s.');\n END IF;\n RETURN CASE WHEN ids.COUNT = 1 THEN ids(1) END;\n END;\n\n FUNCTION make_node(p_labels VARCHAR2) RETURN NUMBER IS\n v NUMBER;\n BEGIN\n INSERT INTO drivine_node (labels, props) VALUES (JSON(p_labels), JSON('{}')) RETURNING nid INTO v;\n RETURN v;\n END;\n\n FUNCTION has(a JSON_ARRAY_T, s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n FOR i IN 0 .. a.get_size - 1 LOOP\n IF a.get_string(i) = s THEN RETURN TRUE; END IF;\n END LOOP;\n RETURN FALSE;\n END;\n\n -- Merges the patch into the node's properties, adds and removes labels, and records an open label field's labels.\n -- Whether any of it changed the node.\n FUNCTION apply_node(p NUMBER, p_props CLOB, p_add VARCHAR2, p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN)\n RETURN BOOLEAN IS\n old_props JSON;\n new_props JSON;\n old_labels JSON;\n labels JSON_ARRAY_T;\n kept JSON_ARRAY_T := JSON_ARRAY_T();\n additions JSON_ARRAY_T := JSON_ARRAY_T.parse(p_add);\n removals JSON_ARRAY_T := JSON_ARRAY_T.parse(p_remove);\n props JSON_OBJECT_T;\n recorded JSON_ARRAY_T;\n same NUMBER;\n relabelled BOOLEAN := FALSE;\n kept_text VARCHAR2(4000);\n BEGIN\n SELECT props, labels INTO old_props, old_labels FROM drivine_node WHERE nid = p FOR UPDATE;\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n labels := JSON_ARRAY_T.parse(JSON_SERIALIZE(old_labels));\n FOR i IN 0 .. labels.get_size - 1 LOOP\n IF has(removals, labels.get_string(i)) THEN relabelled := TRUE; ELSE kept.append(labels.get_string(i)); END IF;\n END LOOP;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(kept, additions.get_string(i)) THEN kept.append(additions.get_string(i)); relabelled := TRUE; END IF;\n END LOOP;\n IF p_record IS NOT NULL THEN\n props := JSON_OBJECT_T(new_props);\n IF p_replace OR NOT props.has(p_record) THEN recorded := JSON_ARRAY_T();\n ELSE recorded := props.get_array(p_record); END IF;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(recorded, additions.get_string(i)) THEN recorded.append(additions.get_string(i)); END IF;\n END LOOP;\n props.put(p_record, recorded);\n new_props := props.to_json;\n END IF;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n kept_text := kept.to_string();\n UPDATE drivine_node SET props = new_props, labels = JSON(kept_text) WHERE nid = p;\n RETURN same = 0 OR relabelled;\n END;\n\n FUNCTION write_node(p_match VARCHAR2, p_id VARCHAR2, p_labels VARCHAR2, p_props CLOB, p_add VARCHAR2,\n p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN) RETURN NUMBER IS\n v NUMBER := find_node(p_match, p_id);\n made BOOLEAN := v IS NULL;\n BEGIN\n IF made THEN v := make_node(p_labels); END IF;\n restamp(v, apply_node(v, p_props, p_add, p_remove, p_record, p_replace) OR made);\n RETURN v;\n END;\n\n -- Joins two nodes by a relationship of the type, unless one is there (in either direction, for BOTH), and merges\n -- its properties. Both ends are marked when one is made or its properties change.\n PROCEDURE link(p_from NUMBER, p_to NUMBER, p_type VARCHAR2, p_dir VARCHAR2, p_props CLOB) IS\n e NUMBER;\n old_props JSON;\n new_props JSON;\n same NUMBER;\n BEGIN\n BEGIN\n SELECT eid, props INTO e, old_props FROM drivine_edge\n WHERE type = p_type AND ((p_dir IN ('OUT', 'BOTH') AND src = p_from AND dst = p_to)\n OR (p_dir IN ('IN', 'BOTH') AND src = p_to AND dst = p_from))\n FETCH FIRST 1 ROW ONLY;\n EXCEPTION WHEN NO_DATA_FOUND THEN e := NULL;\n END;\n IF e IS NULL THEN\n INSERT INTO drivine_edge (src, dst, type, props) VALUES (CASE WHEN p_dir = 'IN' THEN p_to ELSE p_from END,\n CASE WHEN p_dir = 'IN' THEN p_from ELSE p_to END, p_type, JSON(NVL(p_props, '{}') EXTENDED));\n relink(p_from);\n relink(p_to);\n ELSIF p_props IS NOT NULL THEN\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n IF same = 0 THEN\n UPDATE drivine_edge SET props = new_props WHERE eid = e;\n relink(p_from);\n relink(p_to);\n END IF;\n END IF;\n END;\n\n -- Removes the relationships p_dropped selects from the root, marks both ends, and deletes each removed node\n -- p_unreferenced counts as no longer referred to.\n PROCEDURE trim_edges(p_root NUMBER, p_dropped VARCHAR2, p_keep VARCHAR2, p_unreferenced VARCHAR2, p_held VARCHAR2) IS\n eids SYS.ODCINUMBERLIST;\n nids SYS.ODCINUMBERLIST;\n unreferenced NUMBER;\n BEGIN\n EXECUTE IMMEDIATE p_dropped BULK COLLECT INTO eids, nids USING p_root, p_keep;\n FOR i IN 1 .. eids.COUNT LOOP\n DELETE FROM drivine_edge WHERE eid = eids(i);\n END LOOP;\n FOR i IN 1 .. nids.COUNT LOOP\n relink(nids(i));\n END LOOP;\n IF eids.COUNT > 0 THEN relink(p_root); END IF;\n IF p_unreferenced IS NOT NULL THEN\n FOR i IN 1 .. nids.COUNT LOOP\n IF nids(i) <> p_root THEN\n EXECUTE IMMEDIATE p_unreferenced INTO unreferenced USING nids(i), p_held;\n IF unreferenced > 0 THEN DELETE FROM drivine_node WHERE nid = nids(i); END IF;\n END IF;\n END LOOP;\n END IF;\n END;\nBEGIN\n SELECT /*+ NO_JSON_TABLE_TRANSFORM */ NVL(JSON_ARRAYAGG(JSON_OBJECT('id' VALUE JSON_QUERY(n.props, '$.\"uuid\"'), 'stamp' VALUE NVL(n.stamp, '-')) RETURNING CLOB), '[]') INTO v_before0 FROM drivine_node n WHERE n.nid IN (SELECT /*+ NO_JSON_TABLE_TRANSFORM */ f.nid + 0 FROM JSON_TABLE(JSON($x_1 EXTENDED), '$[*]' COLUMNS (v JSON PATH '$')) i CROSS JOIN drivine_node f WHERE CASE WHEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END IS NOT NULL THEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(f.props, '$.uuid.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) END END = CASE WHEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END IS NOT NULL THEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(i.v, '$.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) END END AND CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END = CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END AND INSTR(',' || LTRIM(RTRIM(JSON_SERIALIZE(f.labels), ']'), '[') || ',\"', ',\"Person\",\"') > 0);\n SELECT /*+ NO_JSON_TABLE_TRANSFORM */ NVL(JSON_ARRAYAGG(JSON_OBJECT('id' VALUE JSON_QUERY(n.props, '$.\"uuid\"'), 'stamp' VALUE NVL(n.stamp, '-')) RETURNING CLOB), '[]') INTO v_before1 FROM drivine_node n WHERE n.nid IN (SELECT /*+ NO_JSON_TABLE_TRANSFORM */ f.nid + 0 FROM JSON_TABLE(JSON($x_2 EXTENDED), '$[*]' COLUMNS (v JSON PATH '$')) i CROSS JOIN drivine_node f WHERE CASE WHEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END IS NOT NULL THEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(f.props, '$.uuid.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) END END = CASE WHEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END IS NOT NULL THEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(i.v, '$.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) END END AND CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END = CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END AND INSTR(',' || LTRIM(RTRIM(JSON_SERIALIZE(f.labels), ']'), '[') || ',\"', ',\"Organization\",\"') > 0);\n v_root := write_node($x_3, $x_4, $x_5, $x_6, '[]', '[]', NULL, FALSE);\n v_root := write_node($x_7, $x_8, $x_9, $x_10, '[]', '[]', NULL, FALSE);\n v_root := write_node($x_11, $x_12, $x_13, $x_14, '[]', '[]', NULL, FALSE);\n SELECT /*+ NO_JSON_TABLE_TRANSFORM */ NVL(JSON_ARRAYAGG(JSON_OBJECT('id' VALUE JSON_QUERY(n.props, '$.\"uuid\"'), 'stamp' VALUE NVL(n.stamp, '-')) RETURNING CLOB), '[]') INTO v_after0 FROM drivine_node n WHERE n.nid IN (SELECT /*+ NO_JSON_TABLE_TRANSFORM */ f.nid + 0 FROM JSON_TABLE(JSON($x_15 EXTENDED), '$[*]' COLUMNS (v JSON PATH '$')) i CROSS JOIN drivine_node f WHERE CASE WHEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END IS NOT NULL THEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(f.props, '$.uuid.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) END END = CASE WHEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END IS NOT NULL THEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(i.v, '$.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) END END AND CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END = CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END AND INSTR(',' || LTRIM(RTRIM(JSON_SERIALIZE(f.labels), ']'), '[') || ',\"', ',\"Person\",\"') > 0);\n SELECT /*+ NO_JSON_TABLE_TRANSFORM */ NVL(JSON_ARRAYAGG(JSON_OBJECT('id' VALUE JSON_QUERY(n.props, '$.\"uuid\"'), 'stamp' VALUE NVL(n.stamp, '-')) RETURNING CLOB), '[]') INTO v_after1 FROM drivine_node n WHERE n.nid IN (SELECT /*+ NO_JSON_TABLE_TRANSFORM */ f.nid + 0 FROM JSON_TABLE(JSON($x_16 EXTENDED), '$[*]' COLUMNS (v JSON PATH '$')) i CROSS JOIN drivine_node f WHERE CASE WHEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END IS NOT NULL THEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(f.props, '$.uuid.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) END END = CASE WHEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END IS NOT NULL THEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(i.v, '$.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) END END AND CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END = CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END AND INSTR(',' || LTRIM(RTRIM(JSON_SERIALIZE(f.labels), ']'), '[') || ',\"', ',\"Organization\",\"') > 0);\n OPEN c FOR SELECT JSON_OBJECT('groups' VALUE JSON_ARRAY(JSON_OBJECT('before' VALUE JSON(v_before0), 'after' VALUE JSON(v_after0) RETURNING JSON), JSON_OBJECT('before' VALUE JSON(v_before1), 'after' VALUE JSON(v_after1) RETURNING JSON) RETURNING JSON) RETURNING CLOB) AS \"result:json\" FROM dual;\n DBMS_SQL.RETURN_RESULT(c);\nEND;", - "Account.save": "DECLARE\n v_offered VARCHAR2(40) := $x_stamp;\n v_root NUMBER;\n v_found VARCHAR2(40);\n v_made BOOLEAN := FALSE;\n v_ok BOOLEAN := TRUE;\n c SYS_REFCURSOR;\n\n\n FUNCTION two_tokens(s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n RETURN s IS NOT NULL AND LENGTH(s) = 33 AND SUBSTR(s, 17, 1) = ':';\n END;\n\n PROCEDURE restamp(p NUMBER, p_changed BOOLEAN) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF p_changed OR NOT two_tokens(s) THEN\n IF two_tokens(s) THEN s := SUBSTR(v_offered, 1, 16) || SUBSTR(s, 17);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END IF;\n END;\n\n PROCEDURE relink(p NUMBER) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF two_tokens(s) THEN s := SUBSTR(s, 1, 17) || SUBSTR(v_offered, 18);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END;\n\n FUNCTION find_node(p_match VARCHAR2, p_id VARCHAR2) RETURN NUMBER IS\n ids SYS.ODCINUMBERLIST;\n BEGIN\n EXECUTE IMMEDIATE 'SELECT nid FROM drivine_node n WHERE ' || p_match BULK COLLECT INTO ids USING p_id;\n IF ids.COUNT > 1 THEN\n RAISE_APPLICATION_ERROR(-20001, ids.COUNT || ' nodes have the id ' || p_id || '. An id is one node''s.');\n END IF;\n RETURN CASE WHEN ids.COUNT = 1 THEN ids(1) END;\n END;\n\n FUNCTION make_node(p_labels VARCHAR2) RETURN NUMBER IS\n v NUMBER;\n BEGIN\n INSERT INTO drivine_node (labels, props) VALUES (JSON(p_labels), JSON('{}')) RETURNING nid INTO v;\n RETURN v;\n END;\n\n FUNCTION has(a JSON_ARRAY_T, s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n FOR i IN 0 .. a.get_size - 1 LOOP\n IF a.get_string(i) = s THEN RETURN TRUE; END IF;\n END LOOP;\n RETURN FALSE;\n END;\n\n -- Merges the patch into the node's properties, adds and removes labels, and records an open label field's labels.\n -- Whether any of it changed the node.\n FUNCTION apply_node(p NUMBER, p_props CLOB, p_add VARCHAR2, p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN)\n RETURN BOOLEAN IS\n old_props JSON;\n new_props JSON;\n old_labels JSON;\n labels JSON_ARRAY_T;\n kept JSON_ARRAY_T := JSON_ARRAY_T();\n additions JSON_ARRAY_T := JSON_ARRAY_T.parse(p_add);\n removals JSON_ARRAY_T := JSON_ARRAY_T.parse(p_remove);\n props JSON_OBJECT_T;\n recorded JSON_ARRAY_T;\n same NUMBER;\n relabelled BOOLEAN := FALSE;\n kept_text VARCHAR2(4000);\n BEGIN\n SELECT props, labels INTO old_props, old_labels FROM drivine_node WHERE nid = p FOR UPDATE;\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n labels := JSON_ARRAY_T.parse(JSON_SERIALIZE(old_labels));\n FOR i IN 0 .. labels.get_size - 1 LOOP\n IF has(removals, labels.get_string(i)) THEN relabelled := TRUE; ELSE kept.append(labels.get_string(i)); END IF;\n END LOOP;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(kept, additions.get_string(i)) THEN kept.append(additions.get_string(i)); relabelled := TRUE; END IF;\n END LOOP;\n IF p_record IS NOT NULL THEN\n props := JSON_OBJECT_T(new_props);\n IF p_replace OR NOT props.has(p_record) THEN recorded := JSON_ARRAY_T();\n ELSE recorded := props.get_array(p_record); END IF;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(recorded, additions.get_string(i)) THEN recorded.append(additions.get_string(i)); END IF;\n END LOOP;\n props.put(p_record, recorded);\n new_props := props.to_json;\n END IF;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n kept_text := kept.to_string();\n UPDATE drivine_node SET props = new_props, labels = JSON(kept_text) WHERE nid = p;\n RETURN same = 0 OR relabelled;\n END;\n\n FUNCTION write_node(p_match VARCHAR2, p_id VARCHAR2, p_labels VARCHAR2, p_props CLOB, p_add VARCHAR2,\n p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN) RETURN NUMBER IS\n v NUMBER := find_node(p_match, p_id);\n made BOOLEAN := v IS NULL;\n BEGIN\n IF made THEN v := make_node(p_labels); END IF;\n restamp(v, apply_node(v, p_props, p_add, p_remove, p_record, p_replace) OR made);\n RETURN v;\n END;\n\n -- Joins two nodes by a relationship of the type, unless one is there (in either direction, for BOTH), and merges\n -- its properties. Both ends are marked when one is made or its properties change.\n PROCEDURE link(p_from NUMBER, p_to NUMBER, p_type VARCHAR2, p_dir VARCHAR2, p_props CLOB) IS\n e NUMBER;\n old_props JSON;\n new_props JSON;\n same NUMBER;\n BEGIN\n BEGIN\n SELECT eid, props INTO e, old_props FROM drivine_edge\n WHERE type = p_type AND ((p_dir IN ('OUT', 'BOTH') AND src = p_from AND dst = p_to)\n OR (p_dir IN ('IN', 'BOTH') AND src = p_to AND dst = p_from))\n FETCH FIRST 1 ROW ONLY;\n EXCEPTION WHEN NO_DATA_FOUND THEN e := NULL;\n END;\n IF e IS NULL THEN\n INSERT INTO drivine_edge (src, dst, type, props) VALUES (CASE WHEN p_dir = 'IN' THEN p_to ELSE p_from END,\n CASE WHEN p_dir = 'IN' THEN p_from ELSE p_to END, p_type, JSON(NVL(p_props, '{}') EXTENDED));\n relink(p_from);\n relink(p_to);\n ELSIF p_props IS NOT NULL THEN\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n IF same = 0 THEN\n UPDATE drivine_edge SET props = new_props WHERE eid = e;\n relink(p_from);\n relink(p_to);\n END IF;\n END IF;\n END;\n\n -- Removes the relationships p_dropped selects from the root, marks both ends, and deletes each removed node\n -- p_unreferenced counts as no longer referred to.\n PROCEDURE trim_edges(p_root NUMBER, p_dropped VARCHAR2, p_keep VARCHAR2, p_unreferenced VARCHAR2, p_held VARCHAR2) IS\n eids SYS.ODCINUMBERLIST;\n nids SYS.ODCINUMBERLIST;\n unreferenced NUMBER;\n BEGIN\n EXECUTE IMMEDIATE p_dropped BULK COLLECT INTO eids, nids USING p_root, p_keep;\n FOR i IN 1 .. eids.COUNT LOOP\n DELETE FROM drivine_edge WHERE eid = eids(i);\n END LOOP;\n FOR i IN 1 .. nids.COUNT LOOP\n relink(nids(i));\n END LOOP;\n IF eids.COUNT > 0 THEN relink(p_root); END IF;\n IF p_unreferenced IS NOT NULL THEN\n FOR i IN 1 .. nids.COUNT LOOP\n IF nids(i) <> p_root THEN\n EXECUTE IMMEDIATE p_unreferenced INTO unreferenced USING nids(i), p_held;\n IF unreferenced > 0 THEN DELETE FROM drivine_node WHERE nid = nids(i); END IF;\n END IF;\n END LOOP;\n END IF;\n END;\n\n -- Writes the lowercased copy of each property p_keys names from what the node holds, and removes the copy of one\n -- that holds nothing. Called once the write's change is told: the copies are no part of it.\n PROCEDURE lower_node(p NUMBER, p_keys VARCHAR2) IS\n held JSON;\n o JSON_OBJECT_T;\n lowered JSON_ARRAY_T := JSON_ARRAY_T.parse(p_keys);\n k VARCHAR2(4000);\n v CLOB;\n BEGIN\n SELECT props INTO held FROM drivine_node WHERE nid = p;\n o := JSON_OBJECT_T(held);\n FOR i IN 0 .. lowered.get_size - 1 LOOP\n k := lowered.get_string(i);\n v := NULL;\n IF o.has(k) AND NOT o.get(k).is_null THEN v := LOWER(o.get_clob(k)); END IF;\n -- Oracle keeps an empty text as null, so an empty property has no copy.\n IF v IS NULL OR LENGTH(v) = 0 THEN o.remove('__drivine.lower.' || k);\n ELSE o.put('__drivine.lower.' || k, v); END IF;\n END LOOP;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n held := o.to_json;\n UPDATE drivine_node SET props = held WHERE nid = p;\n END;\nBEGIN\n v_root := find_node($x_1, $x_2);\n IF v_ok THEN\n IF v_root IS NULL THEN\n v_root := make_node($x_3);\n v_made := TRUE;\n ELSIF v_found IS NULL THEN\n SELECT NVL(stamp, '-') INTO v_found FROM drivine_node WHERE nid = v_root FOR UPDATE;\n END IF;\n restamp(v_root, apply_node(v_root, $x_4, $x_5, $x_6, NULL, FALSE) OR v_made);\n lower_node(v_root, $x_7);\n END IF;\n IF v_ok THEN\n OPEN c FOR SELECT JSON_OBJECT('found' VALUE CASE WHEN v_made THEN NULL ELSE v_found END, 'now' VALUE (SELECT stamp FROM drivine_node WHERE nid = v_root), 'related' VALUE JSON('[]') RETURNING CLOB) AS \"result:json\" FROM dual;\n ELSE\n OPEN c FOR SELECT 1 AS \"result\" FROM dual WHERE 1 = 0;\n END IF;\n DBMS_SQL.RETURN_RESULT(c);\nEND;", - "AccountFriends.save": "DECLARE\n v_offered VARCHAR2(40) := $x_stamp;\n v_root NUMBER;\n v_found VARCHAR2(40);\n v_made BOOLEAN := FALSE;\n v_ok BOOLEAN := TRUE;\n c SYS_REFCURSOR;\n v_n14 NUMBER;\n v_before0 CLOB;\n v_after0 CLOB;\n\n FUNCTION two_tokens(s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n RETURN s IS NOT NULL AND LENGTH(s) = 33 AND SUBSTR(s, 17, 1) = ':';\n END;\n\n PROCEDURE restamp(p NUMBER, p_changed BOOLEAN) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF p_changed OR NOT two_tokens(s) THEN\n IF two_tokens(s) THEN s := SUBSTR(v_offered, 1, 16) || SUBSTR(s, 17);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END IF;\n END;\n\n PROCEDURE relink(p NUMBER) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF two_tokens(s) THEN s := SUBSTR(s, 1, 17) || SUBSTR(v_offered, 18);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END;\n\n FUNCTION find_node(p_match VARCHAR2, p_id VARCHAR2) RETURN NUMBER IS\n ids SYS.ODCINUMBERLIST;\n BEGIN\n EXECUTE IMMEDIATE 'SELECT nid FROM drivine_node n WHERE ' || p_match BULK COLLECT INTO ids USING p_id;\n IF ids.COUNT > 1 THEN\n RAISE_APPLICATION_ERROR(-20001, ids.COUNT || ' nodes have the id ' || p_id || '. An id is one node''s.');\n END IF;\n RETURN CASE WHEN ids.COUNT = 1 THEN ids(1) END;\n END;\n\n FUNCTION make_node(p_labels VARCHAR2) RETURN NUMBER IS\n v NUMBER;\n BEGIN\n INSERT INTO drivine_node (labels, props) VALUES (JSON(p_labels), JSON('{}')) RETURNING nid INTO v;\n RETURN v;\n END;\n\n FUNCTION has(a JSON_ARRAY_T, s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n FOR i IN 0 .. a.get_size - 1 LOOP\n IF a.get_string(i) = s THEN RETURN TRUE; END IF;\n END LOOP;\n RETURN FALSE;\n END;\n\n -- Merges the patch into the node's properties, adds and removes labels, and records an open label field's labels.\n -- Whether any of it changed the node.\n FUNCTION apply_node(p NUMBER, p_props CLOB, p_add VARCHAR2, p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN)\n RETURN BOOLEAN IS\n old_props JSON;\n new_props JSON;\n old_labels JSON;\n labels JSON_ARRAY_T;\n kept JSON_ARRAY_T := JSON_ARRAY_T();\n additions JSON_ARRAY_T := JSON_ARRAY_T.parse(p_add);\n removals JSON_ARRAY_T := JSON_ARRAY_T.parse(p_remove);\n props JSON_OBJECT_T;\n recorded JSON_ARRAY_T;\n same NUMBER;\n relabelled BOOLEAN := FALSE;\n kept_text VARCHAR2(4000);\n BEGIN\n SELECT props, labels INTO old_props, old_labels FROM drivine_node WHERE nid = p FOR UPDATE;\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n labels := JSON_ARRAY_T.parse(JSON_SERIALIZE(old_labels));\n FOR i IN 0 .. labels.get_size - 1 LOOP\n IF has(removals, labels.get_string(i)) THEN relabelled := TRUE; ELSE kept.append(labels.get_string(i)); END IF;\n END LOOP;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(kept, additions.get_string(i)) THEN kept.append(additions.get_string(i)); relabelled := TRUE; END IF;\n END LOOP;\n IF p_record IS NOT NULL THEN\n props := JSON_OBJECT_T(new_props);\n IF p_replace OR NOT props.has(p_record) THEN recorded := JSON_ARRAY_T();\n ELSE recorded := props.get_array(p_record); END IF;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(recorded, additions.get_string(i)) THEN recorded.append(additions.get_string(i)); END IF;\n END LOOP;\n props.put(p_record, recorded);\n new_props := props.to_json;\n END IF;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n kept_text := kept.to_string();\n UPDATE drivine_node SET props = new_props, labels = JSON(kept_text) WHERE nid = p;\n RETURN same = 0 OR relabelled;\n END;\n\n FUNCTION write_node(p_match VARCHAR2, p_id VARCHAR2, p_labels VARCHAR2, p_props CLOB, p_add VARCHAR2,\n p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN) RETURN NUMBER IS\n v NUMBER := find_node(p_match, p_id);\n made BOOLEAN := v IS NULL;\n BEGIN\n IF made THEN v := make_node(p_labels); END IF;\n restamp(v, apply_node(v, p_props, p_add, p_remove, p_record, p_replace) OR made);\n RETURN v;\n END;\n\n -- Joins two nodes by a relationship of the type, unless one is there (in either direction, for BOTH), and merges\n -- its properties. Both ends are marked when one is made or its properties change.\n PROCEDURE link(p_from NUMBER, p_to NUMBER, p_type VARCHAR2, p_dir VARCHAR2, p_props CLOB) IS\n e NUMBER;\n old_props JSON;\n new_props JSON;\n same NUMBER;\n BEGIN\n BEGIN\n SELECT eid, props INTO e, old_props FROM drivine_edge\n WHERE type = p_type AND ((p_dir IN ('OUT', 'BOTH') AND src = p_from AND dst = p_to)\n OR (p_dir IN ('IN', 'BOTH') AND src = p_to AND dst = p_from))\n FETCH FIRST 1 ROW ONLY;\n EXCEPTION WHEN NO_DATA_FOUND THEN e := NULL;\n END;\n IF e IS NULL THEN\n INSERT INTO drivine_edge (src, dst, type, props) VALUES (CASE WHEN p_dir = 'IN' THEN p_to ELSE p_from END,\n CASE WHEN p_dir = 'IN' THEN p_from ELSE p_to END, p_type, JSON(NVL(p_props, '{}') EXTENDED));\n relink(p_from);\n relink(p_to);\n ELSIF p_props IS NOT NULL THEN\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n IF same = 0 THEN\n UPDATE drivine_edge SET props = new_props WHERE eid = e;\n relink(p_from);\n relink(p_to);\n END IF;\n END IF;\n END;\n\n -- Removes the relationships p_dropped selects from the root, marks both ends, and deletes each removed node\n -- p_unreferenced counts as no longer referred to.\n PROCEDURE trim_edges(p_root NUMBER, p_dropped VARCHAR2, p_keep VARCHAR2, p_unreferenced VARCHAR2, p_held VARCHAR2) IS\n eids SYS.ODCINUMBERLIST;\n nids SYS.ODCINUMBERLIST;\n unreferenced NUMBER;\n BEGIN\n EXECUTE IMMEDIATE p_dropped BULK COLLECT INTO eids, nids USING p_root, p_keep;\n FOR i IN 1 .. eids.COUNT LOOP\n DELETE FROM drivine_edge WHERE eid = eids(i);\n END LOOP;\n FOR i IN 1 .. nids.COUNT LOOP\n relink(nids(i));\n END LOOP;\n IF eids.COUNT > 0 THEN relink(p_root); END IF;\n IF p_unreferenced IS NOT NULL THEN\n FOR i IN 1 .. nids.COUNT LOOP\n IF nids(i) <> p_root THEN\n EXECUTE IMMEDIATE p_unreferenced INTO unreferenced USING nids(i), p_held;\n IF unreferenced > 0 THEN DELETE FROM drivine_node WHERE nid = nids(i); END IF;\n END IF;\n END LOOP;\n END IF;\n END;\n\n -- Writes the lowercased copy of each property p_keys names from what the node holds, and removes the copy of one\n -- that holds nothing. Called once the write's change is told: the copies are no part of it.\n PROCEDURE lower_node(p NUMBER, p_keys VARCHAR2) IS\n held JSON;\n o JSON_OBJECT_T;\n lowered JSON_ARRAY_T := JSON_ARRAY_T.parse(p_keys);\n k VARCHAR2(4000);\n v CLOB;\n BEGIN\n SELECT props INTO held FROM drivine_node WHERE nid = p;\n o := JSON_OBJECT_T(held);\n FOR i IN 0 .. lowered.get_size - 1 LOOP\n k := lowered.get_string(i);\n v := NULL;\n IF o.has(k) AND NOT o.get(k).is_null THEN v := LOWER(o.get_clob(k)); END IF;\n -- Oracle keeps an empty text as null, so an empty property has no copy.\n IF v IS NULL OR LENGTH(v) = 0 THEN o.remove('__drivine.lower.' || k);\n ELSE o.put('__drivine.lower.' || k, v); END IF;\n END LOOP;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n held := o.to_json;\n UPDATE drivine_node SET props = held WHERE nid = p;\n END;\nBEGIN\n v_root := find_node($x_1, $x_2);\n IF v_ok THEN\n IF v_root IS NULL THEN\n v_root := make_node($x_3);\n v_made := TRUE;\n ELSIF v_found IS NULL THEN\n SELECT NVL(stamp, '-') INTO v_found FROM drivine_node WHERE nid = v_root FOR UPDATE;\n END IF;\n restamp(v_root, apply_node(v_root, $x_4, $x_5, $x_6, NULL, FALSE) OR v_made);\n lower_node(v_root, $x_7);\n END IF;\n SELECT /*+ NO_JSON_TABLE_TRANSFORM */ NVL(JSON_ARRAYAGG(JSON_OBJECT('id' VALUE JSON_QUERY(n.props, '$.\"uuid\"'), 'stamp' VALUE NVL(n.stamp, '-')) RETURNING CLOB), '[]') INTO v_before0 FROM drivine_node n WHERE n.nid IN (SELECT /*+ NO_JSON_TABLE_TRANSFORM */ f.nid + 0 FROM JSON_TABLE(JSON($x_16 EXTENDED), '$[*]' COLUMNS (v JSON PATH '$')) i CROSS JOIN drivine_node f WHERE CASE WHEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END IS NOT NULL THEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(f.props, '$.uuid.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) END END = CASE WHEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END IS NOT NULL THEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(i.v, '$.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) END END AND CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END = CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END AND INSTR(',' || LTRIM(RTRIM(JSON_SERIALIZE(f.labels), ']'), '[') || ',\"', ',\"Account\",\"') > 0);\n v_n14 := write_node($x_8, $x_9, $x_10, $x_11, $x_12, $x_13, NULL, FALSE);\n lower_node(v_n14, $x_15);\n link(v_root, v_n14, 'KNOWS', 'OUT', NULL);\n SELECT /*+ NO_JSON_TABLE_TRANSFORM */ NVL(JSON_ARRAYAGG(JSON_OBJECT('id' VALUE JSON_QUERY(n.props, '$.\"uuid\"'), 'stamp' VALUE NVL(n.stamp, '-')) RETURNING CLOB), '[]') INTO v_after0 FROM drivine_node n WHERE n.nid IN (SELECT /*+ NO_JSON_TABLE_TRANSFORM */ f.nid + 0 FROM JSON_TABLE(JSON($x_17 EXTENDED), '$[*]' COLUMNS (v JSON PATH '$')) i CROSS JOIN drivine_node f WHERE CASE WHEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END IS NOT NULL THEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(f.props, '$.uuid.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) END END = CASE WHEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END IS NOT NULL THEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(i.v, '$.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) END END AND CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END = CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END AND INSTR(',' || LTRIM(RTRIM(JSON_SERIALIZE(f.labels), ']'), '[') || ',\"', ',\"Account\",\"') > 0);\n IF v_ok THEN\n OPEN c FOR SELECT JSON_OBJECT('found' VALUE CASE WHEN v_made THEN NULL ELSE v_found END, 'now' VALUE (SELECT stamp FROM drivine_node WHERE nid = v_root), 'related' VALUE JSON_ARRAY(JSON_OBJECT('before' VALUE JSON(v_before0), 'after' VALUE JSON(v_after0) RETURNING JSON) RETURNING JSON) RETURNING CLOB) AS \"result:json\" FROM dual;\n ELSE\n OPEN c FOR SELECT 1 AS \"result\" FROM dual WHERE 1 = 0;\n END IF;\n DBMS_SQL.RETURN_RESULT(c);\nEND;", - "saveAll.accounts": "DECLARE\n v_offered VARCHAR2(40) := $x_stamp;\n v_root NUMBER;\n v_found VARCHAR2(40);\n v_made BOOLEAN := FALSE;\n v_ok BOOLEAN := TRUE;\n c SYS_REFCURSOR;\n v_before0 CLOB;\n v_after0 CLOB;\n\n FUNCTION two_tokens(s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n RETURN s IS NOT NULL AND LENGTH(s) = 33 AND SUBSTR(s, 17, 1) = ':';\n END;\n\n PROCEDURE restamp(p NUMBER, p_changed BOOLEAN) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF p_changed OR NOT two_tokens(s) THEN\n IF two_tokens(s) THEN s := SUBSTR(v_offered, 1, 16) || SUBSTR(s, 17);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END IF;\n END;\n\n PROCEDURE relink(p NUMBER) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF two_tokens(s) THEN s := SUBSTR(s, 1, 17) || SUBSTR(v_offered, 18);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END;\n\n FUNCTION find_node(p_match VARCHAR2, p_id VARCHAR2) RETURN NUMBER IS\n ids SYS.ODCINUMBERLIST;\n BEGIN\n EXECUTE IMMEDIATE 'SELECT nid FROM drivine_node n WHERE ' || p_match BULK COLLECT INTO ids USING p_id;\n IF ids.COUNT > 1 THEN\n RAISE_APPLICATION_ERROR(-20001, ids.COUNT || ' nodes have the id ' || p_id || '. An id is one node''s.');\n END IF;\n RETURN CASE WHEN ids.COUNT = 1 THEN ids(1) END;\n END;\n\n FUNCTION make_node(p_labels VARCHAR2) RETURN NUMBER IS\n v NUMBER;\n BEGIN\n INSERT INTO drivine_node (labels, props) VALUES (JSON(p_labels), JSON('{}')) RETURNING nid INTO v;\n RETURN v;\n END;\n\n FUNCTION has(a JSON_ARRAY_T, s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n FOR i IN 0 .. a.get_size - 1 LOOP\n IF a.get_string(i) = s THEN RETURN TRUE; END IF;\n END LOOP;\n RETURN FALSE;\n END;\n\n -- Merges the patch into the node's properties, adds and removes labels, and records an open label field's labels.\n -- Whether any of it changed the node.\n FUNCTION apply_node(p NUMBER, p_props CLOB, p_add VARCHAR2, p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN)\n RETURN BOOLEAN IS\n old_props JSON;\n new_props JSON;\n old_labels JSON;\n labels JSON_ARRAY_T;\n kept JSON_ARRAY_T := JSON_ARRAY_T();\n additions JSON_ARRAY_T := JSON_ARRAY_T.parse(p_add);\n removals JSON_ARRAY_T := JSON_ARRAY_T.parse(p_remove);\n props JSON_OBJECT_T;\n recorded JSON_ARRAY_T;\n same NUMBER;\n relabelled BOOLEAN := FALSE;\n kept_text VARCHAR2(4000);\n BEGIN\n SELECT props, labels INTO old_props, old_labels FROM drivine_node WHERE nid = p FOR UPDATE;\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n labels := JSON_ARRAY_T.parse(JSON_SERIALIZE(old_labels));\n FOR i IN 0 .. labels.get_size - 1 LOOP\n IF has(removals, labels.get_string(i)) THEN relabelled := TRUE; ELSE kept.append(labels.get_string(i)); END IF;\n END LOOP;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(kept, additions.get_string(i)) THEN kept.append(additions.get_string(i)); relabelled := TRUE; END IF;\n END LOOP;\n IF p_record IS NOT NULL THEN\n props := JSON_OBJECT_T(new_props);\n IF p_replace OR NOT props.has(p_record) THEN recorded := JSON_ARRAY_T();\n ELSE recorded := props.get_array(p_record); END IF;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(recorded, additions.get_string(i)) THEN recorded.append(additions.get_string(i)); END IF;\n END LOOP;\n props.put(p_record, recorded);\n new_props := props.to_json;\n END IF;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n kept_text := kept.to_string();\n UPDATE drivine_node SET props = new_props, labels = JSON(kept_text) WHERE nid = p;\n RETURN same = 0 OR relabelled;\n END;\n\n FUNCTION write_node(p_match VARCHAR2, p_id VARCHAR2, p_labels VARCHAR2, p_props CLOB, p_add VARCHAR2,\n p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN) RETURN NUMBER IS\n v NUMBER := find_node(p_match, p_id);\n made BOOLEAN := v IS NULL;\n BEGIN\n IF made THEN v := make_node(p_labels); END IF;\n restamp(v, apply_node(v, p_props, p_add, p_remove, p_record, p_replace) OR made);\n RETURN v;\n END;\n\n -- Joins two nodes by a relationship of the type, unless one is there (in either direction, for BOTH), and merges\n -- its properties. Both ends are marked when one is made or its properties change.\n PROCEDURE link(p_from NUMBER, p_to NUMBER, p_type VARCHAR2, p_dir VARCHAR2, p_props CLOB) IS\n e NUMBER;\n old_props JSON;\n new_props JSON;\n same NUMBER;\n BEGIN\n BEGIN\n SELECT eid, props INTO e, old_props FROM drivine_edge\n WHERE type = p_type AND ((p_dir IN ('OUT', 'BOTH') AND src = p_from AND dst = p_to)\n OR (p_dir IN ('IN', 'BOTH') AND src = p_to AND dst = p_from))\n FETCH FIRST 1 ROW ONLY;\n EXCEPTION WHEN NO_DATA_FOUND THEN e := NULL;\n END;\n IF e IS NULL THEN\n INSERT INTO drivine_edge (src, dst, type, props) VALUES (CASE WHEN p_dir = 'IN' THEN p_to ELSE p_from END,\n CASE WHEN p_dir = 'IN' THEN p_from ELSE p_to END, p_type, JSON(NVL(p_props, '{}') EXTENDED));\n relink(p_from);\n relink(p_to);\n ELSIF p_props IS NOT NULL THEN\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n IF same = 0 THEN\n UPDATE drivine_edge SET props = new_props WHERE eid = e;\n relink(p_from);\n relink(p_to);\n END IF;\n END IF;\n END;\n\n -- Removes the relationships p_dropped selects from the root, marks both ends, and deletes each removed node\n -- p_unreferenced counts as no longer referred to.\n PROCEDURE trim_edges(p_root NUMBER, p_dropped VARCHAR2, p_keep VARCHAR2, p_unreferenced VARCHAR2, p_held VARCHAR2) IS\n eids SYS.ODCINUMBERLIST;\n nids SYS.ODCINUMBERLIST;\n unreferenced NUMBER;\n BEGIN\n EXECUTE IMMEDIATE p_dropped BULK COLLECT INTO eids, nids USING p_root, p_keep;\n FOR i IN 1 .. eids.COUNT LOOP\n DELETE FROM drivine_edge WHERE eid = eids(i);\n END LOOP;\n FOR i IN 1 .. nids.COUNT LOOP\n relink(nids(i));\n END LOOP;\n IF eids.COUNT > 0 THEN relink(p_root); END IF;\n IF p_unreferenced IS NOT NULL THEN\n FOR i IN 1 .. nids.COUNT LOOP\n IF nids(i) <> p_root THEN\n EXECUTE IMMEDIATE p_unreferenced INTO unreferenced USING nids(i), p_held;\n IF unreferenced > 0 THEN DELETE FROM drivine_node WHERE nid = nids(i); END IF;\n END IF;\n END LOOP;\n END IF;\n END;\n\n -- Writes the lowercased copy of each property p_keys names from what the node holds, and removes the copy of one\n -- that holds nothing. Called once the write's change is told: the copies are no part of it.\n PROCEDURE lower_node(p NUMBER, p_keys VARCHAR2) IS\n held JSON;\n o JSON_OBJECT_T;\n lowered JSON_ARRAY_T := JSON_ARRAY_T.parse(p_keys);\n k VARCHAR2(4000);\n v CLOB;\n BEGIN\n SELECT props INTO held FROM drivine_node WHERE nid = p;\n o := JSON_OBJECT_T(held);\n FOR i IN 0 .. lowered.get_size - 1 LOOP\n k := lowered.get_string(i);\n v := NULL;\n IF o.has(k) AND NOT o.get(k).is_null THEN v := LOWER(o.get_clob(k)); END IF;\n -- Oracle keeps an empty text as null, so an empty property has no copy.\n IF v IS NULL OR LENGTH(v) = 0 THEN o.remove('__drivine.lower.' || k);\n ELSE o.put('__drivine.lower.' || k, v); END IF;\n END LOOP;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n held := o.to_json;\n UPDATE drivine_node SET props = held WHERE nid = p;\n END;\nBEGIN\n SELECT /*+ NO_JSON_TABLE_TRANSFORM */ NVL(JSON_ARRAYAGG(JSON_OBJECT('id' VALUE JSON_QUERY(n.props, '$.\"uuid\"'), 'stamp' VALUE NVL(n.stamp, '-')) RETURNING CLOB), '[]') INTO v_before0 FROM drivine_node n WHERE n.nid IN (SELECT /*+ NO_JSON_TABLE_TRANSFORM */ f.nid + 0 FROM JSON_TABLE(JSON($x_1 EXTENDED), '$[*]' COLUMNS (v JSON PATH '$')) i CROSS JOIN drivine_node f WHERE CASE WHEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END IS NOT NULL THEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(f.props, '$.uuid.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) END END = CASE WHEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END IS NOT NULL THEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(i.v, '$.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) END END AND CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END = CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END AND INSTR(',' || LTRIM(RTRIM(JSON_SERIALIZE(f.labels), ']'), '[') || ',\"', ',\"Account\",\"') > 0);\n v_root := write_node($x_2, $x_3, $x_4, $x_5, '[]', '[]', NULL, FALSE);\n lower_node(v_root, $x_lower0);\n SELECT /*+ NO_JSON_TABLE_TRANSFORM */ NVL(JSON_ARRAYAGG(JSON_OBJECT('id' VALUE JSON_QUERY(n.props, '$.\"uuid\"'), 'stamp' VALUE NVL(n.stamp, '-')) RETURNING CLOB), '[]') INTO v_after0 FROM drivine_node n WHERE n.nid IN (SELECT /*+ NO_JSON_TABLE_TRANSFORM */ f.nid + 0 FROM JSON_TABLE(JSON($x_6 EXTENDED), '$[*]' COLUMNS (v JSON PATH '$')) i CROSS JOIN drivine_node f WHERE CASE WHEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END IS NOT NULL THEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(f.props, '$.uuid.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) END END = CASE WHEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END IS NOT NULL THEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(i.v, '$.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) END END AND CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END = CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END AND INSTR(',' || LTRIM(RTRIM(JSON_SERIALIZE(f.labels), ']'), '[') || ',\"', ',\"Account\",\"') > 0);\n OPEN c FOR SELECT JSON_OBJECT('groups' VALUE JSON_ARRAY(JSON_OBJECT('before' VALUE JSON(v_before0), 'after' VALUE JSON(v_after0) RETURNING JSON) RETURNING JSON) RETURNING CLOB) AS \"result:json\" FROM dual;\n DBMS_SQL.RETURN_RESULT(c);\nEND;", - "Account.patch": "DECLARE\n v_offered VARCHAR2(40) := $x_stamp;\n v_root NUMBER;\n v_found VARCHAR2(40);\n v_made BOOLEAN := FALSE;\n v_ok BOOLEAN := TRUE;\n c SYS_REFCURSOR;\n v_meets NUMBER;\n v_changed BOOLEAN := FALSE;\n v_id VARCHAR2(4000);\n v_patch CLOB;\n\n FUNCTION two_tokens(s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n RETURN s IS NOT NULL AND LENGTH(s) = 33 AND SUBSTR(s, 17, 1) = ':';\n END;\n\n PROCEDURE restamp(p NUMBER, p_changed BOOLEAN) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF p_changed OR NOT two_tokens(s) THEN\n IF two_tokens(s) THEN s := SUBSTR(v_offered, 1, 16) || SUBSTR(s, 17);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END IF;\n END;\n\n PROCEDURE relink(p NUMBER) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF two_tokens(s) THEN s := SUBSTR(s, 1, 17) || SUBSTR(v_offered, 18);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END;\n\n FUNCTION find_node(p_match VARCHAR2, p_id VARCHAR2) RETURN NUMBER IS\n ids SYS.ODCINUMBERLIST;\n BEGIN\n EXECUTE IMMEDIATE 'SELECT nid FROM drivine_node n WHERE ' || p_match BULK COLLECT INTO ids USING p_id;\n IF ids.COUNT > 1 THEN\n RAISE_APPLICATION_ERROR(-20001, ids.COUNT || ' nodes have the id ' || p_id || '. An id is one node''s.');\n END IF;\n RETURN CASE WHEN ids.COUNT = 1 THEN ids(1) END;\n END;\n\n FUNCTION make_node(p_labels VARCHAR2) RETURN NUMBER IS\n v NUMBER;\n BEGIN\n INSERT INTO drivine_node (labels, props) VALUES (JSON(p_labels), JSON('{}')) RETURNING nid INTO v;\n RETURN v;\n END;\n\n FUNCTION has(a JSON_ARRAY_T, s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n FOR i IN 0 .. a.get_size - 1 LOOP\n IF a.get_string(i) = s THEN RETURN TRUE; END IF;\n END LOOP;\n RETURN FALSE;\n END;\n\n -- Merges the patch into the node's properties, adds and removes labels, and records an open label field's labels.\n -- Whether any of it changed the node.\n FUNCTION apply_node(p NUMBER, p_props CLOB, p_add VARCHAR2, p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN)\n RETURN BOOLEAN IS\n old_props JSON;\n new_props JSON;\n old_labels JSON;\n labels JSON_ARRAY_T;\n kept JSON_ARRAY_T := JSON_ARRAY_T();\n additions JSON_ARRAY_T := JSON_ARRAY_T.parse(p_add);\n removals JSON_ARRAY_T := JSON_ARRAY_T.parse(p_remove);\n props JSON_OBJECT_T;\n recorded JSON_ARRAY_T;\n same NUMBER;\n relabelled BOOLEAN := FALSE;\n kept_text VARCHAR2(4000);\n BEGIN\n SELECT props, labels INTO old_props, old_labels FROM drivine_node WHERE nid = p FOR UPDATE;\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n labels := JSON_ARRAY_T.parse(JSON_SERIALIZE(old_labels));\n FOR i IN 0 .. labels.get_size - 1 LOOP\n IF has(removals, labels.get_string(i)) THEN relabelled := TRUE; ELSE kept.append(labels.get_string(i)); END IF;\n END LOOP;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(kept, additions.get_string(i)) THEN kept.append(additions.get_string(i)); relabelled := TRUE; END IF;\n END LOOP;\n IF p_record IS NOT NULL THEN\n props := JSON_OBJECT_T(new_props);\n IF p_replace OR NOT props.has(p_record) THEN recorded := JSON_ARRAY_T();\n ELSE recorded := props.get_array(p_record); END IF;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(recorded, additions.get_string(i)) THEN recorded.append(additions.get_string(i)); END IF;\n END LOOP;\n props.put(p_record, recorded);\n new_props := props.to_json;\n END IF;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n kept_text := kept.to_string();\n UPDATE drivine_node SET props = new_props, labels = JSON(kept_text) WHERE nid = p;\n RETURN same = 0 OR relabelled;\n END;\n\n FUNCTION write_node(p_match VARCHAR2, p_id VARCHAR2, p_labels VARCHAR2, p_props CLOB, p_add VARCHAR2,\n p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN) RETURN NUMBER IS\n v NUMBER := find_node(p_match, p_id);\n made BOOLEAN := v IS NULL;\n BEGIN\n IF made THEN v := make_node(p_labels); END IF;\n restamp(v, apply_node(v, p_props, p_add, p_remove, p_record, p_replace) OR made);\n RETURN v;\n END;\n\n -- Joins two nodes by a relationship of the type, unless one is there (in either direction, for BOTH), and merges\n -- its properties. Both ends are marked when one is made or its properties change.\n PROCEDURE link(p_from NUMBER, p_to NUMBER, p_type VARCHAR2, p_dir VARCHAR2, p_props CLOB) IS\n e NUMBER;\n old_props JSON;\n new_props JSON;\n same NUMBER;\n BEGIN\n BEGIN\n SELECT eid, props INTO e, old_props FROM drivine_edge\n WHERE type = p_type AND ((p_dir IN ('OUT', 'BOTH') AND src = p_from AND dst = p_to)\n OR (p_dir IN ('IN', 'BOTH') AND src = p_to AND dst = p_from))\n FETCH FIRST 1 ROW ONLY;\n EXCEPTION WHEN NO_DATA_FOUND THEN e := NULL;\n END;\n IF e IS NULL THEN\n INSERT INTO drivine_edge (src, dst, type, props) VALUES (CASE WHEN p_dir = 'IN' THEN p_to ELSE p_from END,\n CASE WHEN p_dir = 'IN' THEN p_from ELSE p_to END, p_type, JSON(NVL(p_props, '{}') EXTENDED));\n relink(p_from);\n relink(p_to);\n ELSIF p_props IS NOT NULL THEN\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n IF same = 0 THEN\n UPDATE drivine_edge SET props = new_props WHERE eid = e;\n relink(p_from);\n relink(p_to);\n END IF;\n END IF;\n END;\n\n -- Removes the relationships p_dropped selects from the root, marks both ends, and deletes each removed node\n -- p_unreferenced counts as no longer referred to.\n PROCEDURE trim_edges(p_root NUMBER, p_dropped VARCHAR2, p_keep VARCHAR2, p_unreferenced VARCHAR2, p_held VARCHAR2) IS\n eids SYS.ODCINUMBERLIST;\n nids SYS.ODCINUMBERLIST;\n unreferenced NUMBER;\n BEGIN\n EXECUTE IMMEDIATE p_dropped BULK COLLECT INTO eids, nids USING p_root, p_keep;\n FOR i IN 1 .. eids.COUNT LOOP\n DELETE FROM drivine_edge WHERE eid = eids(i);\n END LOOP;\n FOR i IN 1 .. nids.COUNT LOOP\n relink(nids(i));\n END LOOP;\n IF eids.COUNT > 0 THEN relink(p_root); END IF;\n IF p_unreferenced IS NOT NULL THEN\n FOR i IN 1 .. nids.COUNT LOOP\n IF nids(i) <> p_root THEN\n EXECUTE IMMEDIATE p_unreferenced INTO unreferenced USING nids(i), p_held;\n IF unreferenced > 0 THEN DELETE FROM drivine_node WHERE nid = nids(i); END IF;\n END IF;\n END LOOP;\n END IF;\n END;\n\n -- Writes the lowercased copy of each property p_keys names from what the node holds, and removes the copy of one\n -- that holds nothing. Called once the write's change is told: the copies are no part of it.\n PROCEDURE lower_node(p NUMBER, p_keys VARCHAR2) IS\n held JSON;\n o JSON_OBJECT_T;\n lowered JSON_ARRAY_T := JSON_ARRAY_T.parse(p_keys);\n k VARCHAR2(4000);\n v CLOB;\n BEGIN\n SELECT props INTO held FROM drivine_node WHERE nid = p;\n o := JSON_OBJECT_T(held);\n FOR i IN 0 .. lowered.get_size - 1 LOOP\n k := lowered.get_string(i);\n v := NULL;\n IF o.has(k) AND NOT o.get(k).is_null THEN v := LOWER(o.get_clob(k)); END IF;\n -- Oracle keeps an empty text as null, so an empty property has no copy.\n IF v IS NULL OR LENGTH(v) = 0 THEN o.remove('__drivine.lower.' || k);\n ELSE o.put('__drivine.lower.' || k, v); END IF;\n END LOOP;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n held := o.to_json;\n UPDATE drivine_node SET props = held WHERE nid = p;\n END;\nBEGIN\n v_id := $id;\n v_root := find_node($x_match, v_id);\n IF v_root IS NOT NULL THEN\n SELECT stamp INTO v_found FROM drivine_node WHERE nid = v_root FOR UPDATE;\n v_meets := 1;\n IF v_meets > 0 THEN\n SELECT JSON_SERIALIZE(JSON_MERGEPATCH(JSON($set EXTENDED), JSON(NVL($vectors, '{}') EXTENDED)) RETURNING CLOB EXTENDED) INTO v_patch FROM dual;\n v_changed := apply_node(v_root, v_patch, '[]', '[]', NULL, FALSE);\n restamp(v_root, v_changed);\n IF v_changed THEN lower_node(v_root, $x_lower1); END IF;\n END IF;\n END IF;\n OPEN c FOR SELECT JSON_OBJECT('found' VALUE v_root IS NOT NULL, 'applied' VALUE NVL(v_meets, 0) > 0, 'changed' VALUE v_changed RETURNING CLOB) AS \"result:json\" FROM dual;\n DBMS_SQL.RETURN_RESULT(c);\nEND;", - "Account.patchAll": "DECLARE\n v_offered VARCHAR2(40) := $x_stamp;\n v_root NUMBER;\n v_found VARCHAR2(40);\n v_made BOOLEAN := FALSE;\n v_ok BOOLEAN := TRUE;\n c SYS_REFCURSOR;\n v_changed BOOLEAN;\n v_out SYS.ODCIVARCHAR2LIST := SYS.ODCIVARCHAR2LIST();\n TYPE texts IS TABLE OF CLOB;\n v_ids texts;\n v_patches texts;\n\n FUNCTION two_tokens(s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n RETURN s IS NOT NULL AND LENGTH(s) = 33 AND SUBSTR(s, 17, 1) = ':';\n END;\n\n PROCEDURE restamp(p NUMBER, p_changed BOOLEAN) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF p_changed OR NOT two_tokens(s) THEN\n IF two_tokens(s) THEN s := SUBSTR(v_offered, 1, 16) || SUBSTR(s, 17);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END IF;\n END;\n\n PROCEDURE relink(p NUMBER) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF two_tokens(s) THEN s := SUBSTR(s, 1, 17) || SUBSTR(v_offered, 18);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END;\n\n FUNCTION find_node(p_match VARCHAR2, p_id VARCHAR2) RETURN NUMBER IS\n ids SYS.ODCINUMBERLIST;\n BEGIN\n EXECUTE IMMEDIATE 'SELECT nid FROM drivine_node n WHERE ' || p_match BULK COLLECT INTO ids USING p_id;\n IF ids.COUNT > 1 THEN\n RAISE_APPLICATION_ERROR(-20001, ids.COUNT || ' nodes have the id ' || p_id || '. An id is one node''s.');\n END IF;\n RETURN CASE WHEN ids.COUNT = 1 THEN ids(1) END;\n END;\n\n FUNCTION make_node(p_labels VARCHAR2) RETURN NUMBER IS\n v NUMBER;\n BEGIN\n INSERT INTO drivine_node (labels, props) VALUES (JSON(p_labels), JSON('{}')) RETURNING nid INTO v;\n RETURN v;\n END;\n\n FUNCTION has(a JSON_ARRAY_T, s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n FOR i IN 0 .. a.get_size - 1 LOOP\n IF a.get_string(i) = s THEN RETURN TRUE; END IF;\n END LOOP;\n RETURN FALSE;\n END;\n\n -- Merges the patch into the node's properties, adds and removes labels, and records an open label field's labels.\n -- Whether any of it changed the node.\n FUNCTION apply_node(p NUMBER, p_props CLOB, p_add VARCHAR2, p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN)\n RETURN BOOLEAN IS\n old_props JSON;\n new_props JSON;\n old_labels JSON;\n labels JSON_ARRAY_T;\n kept JSON_ARRAY_T := JSON_ARRAY_T();\n additions JSON_ARRAY_T := JSON_ARRAY_T.parse(p_add);\n removals JSON_ARRAY_T := JSON_ARRAY_T.parse(p_remove);\n props JSON_OBJECT_T;\n recorded JSON_ARRAY_T;\n same NUMBER;\n relabelled BOOLEAN := FALSE;\n kept_text VARCHAR2(4000);\n BEGIN\n SELECT props, labels INTO old_props, old_labels FROM drivine_node WHERE nid = p FOR UPDATE;\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n labels := JSON_ARRAY_T.parse(JSON_SERIALIZE(old_labels));\n FOR i IN 0 .. labels.get_size - 1 LOOP\n IF has(removals, labels.get_string(i)) THEN relabelled := TRUE; ELSE kept.append(labels.get_string(i)); END IF;\n END LOOP;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(kept, additions.get_string(i)) THEN kept.append(additions.get_string(i)); relabelled := TRUE; END IF;\n END LOOP;\n IF p_record IS NOT NULL THEN\n props := JSON_OBJECT_T(new_props);\n IF p_replace OR NOT props.has(p_record) THEN recorded := JSON_ARRAY_T();\n ELSE recorded := props.get_array(p_record); END IF;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(recorded, additions.get_string(i)) THEN recorded.append(additions.get_string(i)); END IF;\n END LOOP;\n props.put(p_record, recorded);\n new_props := props.to_json;\n END IF;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n kept_text := kept.to_string();\n UPDATE drivine_node SET props = new_props, labels = JSON(kept_text) WHERE nid = p;\n RETURN same = 0 OR relabelled;\n END;\n\n FUNCTION write_node(p_match VARCHAR2, p_id VARCHAR2, p_labels VARCHAR2, p_props CLOB, p_add VARCHAR2,\n p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN) RETURN NUMBER IS\n v NUMBER := find_node(p_match, p_id);\n made BOOLEAN := v IS NULL;\n BEGIN\n IF made THEN v := make_node(p_labels); END IF;\n restamp(v, apply_node(v, p_props, p_add, p_remove, p_record, p_replace) OR made);\n RETURN v;\n END;\n\n -- Joins two nodes by a relationship of the type, unless one is there (in either direction, for BOTH), and merges\n -- its properties. Both ends are marked when one is made or its properties change.\n PROCEDURE link(p_from NUMBER, p_to NUMBER, p_type VARCHAR2, p_dir VARCHAR2, p_props CLOB) IS\n e NUMBER;\n old_props JSON;\n new_props JSON;\n same NUMBER;\n BEGIN\n BEGIN\n SELECT eid, props INTO e, old_props FROM drivine_edge\n WHERE type = p_type AND ((p_dir IN ('OUT', 'BOTH') AND src = p_from AND dst = p_to)\n OR (p_dir IN ('IN', 'BOTH') AND src = p_to AND dst = p_from))\n FETCH FIRST 1 ROW ONLY;\n EXCEPTION WHEN NO_DATA_FOUND THEN e := NULL;\n END;\n IF e IS NULL THEN\n INSERT INTO drivine_edge (src, dst, type, props) VALUES (CASE WHEN p_dir = 'IN' THEN p_to ELSE p_from END,\n CASE WHEN p_dir = 'IN' THEN p_from ELSE p_to END, p_type, JSON(NVL(p_props, '{}') EXTENDED));\n relink(p_from);\n relink(p_to);\n ELSIF p_props IS NOT NULL THEN\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n IF same = 0 THEN\n UPDATE drivine_edge SET props = new_props WHERE eid = e;\n relink(p_from);\n relink(p_to);\n END IF;\n END IF;\n END;\n\n -- Removes the relationships p_dropped selects from the root, marks both ends, and deletes each removed node\n -- p_unreferenced counts as no longer referred to.\n PROCEDURE trim_edges(p_root NUMBER, p_dropped VARCHAR2, p_keep VARCHAR2, p_unreferenced VARCHAR2, p_held VARCHAR2) IS\n eids SYS.ODCINUMBERLIST;\n nids SYS.ODCINUMBERLIST;\n unreferenced NUMBER;\n BEGIN\n EXECUTE IMMEDIATE p_dropped BULK COLLECT INTO eids, nids USING p_root, p_keep;\n FOR i IN 1 .. eids.COUNT LOOP\n DELETE FROM drivine_edge WHERE eid = eids(i);\n END LOOP;\n FOR i IN 1 .. nids.COUNT LOOP\n relink(nids(i));\n END LOOP;\n IF eids.COUNT > 0 THEN relink(p_root); END IF;\n IF p_unreferenced IS NOT NULL THEN\n FOR i IN 1 .. nids.COUNT LOOP\n IF nids(i) <> p_root THEN\n EXECUTE IMMEDIATE p_unreferenced INTO unreferenced USING nids(i), p_held;\n IF unreferenced > 0 THEN DELETE FROM drivine_node WHERE nid = nids(i); END IF;\n END IF;\n END LOOP;\n END IF;\n END;\n\n -- Writes the lowercased copy of each property p_keys names from what the node holds, and removes the copy of one\n -- that holds nothing. Called once the write's change is told: the copies are no part of it.\n PROCEDURE lower_node(p NUMBER, p_keys VARCHAR2) IS\n held JSON;\n o JSON_OBJECT_T;\n lowered JSON_ARRAY_T := JSON_ARRAY_T.parse(p_keys);\n k VARCHAR2(4000);\n v CLOB;\n BEGIN\n SELECT props INTO held FROM drivine_node WHERE nid = p;\n o := JSON_OBJECT_T(held);\n FOR i IN 0 .. lowered.get_size - 1 LOOP\n k := lowered.get_string(i);\n v := NULL;\n IF o.has(k) AND NOT o.get(k).is_null THEN v := LOWER(o.get_clob(k)); END IF;\n -- Oracle keeps an empty text as null, so an empty property has no copy.\n IF v IS NULL OR LENGTH(v) = 0 THEN o.remove('__drivine.lower.' || k);\n ELSE o.put('__drivine.lower.' || k, v); END IF;\n END LOOP;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n held := o.to_json;\n UPDATE drivine_node SET props = held WHERE nid = p;\n END;\nBEGIN\n SELECT JSON_SERIALIZE(id RETURNING CLOB EXTENDED), JSON_SERIALIZE(JSON_MERGEPATCH(NVL(props, JSON('{}')), NVL(vectors, JSON('{}'))) RETURNING CLOB EXTENDED) BULK COLLECT INTO v_ids, v_patches FROM JSON_TABLE(JSON($rows EXTENDED), '$[*]' COLUMNS (id JSON PATH '$.id', props JSON PATH '$.props', vectors JSON PATH '$.vectors'));\n FOR i IN 1 .. v_ids.COUNT LOOP\n v_root := find_node($x_match, v_ids(i));\n v_changed := FALSE;\n IF v_root IS NOT NULL THEN\n SELECT stamp INTO v_found FROM drivine_node WHERE nid = v_root FOR UPDATE;\n v_changed := apply_node(v_root, v_patches(i), '[]', '[]', NULL, FALSE);\n restamp(v_root, v_changed);\n IF v_changed THEN lower_node(v_root, $x_lower1); END IF;\n END IF;\n v_out.EXTEND;\n SELECT JSON_SERIALIZE(JSON_OBJECT('id' VALUE JSON(v_ids(i) EXTENDED), 'found' VALUE v_root IS NOT NULL, 'changed' VALUE v_changed)) INTO v_out(v_out.COUNT) FROM dual;\n END LOOP;\n OPEN c FOR SELECT column_value AS \"result:json\" FROM TABLE(v_out);\n DBMS_SQL.RETURN_RESULT(c);\nEND;", + "Account.save": "DECLARE\n v_offered VARCHAR2(40) := $x_stamp;\n v_root NUMBER;\n v_found VARCHAR2(40);\n v_made BOOLEAN := FALSE;\n v_ok BOOLEAN := TRUE;\n c SYS_REFCURSOR;\n\n\n FUNCTION two_tokens(s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n RETURN s IS NOT NULL AND LENGTH(s) = 33 AND SUBSTR(s, 17, 1) = ':';\n END;\n\n PROCEDURE restamp(p NUMBER, p_changed BOOLEAN) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF p_changed OR NOT two_tokens(s) THEN\n IF two_tokens(s) THEN s := SUBSTR(v_offered, 1, 16) || SUBSTR(s, 17);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END IF;\n END;\n\n PROCEDURE relink(p NUMBER) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF two_tokens(s) THEN s := SUBSTR(s, 1, 17) || SUBSTR(v_offered, 18);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END;\n\n FUNCTION find_node(p_match VARCHAR2, p_id VARCHAR2) RETURN NUMBER IS\n ids SYS.ODCINUMBERLIST;\n BEGIN\n EXECUTE IMMEDIATE 'SELECT nid FROM drivine_node n WHERE ' || p_match BULK COLLECT INTO ids USING p_id;\n IF ids.COUNT > 1 THEN\n RAISE_APPLICATION_ERROR(-20001, ids.COUNT || ' nodes have the id ' || p_id || '. An id is one node''s.');\n END IF;\n RETURN CASE WHEN ids.COUNT = 1 THEN ids(1) END;\n END;\n\n FUNCTION make_node(p_labels VARCHAR2) RETURN NUMBER IS\n v NUMBER;\n BEGIN\n INSERT INTO drivine_node (labels, props) VALUES (JSON(p_labels), JSON('{}')) RETURNING nid INTO v;\n RETURN v;\n END;\n\n FUNCTION has(a JSON_ARRAY_T, s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n FOR i IN 0 .. a.get_size - 1 LOOP\n IF a.get_string(i) = s THEN RETURN TRUE; END IF;\n END LOOP;\n RETURN FALSE;\n END;\n\n -- Merges the patch into the node's properties, adds and removes labels, and records an open label field's labels.\n -- Whether any of it changed the node.\n FUNCTION apply_node(p NUMBER, p_props CLOB, p_add VARCHAR2, p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN)\n RETURN BOOLEAN IS\n old_props JSON;\n new_props JSON;\n old_labels JSON;\n labels JSON_ARRAY_T;\n kept JSON_ARRAY_T := JSON_ARRAY_T();\n additions JSON_ARRAY_T := JSON_ARRAY_T.parse(p_add);\n removals JSON_ARRAY_T := JSON_ARRAY_T.parse(p_remove);\n props JSON_OBJECT_T;\n recorded JSON_ARRAY_T;\n same NUMBER;\n relabelled BOOLEAN := FALSE;\n kept_text VARCHAR2(4000);\n BEGIN\n SELECT props, labels INTO old_props, old_labels FROM drivine_node WHERE nid = p FOR UPDATE;\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n labels := JSON_ARRAY_T.parse(JSON_SERIALIZE(old_labels));\n FOR i IN 0 .. labels.get_size - 1 LOOP\n IF has(removals, labels.get_string(i)) THEN relabelled := TRUE; ELSE kept.append(labels.get_string(i)); END IF;\n END LOOP;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(kept, additions.get_string(i)) THEN kept.append(additions.get_string(i)); relabelled := TRUE; END IF;\n END LOOP;\n IF p_record IS NOT NULL THEN\n props := JSON_OBJECT_T(new_props);\n IF p_replace OR NOT props.has(p_record) THEN recorded := JSON_ARRAY_T();\n ELSE recorded := props.get_array(p_record); END IF;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(recorded, additions.get_string(i)) THEN recorded.append(additions.get_string(i)); END IF;\n END LOOP;\n props.put(p_record, recorded);\n new_props := props.to_json;\n END IF;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n kept_text := kept.to_string();\n UPDATE drivine_node SET props = new_props, labels = JSON(kept_text) WHERE nid = p;\n RETURN same = 0 OR relabelled;\n END;\n\n FUNCTION write_node(p_match VARCHAR2, p_id VARCHAR2, p_labels VARCHAR2, p_props CLOB, p_add VARCHAR2,\n p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN) RETURN NUMBER IS\n v NUMBER := find_node(p_match, p_id);\n made BOOLEAN := v IS NULL;\n BEGIN\n IF made THEN v := make_node(p_labels); END IF;\n restamp(v, apply_node(v, p_props, p_add, p_remove, p_record, p_replace) OR made);\n RETURN v;\n END;\n\n -- Joins two nodes by a relationship of the type, unless one is there (in either direction, for BOTH), and merges\n -- its properties. Both ends are marked when one is made or its properties change.\n PROCEDURE link(p_from NUMBER, p_to NUMBER, p_type VARCHAR2, p_dir VARCHAR2, p_props CLOB) IS\n e NUMBER;\n old_props JSON;\n new_props JSON;\n same NUMBER;\n BEGIN\n BEGIN\n SELECT eid, props INTO e, old_props FROM drivine_edge\n WHERE type = p_type AND ((p_dir IN ('OUT', 'BOTH') AND src = p_from AND dst = p_to)\n OR (p_dir IN ('IN', 'BOTH') AND src = p_to AND dst = p_from))\n FETCH FIRST 1 ROW ONLY;\n EXCEPTION WHEN NO_DATA_FOUND THEN e := NULL;\n END;\n IF e IS NULL THEN\n INSERT INTO drivine_edge (src, dst, type, props) VALUES (CASE WHEN p_dir = 'IN' THEN p_to ELSE p_from END,\n CASE WHEN p_dir = 'IN' THEN p_from ELSE p_to END, p_type, JSON(NVL(p_props, '{}') EXTENDED));\n relink(p_from);\n relink(p_to);\n ELSIF p_props IS NOT NULL THEN\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n IF same = 0 THEN\n UPDATE drivine_edge SET props = new_props WHERE eid = e;\n relink(p_from);\n relink(p_to);\n END IF;\n END IF;\n END;\n\n -- Removes the relationships p_dropped selects from the root, marks both ends, and deletes each removed node\n -- p_unreferenced counts as no longer referred to.\n PROCEDURE trim_edges(p_root NUMBER, p_dropped VARCHAR2, p_keep VARCHAR2, p_unreferenced VARCHAR2, p_held VARCHAR2) IS\n eids SYS.ODCINUMBERLIST;\n nids SYS.ODCINUMBERLIST;\n unreferenced NUMBER;\n BEGIN\n EXECUTE IMMEDIATE p_dropped BULK COLLECT INTO eids, nids USING p_root, p_keep;\n FOR i IN 1 .. eids.COUNT LOOP\n DELETE FROM drivine_edge WHERE eid = eids(i);\n END LOOP;\n FOR i IN 1 .. nids.COUNT LOOP\n relink(nids(i));\n END LOOP;\n IF eids.COUNT > 0 THEN relink(p_root); END IF;\n IF p_unreferenced IS NOT NULL THEN\n FOR i IN 1 .. nids.COUNT LOOP\n IF nids(i) <> p_root THEN\n EXECUTE IMMEDIATE p_unreferenced INTO unreferenced USING nids(i), p_held;\n IF unreferenced > 0 THEN DELETE FROM drivine_node WHERE nid = nids(i); END IF;\n END IF;\n END LOOP;\n END IF;\n END;\n\n -- Writes the lowercased copy of each property p_keys names from what the node holds, and removes the copy of one\n -- that holds nothing. Called once the write's change is told: the copies are no part of it.\n PROCEDURE lower_node(p NUMBER, p_keys VARCHAR2) IS\n held JSON;\n o JSON_OBJECT_T;\n lowered JSON_ARRAY_T := JSON_ARRAY_T.parse(p_keys);\n k VARCHAR2(4000);\n c VARCHAR2(4000);\n v CLOB;\n BEGIN\n SELECT props INTO held FROM drivine_node WHERE nid = p;\n o := JSON_OBJECT_T(held);\n FOR i IN 0 .. lowered.get_size - 1 LOOP\n k := lowered.get_string(i);\n -- k is the property as the node holds it; its copy is named for the user's name, $mail for $$mail.\n c := '__drivine.lower.' || CASE WHEN SUBSTR(k, 1, 2) = '$$' THEN SUBSTR(k, 2) ELSE k END;\n v := NULL;\n IF o.has(k) AND NOT o.get(k).is_null THEN v := LOWER(o.get_clob(k)); END IF;\n -- Oracle keeps an empty text as null, so an empty property has no copy.\n IF v IS NULL OR LENGTH(v) = 0 THEN o.remove(c);\n ELSE o.put(c, v); END IF;\n END LOOP;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n held := o.to_json;\n UPDATE drivine_node SET props = held WHERE nid = p;\n END;\nBEGIN\n v_root := find_node($x_1, $x_2);\n IF v_ok THEN\n IF v_root IS NULL THEN\n v_root := make_node($x_3);\n v_made := TRUE;\n ELSIF v_found IS NULL THEN\n SELECT NVL(stamp, '-') INTO v_found FROM drivine_node WHERE nid = v_root FOR UPDATE;\n END IF;\n restamp(v_root, apply_node(v_root, $x_4, $x_5, $x_6, NULL, FALSE) OR v_made);\n lower_node(v_root, $x_7);\n END IF;\n IF v_ok THEN\n OPEN c FOR SELECT JSON_OBJECT('found' VALUE CASE WHEN v_made THEN NULL ELSE v_found END, 'now' VALUE (SELECT stamp FROM drivine_node WHERE nid = v_root), 'related' VALUE JSON('[]') RETURNING CLOB) AS \"result:json\" FROM dual;\n ELSE\n OPEN c FOR SELECT 1 AS \"result\" FROM dual WHERE 1 = 0;\n END IF;\n DBMS_SQL.RETURN_RESULT(c);\nEND;", + "AccountFriends.save": "DECLARE\n v_offered VARCHAR2(40) := $x_stamp;\n v_root NUMBER;\n v_found VARCHAR2(40);\n v_made BOOLEAN := FALSE;\n v_ok BOOLEAN := TRUE;\n c SYS_REFCURSOR;\n v_n14 NUMBER;\n v_before0 CLOB;\n v_after0 CLOB;\n\n FUNCTION two_tokens(s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n RETURN s IS NOT NULL AND LENGTH(s) = 33 AND SUBSTR(s, 17, 1) = ':';\n END;\n\n PROCEDURE restamp(p NUMBER, p_changed BOOLEAN) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF p_changed OR NOT two_tokens(s) THEN\n IF two_tokens(s) THEN s := SUBSTR(v_offered, 1, 16) || SUBSTR(s, 17);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END IF;\n END;\n\n PROCEDURE relink(p NUMBER) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF two_tokens(s) THEN s := SUBSTR(s, 1, 17) || SUBSTR(v_offered, 18);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END;\n\n FUNCTION find_node(p_match VARCHAR2, p_id VARCHAR2) RETURN NUMBER IS\n ids SYS.ODCINUMBERLIST;\n BEGIN\n EXECUTE IMMEDIATE 'SELECT nid FROM drivine_node n WHERE ' || p_match BULK COLLECT INTO ids USING p_id;\n IF ids.COUNT > 1 THEN\n RAISE_APPLICATION_ERROR(-20001, ids.COUNT || ' nodes have the id ' || p_id || '. An id is one node''s.');\n END IF;\n RETURN CASE WHEN ids.COUNT = 1 THEN ids(1) END;\n END;\n\n FUNCTION make_node(p_labels VARCHAR2) RETURN NUMBER IS\n v NUMBER;\n BEGIN\n INSERT INTO drivine_node (labels, props) VALUES (JSON(p_labels), JSON('{}')) RETURNING nid INTO v;\n RETURN v;\n END;\n\n FUNCTION has(a JSON_ARRAY_T, s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n FOR i IN 0 .. a.get_size - 1 LOOP\n IF a.get_string(i) = s THEN RETURN TRUE; END IF;\n END LOOP;\n RETURN FALSE;\n END;\n\n -- Merges the patch into the node's properties, adds and removes labels, and records an open label field's labels.\n -- Whether any of it changed the node.\n FUNCTION apply_node(p NUMBER, p_props CLOB, p_add VARCHAR2, p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN)\n RETURN BOOLEAN IS\n old_props JSON;\n new_props JSON;\n old_labels JSON;\n labels JSON_ARRAY_T;\n kept JSON_ARRAY_T := JSON_ARRAY_T();\n additions JSON_ARRAY_T := JSON_ARRAY_T.parse(p_add);\n removals JSON_ARRAY_T := JSON_ARRAY_T.parse(p_remove);\n props JSON_OBJECT_T;\n recorded JSON_ARRAY_T;\n same NUMBER;\n relabelled BOOLEAN := FALSE;\n kept_text VARCHAR2(4000);\n BEGIN\n SELECT props, labels INTO old_props, old_labels FROM drivine_node WHERE nid = p FOR UPDATE;\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n labels := JSON_ARRAY_T.parse(JSON_SERIALIZE(old_labels));\n FOR i IN 0 .. labels.get_size - 1 LOOP\n IF has(removals, labels.get_string(i)) THEN relabelled := TRUE; ELSE kept.append(labels.get_string(i)); END IF;\n END LOOP;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(kept, additions.get_string(i)) THEN kept.append(additions.get_string(i)); relabelled := TRUE; END IF;\n END LOOP;\n IF p_record IS NOT NULL THEN\n props := JSON_OBJECT_T(new_props);\n IF p_replace OR NOT props.has(p_record) THEN recorded := JSON_ARRAY_T();\n ELSE recorded := props.get_array(p_record); END IF;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(recorded, additions.get_string(i)) THEN recorded.append(additions.get_string(i)); END IF;\n END LOOP;\n props.put(p_record, recorded);\n new_props := props.to_json;\n END IF;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n kept_text := kept.to_string();\n UPDATE drivine_node SET props = new_props, labels = JSON(kept_text) WHERE nid = p;\n RETURN same = 0 OR relabelled;\n END;\n\n FUNCTION write_node(p_match VARCHAR2, p_id VARCHAR2, p_labels VARCHAR2, p_props CLOB, p_add VARCHAR2,\n p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN) RETURN NUMBER IS\n v NUMBER := find_node(p_match, p_id);\n made BOOLEAN := v IS NULL;\n BEGIN\n IF made THEN v := make_node(p_labels); END IF;\n restamp(v, apply_node(v, p_props, p_add, p_remove, p_record, p_replace) OR made);\n RETURN v;\n END;\n\n -- Joins two nodes by a relationship of the type, unless one is there (in either direction, for BOTH), and merges\n -- its properties. Both ends are marked when one is made or its properties change.\n PROCEDURE link(p_from NUMBER, p_to NUMBER, p_type VARCHAR2, p_dir VARCHAR2, p_props CLOB) IS\n e NUMBER;\n old_props JSON;\n new_props JSON;\n same NUMBER;\n BEGIN\n BEGIN\n SELECT eid, props INTO e, old_props FROM drivine_edge\n WHERE type = p_type AND ((p_dir IN ('OUT', 'BOTH') AND src = p_from AND dst = p_to)\n OR (p_dir IN ('IN', 'BOTH') AND src = p_to AND dst = p_from))\n FETCH FIRST 1 ROW ONLY;\n EXCEPTION WHEN NO_DATA_FOUND THEN e := NULL;\n END;\n IF e IS NULL THEN\n INSERT INTO drivine_edge (src, dst, type, props) VALUES (CASE WHEN p_dir = 'IN' THEN p_to ELSE p_from END,\n CASE WHEN p_dir = 'IN' THEN p_from ELSE p_to END, p_type, JSON(NVL(p_props, '{}') EXTENDED));\n relink(p_from);\n relink(p_to);\n ELSIF p_props IS NOT NULL THEN\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n IF same = 0 THEN\n UPDATE drivine_edge SET props = new_props WHERE eid = e;\n relink(p_from);\n relink(p_to);\n END IF;\n END IF;\n END;\n\n -- Removes the relationships p_dropped selects from the root, marks both ends, and deletes each removed node\n -- p_unreferenced counts as no longer referred to.\n PROCEDURE trim_edges(p_root NUMBER, p_dropped VARCHAR2, p_keep VARCHAR2, p_unreferenced VARCHAR2, p_held VARCHAR2) IS\n eids SYS.ODCINUMBERLIST;\n nids SYS.ODCINUMBERLIST;\n unreferenced NUMBER;\n BEGIN\n EXECUTE IMMEDIATE p_dropped BULK COLLECT INTO eids, nids USING p_root, p_keep;\n FOR i IN 1 .. eids.COUNT LOOP\n DELETE FROM drivine_edge WHERE eid = eids(i);\n END LOOP;\n FOR i IN 1 .. nids.COUNT LOOP\n relink(nids(i));\n END LOOP;\n IF eids.COUNT > 0 THEN relink(p_root); END IF;\n IF p_unreferenced IS NOT NULL THEN\n FOR i IN 1 .. nids.COUNT LOOP\n IF nids(i) <> p_root THEN\n EXECUTE IMMEDIATE p_unreferenced INTO unreferenced USING nids(i), p_held;\n IF unreferenced > 0 THEN DELETE FROM drivine_node WHERE nid = nids(i); END IF;\n END IF;\n END LOOP;\n END IF;\n END;\n\n -- Writes the lowercased copy of each property p_keys names from what the node holds, and removes the copy of one\n -- that holds nothing. Called once the write's change is told: the copies are no part of it.\n PROCEDURE lower_node(p NUMBER, p_keys VARCHAR2) IS\n held JSON;\n o JSON_OBJECT_T;\n lowered JSON_ARRAY_T := JSON_ARRAY_T.parse(p_keys);\n k VARCHAR2(4000);\n c VARCHAR2(4000);\n v CLOB;\n BEGIN\n SELECT props INTO held FROM drivine_node WHERE nid = p;\n o := JSON_OBJECT_T(held);\n FOR i IN 0 .. lowered.get_size - 1 LOOP\n k := lowered.get_string(i);\n -- k is the property as the node holds it; its copy is named for the user's name, $mail for $$mail.\n c := '__drivine.lower.' || CASE WHEN SUBSTR(k, 1, 2) = '$$' THEN SUBSTR(k, 2) ELSE k END;\n v := NULL;\n IF o.has(k) AND NOT o.get(k).is_null THEN v := LOWER(o.get_clob(k)); END IF;\n -- Oracle keeps an empty text as null, so an empty property has no copy.\n IF v IS NULL OR LENGTH(v) = 0 THEN o.remove(c);\n ELSE o.put(c, v); END IF;\n END LOOP;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n held := o.to_json;\n UPDATE drivine_node SET props = held WHERE nid = p;\n END;\nBEGIN\n v_root := find_node($x_1, $x_2);\n IF v_ok THEN\n IF v_root IS NULL THEN\n v_root := make_node($x_3);\n v_made := TRUE;\n ELSIF v_found IS NULL THEN\n SELECT NVL(stamp, '-') INTO v_found FROM drivine_node WHERE nid = v_root FOR UPDATE;\n END IF;\n restamp(v_root, apply_node(v_root, $x_4, $x_5, $x_6, NULL, FALSE) OR v_made);\n lower_node(v_root, $x_7);\n END IF;\n SELECT /*+ NO_JSON_TABLE_TRANSFORM */ NVL(JSON_ARRAYAGG(JSON_OBJECT('id' VALUE JSON_QUERY(n.props, '$.\"uuid\"'), 'stamp' VALUE NVL(n.stamp, '-')) RETURNING CLOB), '[]') INTO v_before0 FROM drivine_node n WHERE n.nid IN (SELECT /*+ NO_JSON_TABLE_TRANSFORM */ f.nid + 0 FROM JSON_TABLE(JSON($x_16 EXTENDED), '$[*]' COLUMNS (v JSON PATH '$')) i CROSS JOIN drivine_node f WHERE CASE WHEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END IS NOT NULL THEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(f.props, '$.uuid.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) END END = CASE WHEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END IS NOT NULL THEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(i.v, '$.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) END END AND CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END = CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END AND INSTR(',' || LTRIM(RTRIM(JSON_SERIALIZE(f.labels), ']'), '[') || ',\"', ',\"Account\",\"') > 0);\n v_n14 := write_node($x_8, $x_9, $x_10, $x_11, $x_12, $x_13, NULL, FALSE);\n lower_node(v_n14, $x_15);\n link(v_root, v_n14, 'KNOWS', 'OUT', NULL);\n SELECT /*+ NO_JSON_TABLE_TRANSFORM */ NVL(JSON_ARRAYAGG(JSON_OBJECT('id' VALUE JSON_QUERY(n.props, '$.\"uuid\"'), 'stamp' VALUE NVL(n.stamp, '-')) RETURNING CLOB), '[]') INTO v_after0 FROM drivine_node n WHERE n.nid IN (SELECT /*+ NO_JSON_TABLE_TRANSFORM */ f.nid + 0 FROM JSON_TABLE(JSON($x_17 EXTENDED), '$[*]' COLUMNS (v JSON PATH '$')) i CROSS JOIN drivine_node f WHERE CASE WHEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END IS NOT NULL THEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(f.props, '$.uuid.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) END END = CASE WHEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END IS NOT NULL THEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(i.v, '$.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) END END AND CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END = CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END AND INSTR(',' || LTRIM(RTRIM(JSON_SERIALIZE(f.labels), ']'), '[') || ',\"', ',\"Account\",\"') > 0);\n IF v_ok THEN\n OPEN c FOR SELECT JSON_OBJECT('found' VALUE CASE WHEN v_made THEN NULL ELSE v_found END, 'now' VALUE (SELECT stamp FROM drivine_node WHERE nid = v_root), 'related' VALUE JSON_ARRAY(JSON_OBJECT('before' VALUE JSON(v_before0), 'after' VALUE JSON(v_after0) RETURNING JSON) RETURNING JSON) RETURNING CLOB) AS \"result:json\" FROM dual;\n ELSE\n OPEN c FOR SELECT 1 AS \"result\" FROM dual WHERE 1 = 0;\n END IF;\n DBMS_SQL.RETURN_RESULT(c);\nEND;", + "saveAll.accounts": "DECLARE\n v_offered VARCHAR2(40) := $x_stamp;\n v_root NUMBER;\n v_found VARCHAR2(40);\n v_made BOOLEAN := FALSE;\n v_ok BOOLEAN := TRUE;\n c SYS_REFCURSOR;\n v_before0 CLOB;\n v_after0 CLOB;\n\n FUNCTION two_tokens(s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n RETURN s IS NOT NULL AND LENGTH(s) = 33 AND SUBSTR(s, 17, 1) = ':';\n END;\n\n PROCEDURE restamp(p NUMBER, p_changed BOOLEAN) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF p_changed OR NOT two_tokens(s) THEN\n IF two_tokens(s) THEN s := SUBSTR(v_offered, 1, 16) || SUBSTR(s, 17);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END IF;\n END;\n\n PROCEDURE relink(p NUMBER) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF two_tokens(s) THEN s := SUBSTR(s, 1, 17) || SUBSTR(v_offered, 18);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END;\n\n FUNCTION find_node(p_match VARCHAR2, p_id VARCHAR2) RETURN NUMBER IS\n ids SYS.ODCINUMBERLIST;\n BEGIN\n EXECUTE IMMEDIATE 'SELECT nid FROM drivine_node n WHERE ' || p_match BULK COLLECT INTO ids USING p_id;\n IF ids.COUNT > 1 THEN\n RAISE_APPLICATION_ERROR(-20001, ids.COUNT || ' nodes have the id ' || p_id || '. An id is one node''s.');\n END IF;\n RETURN CASE WHEN ids.COUNT = 1 THEN ids(1) END;\n END;\n\n FUNCTION make_node(p_labels VARCHAR2) RETURN NUMBER IS\n v NUMBER;\n BEGIN\n INSERT INTO drivine_node (labels, props) VALUES (JSON(p_labels), JSON('{}')) RETURNING nid INTO v;\n RETURN v;\n END;\n\n FUNCTION has(a JSON_ARRAY_T, s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n FOR i IN 0 .. a.get_size - 1 LOOP\n IF a.get_string(i) = s THEN RETURN TRUE; END IF;\n END LOOP;\n RETURN FALSE;\n END;\n\n -- Merges the patch into the node's properties, adds and removes labels, and records an open label field's labels.\n -- Whether any of it changed the node.\n FUNCTION apply_node(p NUMBER, p_props CLOB, p_add VARCHAR2, p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN)\n RETURN BOOLEAN IS\n old_props JSON;\n new_props JSON;\n old_labels JSON;\n labels JSON_ARRAY_T;\n kept JSON_ARRAY_T := JSON_ARRAY_T();\n additions JSON_ARRAY_T := JSON_ARRAY_T.parse(p_add);\n removals JSON_ARRAY_T := JSON_ARRAY_T.parse(p_remove);\n props JSON_OBJECT_T;\n recorded JSON_ARRAY_T;\n same NUMBER;\n relabelled BOOLEAN := FALSE;\n kept_text VARCHAR2(4000);\n BEGIN\n SELECT props, labels INTO old_props, old_labels FROM drivine_node WHERE nid = p FOR UPDATE;\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n labels := JSON_ARRAY_T.parse(JSON_SERIALIZE(old_labels));\n FOR i IN 0 .. labels.get_size - 1 LOOP\n IF has(removals, labels.get_string(i)) THEN relabelled := TRUE; ELSE kept.append(labels.get_string(i)); END IF;\n END LOOP;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(kept, additions.get_string(i)) THEN kept.append(additions.get_string(i)); relabelled := TRUE; END IF;\n END LOOP;\n IF p_record IS NOT NULL THEN\n props := JSON_OBJECT_T(new_props);\n IF p_replace OR NOT props.has(p_record) THEN recorded := JSON_ARRAY_T();\n ELSE recorded := props.get_array(p_record); END IF;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(recorded, additions.get_string(i)) THEN recorded.append(additions.get_string(i)); END IF;\n END LOOP;\n props.put(p_record, recorded);\n new_props := props.to_json;\n END IF;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n kept_text := kept.to_string();\n UPDATE drivine_node SET props = new_props, labels = JSON(kept_text) WHERE nid = p;\n RETURN same = 0 OR relabelled;\n END;\n\n FUNCTION write_node(p_match VARCHAR2, p_id VARCHAR2, p_labels VARCHAR2, p_props CLOB, p_add VARCHAR2,\n p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN) RETURN NUMBER IS\n v NUMBER := find_node(p_match, p_id);\n made BOOLEAN := v IS NULL;\n BEGIN\n IF made THEN v := make_node(p_labels); END IF;\n restamp(v, apply_node(v, p_props, p_add, p_remove, p_record, p_replace) OR made);\n RETURN v;\n END;\n\n -- Joins two nodes by a relationship of the type, unless one is there (in either direction, for BOTH), and merges\n -- its properties. Both ends are marked when one is made or its properties change.\n PROCEDURE link(p_from NUMBER, p_to NUMBER, p_type VARCHAR2, p_dir VARCHAR2, p_props CLOB) IS\n e NUMBER;\n old_props JSON;\n new_props JSON;\n same NUMBER;\n BEGIN\n BEGIN\n SELECT eid, props INTO e, old_props FROM drivine_edge\n WHERE type = p_type AND ((p_dir IN ('OUT', 'BOTH') AND src = p_from AND dst = p_to)\n OR (p_dir IN ('IN', 'BOTH') AND src = p_to AND dst = p_from))\n FETCH FIRST 1 ROW ONLY;\n EXCEPTION WHEN NO_DATA_FOUND THEN e := NULL;\n END;\n IF e IS NULL THEN\n INSERT INTO drivine_edge (src, dst, type, props) VALUES (CASE WHEN p_dir = 'IN' THEN p_to ELSE p_from END,\n CASE WHEN p_dir = 'IN' THEN p_from ELSE p_to END, p_type, JSON(NVL(p_props, '{}') EXTENDED));\n relink(p_from);\n relink(p_to);\n ELSIF p_props IS NOT NULL THEN\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n IF same = 0 THEN\n UPDATE drivine_edge SET props = new_props WHERE eid = e;\n relink(p_from);\n relink(p_to);\n END IF;\n END IF;\n END;\n\n -- Removes the relationships p_dropped selects from the root, marks both ends, and deletes each removed node\n -- p_unreferenced counts as no longer referred to.\n PROCEDURE trim_edges(p_root NUMBER, p_dropped VARCHAR2, p_keep VARCHAR2, p_unreferenced VARCHAR2, p_held VARCHAR2) IS\n eids SYS.ODCINUMBERLIST;\n nids SYS.ODCINUMBERLIST;\n unreferenced NUMBER;\n BEGIN\n EXECUTE IMMEDIATE p_dropped BULK COLLECT INTO eids, nids USING p_root, p_keep;\n FOR i IN 1 .. eids.COUNT LOOP\n DELETE FROM drivine_edge WHERE eid = eids(i);\n END LOOP;\n FOR i IN 1 .. nids.COUNT LOOP\n relink(nids(i));\n END LOOP;\n IF eids.COUNT > 0 THEN relink(p_root); END IF;\n IF p_unreferenced IS NOT NULL THEN\n FOR i IN 1 .. nids.COUNT LOOP\n IF nids(i) <> p_root THEN\n EXECUTE IMMEDIATE p_unreferenced INTO unreferenced USING nids(i), p_held;\n IF unreferenced > 0 THEN DELETE FROM drivine_node WHERE nid = nids(i); END IF;\n END IF;\n END LOOP;\n END IF;\n END;\n\n -- Writes the lowercased copy of each property p_keys names from what the node holds, and removes the copy of one\n -- that holds nothing. Called once the write's change is told: the copies are no part of it.\n PROCEDURE lower_node(p NUMBER, p_keys VARCHAR2) IS\n held JSON;\n o JSON_OBJECT_T;\n lowered JSON_ARRAY_T := JSON_ARRAY_T.parse(p_keys);\n k VARCHAR2(4000);\n c VARCHAR2(4000);\n v CLOB;\n BEGIN\n SELECT props INTO held FROM drivine_node WHERE nid = p;\n o := JSON_OBJECT_T(held);\n FOR i IN 0 .. lowered.get_size - 1 LOOP\n k := lowered.get_string(i);\n -- k is the property as the node holds it; its copy is named for the user's name, $mail for $$mail.\n c := '__drivine.lower.' || CASE WHEN SUBSTR(k, 1, 2) = '$$' THEN SUBSTR(k, 2) ELSE k END;\n v := NULL;\n IF o.has(k) AND NOT o.get(k).is_null THEN v := LOWER(o.get_clob(k)); END IF;\n -- Oracle keeps an empty text as null, so an empty property has no copy.\n IF v IS NULL OR LENGTH(v) = 0 THEN o.remove(c);\n ELSE o.put(c, v); END IF;\n END LOOP;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n held := o.to_json;\n UPDATE drivine_node SET props = held WHERE nid = p;\n END;\nBEGIN\n SELECT /*+ NO_JSON_TABLE_TRANSFORM */ NVL(JSON_ARRAYAGG(JSON_OBJECT('id' VALUE JSON_QUERY(n.props, '$.\"uuid\"'), 'stamp' VALUE NVL(n.stamp, '-')) RETURNING CLOB), '[]') INTO v_before0 FROM drivine_node n WHERE n.nid IN (SELECT /*+ NO_JSON_TABLE_TRANSFORM */ f.nid + 0 FROM JSON_TABLE(JSON($x_1 EXTENDED), '$[*]' COLUMNS (v JSON PATH '$')) i CROSS JOIN drivine_node f WHERE CASE WHEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END IS NOT NULL THEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(f.props, '$.uuid.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) END END = CASE WHEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END IS NOT NULL THEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(i.v, '$.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) END END AND CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END = CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END AND INSTR(',' || LTRIM(RTRIM(JSON_SERIALIZE(f.labels), ']'), '[') || ',\"', ',\"Account\",\"') > 0);\n v_root := write_node($x_2, $x_3, $x_4, $x_5, '[]', '[]', NULL, FALSE);\n lower_node(v_root, $x_lower0);\n SELECT /*+ NO_JSON_TABLE_TRANSFORM */ NVL(JSON_ARRAYAGG(JSON_OBJECT('id' VALUE JSON_QUERY(n.props, '$.\"uuid\"'), 'stamp' VALUE NVL(n.stamp, '-')) RETURNING CLOB), '[]') INTO v_after0 FROM drivine_node n WHERE n.nid IN (SELECT /*+ NO_JSON_TABLE_TRANSFORM */ f.nid + 0 FROM JSON_TABLE(JSON($x_6 EXTENDED), '$[*]' COLUMNS (v JSON PATH '$')) i CROSS JOIN drivine_node f WHERE CASE WHEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END IS NOT NULL THEN CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(f.props, '$.uuid.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) END END = CASE WHEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END IS NOT NULL THEN CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN JSON_VALUE(i.v, '$.\"$t\"' RETURNING VARCHAR2(16)) WHEN 'double' THEN 'number' ELSE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) END END AND CASE JSON_VALUE(f.props, '$.uuid.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(f.props, '$.uuid.s'), JSON_VALUE(f.props, '$.uuid.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(f.props, '$.uuid' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(f.props, '$.uuid') END = CASE JSON_VALUE(i.v, '$.type()' RETURNING VARCHAR2(16)) WHEN 'object' THEN COALESCE(JSON_VALUE(i.v, '$.s'), JSON_VALUE(i.v, '$.v')) WHEN 'number' THEN TO_CHAR(JSON_VALUE(i.v, '$' RETURNING NUMBER), 'TM9') WHEN 'double' THEN CASE WHEN JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) = FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) >= -9223372036854775808 AND JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) < 9223372036854775808 THEN TO_CHAR(CAST(FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) AS NUMBER) * 4294967296 + CAST(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) - FLOOR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE) / 4294967296) * 4294967296 AS NUMBER), 'TM9') ELSE 'd' || TO_CHAR(JSON_VALUE(i.v, '$' RETURNING BINARY_DOUBLE)) END ELSE JSON_VALUE(i.v, '$') END AND INSTR(',' || LTRIM(RTRIM(JSON_SERIALIZE(f.labels), ']'), '[') || ',\"', ',\"Account\",\"') > 0);\n OPEN c FOR SELECT JSON_OBJECT('groups' VALUE JSON_ARRAY(JSON_OBJECT('before' VALUE JSON(v_before0), 'after' VALUE JSON(v_after0) RETURNING JSON) RETURNING JSON) RETURNING CLOB) AS \"result:json\" FROM dual;\n DBMS_SQL.RETURN_RESULT(c);\nEND;", + "Account.patch": "DECLARE\n v_offered VARCHAR2(40) := $x_stamp;\n v_root NUMBER;\n v_found VARCHAR2(40);\n v_made BOOLEAN := FALSE;\n v_ok BOOLEAN := TRUE;\n c SYS_REFCURSOR;\n v_meets NUMBER;\n v_changed BOOLEAN := FALSE;\n v_id VARCHAR2(4000);\n v_patch CLOB;\n\n FUNCTION two_tokens(s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n RETURN s IS NOT NULL AND LENGTH(s) = 33 AND SUBSTR(s, 17, 1) = ':';\n END;\n\n PROCEDURE restamp(p NUMBER, p_changed BOOLEAN) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF p_changed OR NOT two_tokens(s) THEN\n IF two_tokens(s) THEN s := SUBSTR(v_offered, 1, 16) || SUBSTR(s, 17);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END IF;\n END;\n\n PROCEDURE relink(p NUMBER) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF two_tokens(s) THEN s := SUBSTR(s, 1, 17) || SUBSTR(v_offered, 18);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END;\n\n FUNCTION find_node(p_match VARCHAR2, p_id VARCHAR2) RETURN NUMBER IS\n ids SYS.ODCINUMBERLIST;\n BEGIN\n EXECUTE IMMEDIATE 'SELECT nid FROM drivine_node n WHERE ' || p_match BULK COLLECT INTO ids USING p_id;\n IF ids.COUNT > 1 THEN\n RAISE_APPLICATION_ERROR(-20001, ids.COUNT || ' nodes have the id ' || p_id || '. An id is one node''s.');\n END IF;\n RETURN CASE WHEN ids.COUNT = 1 THEN ids(1) END;\n END;\n\n FUNCTION make_node(p_labels VARCHAR2) RETURN NUMBER IS\n v NUMBER;\n BEGIN\n INSERT INTO drivine_node (labels, props) VALUES (JSON(p_labels), JSON('{}')) RETURNING nid INTO v;\n RETURN v;\n END;\n\n FUNCTION has(a JSON_ARRAY_T, s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n FOR i IN 0 .. a.get_size - 1 LOOP\n IF a.get_string(i) = s THEN RETURN TRUE; END IF;\n END LOOP;\n RETURN FALSE;\n END;\n\n -- Merges the patch into the node's properties, adds and removes labels, and records an open label field's labels.\n -- Whether any of it changed the node.\n FUNCTION apply_node(p NUMBER, p_props CLOB, p_add VARCHAR2, p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN)\n RETURN BOOLEAN IS\n old_props JSON;\n new_props JSON;\n old_labels JSON;\n labels JSON_ARRAY_T;\n kept JSON_ARRAY_T := JSON_ARRAY_T();\n additions JSON_ARRAY_T := JSON_ARRAY_T.parse(p_add);\n removals JSON_ARRAY_T := JSON_ARRAY_T.parse(p_remove);\n props JSON_OBJECT_T;\n recorded JSON_ARRAY_T;\n same NUMBER;\n relabelled BOOLEAN := FALSE;\n kept_text VARCHAR2(4000);\n BEGIN\n SELECT props, labels INTO old_props, old_labels FROM drivine_node WHERE nid = p FOR UPDATE;\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n labels := JSON_ARRAY_T.parse(JSON_SERIALIZE(old_labels));\n FOR i IN 0 .. labels.get_size - 1 LOOP\n IF has(removals, labels.get_string(i)) THEN relabelled := TRUE; ELSE kept.append(labels.get_string(i)); END IF;\n END LOOP;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(kept, additions.get_string(i)) THEN kept.append(additions.get_string(i)); relabelled := TRUE; END IF;\n END LOOP;\n IF p_record IS NOT NULL THEN\n props := JSON_OBJECT_T(new_props);\n IF p_replace OR NOT props.has(p_record) THEN recorded := JSON_ARRAY_T();\n ELSE recorded := props.get_array(p_record); END IF;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(recorded, additions.get_string(i)) THEN recorded.append(additions.get_string(i)); END IF;\n END LOOP;\n props.put(p_record, recorded);\n new_props := props.to_json;\n END IF;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n kept_text := kept.to_string();\n UPDATE drivine_node SET props = new_props, labels = JSON(kept_text) WHERE nid = p;\n RETURN same = 0 OR relabelled;\n END;\n\n FUNCTION write_node(p_match VARCHAR2, p_id VARCHAR2, p_labels VARCHAR2, p_props CLOB, p_add VARCHAR2,\n p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN) RETURN NUMBER IS\n v NUMBER := find_node(p_match, p_id);\n made BOOLEAN := v IS NULL;\n BEGIN\n IF made THEN v := make_node(p_labels); END IF;\n restamp(v, apply_node(v, p_props, p_add, p_remove, p_record, p_replace) OR made);\n RETURN v;\n END;\n\n -- Joins two nodes by a relationship of the type, unless one is there (in either direction, for BOTH), and merges\n -- its properties. Both ends are marked when one is made or its properties change.\n PROCEDURE link(p_from NUMBER, p_to NUMBER, p_type VARCHAR2, p_dir VARCHAR2, p_props CLOB) IS\n e NUMBER;\n old_props JSON;\n new_props JSON;\n same NUMBER;\n BEGIN\n BEGIN\n SELECT eid, props INTO e, old_props FROM drivine_edge\n WHERE type = p_type AND ((p_dir IN ('OUT', 'BOTH') AND src = p_from AND dst = p_to)\n OR (p_dir IN ('IN', 'BOTH') AND src = p_to AND dst = p_from))\n FETCH FIRST 1 ROW ONLY;\n EXCEPTION WHEN NO_DATA_FOUND THEN e := NULL;\n END;\n IF e IS NULL THEN\n INSERT INTO drivine_edge (src, dst, type, props) VALUES (CASE WHEN p_dir = 'IN' THEN p_to ELSE p_from END,\n CASE WHEN p_dir = 'IN' THEN p_from ELSE p_to END, p_type, JSON(NVL(p_props, '{}') EXTENDED));\n relink(p_from);\n relink(p_to);\n ELSIF p_props IS NOT NULL THEN\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n IF same = 0 THEN\n UPDATE drivine_edge SET props = new_props WHERE eid = e;\n relink(p_from);\n relink(p_to);\n END IF;\n END IF;\n END;\n\n -- Removes the relationships p_dropped selects from the root, marks both ends, and deletes each removed node\n -- p_unreferenced counts as no longer referred to.\n PROCEDURE trim_edges(p_root NUMBER, p_dropped VARCHAR2, p_keep VARCHAR2, p_unreferenced VARCHAR2, p_held VARCHAR2) IS\n eids SYS.ODCINUMBERLIST;\n nids SYS.ODCINUMBERLIST;\n unreferenced NUMBER;\n BEGIN\n EXECUTE IMMEDIATE p_dropped BULK COLLECT INTO eids, nids USING p_root, p_keep;\n FOR i IN 1 .. eids.COUNT LOOP\n DELETE FROM drivine_edge WHERE eid = eids(i);\n END LOOP;\n FOR i IN 1 .. nids.COUNT LOOP\n relink(nids(i));\n END LOOP;\n IF eids.COUNT > 0 THEN relink(p_root); END IF;\n IF p_unreferenced IS NOT NULL THEN\n FOR i IN 1 .. nids.COUNT LOOP\n IF nids(i) <> p_root THEN\n EXECUTE IMMEDIATE p_unreferenced INTO unreferenced USING nids(i), p_held;\n IF unreferenced > 0 THEN DELETE FROM drivine_node WHERE nid = nids(i); END IF;\n END IF;\n END LOOP;\n END IF;\n END;\n\n -- Writes the lowercased copy of each property p_keys names from what the node holds, and removes the copy of one\n -- that holds nothing. Called once the write's change is told: the copies are no part of it.\n PROCEDURE lower_node(p NUMBER, p_keys VARCHAR2) IS\n held JSON;\n o JSON_OBJECT_T;\n lowered JSON_ARRAY_T := JSON_ARRAY_T.parse(p_keys);\n k VARCHAR2(4000);\n c VARCHAR2(4000);\n v CLOB;\n BEGIN\n SELECT props INTO held FROM drivine_node WHERE nid = p;\n o := JSON_OBJECT_T(held);\n FOR i IN 0 .. lowered.get_size - 1 LOOP\n k := lowered.get_string(i);\n -- k is the property as the node holds it; its copy is named for the user's name, $mail for $$mail.\n c := '__drivine.lower.' || CASE WHEN SUBSTR(k, 1, 2) = '$$' THEN SUBSTR(k, 2) ELSE k END;\n v := NULL;\n IF o.has(k) AND NOT o.get(k).is_null THEN v := LOWER(o.get_clob(k)); END IF;\n -- Oracle keeps an empty text as null, so an empty property has no copy.\n IF v IS NULL OR LENGTH(v) = 0 THEN o.remove(c);\n ELSE o.put(c, v); END IF;\n END LOOP;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n held := o.to_json;\n UPDATE drivine_node SET props = held WHERE nid = p;\n END;\nBEGIN\n v_id := $id;\n v_root := find_node($x_match, v_id);\n IF v_root IS NOT NULL THEN\n SELECT stamp INTO v_found FROM drivine_node WHERE nid = v_root FOR UPDATE;\n v_meets := 1;\n IF v_meets > 0 THEN\n SELECT JSON_SERIALIZE(JSON_MERGEPATCH(JSON($set EXTENDED), JSON(NVL($vectors, '{}') EXTENDED)) RETURNING CLOB EXTENDED) INTO v_patch FROM dual;\n v_changed := apply_node(v_root, v_patch, '[]', '[]', NULL, FALSE);\n restamp(v_root, v_changed);\n IF v_changed THEN lower_node(v_root, $x_lower1); END IF;\n END IF;\n END IF;\n OPEN c FOR SELECT JSON_OBJECT('found' VALUE v_root IS NOT NULL, 'applied' VALUE NVL(v_meets, 0) > 0, 'changed' VALUE v_changed RETURNING CLOB) AS \"result:json\" FROM dual;\n DBMS_SQL.RETURN_RESULT(c);\nEND;", + "Account.patchAll": "DECLARE\n v_offered VARCHAR2(40) := $x_stamp;\n v_root NUMBER;\n v_found VARCHAR2(40);\n v_made BOOLEAN := FALSE;\n v_ok BOOLEAN := TRUE;\n c SYS_REFCURSOR;\n v_changed BOOLEAN;\n v_out SYS.ODCIVARCHAR2LIST := SYS.ODCIVARCHAR2LIST();\n TYPE texts IS TABLE OF CLOB;\n v_ids texts;\n v_patches texts;\n\n FUNCTION two_tokens(s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n RETURN s IS NOT NULL AND LENGTH(s) = 33 AND SUBSTR(s, 17, 1) = ':';\n END;\n\n PROCEDURE restamp(p NUMBER, p_changed BOOLEAN) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF p_changed OR NOT two_tokens(s) THEN\n IF two_tokens(s) THEN s := SUBSTR(v_offered, 1, 16) || SUBSTR(s, 17);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END IF;\n END;\n\n PROCEDURE relink(p NUMBER) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF two_tokens(s) THEN s := SUBSTR(s, 1, 17) || SUBSTR(v_offered, 18);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END;\n\n FUNCTION find_node(p_match VARCHAR2, p_id VARCHAR2) RETURN NUMBER IS\n ids SYS.ODCINUMBERLIST;\n BEGIN\n EXECUTE IMMEDIATE 'SELECT nid FROM drivine_node n WHERE ' || p_match BULK COLLECT INTO ids USING p_id;\n IF ids.COUNT > 1 THEN\n RAISE_APPLICATION_ERROR(-20001, ids.COUNT || ' nodes have the id ' || p_id || '. An id is one node''s.');\n END IF;\n RETURN CASE WHEN ids.COUNT = 1 THEN ids(1) END;\n END;\n\n FUNCTION make_node(p_labels VARCHAR2) RETURN NUMBER IS\n v NUMBER;\n BEGIN\n INSERT INTO drivine_node (labels, props) VALUES (JSON(p_labels), JSON('{}')) RETURNING nid INTO v;\n RETURN v;\n END;\n\n FUNCTION has(a JSON_ARRAY_T, s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n FOR i IN 0 .. a.get_size - 1 LOOP\n IF a.get_string(i) = s THEN RETURN TRUE; END IF;\n END LOOP;\n RETURN FALSE;\n END;\n\n -- Merges the patch into the node's properties, adds and removes labels, and records an open label field's labels.\n -- Whether any of it changed the node.\n FUNCTION apply_node(p NUMBER, p_props CLOB, p_add VARCHAR2, p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN)\n RETURN BOOLEAN IS\n old_props JSON;\n new_props JSON;\n old_labels JSON;\n labels JSON_ARRAY_T;\n kept JSON_ARRAY_T := JSON_ARRAY_T();\n additions JSON_ARRAY_T := JSON_ARRAY_T.parse(p_add);\n removals JSON_ARRAY_T := JSON_ARRAY_T.parse(p_remove);\n props JSON_OBJECT_T;\n recorded JSON_ARRAY_T;\n same NUMBER;\n relabelled BOOLEAN := FALSE;\n kept_text VARCHAR2(4000);\n BEGIN\n SELECT props, labels INTO old_props, old_labels FROM drivine_node WHERE nid = p FOR UPDATE;\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n labels := JSON_ARRAY_T.parse(JSON_SERIALIZE(old_labels));\n FOR i IN 0 .. labels.get_size - 1 LOOP\n IF has(removals, labels.get_string(i)) THEN relabelled := TRUE; ELSE kept.append(labels.get_string(i)); END IF;\n END LOOP;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(kept, additions.get_string(i)) THEN kept.append(additions.get_string(i)); relabelled := TRUE; END IF;\n END LOOP;\n IF p_record IS NOT NULL THEN\n props := JSON_OBJECT_T(new_props);\n IF p_replace OR NOT props.has(p_record) THEN recorded := JSON_ARRAY_T();\n ELSE recorded := props.get_array(p_record); END IF;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(recorded, additions.get_string(i)) THEN recorded.append(additions.get_string(i)); END IF;\n END LOOP;\n props.put(p_record, recorded);\n new_props := props.to_json;\n END IF;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n kept_text := kept.to_string();\n UPDATE drivine_node SET props = new_props, labels = JSON(kept_text) WHERE nid = p;\n RETURN same = 0 OR relabelled;\n END;\n\n FUNCTION write_node(p_match VARCHAR2, p_id VARCHAR2, p_labels VARCHAR2, p_props CLOB, p_add VARCHAR2,\n p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN) RETURN NUMBER IS\n v NUMBER := find_node(p_match, p_id);\n made BOOLEAN := v IS NULL;\n BEGIN\n IF made THEN v := make_node(p_labels); END IF;\n restamp(v, apply_node(v, p_props, p_add, p_remove, p_record, p_replace) OR made);\n RETURN v;\n END;\n\n -- Joins two nodes by a relationship of the type, unless one is there (in either direction, for BOTH), and merges\n -- its properties. Both ends are marked when one is made or its properties change.\n PROCEDURE link(p_from NUMBER, p_to NUMBER, p_type VARCHAR2, p_dir VARCHAR2, p_props CLOB) IS\n e NUMBER;\n old_props JSON;\n new_props JSON;\n same NUMBER;\n BEGIN\n BEGIN\n SELECT eid, props INTO e, old_props FROM drivine_edge\n WHERE type = p_type AND ((p_dir IN ('OUT', 'BOTH') AND src = p_from AND dst = p_to)\n OR (p_dir IN ('IN', 'BOTH') AND src = p_to AND dst = p_from))\n FETCH FIRST 1 ROW ONLY;\n EXCEPTION WHEN NO_DATA_FOUND THEN e := NULL;\n END;\n IF e IS NULL THEN\n INSERT INTO drivine_edge (src, dst, type, props) VALUES (CASE WHEN p_dir = 'IN' THEN p_to ELSE p_from END,\n CASE WHEN p_dir = 'IN' THEN p_from ELSE p_to END, p_type, JSON(NVL(p_props, '{}') EXTENDED));\n relink(p_from);\n relink(p_to);\n ELSIF p_props IS NOT NULL THEN\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n IF same = 0 THEN\n UPDATE drivine_edge SET props = new_props WHERE eid = e;\n relink(p_from);\n relink(p_to);\n END IF;\n END IF;\n END;\n\n -- Removes the relationships p_dropped selects from the root, marks both ends, and deletes each removed node\n -- p_unreferenced counts as no longer referred to.\n PROCEDURE trim_edges(p_root NUMBER, p_dropped VARCHAR2, p_keep VARCHAR2, p_unreferenced VARCHAR2, p_held VARCHAR2) IS\n eids SYS.ODCINUMBERLIST;\n nids SYS.ODCINUMBERLIST;\n unreferenced NUMBER;\n BEGIN\n EXECUTE IMMEDIATE p_dropped BULK COLLECT INTO eids, nids USING p_root, p_keep;\n FOR i IN 1 .. eids.COUNT LOOP\n DELETE FROM drivine_edge WHERE eid = eids(i);\n END LOOP;\n FOR i IN 1 .. nids.COUNT LOOP\n relink(nids(i));\n END LOOP;\n IF eids.COUNT > 0 THEN relink(p_root); END IF;\n IF p_unreferenced IS NOT NULL THEN\n FOR i IN 1 .. nids.COUNT LOOP\n IF nids(i) <> p_root THEN\n EXECUTE IMMEDIATE p_unreferenced INTO unreferenced USING nids(i), p_held;\n IF unreferenced > 0 THEN DELETE FROM drivine_node WHERE nid = nids(i); END IF;\n END IF;\n END LOOP;\n END IF;\n END;\n\n -- Writes the lowercased copy of each property p_keys names from what the node holds, and removes the copy of one\n -- that holds nothing. Called once the write's change is told: the copies are no part of it.\n PROCEDURE lower_node(p NUMBER, p_keys VARCHAR2) IS\n held JSON;\n o JSON_OBJECT_T;\n lowered JSON_ARRAY_T := JSON_ARRAY_T.parse(p_keys);\n k VARCHAR2(4000);\n c VARCHAR2(4000);\n v CLOB;\n BEGIN\n SELECT props INTO held FROM drivine_node WHERE nid = p;\n o := JSON_OBJECT_T(held);\n FOR i IN 0 .. lowered.get_size - 1 LOOP\n k := lowered.get_string(i);\n -- k is the property as the node holds it; its copy is named for the user's name, $mail for $$mail.\n c := '__drivine.lower.' || CASE WHEN SUBSTR(k, 1, 2) = '$$' THEN SUBSTR(k, 2) ELSE k END;\n v := NULL;\n IF o.has(k) AND NOT o.get(k).is_null THEN v := LOWER(o.get_clob(k)); END IF;\n -- Oracle keeps an empty text as null, so an empty property has no copy.\n IF v IS NULL OR LENGTH(v) = 0 THEN o.remove(c);\n ELSE o.put(c, v); END IF;\n END LOOP;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n held := o.to_json;\n UPDATE drivine_node SET props = held WHERE nid = p;\n END;\nBEGIN\n SELECT JSON_SERIALIZE(id RETURNING CLOB EXTENDED), JSON_SERIALIZE(JSON_MERGEPATCH(NVL(props, JSON('{}')), NVL(vectors, JSON('{}'))) RETURNING CLOB EXTENDED) BULK COLLECT INTO v_ids, v_patches FROM JSON_TABLE(JSON($rows EXTENDED), '$[*]' COLUMNS (id JSON PATH '$.id', props JSON PATH '$.props', vectors JSON PATH '$.vectors'));\n FOR i IN 1 .. v_ids.COUNT LOOP\n v_root := find_node($x_match, v_ids(i));\n v_changed := FALSE;\n IF v_root IS NOT NULL THEN\n SELECT stamp INTO v_found FROM drivine_node WHERE nid = v_root FOR UPDATE;\n v_changed := apply_node(v_root, v_patches(i), '[]', '[]', NULL, FALSE);\n restamp(v_root, v_changed);\n IF v_changed THEN lower_node(v_root, $x_lower1); END IF;\n END IF;\n v_out.EXTEND;\n SELECT JSON_SERIALIZE(JSON_OBJECT('id' VALUE JSON(v_ids(i) EXTENDED), 'found' VALUE v_root IS NOT NULL, 'changed' VALUE v_changed)) INTO v_out(v_out.COUNT) FROM dual;\n END LOOP;\n OPEN c FOR SELECT column_value AS \"result:json\" FROM TABLE(v_out);\n DBMS_SQL.RETURN_RESULT(c);\nEND;", "Person.patch": "DECLARE\n v_offered VARCHAR2(40) := $x_stamp;\n v_root NUMBER;\n v_found VARCHAR2(40);\n v_made BOOLEAN := FALSE;\n v_ok BOOLEAN := TRUE;\n c SYS_REFCURSOR;\n v_meets NUMBER;\n v_changed BOOLEAN := FALSE;\n v_id VARCHAR2(4000);\n v_patch CLOB;\n\n FUNCTION two_tokens(s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n RETURN s IS NOT NULL AND LENGTH(s) = 33 AND SUBSTR(s, 17, 1) = ':';\n END;\n\n PROCEDURE restamp(p NUMBER, p_changed BOOLEAN) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF p_changed OR NOT two_tokens(s) THEN\n IF two_tokens(s) THEN s := SUBSTR(v_offered, 1, 16) || SUBSTR(s, 17);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END IF;\n END;\n\n PROCEDURE relink(p NUMBER) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF two_tokens(s) THEN s := SUBSTR(s, 1, 17) || SUBSTR(v_offered, 18);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END;\n\n FUNCTION find_node(p_match VARCHAR2, p_id VARCHAR2) RETURN NUMBER IS\n ids SYS.ODCINUMBERLIST;\n BEGIN\n EXECUTE IMMEDIATE 'SELECT nid FROM drivine_node n WHERE ' || p_match BULK COLLECT INTO ids USING p_id;\n IF ids.COUNT > 1 THEN\n RAISE_APPLICATION_ERROR(-20001, ids.COUNT || ' nodes have the id ' || p_id || '. An id is one node''s.');\n END IF;\n RETURN CASE WHEN ids.COUNT = 1 THEN ids(1) END;\n END;\n\n FUNCTION make_node(p_labels VARCHAR2) RETURN NUMBER IS\n v NUMBER;\n BEGIN\n INSERT INTO drivine_node (labels, props) VALUES (JSON(p_labels), JSON('{}')) RETURNING nid INTO v;\n RETURN v;\n END;\n\n FUNCTION has(a JSON_ARRAY_T, s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n FOR i IN 0 .. a.get_size - 1 LOOP\n IF a.get_string(i) = s THEN RETURN TRUE; END IF;\n END LOOP;\n RETURN FALSE;\n END;\n\n -- Merges the patch into the node's properties, adds and removes labels, and records an open label field's labels.\n -- Whether any of it changed the node.\n FUNCTION apply_node(p NUMBER, p_props CLOB, p_add VARCHAR2, p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN)\n RETURN BOOLEAN IS\n old_props JSON;\n new_props JSON;\n old_labels JSON;\n labels JSON_ARRAY_T;\n kept JSON_ARRAY_T := JSON_ARRAY_T();\n additions JSON_ARRAY_T := JSON_ARRAY_T.parse(p_add);\n removals JSON_ARRAY_T := JSON_ARRAY_T.parse(p_remove);\n props JSON_OBJECT_T;\n recorded JSON_ARRAY_T;\n same NUMBER;\n relabelled BOOLEAN := FALSE;\n kept_text VARCHAR2(4000);\n BEGIN\n SELECT props, labels INTO old_props, old_labels FROM drivine_node WHERE nid = p FOR UPDATE;\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n labels := JSON_ARRAY_T.parse(JSON_SERIALIZE(old_labels));\n FOR i IN 0 .. labels.get_size - 1 LOOP\n IF has(removals, labels.get_string(i)) THEN relabelled := TRUE; ELSE kept.append(labels.get_string(i)); END IF;\n END LOOP;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(kept, additions.get_string(i)) THEN kept.append(additions.get_string(i)); relabelled := TRUE; END IF;\n END LOOP;\n IF p_record IS NOT NULL THEN\n props := JSON_OBJECT_T(new_props);\n IF p_replace OR NOT props.has(p_record) THEN recorded := JSON_ARRAY_T();\n ELSE recorded := props.get_array(p_record); END IF;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(recorded, additions.get_string(i)) THEN recorded.append(additions.get_string(i)); END IF;\n END LOOP;\n props.put(p_record, recorded);\n new_props := props.to_json;\n END IF;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n kept_text := kept.to_string();\n UPDATE drivine_node SET props = new_props, labels = JSON(kept_text) WHERE nid = p;\n RETURN same = 0 OR relabelled;\n END;\n\n FUNCTION write_node(p_match VARCHAR2, p_id VARCHAR2, p_labels VARCHAR2, p_props CLOB, p_add VARCHAR2,\n p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN) RETURN NUMBER IS\n v NUMBER := find_node(p_match, p_id);\n made BOOLEAN := v IS NULL;\n BEGIN\n IF made THEN v := make_node(p_labels); END IF;\n restamp(v, apply_node(v, p_props, p_add, p_remove, p_record, p_replace) OR made);\n RETURN v;\n END;\n\n -- Joins two nodes by a relationship of the type, unless one is there (in either direction, for BOTH), and merges\n -- its properties. Both ends are marked when one is made or its properties change.\n PROCEDURE link(p_from NUMBER, p_to NUMBER, p_type VARCHAR2, p_dir VARCHAR2, p_props CLOB) IS\n e NUMBER;\n old_props JSON;\n new_props JSON;\n same NUMBER;\n BEGIN\n BEGIN\n SELECT eid, props INTO e, old_props FROM drivine_edge\n WHERE type = p_type AND ((p_dir IN ('OUT', 'BOTH') AND src = p_from AND dst = p_to)\n OR (p_dir IN ('IN', 'BOTH') AND src = p_to AND dst = p_from))\n FETCH FIRST 1 ROW ONLY;\n EXCEPTION WHEN NO_DATA_FOUND THEN e := NULL;\n END;\n IF e IS NULL THEN\n INSERT INTO drivine_edge (src, dst, type, props) VALUES (CASE WHEN p_dir = 'IN' THEN p_to ELSE p_from END,\n CASE WHEN p_dir = 'IN' THEN p_from ELSE p_to END, p_type, JSON(NVL(p_props, '{}') EXTENDED));\n relink(p_from);\n relink(p_to);\n ELSIF p_props IS NOT NULL THEN\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n IF same = 0 THEN\n UPDATE drivine_edge SET props = new_props WHERE eid = e;\n relink(p_from);\n relink(p_to);\n END IF;\n END IF;\n END;\n\n -- Removes the relationships p_dropped selects from the root, marks both ends, and deletes each removed node\n -- p_unreferenced counts as no longer referred to.\n PROCEDURE trim_edges(p_root NUMBER, p_dropped VARCHAR2, p_keep VARCHAR2, p_unreferenced VARCHAR2, p_held VARCHAR2) IS\n eids SYS.ODCINUMBERLIST;\n nids SYS.ODCINUMBERLIST;\n unreferenced NUMBER;\n BEGIN\n EXECUTE IMMEDIATE p_dropped BULK COLLECT INTO eids, nids USING p_root, p_keep;\n FOR i IN 1 .. eids.COUNT LOOP\n DELETE FROM drivine_edge WHERE eid = eids(i);\n END LOOP;\n FOR i IN 1 .. nids.COUNT LOOP\n relink(nids(i));\n END LOOP;\n IF eids.COUNT > 0 THEN relink(p_root); END IF;\n IF p_unreferenced IS NOT NULL THEN\n FOR i IN 1 .. nids.COUNT LOOP\n IF nids(i) <> p_root THEN\n EXECUTE IMMEDIATE p_unreferenced INTO unreferenced USING nids(i), p_held;\n IF unreferenced > 0 THEN DELETE FROM drivine_node WHERE nid = nids(i); END IF;\n END IF;\n END LOOP;\n END IF;\n END;\nBEGIN\n v_id := $id;\n v_root := find_node($x_match, v_id);\n IF v_root IS NOT NULL THEN\n SELECT stamp INTO v_found FROM drivine_node WHERE nid = v_root FOR UPDATE;\n v_meets := 1;\n IF v_meets > 0 THEN\n SELECT JSON_SERIALIZE(JSON_MERGEPATCH(JSON($set EXTENDED), JSON(NVL($vectors, '{}') EXTENDED)) RETURNING CLOB EXTENDED) INTO v_patch FROM dual;\n v_changed := apply_node(v_root, v_patch, '[]', '[]', NULL, FALSE);\n restamp(v_root, v_changed);\n END IF;\n END IF;\n OPEN c FOR SELECT JSON_OBJECT('found' VALUE v_root IS NOT NULL, 'applied' VALUE NVL(v_meets, 0) > 0, 'changed' VALUE v_changed RETURNING CLOB) AS \"result:json\" FROM dual;\n DBMS_SQL.RETURN_RESULT(c);\nEND;", "Person.patch.where": "DECLARE\n v_offered VARCHAR2(40) := $x_stamp;\n v_root NUMBER;\n v_found VARCHAR2(40);\n v_made BOOLEAN := FALSE;\n v_ok BOOLEAN := TRUE;\n c SYS_REFCURSOR;\n v_meets NUMBER;\n v_changed BOOLEAN := FALSE;\n v_id VARCHAR2(4000);\n v_patch CLOB;\n\n FUNCTION two_tokens(s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n RETURN s IS NOT NULL AND LENGTH(s) = 33 AND SUBSTR(s, 17, 1) = ':';\n END;\n\n PROCEDURE restamp(p NUMBER, p_changed BOOLEAN) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF p_changed OR NOT two_tokens(s) THEN\n IF two_tokens(s) THEN s := SUBSTR(v_offered, 1, 16) || SUBSTR(s, 17);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END IF;\n END;\n\n PROCEDURE relink(p NUMBER) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF two_tokens(s) THEN s := SUBSTR(s, 1, 17) || SUBSTR(v_offered, 18);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END;\n\n FUNCTION find_node(p_match VARCHAR2, p_id VARCHAR2) RETURN NUMBER IS\n ids SYS.ODCINUMBERLIST;\n BEGIN\n EXECUTE IMMEDIATE 'SELECT nid FROM drivine_node n WHERE ' || p_match BULK COLLECT INTO ids USING p_id;\n IF ids.COUNT > 1 THEN\n RAISE_APPLICATION_ERROR(-20001, ids.COUNT || ' nodes have the id ' || p_id || '. An id is one node''s.');\n END IF;\n RETURN CASE WHEN ids.COUNT = 1 THEN ids(1) END;\n END;\n\n FUNCTION make_node(p_labels VARCHAR2) RETURN NUMBER IS\n v NUMBER;\n BEGIN\n INSERT INTO drivine_node (labels, props) VALUES (JSON(p_labels), JSON('{}')) RETURNING nid INTO v;\n RETURN v;\n END;\n\n FUNCTION has(a JSON_ARRAY_T, s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n FOR i IN 0 .. a.get_size - 1 LOOP\n IF a.get_string(i) = s THEN RETURN TRUE; END IF;\n END LOOP;\n RETURN FALSE;\n END;\n\n -- Merges the patch into the node's properties, adds and removes labels, and records an open label field's labels.\n -- Whether any of it changed the node.\n FUNCTION apply_node(p NUMBER, p_props CLOB, p_add VARCHAR2, p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN)\n RETURN BOOLEAN IS\n old_props JSON;\n new_props JSON;\n old_labels JSON;\n labels JSON_ARRAY_T;\n kept JSON_ARRAY_T := JSON_ARRAY_T();\n additions JSON_ARRAY_T := JSON_ARRAY_T.parse(p_add);\n removals JSON_ARRAY_T := JSON_ARRAY_T.parse(p_remove);\n props JSON_OBJECT_T;\n recorded JSON_ARRAY_T;\n same NUMBER;\n relabelled BOOLEAN := FALSE;\n kept_text VARCHAR2(4000);\n BEGIN\n SELECT props, labels INTO old_props, old_labels FROM drivine_node WHERE nid = p FOR UPDATE;\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n labels := JSON_ARRAY_T.parse(JSON_SERIALIZE(old_labels));\n FOR i IN 0 .. labels.get_size - 1 LOOP\n IF has(removals, labels.get_string(i)) THEN relabelled := TRUE; ELSE kept.append(labels.get_string(i)); END IF;\n END LOOP;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(kept, additions.get_string(i)) THEN kept.append(additions.get_string(i)); relabelled := TRUE; END IF;\n END LOOP;\n IF p_record IS NOT NULL THEN\n props := JSON_OBJECT_T(new_props);\n IF p_replace OR NOT props.has(p_record) THEN recorded := JSON_ARRAY_T();\n ELSE recorded := props.get_array(p_record); END IF;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(recorded, additions.get_string(i)) THEN recorded.append(additions.get_string(i)); END IF;\n END LOOP;\n props.put(p_record, recorded);\n new_props := props.to_json;\n END IF;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n kept_text := kept.to_string();\n UPDATE drivine_node SET props = new_props, labels = JSON(kept_text) WHERE nid = p;\n RETURN same = 0 OR relabelled;\n END;\n\n FUNCTION write_node(p_match VARCHAR2, p_id VARCHAR2, p_labels VARCHAR2, p_props CLOB, p_add VARCHAR2,\n p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN) RETURN NUMBER IS\n v NUMBER := find_node(p_match, p_id);\n made BOOLEAN := v IS NULL;\n BEGIN\n IF made THEN v := make_node(p_labels); END IF;\n restamp(v, apply_node(v, p_props, p_add, p_remove, p_record, p_replace) OR made);\n RETURN v;\n END;\n\n -- Joins two nodes by a relationship of the type, unless one is there (in either direction, for BOTH), and merges\n -- its properties. Both ends are marked when one is made or its properties change.\n PROCEDURE link(p_from NUMBER, p_to NUMBER, p_type VARCHAR2, p_dir VARCHAR2, p_props CLOB) IS\n e NUMBER;\n old_props JSON;\n new_props JSON;\n same NUMBER;\n BEGIN\n BEGIN\n SELECT eid, props INTO e, old_props FROM drivine_edge\n WHERE type = p_type AND ((p_dir IN ('OUT', 'BOTH') AND src = p_from AND dst = p_to)\n OR (p_dir IN ('IN', 'BOTH') AND src = p_to AND dst = p_from))\n FETCH FIRST 1 ROW ONLY;\n EXCEPTION WHEN NO_DATA_FOUND THEN e := NULL;\n END;\n IF e IS NULL THEN\n INSERT INTO drivine_edge (src, dst, type, props) VALUES (CASE WHEN p_dir = 'IN' THEN p_to ELSE p_from END,\n CASE WHEN p_dir = 'IN' THEN p_from ELSE p_to END, p_type, JSON(NVL(p_props, '{}') EXTENDED));\n relink(p_from);\n relink(p_to);\n ELSIF p_props IS NOT NULL THEN\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n IF same = 0 THEN\n UPDATE drivine_edge SET props = new_props WHERE eid = e;\n relink(p_from);\n relink(p_to);\n END IF;\n END IF;\n END;\n\n -- Removes the relationships p_dropped selects from the root, marks both ends, and deletes each removed node\n -- p_unreferenced counts as no longer referred to.\n PROCEDURE trim_edges(p_root NUMBER, p_dropped VARCHAR2, p_keep VARCHAR2, p_unreferenced VARCHAR2, p_held VARCHAR2) IS\n eids SYS.ODCINUMBERLIST;\n nids SYS.ODCINUMBERLIST;\n unreferenced NUMBER;\n BEGIN\n EXECUTE IMMEDIATE p_dropped BULK COLLECT INTO eids, nids USING p_root, p_keep;\n FOR i IN 1 .. eids.COUNT LOOP\n DELETE FROM drivine_edge WHERE eid = eids(i);\n END LOOP;\n FOR i IN 1 .. nids.COUNT LOOP\n relink(nids(i));\n END LOOP;\n IF eids.COUNT > 0 THEN relink(p_root); END IF;\n IF p_unreferenced IS NOT NULL THEN\n FOR i IN 1 .. nids.COUNT LOOP\n IF nids(i) <> p_root THEN\n EXECUTE IMMEDIATE p_unreferenced INTO unreferenced USING nids(i), p_held;\n IF unreferenced > 0 THEN DELETE FROM drivine_node WHERE nid = nids(i); END IF;\n END IF;\n END LOOP;\n END IF;\n END;\nBEGIN\n v_id := $id;\n v_root := find_node($x_match, v_id);\n IF v_root IS NOT NULL THEN\n SELECT stamp INTO v_found FROM drivine_node WHERE nid = v_root FOR UPDATE;\n SELECT COUNT(*) INTO v_meets FROM drivine_node n_ WHERE n_.nid = v_root AND (JSON_VALUE(n_.props, '$.name') = $name);\n IF v_meets > 0 THEN\n SELECT JSON_SERIALIZE(JSON_MERGEPATCH(JSON($set EXTENDED), JSON(NVL($vectors, '{}') EXTENDED)) RETURNING CLOB EXTENDED) INTO v_patch FROM dual;\n v_changed := apply_node(v_root, v_patch, '[]', '[]', NULL, FALSE);\n restamp(v_root, v_changed);\n END IF;\n END IF;\n OPEN c FOR SELECT JSON_OBJECT('found' VALUE v_root IS NOT NULL, 'applied' VALUE NVL(v_meets, 0) > 0, 'changed' VALUE v_changed RETURNING CLOB) AS \"result:json\" FROM dual;\n DBMS_SQL.RETURN_RESULT(c);\nEND;", "Person.patchAll": "DECLARE\n v_offered VARCHAR2(40) := $x_stamp;\n v_root NUMBER;\n v_found VARCHAR2(40);\n v_made BOOLEAN := FALSE;\n v_ok BOOLEAN := TRUE;\n c SYS_REFCURSOR;\n v_changed BOOLEAN;\n v_out SYS.ODCIVARCHAR2LIST := SYS.ODCIVARCHAR2LIST();\n TYPE texts IS TABLE OF CLOB;\n v_ids texts;\n v_patches texts;\n\n FUNCTION two_tokens(s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n RETURN s IS NOT NULL AND LENGTH(s) = 33 AND SUBSTR(s, 17, 1) = ':';\n END;\n\n PROCEDURE restamp(p NUMBER, p_changed BOOLEAN) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF p_changed OR NOT two_tokens(s) THEN\n IF two_tokens(s) THEN s := SUBSTR(v_offered, 1, 16) || SUBSTR(s, 17);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END IF;\n END;\n\n PROCEDURE relink(p NUMBER) IS\n s VARCHAR2(40);\n BEGIN\n SELECT stamp INTO s FROM drivine_node WHERE nid = p;\n IF two_tokens(s) THEN s := SUBSTR(s, 1, 17) || SUBSTR(v_offered, 18);\n ELSE s := v_offered; END IF;\n UPDATE drivine_node SET stamp = s WHERE nid = p;\n END;\n\n FUNCTION find_node(p_match VARCHAR2, p_id VARCHAR2) RETURN NUMBER IS\n ids SYS.ODCINUMBERLIST;\n BEGIN\n EXECUTE IMMEDIATE 'SELECT nid FROM drivine_node n WHERE ' || p_match BULK COLLECT INTO ids USING p_id;\n IF ids.COUNT > 1 THEN\n RAISE_APPLICATION_ERROR(-20001, ids.COUNT || ' nodes have the id ' || p_id || '. An id is one node''s.');\n END IF;\n RETURN CASE WHEN ids.COUNT = 1 THEN ids(1) END;\n END;\n\n FUNCTION make_node(p_labels VARCHAR2) RETURN NUMBER IS\n v NUMBER;\n BEGIN\n INSERT INTO drivine_node (labels, props) VALUES (JSON(p_labels), JSON('{}')) RETURNING nid INTO v;\n RETURN v;\n END;\n\n FUNCTION has(a JSON_ARRAY_T, s VARCHAR2) RETURN BOOLEAN IS\n BEGIN\n FOR i IN 0 .. a.get_size - 1 LOOP\n IF a.get_string(i) = s THEN RETURN TRUE; END IF;\n END LOOP;\n RETURN FALSE;\n END;\n\n -- Merges the patch into the node's properties, adds and removes labels, and records an open label field's labels.\n -- Whether any of it changed the node.\n FUNCTION apply_node(p NUMBER, p_props CLOB, p_add VARCHAR2, p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN)\n RETURN BOOLEAN IS\n old_props JSON;\n new_props JSON;\n old_labels JSON;\n labels JSON_ARRAY_T;\n kept JSON_ARRAY_T := JSON_ARRAY_T();\n additions JSON_ARRAY_T := JSON_ARRAY_T.parse(p_add);\n removals JSON_ARRAY_T := JSON_ARRAY_T.parse(p_remove);\n props JSON_OBJECT_T;\n recorded JSON_ARRAY_T;\n same NUMBER;\n relabelled BOOLEAN := FALSE;\n kept_text VARCHAR2(4000);\n BEGIN\n SELECT props, labels INTO old_props, old_labels FROM drivine_node WHERE nid = p FOR UPDATE;\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n labels := JSON_ARRAY_T.parse(JSON_SERIALIZE(old_labels));\n FOR i IN 0 .. labels.get_size - 1 LOOP\n IF has(removals, labels.get_string(i)) THEN relabelled := TRUE; ELSE kept.append(labels.get_string(i)); END IF;\n END LOOP;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(kept, additions.get_string(i)) THEN kept.append(additions.get_string(i)); relabelled := TRUE; END IF;\n END LOOP;\n IF p_record IS NOT NULL THEN\n props := JSON_OBJECT_T(new_props);\n IF p_replace OR NOT props.has(p_record) THEN recorded := JSON_ARRAY_T();\n ELSE recorded := props.get_array(p_record); END IF;\n FOR i IN 0 .. additions.get_size - 1 LOOP\n IF NOT has(recorded, additions.get_string(i)) THEN recorded.append(additions.get_string(i)); END IF;\n END LOOP;\n props.put(p_record, recorded);\n new_props := props.to_json;\n END IF;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n -- An object type's methods are PL/SQL's: called in SQL, they are refused (ORA-40573).\n kept_text := kept.to_string();\n UPDATE drivine_node SET props = new_props, labels = JSON(kept_text) WHERE nid = p;\n RETURN same = 0 OR relabelled;\n END;\n\n FUNCTION write_node(p_match VARCHAR2, p_id VARCHAR2, p_labels VARCHAR2, p_props CLOB, p_add VARCHAR2,\n p_remove VARCHAR2, p_record VARCHAR2, p_replace BOOLEAN) RETURN NUMBER IS\n v NUMBER := find_node(p_match, p_id);\n made BOOLEAN := v IS NULL;\n BEGIN\n IF made THEN v := make_node(p_labels); END IF;\n restamp(v, apply_node(v, p_props, p_add, p_remove, p_record, p_replace) OR made);\n RETURN v;\n END;\n\n -- Joins two nodes by a relationship of the type, unless one is there (in either direction, for BOTH), and merges\n -- its properties. Both ends are marked when one is made or its properties change.\n PROCEDURE link(p_from NUMBER, p_to NUMBER, p_type VARCHAR2, p_dir VARCHAR2, p_props CLOB) IS\n e NUMBER;\n old_props JSON;\n new_props JSON;\n same NUMBER;\n BEGIN\n BEGIN\n SELECT eid, props INTO e, old_props FROM drivine_edge\n WHERE type = p_type AND ((p_dir IN ('OUT', 'BOTH') AND src = p_from AND dst = p_to)\n OR (p_dir IN ('IN', 'BOTH') AND src = p_to AND dst = p_from))\n FETCH FIRST 1 ROW ONLY;\n EXCEPTION WHEN NO_DATA_FOUND THEN e := NULL;\n END;\n IF e IS NULL THEN\n INSERT INTO drivine_edge (src, dst, type, props) VALUES (CASE WHEN p_dir = 'IN' THEN p_to ELSE p_from END,\n CASE WHEN p_dir = 'IN' THEN p_from ELSE p_to END, p_type, JSON(NVL(p_props, '{}') EXTENDED));\n relink(p_from);\n relink(p_to);\n ELSIF p_props IS NOT NULL THEN\n SELECT JSON_MERGEPATCH(old_props, JSON(p_props EXTENDED)) INTO new_props FROM dual;\n SELECT CASE WHEN JSON_EQUAL(old_props, new_props) THEN 1 ELSE 0 END INTO same FROM dual;\n IF same = 0 THEN\n UPDATE drivine_edge SET props = new_props WHERE eid = e;\n relink(p_from);\n relink(p_to);\n END IF;\n END IF;\n END;\n\n -- Removes the relationships p_dropped selects from the root, marks both ends, and deletes each removed node\n -- p_unreferenced counts as no longer referred to.\n PROCEDURE trim_edges(p_root NUMBER, p_dropped VARCHAR2, p_keep VARCHAR2, p_unreferenced VARCHAR2, p_held VARCHAR2) IS\n eids SYS.ODCINUMBERLIST;\n nids SYS.ODCINUMBERLIST;\n unreferenced NUMBER;\n BEGIN\n EXECUTE IMMEDIATE p_dropped BULK COLLECT INTO eids, nids USING p_root, p_keep;\n FOR i IN 1 .. eids.COUNT LOOP\n DELETE FROM drivine_edge WHERE eid = eids(i);\n END LOOP;\n FOR i IN 1 .. nids.COUNT LOOP\n relink(nids(i));\n END LOOP;\n IF eids.COUNT > 0 THEN relink(p_root); END IF;\n IF p_unreferenced IS NOT NULL THEN\n FOR i IN 1 .. nids.COUNT LOOP\n IF nids(i) <> p_root THEN\n EXECUTE IMMEDIATE p_unreferenced INTO unreferenced USING nids(i), p_held;\n IF unreferenced > 0 THEN DELETE FROM drivine_node WHERE nid = nids(i); END IF;\n END IF;\n END LOOP;\n END IF;\n END;\nBEGIN\n SELECT JSON_SERIALIZE(id RETURNING CLOB EXTENDED), JSON_SERIALIZE(JSON_MERGEPATCH(NVL(props, JSON('{}')), NVL(vectors, JSON('{}'))) RETURNING CLOB EXTENDED) BULK COLLECT INTO v_ids, v_patches FROM JSON_TABLE(JSON($rows EXTENDED), '$[*]' COLUMNS (id JSON PATH '$.id', props JSON PATH '$.props', vectors JSON PATH '$.vectors'));\n FOR i IN 1 .. v_ids.COUNT LOOP\n v_root := find_node($x_match, v_ids(i));\n v_changed := FALSE;\n IF v_root IS NOT NULL THEN\n SELECT stamp INTO v_found FROM drivine_node WHERE nid = v_root FOR UPDATE;\n v_changed := apply_node(v_root, v_patches(i), '[]', '[]', NULL, FALSE);\n restamp(v_root, v_changed);\n END IF;\n v_out.EXTEND;\n SELECT JSON_SERIALIZE(JSON_OBJECT('id' VALUE JSON(v_ids(i) EXTENDED), 'found' VALUE v_root IS NOT NULL, 'changed' VALUE v_changed)) INTO v_out(v_out.COUNT) FROM dual;\n END LOOP;\n OPEN c FOR SELECT column_value AS \"result:json\" FROM TABLE(v_out);\n DBMS_SQL.RETURN_RESULT(c);\nEND;", diff --git a/tests/integration/sql/test_value_layout.py b/tests/integration/sql/test_value_layout.py index 1a705c33..07d951ae 100644 --- a/tests/integration/sql/test_value_layout.py +++ b/tests/integration/sql/test_value_layout.py @@ -10,6 +10,7 @@ import json import math import re +import uuid from datetime import UTC, date, datetime, time, timedelta, timezone from typing import Annotated, Any from zoneinfo import ZoneInfo @@ -33,11 +34,15 @@ cypher_statement, equal_to, key, + lower_copies, max_, node_ref, not_equal_to, + range_index, refs, + uniqueness_constraint, ) +from tests.integration.stores import create, nodes class Held(NodeFragment, labels=["Held"]): @@ -181,11 +186,30 @@ class CaseHeld(NodeFragment, labels=["CaseHeld"]): mail: Annotated[str | None, GraphProperty("$mail"), CaseInsensitive()] = None +def case_held(label: str) -> Any: + """ + A `$mail` held as `$$mail`, of a label of the test's own, so the items and records, which outlive its data, are its + own. + """ + + class Dollared(NodeFragment, labels=[label]): + id: Annotated[str, NodeId()] + mail: Annotated[str | None, GraphProperty("$mail"), CaseInsensitive()] = None + + return Dollared + + +MAIL_COPY = f"{lower_copies.PREFIX}$mail" +"""The copy of `$mail`, named for the user's name as each lookup and the ignore-case index read it.""" + +UNMERGED_539 = ( + "Postgres's save names the copy in the shared module, fixed in compiler v0.56.0 (drivine-cypher-spec#539)" +) + + async def test_a_dollar_named_case_insensitive_property_is_found_ignoring_case( gom: StatelessGraphObjectManager, pm: PersistenceManager ): - if pm.type is not DatabaseType.SQLITE: - pytest.skip("Measured on SQLite only so far (drivine-cypher-spec#355)") await gom.save(CaseHeld(id="c1", mail="A@X.com")) # The query DSL's filter on SQLite is the shared module's fragment, which reads the property as the layout holds # it, `$$mail` (drivine-cypher-spec#235). @@ -193,6 +217,58 @@ async def test_a_dollar_named_case_insensitive_property_is_found_ignoring_case( assert found == ["c1"] +async def test_a_dollar_named_case_insensitive_property_is_found_through_its_copy_saved_before_the_index_and_after( + gom: StatelessGraphObjectManager, pm: PersistenceManager +): + if pm.type is DatabaseType.POSTGRES: + pytest.skip(UNMERGED_539) + label = f"CaseHeld{uuid.uuid4().hex[:10]}" + held = case_held(label) + spec = range_index(label, "$mail", ignore_case=True) + + async def found(mail: str) -> list[str]: + return sorted(it.id for it in await gom.load_all(held, where=refs(held).mail.eq_ignore_case(mail))) + + await gom.save(held(id="d0", mail="Before@X.com")) + assert await found("before@x.COM") == ["d0"] + assert (await gom.indexes.ensure(spec)).status == "CREATED" + await gom.save(held(id="d1", mail="Ann@X.com")) + # The node saved before the index has its copy filled in; the one saved after, written with it + # (drivine-cypher-spec#355). + assert await found("BEFORE@x.com") == ["d0"] + assert await found("ann@x.COM") == ["d1"] + for it in await nodes(pm, label): + assert [k for k in it if k.startswith(lower_copies.PREFIX)] == [MAIL_COPY] + # A node an earlier version saved, its copy under the held name, which no lookup reads: missed, until an ensure of + # the index already there fills its copy in again. + await create(pm, label, {"id": "d2", "$$mail": "Cid@X.com", f"{lower_copies.PREFIX}$$mail": "cid@x.com"}) + assert await found("cid@x.COM") == [] + assert (await gom.indexes.ensure(spec)).status == "ALREADY_MATCHING" + assert await found("CID@x.com") == ["d2"] + assert [it.get(MAIL_COPY) for it in await nodes(pm, label)] == ["before@x.com", "ann@x.com", "cid@x.com"] + await gom.indexes.drop(spec) + + +async def test_two_dollar_named_values_differing_only_in_case_are_refused_under_a_constraint_that_ignores_case( + gom: StatelessGraphObjectManager, pm: PersistenceManager +): + if pm.type is DatabaseType.POSTGRES: + pytest.skip(UNMERGED_539) + label = f"CaseHeldUnique{uuid.uuid4().hex[:10]}" + held = case_held(label) + spec = uniqueness_constraint(label, "$mail", ignore_case=True) + await gom.save(held(id="u1", mail="a@x.com")) + await gom.save(held(id="u2", mail="A@X.com")) + # Over the copy each node holds under the user's name: the two are one value ignoring case. + assert (await gom.constraints.ensure(spec)).status == "VIOLATION" + await gom.delete(held, "u2") + assert (await gom.constraints.ensure(spec)).status == "CREATED" + with pytest.raises(Exception): # noqa: B017 - each store refuses in its own words + await gom.save(held(id="u3", mail="A@X.COM")) + assert (await gom.constraints.ensure(spec)).status == "ALREADY_MATCHING" + await gom.constraints.drop(spec) + + async def test_a_patch_that_removes_a_dollar_named_property_removes_it_as_the_layout_holds_it( gom: StatelessGraphObjectManager, pm: PersistenceManager ): diff --git a/tests/sync_integration/sql/test_value_layout.py b/tests/sync_integration/sql/test_value_layout.py index e0bbea3a..65e95073 100644 --- a/tests/sync_integration/sql/test_value_layout.py +++ b/tests/sync_integration/sql/test_value_layout.py @@ -12,6 +12,7 @@ import json import math import re +import uuid from datetime import UTC, date, datetime, time, timedelta, timezone from typing import Annotated, Any from zoneinfo import ZoneInfo @@ -35,11 +36,15 @@ cypher_statement, equal_to, key, + lower_copies, max_, node_ref, not_equal_to, + range_index, refs, + uniqueness_constraint, ) +from tests.sync_integration.stores import create, nodes class Held(NodeFragment, labels=["Held"]): @@ -183,11 +188,30 @@ class CaseHeld(NodeFragment, labels=["CaseHeld"]): mail: Annotated[str | None, GraphProperty("$mail"), CaseInsensitive()] = None +def case_held(label: str) -> Any: + """ + A `$mail` held as `$$mail`, of a label of the test's own, so the items and records, which outlive its data, are its + own. + """ + + class Dollared(NodeFragment, labels=[label]): + id: Annotated[str, NodeId()] + mail: Annotated[str | None, GraphProperty("$mail"), CaseInsensitive()] = None + + return Dollared + + +MAIL_COPY = f"{lower_copies.PREFIX}$mail" +"""The copy of `$mail`, named for the user's name as each lookup and the ignore-case index read it.""" + +UNMERGED_539 = ( + "Postgres's save names the copy in the shared module, fixed in compiler v0.56.0 (drivine-cypher-spec#539)" +) + + def test_a_dollar_named_case_insensitive_property_is_found_ignoring_case( gom: StatelessGraphObjectManager, pm: PersistenceManager ): - if pm.type is not DatabaseType.SQLITE: - pytest.skip("Measured on SQLite only so far (drivine-cypher-spec#355)") gom.save(CaseHeld(id="c1", mail="A@X.com")) # The query DSL's filter on SQLite is the shared module's fragment, which reads the property as the layout holds # it, `$$mail` (drivine-cypher-spec#235). @@ -195,6 +219,58 @@ def test_a_dollar_named_case_insensitive_property_is_found_ignoring_case( assert found == ["c1"] +def test_a_dollar_named_case_insensitive_property_is_found_through_its_copy_saved_before_the_index_and_after( + gom: StatelessGraphObjectManager, pm: PersistenceManager +): + if pm.type is DatabaseType.POSTGRES: + pytest.skip(UNMERGED_539) + label = f"CaseHeld{uuid.uuid4().hex[:10]}" + held = case_held(label) + spec = range_index(label, "$mail", ignore_case=True) + + def found(mail: str) -> list[str]: + return sorted(it.id for it in gom.load_all(held, where=refs(held).mail.eq_ignore_case(mail))) + + gom.save(held(id="d0", mail="Before@X.com")) + assert found("before@x.COM") == ["d0"] + assert (gom.indexes.ensure(spec)).status == "CREATED" + gom.save(held(id="d1", mail="Ann@X.com")) + # The node saved before the index has its copy filled in; the one saved after, written with it + # (drivine-cypher-spec#355). + assert found("BEFORE@x.com") == ["d0"] + assert found("ann@x.COM") == ["d1"] + for it in nodes(pm, label): + assert [k for k in it if k.startswith(lower_copies.PREFIX)] == [MAIL_COPY] + # A node an earlier version saved, its copy under the held name, which no lookup reads: missed, until an ensure of + # the index already there fills its copy in again. + create(pm, label, {"id": "d2", "$$mail": "Cid@X.com", f"{lower_copies.PREFIX}$$mail": "cid@x.com"}) + assert found("cid@x.COM") == [] + assert (gom.indexes.ensure(spec)).status == "ALREADY_MATCHING" + assert found("CID@x.com") == ["d2"] + assert [it.get(MAIL_COPY) for it in nodes(pm, label)] == ["before@x.com", "ann@x.com", "cid@x.com"] + gom.indexes.drop(spec) + + +def test_two_dollar_named_values_differing_only_in_case_are_refused_under_a_constraint_that_ignores_case( + gom: StatelessGraphObjectManager, pm: PersistenceManager +): + if pm.type is DatabaseType.POSTGRES: + pytest.skip(UNMERGED_539) + label = f"CaseHeldUnique{uuid.uuid4().hex[:10]}" + held = case_held(label) + spec = uniqueness_constraint(label, "$mail", ignore_case=True) + gom.save(held(id="u1", mail="a@x.com")) + gom.save(held(id="u2", mail="A@X.com")) + # Over the copy each node holds under the user's name: the two are one value ignoring case. + assert (gom.constraints.ensure(spec)).status == "VIOLATION" + gom.delete(held, "u2") + assert (gom.constraints.ensure(spec)).status == "CREATED" + with pytest.raises(Exception): # noqa: B017 - each store refuses in its own words + gom.save(held(id="u3", mail="A@X.COM")) + assert (gom.constraints.ensure(spec)).status == "ALREADY_MATCHING" + gom.constraints.drop(spec) + + def test_a_patch_that_removes_a_dollar_named_property_removes_it_as_the_layout_holds_it( gom: StatelessGraphObjectManager, pm: PersistenceManager ): diff --git a/tests/unit/test_case_insensitive.py b/tests/unit/test_case_insensitive.py index c2969c49..0cf8a533 100644 --- a/tests/unit/test_case_insensitive.py +++ b/tests/unit/test_case_insensitive.py @@ -4,6 +4,7 @@ CaseInsensitiveStatementsTest pins them, so a store written by one library is read by the others. """ +import logging from collections.abc import Mapping from typing import Annotated, Any @@ -220,6 +221,11 @@ class Dollared(NodeFragment, labels=["Dollared"]): assert '["$$mail"]' in saved.bindings.values() assert '["$$mail"]' in backend.patch_one(model).bindings.values() assert '["$$mail"]' in backend.patch_many(model).bindings.values() + # Its copy is named for the user's name, $mail for $$mail (drivine-cypher-spec#355). + assert ( + "c := '__drivine.lower.' || CASE WHEN SUBSTR(k, 1, 2) = '$$' THEN SUBSTR(k, 2) ELSE k END;" + ) in saved.text + assert "THEN o.remove(c);" in saved.text and "ELSE o.put(c, v); END IF;" in saved.text def test_on_oracle_a_block_without_copies_declares_no_lower_node(self): from tests.models import Person @@ -333,6 +339,19 @@ def test_on_oracle_a_fill_reads_the_property_and_its_copy_as_clobs(self): ) in fill assert "AND ROWNUM <= 10000" in fill + def test_on_each_sql_store_a_fill_reads_a_dollar_named_property_as_held_and_names_its_copy_for_the_users_name(self): + # Held as $$mail, its copy is __drivine.lower.$mail, as each lookup reads it (drivine-cypher-spec#355). + def fill(type_: DatabaseType) -> str: + return schema_grammar_for(type_).lower_copies.fill("CiDollar", "$mail", 100) + + assert ( + "SET '$.\"__drivine.lower.$mail\"' = LOWER(JSON_VALUE(n.props, '$.\"$$mail\"' RETURNING CLOB))" + ) in fill(DatabaseType.ORACLE) + postgres = fill(DatabaseType.POSTGRES) + assert "jsonb_build_object('__drivine.lower.$mail', lower(n.props ->> '$$mail'))" in postgres + assert "CASE WHEN n.props ->> '$$mail' IS NULL THEN n.props - '__drivine.lower.$mail'" in postgres + assert "IS DISTINCT FROM lower((m.props ->> '$$mail'))" in postgres + class TestLookups: registry = ModelRegistry() @@ -500,6 +519,56 @@ async def resolve() -> None: assert not await LowerCopyCatalog(run, _statements(), resolve).filled_in("Account", "email") + async def test_fills_a_dollar_named_property_in_again_on_a_sql_store_though_recorded_and_not_on_a_graph_engine( + self, caplog: pytest.LogCaptureFixture + ): + # One saved before drivine-cypher-spec#355 held its copy under __drivine.lower.$$mail, or none: every ensure + # fills it in again, and the fill writes only the nodes whose copy is out of step. + postgres = schema_grammar_for(DatabaseType.POSTGRES).lower_copies + recorded = [{"label": "CiDollar", "properties": '["$mail"]'}, {"label": "CiDollar", "properties": '["email"]'}] + engine_recorded = ['["CiDollar","$mail"]', '["CiDollar","email"]'] + + async def resolve() -> None: + return None + + async def fills(statements: LowerCopyStatements, read: list[Any]) -> list[str]: + filled: list[str] = [] + + async def run(statement: str, bindings: Mapping[str, Any] | None) -> list[Any]: + if statement == statements.read: + return read + for property in ("$mail", "email"): + if statement == statements.fill("CiDollar", property, 3): + filled.append(property) + return [{"written": 0}] + return [] + + await LowerCopyCatalog(run, statements, resolve, batch=3).fill_in("CiDollar", ["$mail", "email"], False) + return filled + + with caplog.at_level(logging.DEBUG, logger="drivine.schema"): + assert await fills(postgres, recorded) == ["$mail"] + # A heal that writes nothing, as on every ensure after the first, is logged at debug, not info. + [healed] = [it for it in caplog.records if "CiDollar.$mail" in it.getMessage()] + assert healed.levelno == logging.DEBUG and "on 0 node(s)" in healed.getMessage() + assert await fills(CYPHER_LOWER_COPIES, engine_recorded) == [] + + # The schema planner fills in only what the read leaves out: a $-named record is left out on a SQL store. + async def stored(statements: LowerCopyStatements, rows: list[Any]) -> set[tuple[str, str]]: + async def run(statement: str, bindings: Mapping[str, Any] | None) -> list[Any]: + return rows + + return await LowerCopyCatalog(run, statements, resolve).stored() + + assert await stored(postgres, recorded) == {("CiDollar", "email")} + assert await stored(CYPHER_LOWER_COPIES, engine_recorded) == {("CiDollar", "$mail"), ("CiDollar", "email")} + + # A lookup still reads the whole record. + async def run_recorded(statement: str, bindings: Mapping[str, Any] | None) -> list[Any]: + return recorded + + assert await LowerCopyCatalog(run_recorded, postgres, resolve).filled_in("CiDollar", "$mail") + async def test_is_needed_to_ensure_an_item_that_ignores_case(self): class Empty: async def run(self, statement: Any, bindings: Any = None) -> list[Any]: diff --git a/tests/unit/test_sqlite_plan_writer.py b/tests/unit/test_sqlite_plan_writer.py index bd9b2d09..42066c86 100644 --- a/tests/unit/test_sqlite_plan_writer.py +++ b/tests/unit/test_sqlite_plan_writer.py @@ -127,3 +127,27 @@ def stamp_of(name: str) -> Any: assert [tuple(it) for it in edges] == [("ON_DAY", str(second))] assert stamp_of(str(second)) == held assert sorted(names(db)) == [str(first), str(second)] + + +def test_a_dollar_named_propertys_copy_is_named_for_the_users_name_in_a_save_and_in_the_fill(db: apsw.Connection): + # Held as $$mail, its copy is __drivine.lower.$mail, as each lookup and the ignore-case index read it + # (drivine-cypher-spec#355). + plan = WritePlan("sqlite") + shape = plan.shape(["Person"], "id") + plan.node( + shape, "id", "p1", stored_key("p1", "sqlite"), ["Person"], NodeWrites(set={"$mail": "ÀDA@X"}, lower=["$mail"]) + ) + plan.node(shape, "id", "p2", stored_key("p2", "sqlite"), ["Person"], NodeWrites(set={"$mail": "BOB@X"})) + written(db, plan) + + def props(id: str) -> dict[str, Any]: + [(held,)] = db.execute(f"SELECT props FROM {GRAPH.nodes} WHERE props ->> '$.id' = ?", (id,)).fetchall() + found: dict[str, Any] = json.loads(str(held)) + return found + + assert props("p1")["$$mail"] == "ÀDA@X" and props("p1")["__drivine.lower.$mail"] == "àda@x" + assert "__drivine.lower.$mail" not in props("p2") + [(filled,)] = db.execute("SELECT drivine_lower_copies(?, 'Person', '$mail', 100)", (GRAPH.name,)).fetchall() + assert filled == 1 + assert props("p2")["__drivine.lower.$mail"] == "bob@x" + assert [it for it in props("p1") if it.startswith("__drivine.lower.")] == ["__drivine.lower.$mail"] diff --git a/tests/unit/test_value_layout.py b/tests/unit/test_value_layout.py index b9c9cbe9..538424a9 100644 --- a/tests/unit/test_value_layout.py +++ b/tests/unit/test_value_layout.py @@ -14,7 +14,17 @@ import pytest from drivine.backend.sql.sql_json import to_json -from drivine.backend.sql.value_layout import Held, TagText, held_key, read, read_after, read_value, write, write_props +from drivine.backend.sql.value_layout import ( + Held, + TagText, + held_key, + read, + read_after, + read_value, + user_key, + write, + write_props, +) from drivine.backend.sqlite.sqlite_cypher_values import with_nan from drivine.drivine_error import DrivineError @@ -75,6 +85,13 @@ def test_props_are_written_each_by_its_name_as_the_layout_holds_it(): assert held_key("$a") == "$$a" and held_key("a$") == "a$" +def test_a_held_names_user_name_undoes_held_key_and_a_name_that_is_no_held_one_is_given_back_as_it_is(): + for name in ["k", "$k", "$$k", "k$", "$", ""]: + assert user_key(held_key(name)) == name + # One $ alone is no held name, and keeps its $ (drivine-cypher-spec#355). + assert [user_key("$mail"), user_key("mail")] == ["$mail", "mail"] + + def test_a_held_value_is_spliced_into_the_json_as_it_is_and_a_text_like_its_stand_in_is_left_alone(): held = write({"x": math.inf}, "sqlite") stand_in = "\u0000drivine:held:0" From c81ef24a06242984be5a32cc6f52f7093c401cf4 Mon Sep 17 00:00:00 2001 From: jasper blues Date: Sat, 5 Dec 2026 15:30:46 +1000 Subject: [PATCH 2/6] Say the lowercased copy is named for the user's name (drivine-cypher-spec#355) --- drivine/model/lower_copies.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/drivine/model/lower_copies.py b/drivine/model/lower_copies.py index c9fd27ff..1357af87 100644 --- a/drivine/model/lower_copies.py +++ b/drivine/model/lower_copies.py @@ -23,7 +23,11 @@ def of(property: str) -> str: - """The property the copy of `property`, a stored name, is stored under.""" + """ + The property the copy of `property`, a stored name, is stored under. The name is the one after `GraphProperty`, not + as a SQL store's layout holds it: the copy of `$mail`, held as `$$mail`, is `__drivine.lower.$mail` + (drivine-cypher-spec#355). + """ return f"{PREFIX}{property}" From ba9ad937045ea2754cf0a4b16feb5f6c6bc76220 Mon Sep 17 00:00:00 2001 From: jasper blues Date: Sat, 5 Dec 2026 15:32:55 +1000 Subject: [PATCH 3/6] Say the dollar-named filter test reads the held key on each SQL store (drivine-cypher-spec#355) --- tests/integration/sql/test_value_layout.py | 4 ++-- tests/sync_integration/sql/test_value_layout.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/integration/sql/test_value_layout.py b/tests/integration/sql/test_value_layout.py index 07d951ae..2bc5e834 100644 --- a/tests/integration/sql/test_value_layout.py +++ b/tests/integration/sql/test_value_layout.py @@ -211,8 +211,8 @@ async def test_a_dollar_named_case_insensitive_property_is_found_ignoring_case( gom: StatelessGraphObjectManager, pm: PersistenceManager ): await gom.save(CaseHeld(id="c1", mail="A@X.com")) - # The query DSL's filter on SQLite is the shared module's fragment, which reads the property as the layout holds - # it, `$$mail` (drivine-cypher-spec#235). + # The query DSL's filter on each SQL store is the shared module's fragment, which reads the property as the layout + # holds it, `$$mail` (drivine-cypher-spec#235). found = [it.id for it in await gom.load_all(CaseHeld, where=refs(CaseHeld).mail.eq_ignore_case("a@x.COM"))] assert found == ["c1"] diff --git a/tests/sync_integration/sql/test_value_layout.py b/tests/sync_integration/sql/test_value_layout.py index 65e95073..4c1d0435 100644 --- a/tests/sync_integration/sql/test_value_layout.py +++ b/tests/sync_integration/sql/test_value_layout.py @@ -213,8 +213,8 @@ def test_a_dollar_named_case_insensitive_property_is_found_ignoring_case( gom: StatelessGraphObjectManager, pm: PersistenceManager ): gom.save(CaseHeld(id="c1", mail="A@X.com")) - # The query DSL's filter on SQLite is the shared module's fragment, which reads the property as the layout holds - # it, `$$mail` (drivine-cypher-spec#235). + # The query DSL's filter on each SQL store is the shared module's fragment, which reads the property as the layout + # holds it, `$$mail` (drivine-cypher-spec#235). found = [it.id for it in gom.load_all(CaseHeld, where=refs(CaseHeld).mail.eq_ignore_case("a@x.COM"))] assert found == ["c1"] From a9ddc0867f444c7cb68bb127667ea2ead91a0d83 Mon Sep 17 00:00:00 2001 From: jasper blues Date: Sat, 5 Dec 2026 17:24:49 +1000 Subject: [PATCH 4/6] Build against the Cypher compiler 0.56.0 and its spec, take the host functions' version 3, and the golden statements Drivine for TypeScript writes with it The compiler names a dollar-named property's lowercased copy on Postgres too (drivine-cypher-spec#539), so the two dollar-named tests that skipped Postgres run there. --- drivine/backend/sqlite/sqlite_extension.py | 2 +- pyproject.toml | 8 +- tests/cypher/README.md | 39 +- tests/cypher/VERSION | 2 +- tests/cypher/fragments.yaml | 86 +- tests/cypher/host.yaml | 34 +- tests/cypher/marker.yaml | 39 +- tests/cypher/plans.yaml | 50 + tests/cypher/queries.yaml | 413 ++- tests/cypher/refusals.yaml | 164 + tests/cypher/store.yaml | 29 +- tests/cypher/temporal.yaml | 105 + tests/cypher/writes.yaml | 2889 +++++++++++++++-- tests/golden/statements.json | 36 +- tests/integration/sql/test_value_layout.py | 8 - .../sync_integration/sql/test_value_layout.py | 8 - tests/unit/test_sqlite_extension.py | 2 +- uv.lock | 18 +- 18 files changed, 3505 insertions(+), 427 deletions(-) diff --git a/drivine/backend/sqlite/sqlite_extension.py b/drivine/backend/sqlite/sqlite_extension.py index 7a2de2ba..bacbd868 100644 --- a/drivine/backend/sqlite/sqlite_extension.py +++ b/drivine/backend/sqlite/sqlite_extension.py @@ -26,7 +26,7 @@ ENTRY = "sqlite3_drivinecypher_init" """The extension's entry point in the module's library.""" -HOST_VERSION = 2 +HOST_VERSION = 3 """The version of the host functions' forms compiled SQL expects: the module's `SqliteFunctions.HOST_VERSION`.""" PACKAGE = "drivine-cypher-compiler" diff --git a/pyproject.toml b/pyproject.toml index a63463cf..7bfe980b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,25 +32,25 @@ falkordb = [ # Full-text search on a graph engine is written by the shared compiler too, which the graph engines' extras don't # install, as it has no wheel for every platform they run on (an Intel Mac, musl Linux): `pip install # "drivine[neo4j,compiler]"` where it has one. Pinned as the SQL stores' extras pin it. -compiler = ["drivine-cypher-compiler==0.54.4"] +compiler = ["drivine-cypher-compiler==0.56.0"] # Each SQL store compiles Cypher with the shared compiler, drivine-cypher-compiler, a native library with a wheel for # macOS on Apple Silicon, Linux with glibc (x86_64, aarch64) and Windows x64: in the extras, so `pip install drivine` # installs anywhere and the graph engines work where it has no wheel (an Intel Mac, musl Linux). Pinned exactly, to the # compiler of the Cypher spec tests/cypher is a copy of. oracle = [ "oracledb>=3.0", - "drivine-cypher-compiler==0.54.4", + "drivine-cypher-compiler==0.56.0", ] postgres = [ "psycopg[binary]>=3.2", "psycopg-pool>=3.2", - "drivine-cypher-compiler==0.54.4", + "drivine-cypher-compiler==0.56.0", ] # SQLite is apsw's, which bundles its own SQLite: 3.53.2 or later, which writes a float as text to 17 significant # digits as compiled Cypher needs (3.53.0 and 3.53.1 still write 15), whatever SQLite Python was built with (#376). sqlite = [ "apsw>=3.53.2", - "drivine-cypher-compiler==0.54.4", + "drivine-cypher-compiler==0.56.0", ] [project.entry-points.pytest11] diff --git a/tests/cypher/README.md b/tests/cypher/README.md index 8d38273e..39931aba 100644 --- a/tests/cypher/README.md +++ b/tests/cypher/README.md @@ -13,7 +13,7 @@ These files say what a statement in Drivine's flavour of Cypher gives on each st - `store.yaml`: the store layout's DDL: the tables, the probe that finds them made, and each index and constraint as each store makes, drops and lists it, for the graph `drivine` and a set of schema specs, with a read for each index that the compiler must answer from it. Read with no store. See [The store layout](#the-store-layout). It also holds the store's schema: how each store's listings are read back, whether an item is a spec's and in its shape, and on a SQL store of which form, what each store can do, and what its errors say. See [The store's schema](#the-stores-schema). - `marker.yaml`: the schema marker (`_DrivineSchema`) and the stamps: the statements each store reads and writes the marker and the store's stamp (`DrivineStore`) with, the marker's inventory as it is written and read back, the version token, and the form of the stamp each node carries. Read with no store. See [The schema marker and the stamps](#the-schema-marker-and-the-stamps). - `migrate.yaml`: a store's layout migrated, from layout 1 to layout 2: each row of a node's or a relationship's properties as layout 1 held it, on each SQL store, rewritten as layout 2 holds it, under what the migration is told of the properties' kinds. Read with no store. See [Migrating a store's layout](#migrating-a-stores-layout). -- `bulk.yaml`: a bulk import of nodes and relationships into a SQL store: the statements a library runs, in order, in one transaction, for the graph `drivine`. Read with no store. See [Importing a graph in bulk](#importing-a-graph-in-bulk). +- `bulk.yaml`: a bulk import of nodes and relationships into a SQL store: the statements a library runs, in order, in one transaction, for the graph `drivine`, and the JSON entry's request for them (`json`). Read with no store. See [Importing a graph in bulk](#importing-a-graph-in-bulk). - `plans.yaml`: the schema plan: what a schema manager runs on each store, round by round, for an enforce, a recreate, an ensure or a remove, what the store answers, and what the manager reports, its log lines among it. Read with no store. See [The schema plan](#the-schema-plan). - `declarations.yaml`: what each library's schema declarations give the schema planner: the catalogs of a request, and the items ensured at runtime, for each of a set of declarations. Read with no store. See [The schema declarations](#the-schema-declarations). - `host.yaml`: what each library's own code does with the values compiled SQL hands it: the functions it registers on SQLite, and how it reads a column of a statement's rows. Read with no store. See [Host functions and readers](#host-functions-and-readers). @@ -71,6 +71,8 @@ On Neo4j a statement runs in the database's default language, Cypher 25 on Neo4j - A temporal value made of a list, a number or a truth value fails by name on the SQL stores, where it gave null, as Neo4j fails it (liberation-data/drivine-cypher-spec#331): as the statement compiles where it is written or given, so never null, and as it runs where it is read, a variable's, a function's or a `CASE`'s, which gives null where it is null, as Neo4j's does; a truth value a comparison makes, `date(n.x > 1)`, is refused as it compiles, though it may be null. Neo4j fails only a row that reaches the call, so a statement that gives no rows is refused on the SQL stores where Neo4j gives none. - `+` of a truth value and a number, a truth value, a map or a temporal value, or of a map and a number, gave null on the SQL stores, where Neo4j refuses it (liberation-data/drivine-cypher-spec#341): it is refused as the statement compiles where the compiler knows both kinds. A truth value put together with a text is its text, `'a' + true` is `atrue`, as in Neo4j, where the SQL stores gave null. - A map beside a text or a temporal value in `+`, `*` or `/` of a temporal value other than a duration, and `NOT` of a parameter given a value that is no truth value or of a text put together, gave null on the SQL stores, where Neo4j refuses each as it plans the statement (liberation-data/drivine-cypher-spec#345): each is refused as the statement compiles. A truth value read as the statement runs (a property, a variable or an item) beside a number the statement makes or gives, in `+`, `-`, `*` or `/`, fails the statement there, where it gave null, as Neo4j fails it, by one test of the side read: `n.flag + 1`, `UNWIND $l AS b RETURN b + 1`. Beside a number the statement makes that is null as it runs (`n.flag + toFloat(n.missing)`), it is null, as in Neo4j. Beside a side of a kind left open too (`n.flag + n.n`), a map or a temporal value (`n.flag + date('2024-01-01')`), or worked out rather than read (`head($l) + 1`), it still gives null on the SQL stores, where Neo4j fails; so does a text, a list or a map read as the statement runs in `-`, `*` or `/`, and a truth value in `%` or `^`. A test of each side's kind in every `+` (#341) grew a sum of sums at every level. +- A temporal value the statement makes or a parameter gives, and a parameter given an integer, a text or a map, read as a condition (in a `WHERE`, `AND`, `OR`, `XOR` or `NOT`, a searched `CASE`'s `WHEN`, or a list comprehension's or a quantifier's `WHERE`, carried by a `WITH`, an `UNWIND`, a list's item or a map's entry) gave null on the SQL stores, or kept no row, where Neo4j refuses the statement as it plans it, over no rows too (liberation-data/drivine-cypher-spec#451): each is refused as the statement compiles. A temporal value beside a null written or read in a `coalesce()`, a `CASE` or a list, or an entry of a map taken from a list of maps, which Neo4j types as any value, as it does a sum of one (it refuses a difference, a product or a quotient of one), and a parameter given a float, which Neo4j fails only as a row reads it, a list, which it reads by whether it holds an item, a truth value or null, are compiled as before. +- A value whose kind only the row tells, read as a condition, an index, a key or a list where it is of another kind than that takes, is null on the SQL stores in a statement that only reads, where Neo4j fails it, as a SQL engine orders conditions as it likes and a failure there would fail statements Neo4j runs (liberation-data/drivine-cypher-spec#453, #458). A statement that writes fails there instead, before it writes anything, as Neo4j fails it (#493): a condition that turns out a number, a text, a list or a temporal value; an index of what is no list, or that is no whole number; a text key of what is no map, or of a temporal value but for a field of its kind written in the statement (#484); a node, a relationship, a map the statement makes or gives, or `properties()`, read as a list. A row a condition joined by `AND` at the top of a `WHERE` is false for fails nothing, whichever is written first, nor does a row a pattern finds nothing for, the properties it is given among them; any other such value read fails the statement, even beside a condition that decides, as Neo4j reads `AND`, `OR` and `XOR` in the order its plan gives. A `CASE`'s results and conditions, `coalesce()`'s arguments and a quantifier's items are read only as far as Neo4j reads them. A map or a temporal value only the row tells the kind of is read as a list of it, as Neo4j reads it, in a statement that writes. - A temporal value given at an offset with seconds, a parameter or a value a save writes, is refused by name on the SQL stores, where Neo4j holds it, as the store holds an offset to the minute (see "A time" in [Values in the store](#values-in-the-store)): `{"$temporal": "DATE_TIME", "text": "1971-01-01T00:00:00-00:44:30"}`, or a JVM `OffsetDateTime` or `ZonedDateTime` there, as java.time writes a zone's local mean time (`Africa/Monrovia` before 1972), is refused with "A value given as a DATE_TIME isn't one: 1971-01-01T00:00:00-00:44:30 (the store holds an offset to the minute)." So is any other value given as a temporal value whose text is no value of its kind, `{"$temporal": "DATE", "text": "2024-13-01"}`. Each was bound, or saved, as its text, equal to no temporal value (liberation-data/drivine-cypher-spec#357). `host.yaml`'s "Values a save writes" pins the refusal; the spec's statement files take no parameter given as a temporal value, so a statement's is pinned by the module's tests. - Quote every string. A YAML 1.1 parser, such as SnakeYAML or PyYAML, reads a bare `y`, `yes`, `no` or `on` as a boolean. - `writes.yaml` binds and expects a whole number beyond 2^53. A library whose language reads YAML numbers as doubles skips those steps, named in its own runner. Otherwise a library skips nothing: a store that differs says so with `except` here. @@ -84,6 +86,7 @@ In its rows: - `{$t: "node", labels: […], properties: {…}}` is a node, with its labels in any order; - `{$t: "relationship", type: "…", properties: {…}}` is a relationship; - `{$t: "path", nodes: […], relationships: […]}` is a path, its nodes and relationships in the order it walks them; +- `{$t: "date", v: "2024-02-29"}` is a temporal value, of the kind `$t` names, `date`, `localtime`, `time`, `localdatetime`, `datetime` or `duration`, with its canonical text `v`, as layout 2 holds it: a graph engine's driver gives a temporal value, and a SQL store's typed read gives one from its tag, read with the module's `ValueLayout.readValue(value, typed = true)` (or `read` typed), not its text (#483). A text, as `"2024-02-29"` is, stays a text; - `{$map: {…}}` is the map it holds, for a map with a `$t` key of its own. On a SQL store, the compiler gives the columns, and compiled typed (`typed`) it tags each node and relationship as above in the columns that hold them: see the compiler's README. It is opt-in, and untyped every statement compiles as before. @@ -150,7 +153,7 @@ cases: | Key | What it is | |---|---| -| `tables` | What makes each SQL store's tables, base indexes and functions (`ddl`, and Oracle's `functions`), each statement safe to run again, and how a library finds them made. On Postgres, `ensuring` is one block: the probe of the node table's comment, which holds the version token (`drivine:12`), the graph's advisory lock, and the `ddl`. On Oracle, `ensuring` probes the property graph's `doc` property and each function, and makes what is missing, or a function not of its version. A function whose compile fails (`ORA-24344`, or `ORA-04021` and `ORA-04031`, a busy library cache or a short shared pool) is compiled again after 0.5 s, then 1 s, and after its third try the block fails with `ORA-20818`, naming the function and what `user_errors` says of it (liberation-data/drivine#818). On SQLite, `made` counts what of the `ddl` is there, or is -1 for tables made without `AUTOINCREMENT`, and `file` names the database's file. | +| `tables` | What makes each SQL store's tables, base indexes and functions (`ddl`, and Oracle's `functions`), each statement safe to run again, and how a library finds them made. On Postgres, `ensuring` is one block: the probe of the node table's comment, which holds the version token (`drivine:16`), the graph's advisory lock, and the `ddl`. On Oracle, `ensuring` probes the property graph's `doc` property and each function, and makes what is missing, or a function not of its version. A function whose compile fails (`ORA-24344`, or `ORA-04021` and `ORA-04031`, a busy library cache or a short shared pool) is compiled again after 0.5 s, then 1 s, and after its third try the block fails with `ORA-20818`, naming the function and what `user_errors` says of it (liberation-data/drivine#818). On SQLite, `made` counts what of the `ddl` is there, or is -1 for tables made without `AUTOINCREMENT`, and `file` names the database's file. | | `catalog` | Each store's queries that list its indexes of each kind and its constraints; on a SQL store, the statements that set and take away the `aliases` of the record of `item`; and Oracle's `labelled`, the labelled indexes a library tells the compiler of as `indexedProperties`. On a SQL store a listing of range indexes or of constraints, and on Postgres and Oracle one of vector or full-text indexes, gives, after the records, a row for each object under a name Drivine gives that kind's objects (see [Forms](#the-stores-schema)). | | `names` | The name a SQL store gives an object of `kind` made for `parts`, a label and properties, for a `graph`: `___`, where `layout` is the store layout the object holds values in, `2` (see [Values in the store](#values-in-the-store)). `readable` is the parts' ASCII letters and digits run together, cut to at most 40 and to what leaves room, within the store's longest name (63 on Postgres and SQLite, 128 on Oracle), for the rest and a `_t`. `hash` is Java's `String.hashCode` of the parts joined by U+0000, over their UTF-16 code units, as `Integer.toHexString` writes it: unsigned, in lower case, with no leading zeros (`drivine_ix2_name_337a8b`). | | `refusals` | Why a store refuses a graph's name, or null where it takes it. A graph's name is at most the store's longest name less 16 (47 on Postgres and SQLite, 112 on Oracle): the longest a name adds is `_uq2__`, a hash of 8 with nothing readable, and the `_t`, `_i` or `_r` of an object made beside it, so each fits. | @@ -196,7 +199,7 @@ The canonical text `v`: - **A local time** is `hh:mm:ss`, the seconds always written, then where the second has a fraction a point and the fewest of three, six or nine digits that hold it, as Java's `Instant.toString` writes one: `10:30:00`, `10:30:00.500`, `10:30:00.000001`, `10:30:00.123456789`. - **A time** is a local time and its offset: `Z` for an offset of zero, however it was written (`+00:00`, `-0000`), else `±hh:mm`. Offsets run from -18:00 to +18:00. A named zone (`[Europe/Paris]`) is not held: a library writes the offset it has at that moment. An offset with seconds (`-00:44:30`) is not held either: a value given at one is refused by name (see [Values](#values)). - **A local moment** is a date, `T`, and a local time; **a moment** a date, `T`, and a time. -- **A duration** is its months, days and seconds, kept apart as Cypher keeps them (`P1D` is not `PT24H`), written as Neo4j writes one: `P`, then the whole years and the months left of the months, then the days, then `T` and the whole hours, the whole minutes and the seconds left, with their fraction and no trailing zeros, of the seconds; each part left out where it is 0, each signed on its own (`PT-1H-30M`), and `PT0S` where all are 0. A week is 7 days; a fraction of a month is 30.436875 days, and a fraction of a day 86,400 seconds, carried to the part below. +- **A duration** is its months, days and seconds, kept apart as Cypher keeps them (`P1D` is not `PT24H`), written as Neo4j writes one: `P`, then the whole years and the months left of the months, then the days, then `T` and the whole hours, the whole minutes and the seconds left, with their fraction and no trailing zeros, of the seconds; each part left out where it is 0, each signed on its own (`PT-1H-30M`), and `PT0S` where all are 0. A week is 7 days; a fraction of a month is 30.436875 days, and a fraction of a day 86,400 seconds, carried to the part below. As in Neo4j, a duration's seconds are written to nine places at most, and its length in whole seconds, its fraction left out (a negative duration's seconds floored), a month as 2,629,746 of them and a day as 86,400, lies within a 64-bit integer, each product and sum on the way too, the days' added to the months' before the seconds: a duration text the statement writes past either is refused, and one it works out past the nanosecond fails as it runs (#491); one made from a map of numbers the statement writes past the length is refused too, and one made past it from a map of fields worked out, of a text worked out, or by arithmetic fails as it runs, as Neo4j fails it as it makes the duration, as does one scaled whose whole seconds or nanoseconds of the second, which Neo4j scales each on their own, pass a 64-bit integer (#498). The sort key `s` is worked out of `v` alone, never of a store's timestamp, which holds neither every year nor nanoseconds; so every writer's `s` of a value is the same. Within one `$t`, two values order as their `s` do as text, under any collation, as Neo4j orders them. Values of two kinds have no order, and neither have durations. @@ -360,7 +363,7 @@ nodeStamps: {property: "__drivine.stamp", token: 16, separator: ":", neverStampe | `inventory` | A `record` of keys, the runtime ones and the stale ones; its `items`, as every store but Oracle keeps them; and what is `read` back from them. | | `reads` | A stored text as a store gives it back, or null where no marker or no `items` came back, and what is `read` from it: null for no inventory known, which is not an empty one. `oracle` is what Oracle reads, where it differs, its mark taken off first. | | `versions` | The version tokens of an owner's catalogs, null for one that sets none, and the token the owner records: null where none sets one. | -| `storeStamp` | The stamp's label, the property it is merged on and that property's value, its constraint, and what `assignIfAbsent` binds: `storeId`, a new UUID's text, `assignedAt`, the moment as ISO-8601 text in UTC, as Java's `Instant.toString` writes it (`2026-11-10T05:00:00.123456Z`: the fraction of a second in three, six or nine digits, the fewest that hold it, and none for a whole second), and not a number, as `readStamps` orders by it, `engine`, the store's name as the stamp records it, and `layout`, the store layout the library holds values in: 2 on a SQL store, 1 on a graph engine, whose stamp's statements don't write it. Each store's `engine`, as the stamp records it, its `layout`, its `prepare`, run first as the marker's is, and statements: `assignIfAbsent` makes the stamp where there is none; `readStamps` reads every stamp as a map `{id, assignedAt, layout}`, the oldest first, tie-broken by id, and a library takes the first, so every reader takes the same one; `foreignNodes` gives a row where the graph holds a node other than the stamp and the schema marker; `restampLayout` sets the stamp's `layout`, binding `restampParameters`, only while it holds none, in one statement; and `layoutObjects` lists the indexes, tables and triggers Drivine may have made for the graph, each `{name, type}`. The last two are null on a graph engine. See `storeLayout`. | +| `storeStamp` | The stamp's label, the property it is merged on and that property's value, its constraint, and what `assignIfAbsent` binds: `storeId`, a new UUID's text, `assignedAt`, the moment as ISO-8601 text in UTC, as Java's `Instant.toString` writes it (`2026-11-10T05:00:00.123456Z`: the fraction of a second in three, six or nine digits, the fewest that hold it, and none for a whole second), and not a number, as `readStamps` orders by it, `engine`, the store's name as the stamp records it, and `layout`, the store layout the library holds values in: 2 on a SQL store, 1 on a graph engine, whose stamp's statements don't write it. Each store's `engine`, as the stamp records it, its `layout`, its `prepare`, run first as the marker's is, and statements: `assignIfAbsent` makes the stamp where there is none; `readStamps` reads every stamp as a map `{id, assignedAt, layout}`, the oldest first, tie-broken by id, and a library takes the first, so every reader takes the same one, its `layout` as the store holds it, a number or not, so that one of no layout reaches the verdict to be refused (#479); `foreignNodes` gives a row where the graph holds a node other than the stamp and the schema marker; `restampLayout` sets the stamp's `layout`, binding `restampParameters`, only while it holds none, in one statement; and `layoutObjects` lists the indexes, tables and triggers Drivine may have made for the graph, each `{name, type}`. The last two are null on a graph engine. See `storeLayout`. | | `storeLayout` | The verdict on a graph by its store layout (#161), which `StoreLayout.verdict` gives and each library acts on at its first connection, before any statement runs: before the store's `ensuring`, `ddl` or the stamp's `prepare` makes anything, as this layout's lock table, which they make, is what tells a graph of this layout from an earlier one's where a clear deleted its stamp. A graph whose table of nodes isn't there yet is new, and needs no verdict. Its `firstConnection` cases hold each library to that order, which no statement of the module's can: each library's runner sets a case up on its store (an earlier layout's table of nodes holding a value, with no stamp), opens a manager of the graph, and checks that its first connection refuses it with the message `refuse` (`{graph}` the graph again), not another error, and makes none of `absent`, this layout's lock table. Each case gives the `store`, the `stamps` `readStamps` read (their `layout`, null where a stamp has none), whether `foreignNodes` gave a row, and the `objects` `layoutObjects` listed, each `{name, type}`. Its `verdict` is `"current"`, the graph to be used; `{stamp: {drops}}`, the graph, which holds no node but Drivine's own, re-stamped: each of `drops` run, the earlier layout's indexes, tables and triggers and their catalog records, then `restampLayout` with what `restampParameters` names, then the stamps read back and the objects listed again, and judged again, anything but `"current"` refused, so a store raced by an earlier library is never used on a guess; or `{refuse}`, the message the library refuses the graph with. Which verdict a graph gets is [the layout's verdict](#the-layouts-verdict)'s table. A library from before layouts reads no `layout`, and can't refuse a graph this one wrote: the upgrade notes say not to point one at it. | | `nodeStamps` | The node stamp's property and form, two tokens of `token` hexadecimal digits joined by `separator`. `tokens` are the text of a token of 64 `bits`, as Java's `String.format("%016x")` writes it. Each of `stamps` is a stored stamp, its `nodeToken` (what comes before its first colon, or all of it), its `linksToken` (what comes after, or null for nothing), whether it is `twoTokens` (33 long, with its colon at 16), whether it `hasNodeToken` (longer than 16, with its colon at 16), and the stamp it is `withoutLinks`. `neverStamped` is what a save reports as the stamp of a node it found with none. | @@ -372,10 +375,11 @@ nodeStamps: {property: "__drivine.stamp", token: 16, separator: ":", neverStampe ### The layout's verdict -A SQL store's layout is 2, a graph engine's 1, and a stamp with no `layout`, or one that is no number, is 1: on a graph engine that is its own, on a SQL store an earlier one. The verdict is the first row of this table that the graph meets, and every graph meets one: the module's `StoreLayoutTableTest` asks it of every combination of an engine, the stamps (none, this layout's, an earlier one's, one with no `layout`, one whose `layout` is no number, a later one's, this one's beside a later one's or beside one with no `layout`), whether the graph holds values, the objects the store lists (any set of an earlier layout's generated names, `_ix_…`; this layout's lock table, `_lock2`; its generated names, `_uq2_…`; a later layout's, `_ix3_…`, and its lock table, `_lock3`; a migration's checkpoint, `_migrate2`; another graph's whose name begins with this one's; entries with no name, or one that is no text), and whether the library gives the store's DDL, and holds each to its row. DDL of another store than the graph's is the library's error, thrown. The values are whether the graph holds a node other than Drivine's own; the objects are known as a layout's only by the names it generates, where the library gives the DDL, and a graph engine lists none. +A SQL store's layout is 2, a graph engine's 1, and a stamp with no `layout` is 1, as a library from before layouts stamps: on a graph engine that is its own, on a SQL store an earlier one. A stamp whose `layout` is no whole number from 1, as a number or its text (`"two"`, `2.7`, `0`, `-1`, `4294967298`), is of no layout Drivine writes: it is refused, naming the layout as stored (row 0), never read as another layout and the graph dropped or restamped on the guess (#479); so is a migration that reads one under its lock. The verdict is the first row of this table that the graph meets, and every graph meets one: the module's `StoreLayoutTableTest` asks it of every combination of an engine, the stamps (none, this layout's, as a number and as text, an earlier one's, one with no `layout`, a later one's, this one's beside a later one's or beside one with no `layout`, and those of no layout: a text that is no number, a whole number past an Int's range as a number and as text, a fraction, 0, a negative, and one beside this layout's), whether the graph holds values, the objects the store lists (any set of an earlier layout's generated names, `_ix_…`; this layout's lock table, `_lock2`; its generated names, `_uq2_…`; a later layout's, `_ix3_…`, and its lock table, `_lock3`; a migration's checkpoint, `_migrate2`; another graph's whose name begins with this one's; entries with no name, or one that is no text), and whether the library gives the store's DDL, and holds each to its row. DDL of another store than the graph's is the library's error, thrown. The values are whether the graph holds a node other than Drivine's own; the objects are known as a layout's only by the names it generates, where the library gives the DDL, and a graph engine lists none. | # | Stamps | Values | Objects | Verdict | |---|---|---|---|---| +| 0 | one whose `layout` is no layout | any | any | refused: names the layout as stored, which something other than Drivine wrote | | 1 | of two layouts or more | any | any | refused: stamped by two versions at once | | 2 | of a later layout | any | any | refused: written by a later Drivine, to be upgraded | | 3 | any | any | a later layout's (`_ix3_…`, `_lock3`) | refused, as row 2 | @@ -442,7 +446,7 @@ The module's `LayoutMigrator`, or the JSON request `{"migrate": …}`, runs a mi 5. **Check.** Each uniqueness constraint the catalog recorded as the migration began, kept in its checkpoint, is checked for keys that repeat, by the schema plan's own sample of what a constraint would refuse (`sampleConflictsQuery`), the store's SQL, run uncompiled as the schema plan runs it (a `query` of purpose `collisions`). After the writes it is exact, over layout 2's values, as the index an application's schema plan makes would refuse them: keys that repeat end the run `collisions`, with samples, every value rewritten and the checkpoint kept, so the graph stays refused as part way through a migration. The user corrects the rows with the store's own tools (`psql`, `sqlplus`, `sqlite3`), keeping their values in layout 2, and resumes; the migration offers no statement of its own to run on a graph part way through one. In a dry run the check reads the constraints from the catalog (`uniques`) and runs over layout 1's values, listing possible collisions: it misses values that become equal only once rewritten (two spellings of one moment) and counts a date and the text that spells it as one. 6. **Finish.** Any of the earlier layout's objects still listed are dropped, with their records, as in step 3; every stamp is given this layout (`stampLayout`, or, where the graph has none, the stamp's `assignIfAbsent` with the library's own id and moment: `assignStamp`), and the checkpoint table dropped last (on Oracle with `PURGE`). The graph is then judged again, and must be current, an ordinary graph of this layout (`migrate.yaml`'s `finished`): the run ends `migrated`. **No index or constraint is made**: an application's schema plan makes its declared ones at its next start, as it makes any it finds missing, and until one does, statements give the same answers, unindexed, so slowly on a large graph; a graph migrated with no application has none. **The count store is left empty**, as on any fresh store of this layout, its counts seeded when Drivine begins to keep them (#349). Every report from the check on (a dry run's, `collisions` and `migrated`) says both, as its `notes`; one that ends before it, refused or `invalid`, wrote nothing for them to be of. -The checkpoint, the row `checkpoint` of the checkpoint table, holds the layouts it migrates between, the table and the id the write is after (no table once every row is written), the digest of the declarations, whether it keeps values not of their declared kind, the report so far, the uniqueness constraints the catalog recorded as the migration began (each its label, then its properties), which the check reads, and the fingerprint of the graph as the migration found it: the SHA-256 of Drivine's own nodes and the graph's size, as canonical JSON, none of which a rewrite changes. A resume reads them again, and refuses a graph whose fingerprint differs, one written to under the migration, or declarations, or an option to keep values not of their kind, that differ from those the migration began with. Once every row is written (no table), a resume checks neither: what is left reads the graph as it then is, a row corrected after a collision, or a stamp given this layout before the checkpoint was dropped. While the checkpoint table is there the verdict refuses the graph (row 4), as part way through a migration. Under the lock, a migration that judged the graph beside another that has since finished, and dropped its checkpoint, finds the stamp of this layout and no checkpoint, beginning or resuming, in the table it made again, or made and stopped (the graph refused as part way through until it is begun again): it drops that empty checkpoint table, and ends refused, having written nothing. +The checkpoint, the row `checkpoint` of the checkpoint table, holds the layouts it migrates between, the table and the id the write is after (no table once every row is written), the digest of the declarations, whether it keeps values not of their declared kind, the report so far, the uniqueness constraints the catalog recorded as the migration began (each its label, then its properties), which the check reads, and the fingerprint of the graph as the migration found it: the SHA-256 of Drivine's own nodes and the graph's size, as canonical JSON, none of which a rewrite changes. A resume reads them again, and refuses a graph whose fingerprint differs, one written to under the migration, or declarations, or an option to keep values not of their kind, that differ from those the migration began with. Once every row is written (no table), a resume checks neither: what is left reads the graph as it then is, a row corrected after a collision, or a stamp given this layout before the checkpoint was dropped. While the checkpoint table is there the verdict refuses the graph (row 4), as part way through a migration. Under the lock, a migration that judged the graph beside another that has since finished, and dropped its checkpoint, finds the stamp of this layout and no checkpoint, beginning or resuming, in the table it made again, or made and stopped (the graph refused as part way through until it is begun again): it drops that empty checkpoint table, and ends refused, having written nothing. One that finds there instead a stamp whose `layout` is of no layout, written meanwhile by something other than Drivine, ends refused, having written nothing, and keeps its empty checkpoint table, so the graph stays refused until the stamp is corrected and the migration resumed (#479). An action is one of a `query`, whose rows the planner reads, each one JSON object read into a map; an `execute`, in auto-commit; a `transaction` of `execute`s, all of them or none; `prepare`; `lock`; and `assignStamp`. Each is answered `rows`, `ran` or `failed`, with the store's message, which ends the migration as `failed`. `migrate.yaml`'s `statements` pin each store's statements for the graph `drivine`, and the module's `LayoutMigratorTest` runs a migration against a store kept in memory, stopped after each action of each round and begun again, and requires every row rewritten exactly once and the rows a migration never stopped gives. @@ -478,6 +482,23 @@ table outgrows the cache, each check is a read and a write of the disk. At scale that took the load from 1,870 s to 219 s. Made again at the end, the foreign keys check every relationship, so one with no node at an end fails the import there. The staging tables are temporary, and dropped at the commit. +A library with no Kotlin types asks the module's JSON entry for the statements (drivine-cypher-spec#435): + +```json +{"bulkStatements": {"store": "postgres", "graph": "g", + "nodes": [{"label": "Person", "key": "id"}], + "relationships": [{"from": {"label": "Person", "key": "id"}, "to": {"label": "Repo", "key": "$slug"}}], + "maintenanceMegabytes": 1024}} +``` + +It is answered with the statements `bulk.yaml` pins, by their names. The statements every import runs come once. +`takenKeys` and `makeNodes` come in `nodes`, one entry for each of the request's. `ambiguousKeys`, `findEnds`, +`missingEnds`, `makeEdges` and `relink` come in `relationships`, likewise. The library runs them in `bulk.yaml`'s order and +does the `COPY` itself. `nodes` and `relationships` may be left out, and `maintenanceMegabytes` is 1024 where it is left out. A +store other than Postgres, a key the request doesn't know, or an empty label or property name, which no statement reads, is +answered with an error. `bulk.yaml`'s `json` pins the +request and what is refused. + ## The schema plan A schema manager decides, from what a store says, what to make, drop, recreate and record, and what to report: drivine4j's `SchemaManager`, `IndexManager` and `ConstraintManager`. The compiler's module (`org.drivine.cypher.store.plan`) decides it once for every library, as a planner with no store: `SchemaPlanner(store, markers)`, whose `start(request, now)` and `resume(state, outcomes, now)` each give a round of actions for the library to run, or the plan done and its report. `plans.yaml` pins its decisions: drivine4j's, the reference, captured from its managers over a scripted store, with no store, for the design's cases 1 to 29 (drivine-cypher-spec#135), and the module's own for case 30, the form check, and case 31, the safe recreate of a uniqueness constraint (whose Oracle, Postgres and SQLite listings and errors are captured from each store), and for the public `IndexManager` and `ConstraintManager` calls (`item`). The module's tests check the planner gives them, round by round, on every target. @@ -556,7 +577,7 @@ An action that throws is answered `Failed`, its `causes` as [a cause](#the-store **What drivine4j decides, which the plan keeps.** A version that changed recreates the owner's items, but not one another owner declares; a first version is adopted. An ensure records the version the marker has, and `recreateOnStartup` doesn't apply to it. A runtime item a later manager hasn't been given is kept in the inventory; a recreate marks it stale, and the next ensure of it recreates it. A key recorded and no longer declared is an orphan, reported and left; a marker with no inventory has none. Only a versioned owner keeps its marker single. Under `FAIL_FAST`, a drift, a violation, an index in the way or an index not built fails the owner once every item has been tried, and its marker isn't written; under `WARN`, each is logged and the marker written. A failure drivine4j's manager catches (the marker's, the lowercased copies' fill and record, a TEXT index off Neo4j, a wait that ran out) ends the owner there, its marker unwritten, and the other owners go on; one it doesn't (DDL the store refused for a reason the planner doesn't read, a read of the copies' record) ends the plan, and the library throws the store's error. A remove keeps an item another owner declares or records, its properties in any order. -**An index built online.** An ensure builds an index so that writes to the graph go on while it is built. On Postgres a range, vector or full-text index is made `CREATE INDEX CONCURRENTLY IF NOT EXISTS`, a statement of its own, and a uniqueness constraint's index `CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS`; on Oracle a range index's two indexes are built `ONLINE`, and a uniqueness constraint's, its range index and its unique index, blocking writes, as before: Oracle's online build of its unique index over keys the data repeats is not refused as a blocking one is, and online builds of its range index beside the blocking build of its unique index deadlocked in the library cache (`ORA-04020`). Sessions issuing the same `CREATE INDEX` at once on one Oracle table deadlock in the library cache even so, each held up about nine seconds or refused (`ORA-04020`), so an ensure on Oracle builds in its turn too, as below (drivine4j#777). The index a recreate builds beside the old one is built as before, blocking writes, on both: another session never sees it part built, or left invalid, and so never takes it for the constraint, nor drops it, mid-recreate. Where the store can't build online, the build is run again as one that blocks writes, and so is each after it in the plan, said once at `WARN` (`{engine} can't build an index online here (), so it is built by a statement that blocks writes to the graph's nodes until it is done.`, then what lets it build online). Postgres refuses `CONCURRENTLY` in a transaction (`25001`): `Ensure the schema outside a transaction to build it with writes going on.` Oracle refuses `ONLINE` on an edition without it (`ORA-00439`): `An edition with online index builds (Enterprise Edition) builds it with writes going on.` A library that ensures the schema on a connection a transaction holds takes a savepoint before a Postgres index build, so the transaction outlives the refusal. A Postgres build cut short or failed leaves its index invalid under its name, which `IF NOT EXISTS` would pass over: the listing gives its record the `status` `INVALID`, and the index too among the objects under Drivine's names, as one cut short before its record was written has none. One invalid while a build of it is under way (`pg_stat_progress_create_index`; a build another role runs in the database, whose index Postgres hides, is taken as maybe its) is listed `BUILDING` instead, recorded or not, as its record is written once it is done: an ensure waits for that build, for as long as it waits for an index of its own to build (a uniqueness constraint's for 30 seconds), and reads how it ended, rather than take it as made or drop it, and one still building past that is said so (an index's, as one still being built is) or thrown (a constraint's). An index of another shape under the name is drift at once, being built or not, and isn't waited for; nor is the index a recreate builds first taken as holding the constraint while it is `BUILDING`. An ensure, or a recreate, drops an index left `INVALID` and builds it again; the drop waits until no online build of any index is under way on the node table (`pg_stat_progress_create_index`, as below), and is a block that, where the index is still `INVALID` (no build of it under way: one another session began since is left, and an ensure of a uniqueness constraint waits for it in the listing, as above), holds the index's table (`LOCK TABLE … IN ACCESS EXCLUSIVE MODE`) and drops it (`DROP INDEX`, the one name) only if it is still invalid then, so a valid index another session built meanwhile is left: reads and writes of the graph's nodes wait while an invalid index is dropped, and only then. The block never waits on the table for a build, which would hold up every read and write of it until that build ended, and whose last phase, waiting for the block's snapshot, would deadlock with it. It is said at `INFO` (`Completed Postgres on