import type { Contract } from '@internal/contract/types'; import type { SqlStorage } from '@internal/sql-contract/types'; import type { AnyExpression, SelectAst, TableSource } from '@internal/sql-relational-core/ast'; import { resolveUniqueKeyColumnSets } from '../collection-contract'; import { storageOf } from './changed-by'; import type { ColumnMapping, Edge, NodeId } from './edge'; import { FilterData } from './filter-data'; import { Find } from './find'; import type { Graph } from './graph'; import type { InputEdges, Slots } from './node'; import { StateEdge } from './state'; export function stateEdgeOf( edges: readonly Edge[], table: TableSource, ): StateEdge | undefined { return edges.find( (edge): edge is StateEdge => edge instanceof StateEdge && edge.table.tableName === table.name && edge.table.namespaceId === table.namespaceId, ); } export interface FindAt { readonly id: NodeId; readonly find: Find; } export function keyEdgeFromFind( graph: Graph, contract: Contract, table: TableSource, inputs: InputEdges, ): FindAt | undefined { const [edge, ...others] = [inputs['filter'] ?? []].flat(); if (!(edge instanceof FilterData) || others.length > 0) { return undefined; } const find = graph.nodeAt(edge.from); return find instanceof Find && readsByKey(contract, table, edge.columns) ? { id: edge.from, find } : undefined; } export function readsTableAsFound( graph: Graph, inputs: InputEdges, { id, find }: FindAt, table: TableSource, ): boolean { const read = oneTableReadOf(find.ast); const own = stateEdgeOf([inputs['state'] ?? []].flat(), table); const found = stateEdgeOf([graph.inputsOf(id)['state'] ?? []].flat(), table); return ( read !== undefined && sameTable(read, table) && own !== undefined && own.from === found?.from ); } /** A builder marks a write's condition as its `Find`'s by giving both the same object; equal but separate conditions are not merged. */ export function foldsIntoCondition( graph: Graph, { id, find }: FindAt, where: AnyExpression | undefined, ): boolean { const { filter, link, requires } = graph.inputsOf(id); return ( find.ast.where === where && rowsOfConditionReadOf(find.ast) !== undefined && [filter ?? [], link ?? [], requires ?? []].flat().length === 0 ); } function oneTableReadOf(ast: SelectAst): TableSource | undefined { const { from } = ast; return from?.kind === 'table-source' && ast.joins === undefined && ast.projection.every((item) => item.expr.kind === 'column-ref') ? from : undefined; } export function conditionOnlyReadOf(ast: SelectAst): TableSource | undefined { return ast.orderBy === undefined ? rowsOfConditionReadOf(ast) : undefined; } function rowsOfConditionReadOf(ast: SelectAst): TableSource | undefined { const from = oneTableReadOf(ast); const clauses = [ ast.distinct, ast.distinctOn, ast.groupBy, ast.having, ast.limit, ast.offset, ast.locking, ]; return clauses.every((clause) => clause === undefined) ? from : undefined; } export function sameTable(a: TableSource, b: TableSource): boolean { return a.name === b.name && a.namespaceId === b.namespaceId; } export function readsByKey( contract: Contract, table: TableSource, columns: readonly ColumnMapping[], ): boolean { const read = new Set( columns.flatMap(([source, target]) => source.alias === target.alias && source.expr.kind === 'column-ref' && target.expr.kind === 'column-ref' && source.expr.column === target.expr.column ? [target.expr.column] : [], ), ); if (read.size !== columns.length) { return false; } const { namespaceId, tableName } = storageOf(table); return resolveUniqueKeyColumnSets(contract, namespaceId, tableName).some((key) => key.every((column) => read.has(column)), ); }