Commit | Line | Data |
---|---|---|
5a2812ca AC |
1 | structure Support :> SUPPORT = |
2 | struct | |
3 | ||
4 | open Util Sql Init | |
5 | ||
6 | datatype status = | |
7 | NEW | |
8 | | PENDING | |
9 | | CLOSED | |
10 | ||
11 | type category = { id : int, grp : int, name : string, descr : string } | |
12 | type issue = { id : int, usr : int, cat : int, title : string, priv : bool, status : status, stamp : C.timestamp } | |
13 | type post = { id : int, usr : int, iss : int, body : string, stamp : C.timestamp } | |
14 | type subscription = { usr : int, cat : int } | |
15 | ||
16 | ||
17 | (* Categories *) | |
18 | ||
19 | fun mkCatRow [id, grp, name, descr] = | |
20 | {id = C.intFromSql id, grp = C.intFromSql grp, name = C.stringFromSql name, | |
21 | descr = C.stringFromSql descr} | |
22 | | mkCatRow row = rowError ("category", row) | |
23 | ||
24 | fun lookupCategory id = | |
25 | mkCatRow (C.oneRow (getDb ()) ($`SELECT id, grp, name, descr | |
26 | FROM SupCategory | |
27 | WHERE id = ^(C.intToSql id)`)) | |
28 | ||
29 | fun listCategories () = | |
30 | C.map (getDb ()) mkCatRow ($`SELECT id, grp, name, descr | |
31 | FROM SupCategory | |
32 | ORDER BY name`) | |
33 | ||
34 | fun mkCatRow' (sub :: rest) = | |
35 | (not (C.isNull sub), mkCatRow rest) | |
36 | | mkCatRow' row = Init.rowError ("category'", row) | |
37 | ||
38 | fun listCategoriesWithSubscriptions usr = | |
39 | C.map (getDb ()) mkCatRow' ($`SELECT cat, id, grp, name, descr | |
40 | FROM SupCategory LEFT OUTER JOIN SupSubscription | |
41 | ON (usr = ^(C.intToSql usr) AND cat = id) | |
42 | ORDER BY name`) | |
43 | ||
44 | fun addCategory (grp, name, descr) = | |
45 | let | |
46 | val db = getDb () | |
47 | val id = nextSeq (db, "SupCategorySeq") | |
48 | in | |
49 | C.dml db ($`INSERT INTO SupCategory (id, grp, name, descr) | |
50 | VALUES (^(C.intToSql id), ^(C.intToSql grp), ^(C.stringToSql name), ^(C.stringToSql descr))`); | |
51 | id | |
52 | end | |
53 | ||
54 | fun modCategory (cat : category) = | |
55 | let | |
56 | val db = getDb () | |
57 | in | |
58 | ignore (C.dml db ($`UPDATE SupCategory SET | |
59 | grp = ^(C.intToSql (#grp cat)), name = ^(C.stringToSql (#name cat)), | |
60 | descr = ^(C.stringToSql (#descr cat)) | |
61 | WHERE id = ^(C.intToSql (#id cat))`)) | |
62 | end | |
63 | ||
64 | fun deleteCategory id = | |
65 | ignore (C.dml (getDb ()) ($`DELETE FROM SupCategory WHERE id = ^(C.intToSql id)`)) | |
66 | ||
67 | ||
68 | (* Issues *) | |
69 | ||
70 | val statusToSql = | |
71 | fn NEW => "0" | |
72 | | PENDING => "1" | |
73 | | CLOSED => "2" | |
74 | ||
75 | fun statusFromSql v = | |
76 | case C.intFromSql v of | |
77 | 0 => NEW | |
78 | | 1 => PENDING | |
79 | | 2 => CLOSED | |
80 | | _ => raise Fail "Bad support issue status" | |
81 | ||
82 | fun mkIssueRow [id, usr, cat, title, priv, status, stamp] = | |
83 | {id = C.intFromSql id, usr = C.intFromSql usr, cat = C.intFromSql cat, | |
84 | title = C.stringFromSql title, priv = C.boolFromSql priv, | |
85 | status = statusFromSql status, stamp = C.timestampFromSql stamp} | |
86 | | mkIssueRow row = rowError ("issue", row) | |
87 | ||
88 | fun lookupIssue id = | |
89 | mkIssueRow (C.oneRow (getDb ()) ($`SELECT id, usr, cat, title, priv, status, stamp | |
90 | FROM SupIssue | |
91 | WHERE id = ^(C.intToSql id)`)) | |
92 | ||
93 | fun listIssues () = | |
94 | C.map (getDb ()) mkIssueRow ($`SELECT id, usr, cat, title, priv, status, stamp | |
95 | FROM SupIssue | |
96 | ORDER BY stamp DESC`) | |
97 | ||
1cb3df3f AC |
98 | fun listCategoryIssues cat = |
99 | C.map (getDb ()) mkIssueRow ($`SELECT id, usr, cat, title, priv, status, stamp | |
100 | FROM SupIssue | |
101 | WHERE cat = ^(C.intToSql cat) | |
102 | ORDER BY stamp DESC`) | |
103 | ||
104 | fun listOpenCategoryIssues (cat, usr) = | |
105 | C.map (getDb ()) mkIssueRow ($`SELECT id, usr, cat, title, priv, status, stamp | |
106 | FROM SupIssue | |
107 | WHERE cat = ^(C.intToSql cat) | |
108 | AND status < 2 | |
109 | AND (NOT priv OR usr = ^(C.intToSql usr)) | |
110 | ORDER BY stamp DESC`) | |
111 | ||
112 | fun listOpenCategoryIssuesAdmin cat = | |
113 | C.map (getDb ()) mkIssueRow ($`SELECT id, usr, cat, title, priv, status, stamp | |
114 | FROM SupIssue | |
115 | WHERE cat = ^(C.intToSql cat) | |
116 | AND status < 2 | |
117 | ORDER BY stamp DESC`) | |
118 | ||
5a2812ca AC |
119 | fun addIssue (usr, cat, title, priv, status) = |
120 | let | |
121 | val db = getDb () | |
122 | val id = nextSeq (db, "SupIssueSeq") | |
123 | in | |
124 | C.dml db ($`INSERT INTO SupIssue (id, usr, cat, title, priv, status, stamp) | |
125 | VALUES (^(C.intToSql id), ^(C.intToSql usr), ^(C.intToSql cat), | |
126 | ^(C.stringToSql title), ^(C.boolToSql priv), | |
127 | ^(statusToSql status), CURRENT_TIMESTAMP)`); | |
128 | id | |
129 | end | |
130 | ||
131 | fun modIssue (iss : issue) = | |
132 | let | |
133 | val db = getDb () | |
134 | in | |
135 | ignore (C.dml db ($`UPDATE SupIssue SET | |
136 | usr = ^(C.intToSql (#usr iss)), cat = ^(C.intToSql (#cat iss)), | |
137 | title = ^(C.stringToSql (#title iss)), priv = ^(C.boolToSql (#priv iss)), | |
138 | status = ^(statusToSql (#status iss)) | |
139 | WHERE id = ^(C.intToSql (#id iss))`)) | |
140 | end | |
141 | ||
142 | fun deleteIssue id = | |
143 | ignore (C.dml (getDb ()) ($`DELETE FROM SupIssue WHERE id = ^(C.intToSql id)`)) | |
144 | ||
145 | ||
146 | (* Posts *) | |
147 | ||
148 | fun mkPostRow [id, usr, iss, body, stamp] = | |
149 | {id = C.intFromSql id, usr = C.intFromSql usr, iss = C.intFromSql iss, | |
150 | body = C.stringFromSql body, stamp = C.timestampFromSql stamp} | |
151 | | mkPostRow row = rowError ("post", row) | |
152 | ||
153 | fun lookupPost id = | |
154 | mkPostRow (C.oneRow (getDb ()) ($`SELECT id, usr, iss, body, stamp | |
155 | FROM SupPost | |
156 | WHERE id = ^(C.intToSql id)`)) | |
157 | ||
158 | fun listPosts iss = | |
159 | C.map (getDb ()) mkPostRow ($`SELECT id, usr, iss, body, stamp | |
160 | FROM SupPost | |
161 | WHERE iss = ^(C.intToSql iss) | |
162 | ORDER BY stamp`) | |
163 | ||
164 | fun addPost (usr, iss, body) = | |
165 | let | |
166 | val db = getDb () | |
167 | val id = nextSeq (db, "SupPostSeq") | |
168 | in | |
169 | C.dml db ($`INSERT INTO SupPost (id, usr, iss, body, stamp) | |
170 | VALUES (^(C.intToSql id), ^(C.intToSql usr), ^(C.intToSql iss), | |
171 | ^(C.stringToSql body), CURRENT_TIMESTAMP)`); | |
172 | id | |
173 | end | |
174 | ||
175 | fun modPost (post : post) = | |
176 | let | |
177 | val db = getDb () | |
178 | in | |
179 | ignore (C.dml db ($`UPDATE SupPost SET | |
180 | usr = ^(C.intToSql (#usr post)), iss = ^(C.intToSql (#iss post)), | |
181 | body = ^(C.stringToSql (#body post)) | |
182 | WHERE id = ^(C.intToSql (#id post))`)) | |
183 | end | |
184 | ||
185 | fun deletePost id = | |
186 | ignore (C.dml (getDb ()) ($`DELETE FROM SupPost WHERE id = ^(C.intToSql id)`)) | |
187 | ||
188 | ||
189 | (* Subscriptions *) | |
190 | ||
191 | fun mkSubRow [usr, cat] = | |
192 | {usr = C.intFromSql usr, cat = C.intFromSql cat} | |
193 | | mkSubRow row = rowError ("subscription", row) | |
194 | ||
195 | fun subscribed {usr, cat} = | |
196 | case C.oneRow (getDb ()) ($`SELECT COUNT( * ) FROM SupSubscription | |
197 | WHERE usr = ^(C.intToSql usr) AND cat = ^(C.intToSql cat)`) of | |
198 | [n] => not (C.isNull n) andalso C.intFromSql n > 0 | |
199 | | r => Init.rowError ("subscribed", r) | |
200 | ||
201 | fun subscribe (sub as {usr, cat}) = | |
202 | if subscribed sub then | |
203 | () | |
204 | else | |
205 | ignore (C.dml (getDb ()) ($`INSERT INTO SupSubscription (usr, cat) | |
206 | VALUES (^(C.intToSql usr), ^(C.intToSql cat))`)) | |
207 | ||
208 | fun unsubscribe {usr, cat} = | |
209 | ignore (C.dml (getDb ()) ($`DELETE FROM SupSubscription | |
210 | WHERE usr = ^(C.intToSql usr) AND cat = ^(C.intToSql cat)`)) | |
211 | ||
1cb3df3f | 212 | fun validTitle s = CharVector.exists (fn ch => not (Char.isSpace ch)) s |
5a2812ca AC |
213 | |
214 | end |