Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .changeset/35952-oracle-raw-share-grant-key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'@objectstack/driver-sql': patch
---

feat(driver-sql): on Oracle the share-grant subquery converts `record_id` the way the id list binds a `RAW(16)` uuid key (ADR-0155 O2)

Clause-②: yes
Metadata-protocol: none

No visible change on its own: nothing writes a `SharedRecordIds` leaf yet. The sharing plugin starts writing it at ADR-0155's last step, and this makes that step answer the right rows on Oracle tables keyed by a `RAW(16)` uuid (`SYS_GUID()` keys).

- **Oracle, a `RAW(16)` uuid key.** The compiled subquery `id IN (SELECT record_id FROM sys_record_share WHERE …)` now projects the id list's own conversion when the compared column is a `RAW(16)` uuid column, the same catalog reading that makes the list bind its members as `HEXTORAW(?)`: `HEXTORAW(CASE WHEN REGEXP_LIKE(record_id, UUID_PATTERN, 'i') THEN REPLACE(record_id, '-', '') ELSE 'not-a-uuid' END)`. A grant in upper, lower or mixed case, dashed (8-4-4-4-12) or as 32 undashed digits, names the same row it names in the list. `UUID_PATTERN` is the list's own uuid-text check, written for Oracle from the same constant. Before, Oracle compared the column as its upper-case hex text: the subquery matched no row and its negation every row, with no error.
- **A grant the id list refuses fails the statement.** The list refuses a value that is not uuid text before any statement (`INVALID_FILTER` / 400). Every such stored grant fails the subquery's statement instead, with `DATABASE_ERROR` / 500 (ORA-01465), on the read and on its negation: hex of another length (31 or 33 digits, `0123`), 32 digits with dashes out of place, a grant of dashes only, a blank or a padded uuid, a braced uuid, or text that is not hex. None of them answers a row, and none empties a negation.
- **Unchanged.** Any other Oracle key compiles exactly as before; a `NUMBER` key converts each grant as it converts the list's text binds (measured: the same rows, and the same ORA-01722 for a grant that is not a number). Every other dialect compiles exactly as before.
5 changes: 3 additions & 2 deletions packages/drivers/driver-sql/src/fixed-read-leaf-key-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@
* old type until restart.
*
* Every other dialect is untouched: SQLite compares by column affinity (the
* same rows, measured on step 3), and MySQL, SQL Server and Oracle are named
* as left in the O2 card's report.
* same rows, measured on step 3), MySQL and SQL Server are named as left in
* the O2 card's report, and Oracle converts a `RAW(16)` uuid key's grants
* itself (`oracleUuidRawProjection`, `sql-driver-oracle-dialect.ts`).
*/

