]> git.imager.perl.org - bse.git/blame - site/cgi-bin/modules/BSE/DB/Mysql.pm
don't encode high bit characters in the title for more UTF8 compat
[bse.git] / site / cgi-bin / modules / BSE / DB / Mysql.pm
CommitLineData
a855ba81 1package BSE::DB::Mysql;
dec5de5e 2use strict;
a855ba81 3use DBI;
b19047a6 4use vars qw/@ISA/;
444957b9 5use Carp 'confess';
b19047a6 6@ISA = qw(BSE::DB);
a855ba81 7
5ac2ad24 8use vars qw($VERSION $MAX_CONNECTION_AGE);
a855ba81 9
002dbed5 10use Constants 0.1 qw/$DSN $UN $PW $DBOPTS/;
a855ba81
TC
11
12use Carp;
13
33bccea7
TC
14my $self;
15
a855ba81
TC
16$VERSION = 1.01;
17
5ac2ad24
TC
18$MAX_CONNECTION_AGE = 1200;
19
a855ba81
TC
20my %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,
35select ar.* from article ar, other_parents op
36 where ar.id = op.parentId and op.childId = ?
37order by op.childDisplayOrder desc
3ffa8a72
TC
38EOS
39 'Articles.visibleStepParents' => <<EOS,
40select 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
44order by op.childDisplayOrder desc
721cd24c
TC
45EOS
46 'Articles.stepKids' => <<EOS,
47select ar.* from article ar, other_parents op
48 where op.childId = ar.id and op.parentId = ?
49EOS
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,
54select 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 58EOS
531fb3bc 59 'Articles.ids'=>'select id from article',
147e99e8
TC
60 articlePossibleStepparents => <<EOS,
61select a.id, a.title from article a
62where a.id not in (select parentId from other_parents where childId = ?)
63 and a.id <> ?;
64EOS
65 articlePossibleStepchildren => <<EOS,
66select a.id, a.title from article a
67where a.id not in (select childId from other_parents where parentId = ?)
68 and a.id <> ?;
69EOS
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,
79select * from image where articleId = ? and name = ?
80SQL
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,
92select ar.*, pr.* from article ar, product pr, other_parents op
93 where ar.id = pr.articleId and op.childId = ar.id and op.parentId = ?
94EOS
95 'Products.visibleStep' => <<EOS,
96select 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
99EOS
d7538448
TC
100 'Products.subscriptionDependent' => <<SQL,
101select ar.*, pr.* from article ar, product pr
102 where ar.id = pr.articleId
103 and (pr.subscription_id = ? or subscription_required = ?)
ab2cd916
TC
104SQL
105 'Products.orderProducts' => <<SQL,
106select 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 108SQL
6473c56f 109 deleteProduct => 'delete from product where articleId = ?',
d49667a2
TC
110 'Products.userWishlist' => <<SQL,
111select 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
113order by wi.display_order desc
114SQL
115 bse_userWishlistOrder => <<SQL,
116select product_id, display_order
117from bse_wishlist
118where user_id = ?
119order by display_order desc
120SQL
121 bse_userWishlistReorder => <<SQL,
122update bse_wishlist
123 set display_order = ?
124where user_id = ? and product_id = ?
125SQL
126 bse_addToWishlist => <<SQL,
127insert into bse_wishlist(user_id, product_id, display_order)
128 values(?, ?, ?)
129SQL
130 bse_removeFromWishlist => <<SQL,
131delete from bse_wishlist where user_id = ? and product_id = ?
132SQL
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,
148select * from other_parents where childId = ? order by childDisplayOrder desc
149EOS
150 getOtherParentByParentId => <<EOS,
151select * from other_parents where parentId = ? order by parentDisplayOrder desc
152EOS
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,
172select distinct af.*, oi.id as item_id
173from article_files af, order_item oi
174where af.articleId = oi.productId and oi.orderId = ?
3d3e00ed 175order by oi.id, af.displayOrder desc
abf5bbc6 176SQL
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,
191select 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 193EOS
9063386f 194 SiteUsers => 'select * from site_users',
af74f0b4
TC
195 'SiteUsers.allSubscribers' => <<SQL,
196select 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
200SQL
efcc5a30 201 siteuserAllIds => 'select id from site_users',
dfdeb4fe
TC
202 getBSESiteuserImage => <<SQL,
203select * from bse_siteuser_images
204 where siteuser_id = ? and image_id = ?
205SQL
206 getBSESiteuserImages => <<SQL,
207select * from bse_siteuser_images where siteuser_id = ?
208SQL
209 addBSESiteuserImage => <<SQL,
210insert bse_siteuser_images values(?,?,?,?,?,?,?,?)
211SQL
212 replaceBSESiteuserImage => <<SQL,
213replace bse_siteuser_images values(?,?,?,?,?,?,?,?)
214SQL
215 deleteBSESiteuserImage=> <<SQL,
df37553e 216delete from bse_siteuser_images where siteuser_id = ? and image_id = ?
dfdeb4fe 217SQL
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,
230select 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 232EOS
9063386f
TC
233 'SubscriptionTypes.userSubscribedTo' => <<'EOS',
234select su.* from subscription_types su, subscribed_users us
235 where us.userId = ? and us.subId = su.id
236EOS
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,
267select bs.*, us.* from admin_base bs, admin_users us
268 where bs.id = us.base_id
269 order by logon
270SQL
271 getAdminUserByLogon => <<SQL,
272select bs.*, us.* from admin_base bs, admin_users us
273 where bs.id = us.base_id and us.logon = ?
274SQL
275 getAdminUserByPkey => <<SQL,
276select bs.*, us.* from admin_base bs, admin_users us
277 where bs.id = us.base_id and bs.id = ?
278SQL
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,
283select 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
287SQL
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,
292select bs.*, gr.*
293 from admin_base bs, admin_groups gr
294 where bs.id = gr.base_id
295 order by name
296SQL
297 adminGroupsUsers => <<SQL,
298select 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
302SQL
303 getAdminGroupByName => <<SQL,
304select bs.*, gr.* from admin_base bs, admin_groups gr
305 where bs.id = gr.base_id and gr.name = ?
306SQL
307 getAdminGroupByPkey => <<SQL,
308select bs.*, gr.* from admin_base bs, admin_groups gr
309 where bs.id = gr.base_id and bs.id = ?
310SQL
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,
316select bs.*, ag.* from admin_base bs, admin_groups ag, admin_membership am
317where bs.id = ag.base_id
318 and ( (ag.base_id = am.group_id and am.user_id = ?)
319 or ag.name = 'everyone' )
320SQL
08123550
TC
321
322 addUserToGroup => 'insert into admin_membership values(?,?)',
323 delUserFromGroup => <<SQL,
324delete from admin_membership where user_id = ? and group_id = ?
325SQL
326 deleteGroupUsers => 'delete from admin_membership where group_id = ?',
327
328 articleObjectPerm => <<SQL,
329select * from admin_perms where object_id = ? and admin_id = ?
330SQL
331 addArticleObjectPerm => 'insert into admin_perms values(?,?,?)',
332 replaceArticleObjectPerm => 'replace into admin_perms values(?,?,?)',
4010d92e 333 userPerms => <<SQL,
9168c88c 334select distinct ap.*
4010d92e 335from admin_perms ap
9168c88c 336where ap.admin_id = ?
4010d92e
TC
337SQL
338 groupPerms => <<SQL,
339select distinct ap.*
340from admin_perms ap, admin_membership am
341where ap.admin_id = am.group_id and am.user_id = ?
342SQL
343 commonPerms => <<SQL,
344select distinct ap.*
345from admin_perms ap, admin_groups ag
346where ap.admin_id = ag.base_id and ag.name = 'everyone'
0ec4ac8a
TC
347SQL
348 Subscriptions => 'select * from bse_subscriptions',
349 addSubscription => 'insert bse_subscriptions values(null,?,?,?,?)',
350 replaceSubscription => 'replace bse_subscriptions values(?,?,?,?,?)',
351 deleteSubscription => <<SQL,
352delete from bse_subscriptions where subscription_id = ?
353SQL
354 getSubscriptionByPkey => <<SQL,
355select * from bse_subscriptions where subscription_id = ?
9d576c12
TC
356SQL
357 getSubscriptionByText_id => <<SQL,
358select * from bse_subscriptions where text_id = ?
d7538448
TC
359SQL
360 subscriptionOrderItemCount => <<SQL,
361select count(*) as "count" from order_item where subscription_id = ?
362SQL
363 subscriptionOrderSummary => <<SQL,
364select 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
370SQL
371 subscriptionUserSummary => <<SQL,
372select su.*, us.*
373 from site_users su, bse_user_subscribed us
374where su.id = us.siteuser_id and us.subscription_id = ?
d7538448
TC
375SQL
376 subscriptionProductCount => <<SQL,
377select count(*) as "count" from product
378 where subscription_id = ? or subscription_required = ?
af74f0b4
TC
379SQL
380 removeUserSubscribed => <<SQL,
381delete from bse_user_subscribed where subscription_id = ? and siteuser_id = ?
382SQL
383 addUserSubscribed => <<SQL,
384insert bse_user_subscribed values (?,?,?,?,?)
385SQL
386 subscriptionUserBought => <<SQL,
829c9ed9
TC
387select 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
394SQL
395 userSubscribedEntry => <<SQL,
396select * from bse_user_subscribed
397 where siteuser_id = ? and subscription_id = ?
9d576c12
TC
398SQL
399 siteuserSubscriptions => <<SQL,
400select su.*, us.started_at, us.ends_at, us.max_lapsed
401 from bse_subscriptions su, bse_user_subscribed us
402where us.siteuser_id = ? and us.subscription_id = su.subscription_id
403 and us.ends_at >= curdate()
9168c88c 404SQL
37dd20ad
TC
405
406 addLocation => <<SQL,
407insert bse_locations values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
408SQL
409 replaceLocation => <<SQL,
410replace bse_locations values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
411SQL
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,
417select 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
420SQL
421 addSeminar => 'insert bse_seminars values(?,?)',
422 replaceSeminar => 'replace bse_seminars values(?,?)',
423 getSeminarByPkey => <<SQL,
424select 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
427SQL
428 'Locations.seminarFuture' => <<SQL,
429select distinct lo.*
430 from bse_locations lo, bse_seminar_sessions ss
431where ss.seminar_id = ? and ss.when_at > ?
432 and ss.location_id = lo.id
433order by lo.description
434SQL
435 'Locations.session_id' => <<SQL,
436select lo.*
437 from bse_locations lo, bse_seminar_sessions ss
438where lo.id = ss.location_id and ss.id = ?
37dd20ad 439SQL
16ac5598
TC
440
441 seminarSessionInfo => <<SQL,
442select se.*, lo.description
443 from bse_seminar_sessions se, bse_locations lo
444 where se.seminar_id = ? and se.location_id = lo.id
445order by when_at desc
718a070d
TC
446SQL
447 seminarFutureSessionInfo => <<SQL,
448select 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
452order by when_at desc
16ac5598
TC
453SQL
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,
459select * from bse_seminar_sessions
460 where location_id = ? and when_at = ?
718a070d
TC
461SQL
462 getSeminarSessionBySeminar_id => <<SQL,
463select * from bse_seminar_sessions
464 where seminar_id = ?
16ac5598
TC
465SQL
466 'SeminarSessions.futureSessions' => <<SQL,
467select * from bse_seminar_sessions
468 where seminar_id = ? and when_at >= ?
718a070d
TC
469SQL
470 'SeminarSessions.futureSeminarLocation' => <<SQL,
471select *
472 from bse_seminar_sessions
473 where seminar_id = ? and location_id = ? and when_at > ?
16ac5598
TC
474SQL
475 'SiteUsers.sessionBookings' => <<SQL,
476select su.* from site_users su, bse_seminar_bookings sb
477 where sb.session_id = ? and su.id = sb.siteuser_id
478SQL
479 cancelSeminarSessionBookings => <<SQL,
480delete from bse_seminar_bookings where session_id = ?
481SQL
482 conflictSeminarSessions => <<SQL,
483select bo1.siteuser_id
484 from bse_seminar_bookings bo1, bse_seminar_bookings bo2
485where bo1.session_id = ? and bo2.session_id = ?
486 and bo1.siteuser_id = bo2.siteuser_id
487SQL
488 seminarSessionBookedIds => <<SQL,
489select * from bse_seminar_bookings where session_id = ?
490SQL
11c35ec9
TC
491 addSeminarBooking => <<SQL,
492insert bse_seminar_bookings values(null,?,?,?,?,?,?)
16ac5598
TC
493SQL
494 seminarSessionRollCallEntries => <<SQL,
11c35ec9
TC
495select 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
498where bo.session_id = ? and bo.siteuser_id = su.id
499SQL
718a070d 500 updateSessionRollPresent => <<SQL,
16ac5598
TC
501update bse_seminar_bookings
502 set roll_present = ?
503 where session_id = ? and siteuser_id = ?
718a070d 504SQL
efcc5a30 505 userSeminarSessionBookings => <<SQL,
718a070d
TC
506select session_id
507 from bse_seminar_bookings sb, bse_seminar_sessions ss
508where ss.seminar_id = ? and ss.id = sb.session_id and siteuser_id = ?
efcc5a30
TC
509SQL
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,
517select siteuser_id as "id"
518from bse_siteuser_membership
519where group_id = ?
520SQL
521 siteuserGroupAddMember => <<SQL,
522insert bse_siteuser_membership values(?,?)
523SQL
524 siteuserGroupDeleteMember => <<SQL,
525delete from bse_siteuser_membership where group_id = ? and siteuser_id = ?
526SQL
527 siteuserGroupDeleteAllMembers => <<SQL,
528delete from bse_siteuser_membership where group_id = ?
529SQL
530 siteuserMemberOfGroup => <<SQL,
531select * from bse_siteuser_membership
532where siteuser_id = ? and group_id = ?
533SQL
534 siteuserGroupsForUser => <<SQL,
535select group_id as "id" from bse_siteuser_membership where siteuser_id = ?
c2096d67
TC
536SQL
537
538 articleAccessibleToGroup => <<SQL,
539select * from bse_article_groups
540where article_id = ? and group_id = ?
541SQL
542 siteuserGroupsForArticle => <<SQL,
543select group_id as "id" from bse_article_groups
544where article_id = ?
545SQL
546 articleAddSiteUserGroup => <<SQL,
547insert bse_article_groups values(?,?)
548SQL
549 articleDeleteSiteUserGroup => <<SQL,
550delete from bse_article_groups
551where article_id = ? and group_id = ?
552SQL
553 siteuserGroupDeleteAllPermissions => <<SQL,
554delete from bse_article_groups where group_id = ?
16ac5598 555SQL
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)
561sub _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
573sub _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
583sub _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
597my $get_sql_by_name = 'select sql_statement from sql_statements where name=?';
598
a855ba81
TC
599sub 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
622sub 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
631sub 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
6431;
644