Mercurial > urweb
comparison caching-tests/test.ur @ 2304:6fb9232ade99
Merge Sqlcache
author | Adam Chlipala <adam@chlipala.net> |
---|---|
date | Sun, 20 Dec 2015 14:18:52 -0500 |
parents | e6c5bb62fef8 |
children |
comparison
equal
deleted
inserted
replaced
2201:1091227f535a | 2304:6fb9232ade99 |
---|---|
1 table tab : {Id : int, Val : int, Foo : int} PRIMARY KEY Id | |
2 | |
3 fun cache id = | |
4 res <- oneOrNoRows (SELECT A.Val FROM (tab AS A JOIN tab AS B ON A.Id = B.Id) | |
5 WHERE B.Id = {[id]}); | |
6 return <xml><body> | |
7 cache | |
8 {case res of | |
9 None => <xml>?</xml> | |
10 | Some row => <xml>{[row.A.Val]}</xml>} | |
11 </body></xml> | |
12 | |
13 (* fun cacheAlt id = *) | |
14 (* res <- oneOrNoRows (SELECT Q.Id *) | |
15 (* FROM (SELECT Tab.Id AS Id FROM tab WHERE Tab.Id = {[id]}) *) | |
16 (* AS Q); *) | |
17 (* return <xml><body> *) | |
18 (* cacheAlt *) | |
19 (* {case res of *) | |
20 (* None => <xml>?</xml> *) | |
21 (* | Some row => <xml>{[row.Q.Id]}</xml>} *) | |
22 (* </body></xml> *) | |
23 | |
24 (* fun sillyRecursive {Id = id : int, FooBar = fooBar} = *) | |
25 (* if fooBar <= 0 *) | |
26 (* then 0 *) | |
27 (* else 1 + sillyRecursive {Id = id, FooBar = fooBar - 1} *) | |
28 | |
29 (* fun cacheR (r : {Id : int, FooBar : int}) = *) | |
30 (* res <- oneOrNoRows (SELECT tab.Val *) | |
31 (* FROM tab *) | |
32 (* WHERE tab.Id = {[r.Id]}); *) | |
33 (* return <xml><body> *) | |
34 (* cacheR {[r.FooBar]} *) | |
35 (* {case res of *) | |
36 (* None => <xml>?</xml> *) | |
37 (* | Some row => <xml>{[row.Tab.Val]}</xml>} *) | |
38 (* </body></xml> *) | |
39 | |
40 (* fun cache2 id v = *) | |
41 (* res <- oneOrNoRows (SELECT tab.Val *) | |
42 (* FROM tab *) | |
43 (* WHERE tab.Id = {[id]} AND tab.Val = {[v]}); *) | |
44 (* return <xml><body> *) | |
45 (* Reading {[id]}. *) | |
46 (* {case res of *) | |
47 (* None => <xml>Nope, that's not it.</xml> *) | |
48 (* | Some _ => <xml>Hooray! You guessed it!</xml>} *) | |
49 (* </body></xml> *) | |
50 | |
51 (* fun cache2 id1 id2 = *) | |
52 (* res1 <- oneOrNoRows (SELECT tab.Val *) | |
53 (* FROM tab *) | |
54 (* WHERE tab.Id = {[id1]}); *) | |
55 (* res2 <- oneOrNoRows (SELECT tab.Val *) | |
56 (* FROM tab *) | |
57 (* WHERE tab.Id = {[id2]}); *) | |
58 (* return <xml><body> *) | |
59 (* Reading {[id1]} and {[id2]}. *) | |
60 (* {case (res1, res2) of *) | |
61 (* (Some _, Some _) => <xml>Both are there.</xml> *) | |
62 (* | _ => <xml>One of them is missing.</xml>} *) | |
63 (* </body></xml> *) | |
64 | |
65 fun flush id = | |
66 dml (UPDATE tab | |
67 SET Val = Val * (Id + 2) / Val - 3 | |
68 WHERE Id = {[id]} OR Id = {[id - 1]} OR Id = {[id + 1]}); | |
69 return <xml><body> | |
70 Changed {[id]}! | |
71 </body></xml> | |
72 | |
73 (* fun flash id = *) | |
74 (* dml (UPDATE tab *) | |
75 (* SET Foo = Val *) | |
76 (* WHERE Id = {[id]} OR Id = {[id - 1]} OR Id = {[id + 1]}); *) | |
77 (* return <xml><body> *) | |
78 (* Maybe changed {[id]}? *) | |
79 (* </body></xml> *) | |
80 | |
81 (* fun floosh id = *) | |
82 (* dml (UPDATE tab *) | |
83 (* SET Id = {[id + 1]} *) | |
84 (* WHERE Id = {[id]} OR Id = {[id - 1]} OR Id = {[id + 1]}); *) | |
85 (* return <xml><body> *) | |
86 (* Shifted {[id]}! *) | |
87 (* </body></xml> *) | |
88 | |
89 (* val flush17 = *) | |
90 (* dml (UPDATE tab *) | |
91 (* SET Val = Val * (Id + 2) / Val - 3 *) | |
92 (* WHERE Id = 17); *) | |
93 (* return <xml><body> *) | |
94 (* Changed specifically 17! *) | |
95 (* </body></xml> *) | |
96 | |
97 (* fun flush id = *) | |
98 (* res <- oneOrNoRows (SELECT tab.Val *) | |
99 (* FROM tab *) | |
100 (* WHERE tab.Id = {[id]}); *) | |
101 (* (case res of *) | |
102 (* None => dml (INSERT INTO tab (Id, Val) *) | |
103 (* VALUES ({[id]}, 0)) *) | |
104 (* | Some row => dml (UPDATE tab *) | |
105 (* SET Val = {[row.Tab.Val + 1]} *) | |
106 (* WHERE Id = {[id]} OR Id = {[id + 1]})); *) | |
107 (* return <xml><body> *) | |
108 (* {case res of *) | |
109 (* None => <xml>Initialized {[id]}!</xml> *) | |
110 (* | Some row => <xml>Incremented {[id]}!</xml>} *) | |
111 (* </body></xml> *) |