import type { Knex } from 'knex';
Expand Down
6 changes: 4 additions & 2 deletions packages/drivers/driver-sql/src/sql-driver-filter-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -860,10 +860,12 @@ export class FilterHelpersFacet {
// `field IN (SELECT …)` (`SqlDriver#fixedReadLeafSubquery`), under the
// outer statement's own wall and nothing else. `applyFilters` refused
// it at every other position. The object and column it is compared
// with type the inner read on PostgreSQL (ADR-0155 O2).
// with type the inner read on PostgreSQL (ADR-0155 O2); on Oracle a
// RAW(16) uuid column, read as the list's `HEXTORAW` binding reads it.
if (rawOp === '$in' && isFixedReadLeaf(opValue)) {
const mIn = logicalOp === 'or' ? 'orWhereIn' : 'whereIn';
(builder as any)[mIn](field, this.fixedReadLeafSubquery(opValue, { object: table, column: field, evaluator: this.readableIdsDriverName, transaction: options?.transaction }));
const oracleRawUuid = this.isOracle && this.oracleRawUuidFieldsByObject.get(table ?? '')?.has(localField) === true;
(builder as any)[mIn](field, this.fixedReadLeafSubquery(opValue, { object: table, column: field, evaluator: this.readableIdsDriverName, oracleRawUuid, transaction: options?.transaction }));
continue;
}
// #5222 — a `{ $field }` that is the WHOLE comparand of a scalar
Expand Down

Large diffs are not rendered by default.

32 changes: 31 additions & 1 deletion packages/drivers/driver-sql/src/sql-driver-internal-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ import type { Knex } from 'knex';
import type { SqlDriver } from './sql-driver.js';
import { fixedInnerReadPredicates } from './fixed-read-leaf-compile.js';
import { castFixedReadLeafColumn, fixedReadLeafKeyReading, refuseFixedReadLeafKey } from './fixed-read-leaf-key-type.js';
import { oracleUuidRawProjection } from './sql-driver-oracle-dialect.js';

/** What a fixed-read leaf's `$in` arm says of the outer column the subquery is compared with. */
export interface FixedReadLeafOuter {
/** The outer object (the filter compiler's coercion key), `null` when unknown. */
readonly object: string | null;
/** The outer column, as the statement spells it. */
readonly column: string;
/** The driver named as the evaluator in a PostgreSQL no-reading refusal (`refuseFixedReadLeafKey`). */
readonly evaluator?: string;
/** PostgreSQL: the caller's transaction, on whose connection the key type is read (`fixedReadLeafKeyReading`). */
readonly transaction?: unknown;
/**
* Oracle: the outer column is a `RAW(16)` uuid column, the same reading
* (`oracleRawUuidFieldsByObject`, same object and field) that makes the
* list bind its members as `HEXTORAW(?)`.
*/
readonly oracleRawUuid?: boolean;
}

/**
* The `SqlDriver` members the bodies of {@link InternalHelpersFacet} read — every one of
Expand Down Expand Up @@ -185,8 +204,17 @@ export class InternalHelpersFacet {
* connection, `outer.transaction`) is refused, ADR-0139 D3's
* envelope (`INTERNAL_ERROR` / 500, logged once), ⛔ never compiled
* uncast: a `character(n)` key would miss a padded grant silently (D2).
* - **Oracle, a `RAW(16)` uuid key (O2, ruling A carried to Oracle).** Where
* `outer.oracleRawUuid` says the list binds its members as `HEXTORAW(?)`,
* the projection is the same conversion over the column, behind the
* list's own gate (`oracleUuidRawProjection`): a grant `UUID_TEXT` admits
* is the 16 bytes the list binds; any other grant fails the statement
* (ORA-01465), as the list refuses it before SQL.
* Any other Oracle key projects `record_id` as before (a `NUMBER` key
* converts it the way it converts the list's text binds). Oracle has no
* no-reading refusal: an unprimed key compiles as the list's text binds do.
*/
static fixedReadLeafSubquery(this: Pick<InternalHelpersHost, 'getBuilder' | 'isOracle' | 'knex' | 'logger' | 'physicalSchemaByObject' | 'physicalTableByObject'>, leaf: FixedReadLeaf, outer?: { readonly object: string | null; readonly column: string; readonly evaluator?: string; readonly transaction?: unknown }): Knex.QueryBuilder {
static fixedReadLeafSubquery(this: Pick<InternalHelpersHost, 'getBuilder' | 'isOracle' | 'knex' | 'logger' | 'physicalSchemaByObject' | 'physicalTableByObject'>, leaf: FixedReadLeaf, outer?: FixedReadLeafOuter): Knex.QueryBuilder {
const read = fixedInnerReadPredicates(leaf, { emptyStringIsNull: this.isOracle });
let projection: string | Knex.Raw = read.column;
// [ADR-0155 O2] PostgreSQL only: the physical table as `getBuilder`
Expand All @@ -203,6 +231,8 @@ export class InternalHelpersFacet {
}
if (reading.kind === 'cast') projection = castFixedReadLeafColumn(this.knex, read.column, reading.cast);
}
// [ADR-0155 O2, ruling A carried to Oracle] A RAW(16) uuid key: the list's `HEXTORAW` conversion over the column.
if (this.isOracle && outer?.oracleRawUuid === true) projection = oracleUuidRawProjection(this.knex, read.column);
const inner = this.getBuilder(read.table).select(projection);
for (const [column, value] of read.equals) inner.where(column, value);
for (const [column, values] of read.memberOf) inner.whereIn(column, [...values]);
Expand Down
63 changes: 63 additions & 0 deletions packages/drivers/driver-sql/src/sql-driver-oracle-dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,69 @@ export function oracleUuidRawBinding(
throw err;
}

/**
* {@link UUID_TEXT} as an Oracle regular expression, for `REGEXP_LIKE` with
* the match parameter {@link ORACLE_UUID_TEXT_MATCH}: derived from the one
* constant, never restated, so the subquery's gate and the list's gate cannot
* drift. Oracle's POSIX syntax has no `(?:…)`, so each non-capturing group is
* written as a plain one (the same language). A pattern this derivation cannot
* carry (a flag other than `i`, a `?` knex would read as a binding, a quote
* that would end the SQL literal, a construct still Perl-only) throws at load.
*/
export const ORACLE_UUID_TEXT_PATTERN = oracleRegexFrom(UUID_TEXT, 'i');

/** `REGEXP_LIKE`'s match parameter for {@link ORACLE_UUID_TEXT_PATTERN}: {@link UUID_TEXT}'s `i` flag. */
export const ORACLE_UUID_TEXT_MATCH = 'i';

function oracleRegexFrom(re: RegExp, flags: string): string {
const pattern = re.source.replace(/\(\?:/g, '(');
if (re.flags !== flags || /[?'\\]/.test(pattern)) {
throw new Error(`[driver-sql] UUID_TEXT (/${re.source}/${re.flags}) has no Oracle REGEXP_LIKE spelling here: extend oracleRegexFrom.`);
}
return pattern;
}

/**
* [ADR-0155 O2, ruling A carried to Oracle] {@link oracleUuidRawBinding}'s
* conversion written over a text COLUMN instead of a bound value: the
* projection of a fixed-read leaf's inner read (`record_id`, `VARCHAR2`) when
* the outer column it is compared with is a `RAW(16)` uuid column, as the
* catalog reports it (`SqlDriver#primeOracleRawUuidColumns`, the prime the
* list's binding reads).
*
* ```sql
* "ID" IN (SELECT HEXTORAW(CASE WHEN REGEXP_LIKE("record_id", '<UUID_TEXT>', 'i')
* THEN REPLACE("record_id", '-', '')
* ELSE 'not-a-uuid' END)
* FROM "sys_record_share" WHERE …)
* ```
*
* - **Uuid text converts as the list binds it.** A grant {@link UUID_TEXT}
* admits (8-4-4-4-12 or 32 undashed hex digits, either case) is the same 16
* bytes `HEXTORAW(?)` binds for it in the list, so the leaf answers the
* list's rows.
* - **Anything else fails the statement, loudly.** Every grant the list
* refuses before SQL (`INVALID_FILTER` / 400) projects `'not-a-uuid'`, which
* `HEXTORAW` refuses: ORA-01465, `DATABASE_ERROR` / 500, the statement's
* failure in place of the list's (ADR-0155 Consequences; option A: "a
* malformed grant id fails the statement"). ⛔ Never a bare
* `HEXTORAW(REPLACE(…))`: measured on Oracle Free 23ai, it converts 32
* digits with misplaced dashes to a full key (a row the list refuses), left
* pads 31 digits to 16 bytes (a row the list refuses), and turns a dash-only
* grant into NULL, under which the negation answers no row.
*
* Without any projection Oracle compares the `RAW` column as its upper-case
* hex text, which no dashed or lower-case grant equals: the leaf answers no row
* and its negation every row, with no error.
*/
export function oracleUuidRawProjection(knexInstance: Knex, column: string): Knex.Raw {
return knexInstance.raw(
`HEXTORAW(CASE WHEN REGEXP_LIKE(??, '${ORACLE_UUID_TEXT_PATTERN}', '${ORACLE_UUID_TEXT_MATCH}') ` +
`THEN REPLACE(??, '-', '') ELSE 'not-a-uuid' END)`,
[column, column],
);
}

/** Whether `value` is uuid text a `RAW(16)` column can be bound to — see {@link UUID_TEXT}. */
export function isOracleUuidText(value: unknown): value is string {
return typeof value === 'string' && UUID_TEXT.test(value);
Expand Down
8 changes: 4 additions & 4 deletions packages/drivers/driver-sql/src/sql-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ import { ManagedDriftFacet } from './sql-driver-managed-drift.js';
// [#29520 codemod] Schema Introspection: the methods of the same names below hand the bodies what they read (`SchemaIntrospectionHost`).
import { SchemaIntrospectionFacet } from './sql-driver-schema-introspection.js';
// [#29520 codemod] Internal helpers: the methods of the same names below hand the bodies what they read (`InternalHelpersHost`).
import { InternalHelpersFacet } from './sql-driver-internal-helpers.js';
import { InternalHelpersFacet, type FixedReadLeafOuter } from './sql-driver-internal-helpers.js';
// [#29520 codemod] Filter helpers: the methods of the same names below hand the bodies what they read (`FilterHelpersHost`).
import { FilterHelpersFacet } from './sql-driver-filter-helpers.js';
// [#29520 codemod] Field mapping: the methods of the same names below hand the bodies what they read (`FieldMappingHost`).
Expand Down Expand Up @@ -414,8 +414,8 @@ export class SqlDriver implements IDataDriver {
// (Turso's remote transport compiles the leaf with its own compiler).
readableIdsLeaf: true,
// [ADR-0155 D4] One subquery per `SharedRecordIds` leaf (`applyFilterCondition`)
// on every dialect; same rows as the id list measured on SQLite and PostgreSQL
// only (O2: `fixed-read-leaf-key-type.ts`). Turso's remote transport has its own.
// on every dialect; same rows as the id list measured on SQLite, PostgreSQL, Oracle
// (RAW(16), NUMBER) (O2: `fixed-read-leaf-key-type.ts`, `oracleUuidRawProjection`). Turso: its own.
fixedReadLeaves: ['SharedRecordIds' as const],
};
}
Expand Down Expand Up @@ -6525,7 +6525,7 @@ export class SqlDriver implements IDataDriver {
return InternalHelpersFacet.readableIdsSubquery.call(internalHelpersHost.of(this), leaf, options);
}

private fixedReadLeafSubquery(leaf: FixedReadLeaf, outer?: { readonly object: string | null; readonly column: string; readonly evaluator?: string; readonly transaction?: unknown }): Knex.QueryBuilder {
private fixedReadLeafSubquery(leaf: FixedReadLeaf, outer?: FixedReadLeafOuter): Knex.QueryBuilder {
// [ADR-0155 D2, O2] The body lives in `sql-driver-internal-helpers.ts`, its docblock with it.
return InternalHelpersFacet.fixedReadLeafSubquery.call(internalHelpersHost.of(this), leaf, outer);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"file": "packages/drivers/driver-sql/src/sql-driver-oracle-dialect.ts",
"adrs": [
"ADR-0155"
],
"invariant": "ADR-0155 D-0, D2, O2 (ruling A carried to Oracle): where the outer column is a RAW(16) uuid column by the catalog prime the list's HEXTORAW(?) binding reads (`oracleRawUuidFieldsByObject`, same object and field), the fixed-read leaf's inner read projects `oracleUuidRawProjection`: HEXTORAW(CASE WHEN REGEXP_LIKE(record_id, ORACLE_UUID_TEXT_PATTERN, 'i') THEN REPLACE(record_id, '-', '') ELSE 'not-a-uuid' END), the pattern derived from the list's own UUID_TEXT gate. A grant UUID_TEXT admits is the same 16 bytes the list binds, so the leaf answers the list's rows; every grant the list refuses before SQL (INVALID_FILTER / 400) fails the statement (ORA-01465, DATABASE_ERROR / 500) on the read and its negation. A bare HEXTORAW(REPLACE(...)) is not the gate: it matches misplaced-dash and left-padded 31-digit grants and turns a dash-only grant into NULL, emptying the negation. Without any projection Oracle compares the RAW column as its upper-case hex text and the leaf silently answers no row (its negation every row). Any other Oracle key keeps step 3's statement."
}
Loading