]>
Commit | Line | Data |
---|---|---|
a855ba81 | 1 | package BSE::DB::Mysql; |
dec5de5e | 2 | use strict; |
a855ba81 | 3 | use DBI; |
b19047a6 | 4 | use vars qw/@ISA/; |
444957b9 | 5 | use Carp 'confess'; |
b19047a6 | 6 | @ISA = qw(BSE::DB); |
a855ba81 | 7 | |
5ac2ad24 | 8 | use vars qw($VERSION $MAX_CONNECTION_AGE); |
a855ba81 | 9 | |
002dbed5 | 10 | use Constants 0.1 qw/$DSN $UN $PW $DBOPTS/; |
a855ba81 TC |
11 | |
12 | use Carp; | |
13 | ||
33bccea7 TC |
14 | my $self; |
15 | ||
a855ba81 TC |
16 | $VERSION = 1.01; |
17 | ||
5ac2ad24 TC |
18 | $MAX_CONNECTION_AGE = 1200; |
19 | ||
a855ba81 TC |
20 | my %statements = |
21 | ( | |
dbdff741 TC |
22 | # don't ever load the entire articles table |
23 | #Articles => 'select * from article', | |
a855ba81 | 24 | replaceArticle => |
c76e86ea | 25 | 'replace article values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', |
a855ba81 | 26 | addArticle => |
c76e86ea | 27 | 'insert article values (null, ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', |
a855ba81 | 28 | deleteArticle => 'delete from article where id = ?', |
a855ba81 TC |
29 | getArticleByPkey => 'select * from article where id = ?', |
30 | ||
31 | getArticleByLevel => 'select * from article where level = ?', | |
32 | getArticleByParentid => 'select * from article where parentid = ?', | |
c76e86ea | 33 | getArticleByLinkAlias => 'select * from article where linkAlias = ?', |
99ef7979 TC |
34 | 'Articles.stepParents' => <<EOS, |
35 | select ar.* from article ar, other_parents op | |
36 | where ar.id = op.parentId and op.childId = ? | |
37 | order by op.childDisplayOrder desc | |
3ffa8a72 TC |
38 | EOS |
39 | 'Articles.visibleStepParents' => <<EOS, | |
40 | select ar.* from article ar, other_parents op | |
41 | where ar.id = op.parentId and op.childId = ? | |
42 | and date_format(?, '%Y%m%d') between date_format(op.release, '%Y%m%d') and date_format(op.expire, '%Y%m%d') | |
43 | and listed <> 0 | |
44 | order by op.childDisplayOrder desc | |
721cd24c TC |
45 | EOS |
46 | 'Articles.stepKids' => <<EOS, | |
47 | select ar.* from article ar, other_parents op | |
48 | where op.childId = ar.id and op.parentId = ? | |
49 | EOS | |
4175638b TC |
50 | # originally "... and ? between op.release and op.expire" |
51 | # but since the first argument was a string, mysql treated the comparisons | |
52 | # as string comparisons | |
721cd24c TC |
53 | 'Articles.visibleStepKids' => <<EOS, |
54 | select ar.* from article ar, other_parents op | |
55 | where op.childId = ar.id | |
4175638b | 56 | and op.parentId = ? |
deae2a52 | 57 | and date_format(?, '%Y%m%d') between date_format(op.release, '%Y%m%d') and date_format(op.expire, '%Y%m%d') and listed <> 0 |
99ef7979 | 58 | EOS |
531fb3bc | 59 | 'Articles.ids'=>'select id from article', |
147e99e8 TC |
60 | articlePossibleStepparents => <<EOS, |
61 | select a.id, a.title from article a | |
62 | where a.id not in (select parentId from other_parents where childId = ?) | |
63 | and a.id <> ?; | |
64 | EOS | |
65 | articlePossibleStepchildren => <<EOS, | |
66 | select a.id, a.title from article a | |
67 | where a.id not in (select childId from other_parents where parentId = ?) | |
68 | and a.id <> ?; | |
69 | EOS | |
99ef7979 | 70 | |
721cd24c TC |
71 | Images => 'select * from image', |
72 | replaceImage => | |
e63c3728 TC |
73 | 'replace image values (?,?,?,?,?,?,?,?,?,?,?)', |
74 | addImage => 'insert image values(null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', | |
721cd24c | 75 | deleteImage => 'delete from image where id = ?', |
ca9aa2bf | 76 | getImageByArticleId => 'select * from image where articleId = ? order by displayOrder', |
c2096d67 TC |
77 | getImageByPkey => 'select * from image where id = ?', |
78 | getImageByArticleIdAndName => <<SQL, | |
79 | select * from image where articleId = ? and name = ? | |
80 | SQL | |
721cd24c | 81 | |
a855ba81 TC |
82 | dropIndex => 'delete from searchindex', |
83 | insertIndex => 'insert searchindex values(?, ?, ?, ?)', | |
84 | searchIndex => 'select * from searchindex where id = ?', | |
85 | searchIndexWC => 'select * from searchindex where id like ?', | |
86 | ||
87 | Products=> 'select article.*, product.* from article, product where id = articleId', | |
74b21f6d | 88 | addProduct => 'insert product values(?,?,?,?,?,?,?,?,?,?,?,?)', |
a855ba81 | 89 | getProductByPkey => 'select article.*, product.* from article, product where id=? and articleId = id', |
74b21f6d | 90 | replaceProduct => 'replace product values(?,?,?,?,?,?,?,?,?,?,?,?)', |
99ef7979 TC |
91 | 'Products.stepProducts' => <<EOS, |
92 | select ar.*, pr.* from article ar, product pr, other_parents op | |
93 | where ar.id = pr.articleId and op.childId = ar.id and op.parentId = ? | |
94 | EOS | |
95 | 'Products.visibleStep' => <<EOS, | |
96 | select ar.*, pr.* from article ar, product pr, other_parents op | |
97 | where ar.id = pr.articleId and op.childId = ar.id | |
98 | and op.parentId = ? and ? between op.release and op.expire | |
99 | EOS | |
d7538448 TC |
100 | 'Products.subscriptionDependent' => <<SQL, |
101 | select ar.*, pr.* from article ar, product pr | |
102 | where ar.id = pr.articleId | |
103 | and (pr.subscription_id = ? or subscription_required = ?) | |
ab2cd916 TC |
104 | SQL |
105 | 'Products.orderProducts' => <<SQL, | |
106 | select ar.*, pr.* from article ar, product pr, order_item oi | |
107 | where oi.orderId = ? and oi.productId = ar.id and ar.id = pr.articleId | |
d7538448 | 108 | SQL |
6473c56f | 109 | deleteProduct => 'delete from product where articleId = ?', |
d49667a2 TC |
110 | 'Products.userWishlist' => <<SQL, |
111 | select ar.*, pr.* from article ar, product pr, bse_wishlist wi | |
112 | where wi.user_id = ? and wi.product_id = ar.id and ar.id = pr.articleId | |
113 | order by wi.display_order desc | |
114 | SQL | |
115 | bse_userWishlistOrder => <<SQL, | |
116 | select product_id, display_order | |
117 | from bse_wishlist | |
118 | where user_id = ? | |
119 | order by display_order desc | |
120 | SQL | |
121 | bse_userWishlistReorder => <<SQL, | |
122 | update bse_wishlist | |
123 | set display_order = ? | |
124 | where user_id = ? and product_id = ? | |
125 | SQL | |
126 | bse_addToWishlist => <<SQL, | |
127 | insert into bse_wishlist(user_id, product_id, display_order) | |
128 | values(?, ?, ?) | |
129 | SQL | |
130 | bse_removeFromWishlist => <<SQL, | |
131 | delete from bse_wishlist where user_id = ? and product_id = ? | |
132 | SQL | |
133 | ||
a855ba81 TC |
134 | Orders => 'select * from orders', |
135 | getOrderByPkey => 'select * from orders where id = ?', | |
136 | getOrderItemByOrderId => 'select * from order_item where orderId = ?', | |
74b21f6d TC |
137 | addOrder => 'insert orders values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', |
138 | replaceOrder => 'replace orders values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', | |
139 | addOrderItem => 'insert order_item values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', | |
2ad9fd05 | 140 | replaceOrderItem => 'replace order_item values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', |
edc5d096 | 141 | getOrderByUserId => 'select * from orders where userId = ?', |
5d88571c | 142 | deleteOrdersItems => 'delete from order_item where orderId = ?', |
99ef7979 | 143 | |
6473c56f TC |
144 | getOrderItemByProductId => 'select * from order_item where productId = ?', |
145 | ||
99ef7979 TC |
146 | OtherParents => 'select * from other_parents', |
147 | getOtherParentByChildId => <<EOS, | |
148 | select * from other_parents where childId = ? order by childDisplayOrder desc | |
149 | EOS | |
150 | getOtherParentByParentId => <<EOS, | |
151 | select * from other_parents where parentId = ? order by parentDisplayOrder desc | |
152 | EOS | |
153 | getOtherParentByParentIdAndChildId => | |
154 | 'select * from other_parents where parentId = ? and childId = ?', | |
155 | addOtherParent=>'insert other_parents values(null,?,?,?,?,?,?)', | |
156 | deleteOtherParent => 'delete from other_parents where id = ?', | |
157 | replaceOtherParent=>'replace other_parents values(?,?,?,?,?,?,?)', | |
158 | 'OtherParents.anylinks' => | |
159 | 'select * from other_parents where childId = ? or parentId = ?', | |
edc5d096 | 160 | |
e63c3728 | 161 | ArticleFiles => 'select * from article_files', |
edc5d096 | 162 | addArticleFile => |
e63c3728 | 163 | 'insert into article_files values (null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', |
edc5d096 | 164 | replaceArticleFile => |
e63c3728 | 165 | 'replace article_files values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', |
edc5d096 TC |
166 | deleteArticleFile => 'delete from article_files where id = ?', |
167 | getArticleFileByArticleId => | |
168 | 'select * from article_files where articleId = ? order by displayOrder desc', | |
4afdbb1b | 169 | getArticleFileByPkey => 'select * from article_files where id = ?', |
abf5bbc6 TC |
170 | |
171 | orderFiles =><<SQL, | |
172 | select distinct af.*, oi.id as item_id | |
173 | from article_files af, order_item oi | |
174 | where af.articleId = oi.productId and oi.orderId = ? | |
3d3e00ed | 175 | order by oi.id, af.displayOrder desc |
abf5bbc6 | 176 | SQL |
edc5d096 TC |
177 | |
178 | getSiteUserByUserId => | |
179 | 'select * from site_users where userId = ?', | |
531fb3bc TC |
180 | getSiteUserByPkey => |
181 | 'select * from site_users where id = ?', | |
dfdeb4fe TC |
182 | getSiteUserByAffiliate_name => |
183 | 'select * from site_users where affiliate_name = ?', | |
288ef5b8 TC |
184 | addSiteUser => 'insert site_users values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', |
185 | replaceSiteUser => 'replace site_users values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', | |
b19047a6 TC |
186 | 'SiteUsers.removeSubscriptions'=> |
187 | 'delete from subscribed_users where userId = ?', | |
531fb3bc TC |
188 | 'SiteUsers.removeSub'=> |
189 | 'delete from subscribed_users where userId = ? and subId = ?', | |
190 | 'SiteUsers.subRecipients' => <<EOS, | |
191 | select si.* from site_users si, subscribed_users su | |
dc040d12 | 192 | where confirmed <> 0 and disabled = 0 and si.id = su.userId and su.subId = ? |
531fb3bc | 193 | EOS |
9063386f | 194 | SiteUsers => 'select * from site_users', |
af74f0b4 TC |
195 | 'SiteUsers.allSubscribers' => <<SQL, |
196 | select distinct su.* | |
197 | from site_users su, orders od, order_item oi | |
198 | where su.id = od.siteuser_id and od.id = oi.orderId | |
199 | and oi.subscription_id <> -1 | |
200 | SQL | |
efcc5a30 | 201 | siteuserAllIds => 'select id from site_users', |
dfdeb4fe TC |
202 | getBSESiteuserImage => <<SQL, |
203 | select * from bse_siteuser_images | |
204 | where siteuser_id = ? and image_id = ? | |
205 | SQL | |
206 | getBSESiteuserImages => <<SQL, | |
207 | select * from bse_siteuser_images where siteuser_id = ? | |
208 | SQL | |
209 | addBSESiteuserImage => <<SQL, | |
210 | insert bse_siteuser_images values(?,?,?,?,?,?,?,?) | |
211 | SQL | |
212 | replaceBSESiteuserImage => <<SQL, | |
213 | replace bse_siteuser_images values(?,?,?,?,?,?,?,?) | |
214 | SQL | |
215 | deleteBSESiteuserImage=> <<SQL, | |
df37553e | 216 | delete from bse_siteuser_images where siteuser_id = ? and image_id = ? |
dfdeb4fe | 217 | SQL |
b19047a6 TC |
218 | |
219 | SubscriptionTypes => | |
220 | 'select * from subscription_types', | |
221 | addSubscriptionType=> | |
4ef01459 | 222 | 'insert subscription_types values(null,?,?,?,?,?,?,?,?,?,?,?,?)', |
b19047a6 | 223 | replaceSubscriptionType=> |
4ef01459 | 224 | 'replace subscription_types values(?,?,?,?,?,?,?,?,?,?,?,?,?)', |
b19047a6 TC |
225 | getSubscriptionTypeByPkey => |
226 | 'select * from subscription_types where id = ? order by name', | |
b553afa2 TC |
227 | deleteSubscriptionType => |
228 | 'delete from subscription_types where id = ?', | |
4772671f TC |
229 | subRecipientCount => <<EOS, |
230 | select count(*) as "count" from site_users si, subscribed_users su | |
dc040d12 | 231 | where confirmed <> 0 and disabled = 0 and si.id = su.userId and su.subId = ? |
4772671f | 232 | EOS |
9063386f TC |
233 | 'SubscriptionTypes.userSubscribedTo' => <<'EOS', |
234 | select su.* from subscription_types su, subscribed_users us | |
235 | where us.userId = ? and us.subId = su.id | |
236 | EOS | |
b19047a6 TC |
237 | |
238 | addSubscribedUser=> | |
239 | 'insert subscribed_users values(null,?,?)', | |
240 | getSubscribedUserByUserId => | |
241 | 'select * from subscribed_users where userId = ?', | |
242 | ||
243 | # the following don't work with the row/table classes | |
244 | articlesList => | |
245 | 'select id, title from article order by level, displayOrder desc', | |
246 | ||
247 | getEmailBlackEntryByEmail => | |
248 | 'select * from email_blacklist where email = ?', | |
2a295ea9 TC |
249 | addEmailBlackEntry => |
250 | 'insert email_blacklist values(null,?,?)', | |
b19047a6 TC |
251 | |
252 | addEmailRequest => | |
253 | 'insert email_requests values(null,?,?,?,?)', | |
254 | replaceEmailRequest => | |
255 | 'replace email_requests values(?,?,?,?,?)', | |
531fb3bc TC |
256 | deleteEmailRequest => |
257 | 'delete from email_requests where id = ?', | |
b19047a6 TC |
258 | getEmailRequestByGenEmail => |
259 | 'select * from email_requests where genEmail = ?', | |
6473c56f TC |
260 | |
261 | addAdminBase => 'insert into admin_base values(null, ?)', | |
08123550 TC |
262 | replaceAdminBase => 'replace into admin_base values(?, ?)', |
263 | deleteAdminBase => 'delete from admin_base where id = ?', | |
264 | getAdminBaseByPkey => 'select * from admin_base where id=?', | |
6473c56f TC |
265 | |
266 | AdminUsers => <<SQL, | |
267 | select bs.*, us.* from admin_base bs, admin_users us | |
268 | where bs.id = us.base_id | |
269 | order by logon | |
270 | SQL | |
271 | getAdminUserByLogon => <<SQL, | |
272 | select bs.*, us.* from admin_base bs, admin_users us | |
273 | where bs.id = us.base_id and us.logon = ? | |
274 | SQL | |
275 | getAdminUserByPkey => <<SQL, | |
276 | select bs.*, us.* from admin_base bs, admin_users us | |
277 | where bs.id = us.base_id and bs.id = ? | |
278 | SQL | |
279 | addAdminUser => 'insert into admin_users values(?,?,?,?,?)', | |
08123550 TC |
280 | replaceAdminUser => 'replace into admin_users values(?,?,?,?,?)', |
281 | deleteAdminUser => 'delete from admin_users where base_id = ?', | |
6473c56f TC |
282 | adminUsersGroups => <<SQL, |
283 | select bs.*, gr.* | |
284 | from admin_base bs, admin_groups gr, admin_membership am | |
285 | where bs.id = gr.base_id && am.user_id = ? and am.group_id = bs.id | |
286 | order by gr.name | |
287 | SQL | |
08123550 TC |
288 | userGroups => 'select * from admin_membership where user_id = ?', |
289 | deleteUserGroups => 'delete from admin_membership where user_id = ?', | |
6473c56f TC |
290 | |
291 | AdminGroups => <<SQL, | |
292 | select bs.*, gr.* | |
293 | from admin_base bs, admin_groups gr | |
294 | where bs.id = gr.base_id | |
295 | order by name | |
296 | SQL | |
297 | adminGroupsUsers => <<SQL, | |
298 | select bs.*, us.* | |
299 | from admin_base bs, admin_users us, admin_membership am | |
300 | where bs.id = us.base_id && am.group_id = ? and am.user_id = bs.id | |
301 | order by logon | |
302 | SQL | |
303 | getAdminGroupByName => <<SQL, | |
304 | select bs.*, gr.* from admin_base bs, admin_groups gr | |
305 | where bs.id = gr.base_id and gr.name = ? | |
306 | SQL | |
307 | getAdminGroupByPkey => <<SQL, | |
308 | select bs.*, gr.* from admin_base bs, admin_groups gr | |
309 | where bs.id = gr.base_id and bs.id = ? | |
310 | SQL | |
4d764c34 TC |
311 | addAdminGroup => 'insert into admin_groups values(?,?,?,?,?)', |
312 | replaceAdminGroup => 'replace into admin_groups values(?,?,?,?,?)', | |
08123550 TC |
313 | deleteAdminGroup => 'delete from admin_groups where base_id = ?', |
314 | groupUsers => 'select * from admin_membership where group_id = ?', | |
9168c88c TC |
315 | 'AdminGroups.userPermissionGroups' => <<SQL, |
316 | select bs.*, ag.* from admin_base bs, admin_groups ag, admin_membership am | |
317 | where bs.id = ag.base_id | |
318 | and ( (ag.base_id = am.group_id and am.user_id = ?) | |
319 | or ag.name = 'everyone' ) | |
320 | SQL | |
08123550 TC |
321 | |
322 | addUserToGroup => 'insert into admin_membership values(?,?)', | |
323 | delUserFromGroup => <<SQL, | |
324 | delete from admin_membership where user_id = ? and group_id = ? | |
325 | SQL | |
326 | deleteGroupUsers => 'delete from admin_membership where group_id = ?', | |
327 | ||
328 | articleObjectPerm => <<SQL, | |
329 | select * from admin_perms where object_id = ? and admin_id = ? | |
330 | SQL | |
331 | addArticleObjectPerm => 'insert into admin_perms values(?,?,?)', | |
332 | replaceArticleObjectPerm => 'replace into admin_perms values(?,?,?)', | |
4010d92e | 333 | userPerms => <<SQL, |
9168c88c | 334 | select distinct ap.* |
4010d92e | 335 | from admin_perms ap |
9168c88c | 336 | where ap.admin_id = ? |
4010d92e TC |
337 | SQL |
338 | groupPerms => <<SQL, | |
339 | select distinct ap.* | |
340 | from admin_perms ap, admin_membership am | |
341 | where ap.admin_id = am.group_id and am.user_id = ? | |
342 | SQL | |
343 | commonPerms => <<SQL, | |
344 | select distinct ap.* | |
345 | from admin_perms ap, admin_groups ag | |
346 | where ap.admin_id = ag.base_id and ag.name = 'everyone' | |
0ec4ac8a TC |
347 | SQL |
348 | Subscriptions => 'select * from bse_subscriptions', | |
349 | addSubscription => 'insert bse_subscriptions values(null,?,?,?,?)', | |
350 | replaceSubscription => 'replace bse_subscriptions values(?,?,?,?,?)', | |
351 | deleteSubscription => <<SQL, | |
352 | delete from bse_subscriptions where subscription_id = ? | |
353 | SQL | |
354 | getSubscriptionByPkey => <<SQL, | |
355 | select * from bse_subscriptions where subscription_id = ? | |
9d576c12 TC |
356 | SQL |
357 | getSubscriptionByText_id => <<SQL, | |
358 | select * from bse_subscriptions where text_id = ? | |
d7538448 TC |
359 | SQL |
360 | subscriptionOrderItemCount => <<SQL, | |
361 | select count(*) as "count" from order_item where subscription_id = ? | |
362 | SQL | |
363 | subscriptionOrderSummary => <<SQL, | |
364 | select od.id, od.userId, od.orderDate, od.siteuser_id, | |
9d576c12 | 365 | sum(oi.subscription_period * oi.units) as "subscription_period" |
d7538448 | 366 | from orders od, order_item oi |
d44b5da9 | 367 | where oi.subscription_id = ? and od.id = oi.orderId and od.complete <> 0 |
d7538448 TC |
368 | group by od.id, od.userId, od.orderDate, od.siteuser_id |
369 | order by od.orderDate desc | |
af74f0b4 TC |
370 | SQL |
371 | subscriptionUserSummary => <<SQL, | |
372 | select su.*, us.* | |
373 | from site_users su, bse_user_subscribed us | |
374 | where su.id = us.siteuser_id and us.subscription_id = ? | |
d7538448 TC |
375 | SQL |
376 | subscriptionProductCount => <<SQL, | |
377 | select count(*) as "count" from product | |
378 | where subscription_id = ? or subscription_required = ? | |
af74f0b4 TC |
379 | SQL |
380 | removeUserSubscribed => <<SQL, | |
381 | delete from bse_user_subscribed where subscription_id = ? and siteuser_id = ? | |
382 | SQL | |
383 | addUserSubscribed => <<SQL, | |
384 | insert bse_user_subscribed values (?,?,?,?,?) | |
385 | SQL | |
386 | subscriptionUserBought => <<SQL, | |
829c9ed9 TC |
387 | select od.orderDate, |
388 | oi.subscription_period * oi.units as "subscription_period", | |
389 | oi.max_lapsed, | |
af74f0b4 TC |
390 | od.id as "order_id", oi.id as "item_id", oi.productId as "product_id" |
391 | from orders od, order_item oi | |
392 | where oi.subscription_id = ? and od.id = oi.orderId and od.siteuser_id = ? | |
d44b5da9 | 393 | and od.complete <> 0 |
af74f0b4 TC |
394 | SQL |
395 | userSubscribedEntry => <<SQL, | |
396 | select * from bse_user_subscribed | |
397 | where siteuser_id = ? and subscription_id = ? | |
9d576c12 TC |
398 | SQL |
399 | siteuserSubscriptions => <<SQL, | |
400 | select su.*, us.started_at, us.ends_at, us.max_lapsed | |
401 | from bse_subscriptions su, bse_user_subscribed us | |
402 | where us.siteuser_id = ? and us.subscription_id = su.subscription_id | |
403 | and us.ends_at >= curdate() | |
9168c88c | 404 | SQL |
37dd20ad TC |
405 | |
406 | addLocation => <<SQL, | |
407 | insert bse_locations values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) | |
408 | SQL | |
409 | replaceLocation => <<SQL, | |
410 | replace bse_locations values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) | |
411 | SQL | |
412 | getLocationByPkey => 'select * from bse_locations where id = ?', | |
413 | deleteLocation => 'delete from bse_locations where id = ?', | |
414 | Locations => 'select * from bse_locations order by description', | |
415 | ||
416 | Seminars => <<SQL, | |
417 | select ar.*, pr.*, se.* | |
418 | from article ar, product pr, bse_seminars se | |
718a070d | 419 | where ar.id = pr.articleId and ar.id = se.seminar_id |
37dd20ad TC |
420 | SQL |
421 | addSeminar => 'insert bse_seminars values(?,?)', | |
422 | replaceSeminar => 'replace bse_seminars values(?,?)', | |
423 | getSeminarByPkey => <<SQL, | |
424 | select ar.*, pr.*, se.* | |
425 | from article ar, product pr, bse_seminars se | |
426 | where id = ? and ar.id = pr.articleId and ar.id = se.seminar_id | |
718a070d TC |
427 | SQL |
428 | 'Locations.seminarFuture' => <<SQL, | |
429 | select distinct lo.* | |
430 | from bse_locations lo, bse_seminar_sessions ss | |
431 | where ss.seminar_id = ? and ss.when_at > ? | |
432 | and ss.location_id = lo.id | |
433 | order by lo.description | |
434 | SQL | |
435 | 'Locations.session_id' => <<SQL, | |
436 | select lo.* | |
437 | from bse_locations lo, bse_seminar_sessions ss | |
438 | where lo.id = ss.location_id and ss.id = ? | |
37dd20ad | 439 | SQL |
16ac5598 TC |
440 | |
441 | seminarSessionInfo => <<SQL, | |
442 | select se.*, lo.description | |
443 | from bse_seminar_sessions se, bse_locations lo | |
444 | where se.seminar_id = ? and se.location_id = lo.id | |
445 | order by when_at desc | |
718a070d TC |
446 | SQL |
447 | seminarFutureSessionInfo => <<SQL, | |
448 | select se.*, lo.description, lo.room, lo.street1, lo.street2, lo.suburb, | |
449 | lo.state, lo.country, lo.postcode, lo.public_notes | |
450 | from bse_seminar_sessions se, bse_locations lo | |
451 | where se.seminar_id = ? and se.when_at > ? and se.location_id = lo.id | |
452 | order by when_at desc | |
16ac5598 TC |
453 | SQL |
454 | addSeminarSession => 'insert bse_seminar_sessions values(null,?,?,?,?)', | |
455 | replaceSeminarSession => 'replace bse_seminar_sessions values(?,?,?,?,?)', | |
456 | deleteSeminarSession => 'delete from bse_seminar_sessions where id = ?', | |
457 | getSeminarSessionByPkey => 'select * from bse_seminar_sessions where id = ?', | |
458 | getSeminarSessionByLocation_idAndWhen_at => <<SQL, | |
459 | select * from bse_seminar_sessions | |
460 | where location_id = ? and when_at = ? | |
718a070d TC |
461 | SQL |
462 | getSeminarSessionBySeminar_id => <<SQL, | |
463 | select * from bse_seminar_sessions | |
464 | where seminar_id = ? | |
16ac5598 TC |
465 | SQL |
466 | 'SeminarSessions.futureSessions' => <<SQL, | |
467 | select * from bse_seminar_sessions | |
468 | where seminar_id = ? and when_at >= ? | |
718a070d TC |
469 | SQL |
470 | 'SeminarSessions.futureSeminarLocation' => <<SQL, | |
471 | select * | |
472 | from bse_seminar_sessions | |
473 | where seminar_id = ? and location_id = ? and when_at > ? | |
16ac5598 TC |
474 | SQL |
475 | 'SiteUsers.sessionBookings' => <<SQL, | |
476 | select su.* from site_users su, bse_seminar_bookings sb | |
477 | where sb.session_id = ? and su.id = sb.siteuser_id | |
478 | SQL | |
479 | cancelSeminarSessionBookings => <<SQL, | |
480 | delete from bse_seminar_bookings where session_id = ? | |
481 | SQL | |
482 | conflictSeminarSessions => <<SQL, | |
483 | select bo1.siteuser_id | |
484 | from bse_seminar_bookings bo1, bse_seminar_bookings bo2 | |
485 | where bo1.session_id = ? and bo2.session_id = ? | |
486 | and bo1.siteuser_id = bo2.siteuser_id | |
487 | SQL | |
488 | seminarSessionBookedIds => <<SQL, | |
489 | select * from bse_seminar_bookings where session_id = ? | |
490 | SQL | |
11c35ec9 TC |
491 | addSeminarBooking => <<SQL, |
492 | insert bse_seminar_bookings values(null,?,?,?,?,?,?) | |
16ac5598 TC |
493 | SQL |
494 | seminarSessionRollCallEntries => <<SQL, | |
11c35ec9 TC |
495 | select bo.roll_present, su.id, su.userId, su.name1, su.name2, su.email, |
496 | bo.id as booking_id | |
16ac5598 TC |
497 | from bse_seminar_bookings bo, site_users su |
498 | where bo.session_id = ? and bo.siteuser_id = su.id | |
499 | SQL | |
718a070d | 500 | updateSessionRollPresent => <<SQL, |
16ac5598 TC |
501 | update bse_seminar_bookings |
502 | set roll_present = ? | |
503 | where session_id = ? and siteuser_id = ? | |
718a070d | 504 | SQL |
efcc5a30 | 505 | userSeminarSessionBookings => <<SQL, |
718a070d TC |
506 | select session_id |
507 | from bse_seminar_bookings sb, bse_seminar_sessions ss | |
508 | where ss.seminar_id = ? and ss.id = sb.session_id and siteuser_id = ? | |
efcc5a30 TC |
509 | SQL |
510 | SiteUserGroups => 'select * from bse_siteuser_groups', | |
511 | addSiteUserGroup => 'insert bse_siteuser_groups values(null,?)', | |
512 | replaceSiteUserGroup => 'replace bse_siteuser_groups values(?,?)', | |
513 | deleteSiteUserGroup => 'delete from bse_siteuser_groups where id = ?', | |
514 | getSiteUserGroupByPkey => 'select * from bse_siteuser_groups where id = ?', | |
c5286ebe | 515 | getSiteUserGroupByName => 'select * from bse_siteuser_groups where name = ?', |
efcc5a30 TC |
516 | siteuserGroupMemberIds => <<SQL, |
517 | select siteuser_id as "id" | |
518 | from bse_siteuser_membership | |
519 | where group_id = ? | |
520 | SQL | |
521 | siteuserGroupAddMember => <<SQL, | |
522 | insert bse_siteuser_membership values(?,?) | |
523 | SQL | |
524 | siteuserGroupDeleteMember => <<SQL, | |
525 | delete from bse_siteuser_membership where group_id = ? and siteuser_id = ? | |
526 | SQL | |
527 | siteuserGroupDeleteAllMembers => <<SQL, | |
528 | delete from bse_siteuser_membership where group_id = ? | |
529 | SQL | |
530 | siteuserMemberOfGroup => <<SQL, | |
531 | select * from bse_siteuser_membership | |
532 | where siteuser_id = ? and group_id = ? | |
533 | SQL | |
534 | siteuserGroupsForUser => <<SQL, | |
535 | select group_id as "id" from bse_siteuser_membership where siteuser_id = ? | |
c2096d67 TC |
536 | SQL |
537 | ||
538 | articleAccessibleToGroup => <<SQL, | |
539 | select * from bse_article_groups | |
540 | where article_id = ? and group_id = ? | |
541 | SQL | |
542 | siteuserGroupsForArticle => <<SQL, | |
543 | select group_id as "id" from bse_article_groups | |
544 | where article_id = ? | |
545 | SQL | |
546 | articleAddSiteUserGroup => <<SQL, | |
547 | insert bse_article_groups values(?,?) | |
548 | SQL | |
549 | articleDeleteSiteUserGroup => <<SQL, | |
550 | delete from bse_article_groups | |
551 | where article_id = ? and group_id = ? | |
552 | SQL | |
553 | siteuserGroupDeleteAllPermissions => <<SQL, | |
554 | delete from bse_article_groups where group_id = ? | |
16ac5598 | 555 | SQL |
a855ba81 TC |
556 | ); |
557 | ||
5ac2ad24 TC |
558 | # called when we start working on a new request, mysql seems to have |
559 | # problems with old connections sometimes, or so it seems, so | |
560 | # disconnect occasionally (only matters for fastcgi, mod_perl) | |
561 | sub _startup { | |
562 | my $class = shift; | |
563 | ||
564 | if ($self) { | |
60321291 TC |
565 | unless ($self->{dbh}->ping) { |
566 | print STDERR "Database connection lost - reconnecting\n"; | |
5ac2ad24 TC |
567 | $self->{dbh} = $class->_connect; |
568 | $self->{birth} = time(); | |
569 | } | |
570 | } | |
571 | } | |
572 | ||
573 | sub _connect { | |
574 | my $dbh = DBI->connect( $DSN, $UN, $PW, $DBOPTS) | |
575 | or die "Cannot connect to database: $DBI::errstr"; | |
576 | ||
577 | # this might fail, but I don't care | |
578 | $dbh->do("set session sql_mode='ansi_quotes'"); | |
579 | ||
580 | return $dbh; | |
581 | } | |
582 | ||
a855ba81 TC |
583 | sub _single |
584 | { | |
585 | my $class = shift; | |
5ac2ad24 | 586 | |
a855ba81 TC |
587 | warn "Incorrect number of parameters passed to DatabaseHandle::single\n" unless @_ == 0; |
588 | ||
589 | unless ( defined $self ) { | |
5ac2ad24 | 590 | my $dbh = $class->_connect; |
74b21f6d | 591 | |
5ac2ad24 | 592 | $self = bless { dbh => $dbh, birth => time() }, $class; |
a855ba81 TC |
593 | } |
594 | $self; | |
595 | } | |
596 | ||
dc872a32 TC |
597 | my $get_sql_by_name = 'select sql_statement from sql_statements where name=?'; |
598 | ||
a855ba81 TC |
599 | sub stmt { |
600 | my ($self, $name) = @_; | |
601 | ||
6473c56f | 602 | $name =~ s/BSE.*:://; |
b19047a6 | 603 | |
dc872a32 TC |
604 | my $sql = $statements{$name}; |
605 | unless ($sql) { | |
606 | my @row = $self->{dbh}->selectrow_array($get_sql_by_name, {}, $name); | |
607 | if (@row) { | |
608 | $sql = $row[0]; | |
62533efa | 609 | #print STDERR "Found SQL '$sql'\n"; |
dc872a32 TC |
610 | } |
611 | else { | |
612 | print STDERR "SQL statment $name not found in sql_statements table\n"; | |
613 | } | |
614 | } | |
615 | $sql or confess "Statement named '$name' not found"; | |
616 | my $sth = $self->{dbh}->prepare($sql) | |
a855ba81 TC |
617 | or croak "Cannot prepare $name statment: ",$self->{dbh}->errstr; |
618 | ||
619 | $sth; | |
620 | } | |
621 | ||
622 | sub insert_id { | |
623 | my ($self, $sth) = @_; | |
624 | ||
99ef7979 TC |
625 | my $id = $sth->{"mysql_insertid"}; |
626 | ||
627 | return $id; | |
a855ba81 TC |
628 | } |
629 | ||
630 | # gotta love this | |
631 | sub DESTROY | |
632 | { | |
33bccea7 | 633 | my ($self) = @_; |
a855ba81 TC |
634 | # this is wierd - we only need to reset this on 5.6.x (for x == 0 so |
635 | # far) | |
636 | # Works fine without the reset for 5.005_03 | |
33bccea7 TC |
637 | if ($self->{dbh}) { |
638 | $self->{dbh}->disconnect; | |
639 | delete $self->{dbh}; | |
a855ba81 TC |
640 | } |
641 | } | |
642 | ||
643 | 1; | |
644 |