1 import Base 2 import ./change_tick.bend as Tick 3 import ./world.bend as W 4 import ./compose.bend as Q 5 import ./capabilities.bend as Cap 6 import ./ordinary_query_declaration.bend as Decl 7 import ./ordinary_schema.bend as Schema 8 import ./owner_product.bend as Product 9 import ./resource_field.bend as Field 10 import ./transaction.bend as T 11 # Only the user resource is writable; the affine event log is a retained sibling. 12 def lens(~R:Type,~Logs:Type) -> Product.Lens,R,Logs>: 13 Product.lens(~R,~Logs,~Product.Left{}) 14 def read_returned(~S:Data,~C:Type,~R:Type,~Logs:Type,~E:Data,~V:Data,handle:W.Handle,error:Maybe<&2,W.WorldError>,cursor:Maybe<&2,Tick.Tick>,result:T.Tx,E>,E> & V) -> Q.Frame,E> & V: 15 match result: 16 case (tx,value):(Q.Frame{tx,handle,error,cursor},value) 17 # Advanced opaque-row read of the user resource R in Product.Owner. 18 # The selected field projection returns its owner and detached V; log siblings 19 # remain retained. Raw schema lens/view callbacks are trusted setup. 20 def read(~S:Data,~C:Type,~R:Type,~Logs:Type,~E:Data,~V:Data,~view:R -> R & V,owner:Q.Frame,E>) -> Q.Frame,E> & V: 21 match owner: 22 case Q.Frame{tx,handle,error,cursor}:read_returned(~S,~C,~R,~Logs,~E,~V,handle,error,cursor,Cap.value_read(~T.Tx,E>,E>,~V,~Field.read(~S,~C,~Product.Owner,~E,~R,~Logs,~V,~lens(~R,~Logs),~view),tx)) 23 # Advanced transactional replacement of only user resource R, retaining 24 # affine Logs as siblings. Consumes replacement and returns the row owner; 25 # journal rollback restores the displaced resource, without exposing log writes. 26 def set(~S:Data,~C:Type,~R:Type,~Logs:Type,~E:Data,owner:Q.Frame,E>,replacement:R) -> Q.Frame,E>: 27 match owner: 28 case Q.Frame{tx,handle,error,cursor}:Q.Frame{Field.tx_set(~S,~C,~Product.Owner,~E,~R,~Logs,~lens(~R,~Logs),tx,replacement),handle,error,cursor} 29 def selected(~S:Data,~C:Type,~R:Type,~Logs:Type,~E:Data,owner:Q.Frame,E>) -> Q.Frame,E> & Bool: 30 (owner,True{}) 31 # Declare user-resource read/replacement using its ordinary schema identity. 32 # Selection is unconditional and access metadata derives from that identity. 33 # The typed field grant retains affine log owners; constructing this declaration 34 # neither reads resources nor publishes events. 35 def write(~S:Data,~C:Type,~R:Type,~Logs:Type,~E:Data,~V:Data,~identity:Schema.Resource,~view:R -> R & V) -> Decl.Declaration,E>,Cap.ValueWrite,E>,R,V>>: 36 Decl.Declaration{Cap.ValueWrite{read(~S,~C,~R,~Logs,~E,~V,~view),set(~S,~C,~R,~Logs,~E)},selected(~S,~C,~R,~Logs,~E),[Decl.Clause{Schema.resource_key(~S,~R,identity),Decl.Write{}}]}