ziv@2213: table tab : {Id : int, Val : int} PRIMARY KEY Id
ziv@2203:
ziv@2213: fun cache id =
ziv@2225: res <- oneOrNoRows (SELECT tab.Val
ziv@2225: FROM tab
ziv@2225: WHERE tab.Id = {[id]});
ziv@2204: return
ziv@2213: Reading {[id]}.
ziv@2204: {case res of
ziv@2213: None => ?
ziv@2213: | Some row => {[row.Tab.Val]}}
ziv@2204:
ziv@2203:
ziv@2273: (* fun cache2 id v = *)
ziv@2273: (* res <- oneOrNoRows (SELECT tab.Val *)
ziv@2273: (* FROM tab *)
ziv@2273: (* WHERE tab.Id = {[id]} AND tab.Val = {[v]}); *)
ziv@2273: (* return *)
ziv@2273: (* Reading {[id]}. *)
ziv@2273: (* {case res of *)
ziv@2273: (* None => Nope, that's not it. *)
ziv@2273: (* | Some _ => Hooray! You guessed it!} *)
ziv@2273: (* *)
ziv@2273:
ziv@2273: fun cache2 id1 id2 =
ziv@2273: res1 <- oneOrNoRows (SELECT tab.Val
ziv@2273: FROM tab
ziv@2273: WHERE tab.Id = {[id1]});
ziv@2273: res2 <- oneOrNoRows (SELECT tab.Val
ziv@2273: FROM tab
ziv@2273: WHERE tab.Id = {[id2]});
ziv@2269: return
ziv@2273: Reading {[id1]} and {[id2]}.
ziv@2273: {case (res1, res2) of
ziv@2273: (Some _, Some _) => Both are there.
ziv@2273: | _ => One of them is missing.}
ziv@2269:
ziv@2269:
ziv@2213: fun flush id =
ziv@2239: dml (UPDATE tab
ziv@2239: SET Val = Val * (Id + 2) / Val - 3
ziv@2239: WHERE Id = {[id]} OR Id = {[id - 1]} OR Id = {[id + 1]});
ziv@2204: return
ziv@2235: Changed {[id]}!
ziv@2204:
ziv@2234:
ziv@2242: val flush17 =
ziv@2242: dml (UPDATE tab
ziv@2242: SET Val = Val * (Id + 2) / Val - 3
ziv@2242: WHERE Id = 17);
ziv@2242: return
ziv@2242: Changed specifically 17!
ziv@2242:
ziv@2242:
ziv@2235: (* fun flush id = *)
ziv@2235: (* res <- oneOrNoRows (SELECT tab.Val *)
ziv@2235: (* FROM tab *)
ziv@2235: (* WHERE tab.Id = {[id]}); *)
ziv@2235: (* (case res of *)
ziv@2235: (* None => dml (INSERT INTO tab (Id, Val) *)
ziv@2235: (* VALUES ({[id]}, 0)) *)
ziv@2235: (* | Some row => dml (UPDATE tab *)
ziv@2235: (* SET Val = {[row.Tab.Val + 1]} *)
ziv@2235: (* WHERE Id = {[id]} OR Id = {[id + 1]})); *)
ziv@2235: (* return *)
ziv@2235: (* {case res of *)
ziv@2235: (* None => Initialized {[id]}! *)
ziv@2235: (* | Some row => Incremented {[id]}!} *)
ziv@2235: (* *)