1 package BSE::DB::Mysql;
9 use Constants 0.1 qw/$DSN $UN $PW/;
19 Articles => 'select * from article',
21 'replace article values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
23 'insert article values (null, ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
24 deleteArticle => 'delete from article where id = ?',
25 getArticleByPkey => 'select * from article where id = ?',
27 getArticleByLevel => 'select * from article where level = ?',
28 getArticleByParentid => 'select * from article where parentid = ?',
29 'Articles.stepParents' => <<EOS,
30 select ar.* from article ar, other_parents op
31 where ar.id = op.parentId and op.childId = ?
32 order by op.childDisplayOrder desc
34 'Articles.stepKids' => <<EOS,
35 select ar.* from article ar, other_parents op
36 where op.childId = ar.id and op.parentId = ?
38 # originally "... and ? between op.release and op.expire"
39 # but since the first argument was a string, mysql treated the comparisons
40 # as string comparisons
41 'Articles.visibleStepKids' => <<EOS,
42 select ar.* from article ar, other_parents op
43 where op.childId = ar.id
45 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
47 'Articles.ids'=>'select id from article',
49 Images => 'select * from image',
51 'replace image values (?,?,?,?,?,?,?,?,?)',
52 addImage => 'insert image values(null, ?, ?, ?, ?, ?, ?, ?, ?)',
53 deleteImage => 'delete from image where id = ?',
54 getImageByArticleId => 'select * from image where articleId = ? order by displayOrder',
56 dropIndex => 'delete from searchindex',
57 insertIndex => 'insert searchindex values(?, ?, ?, ?)',
58 searchIndex => 'select * from searchindex where id = ?',
59 searchIndexWC => 'select * from searchindex where id like ?',
61 Products=> 'select article.*, product.* from article, product where id = articleId',
62 addProduct => 'insert product values(?,?,?,?,?,?,?,?,?,?,?)',
63 getProductByPkey => 'select article.*, product.* from article, product where id=? and articleId = id',
64 replaceProduct => 'replace product values(?,?,?,?,?,?,?,?,?,?,?)',
65 'Products.stepProducts' => <<EOS,
66 select ar.*, pr.* from article ar, product pr, other_parents op
67 where ar.id = pr.articleId and op.childId = ar.id and op.parentId = ?
69 'Products.visibleStep' => <<EOS,
70 select ar.*, pr.* from article ar, product pr, other_parents op
71 where ar.id = pr.articleId and op.childId = ar.id
72 and op.parentId = ? and ? between op.release and op.expire
74 'Products.subscriptionDependent' => <<SQL,
75 select ar.*, pr.* from article ar, product pr
76 where ar.id = pr.articleId
77 and (pr.subscription_id = ? or subscription_required = ?)
79 'Products.orderProducts' => <<SQL,
80 select ar.*, pr.* from article ar, product pr, order_item oi
81 where oi.orderId = ? and oi.productId = ar.id and ar.id = pr.articleId
83 deleteProduct => 'delete from product where articleId = ?',
84 Orders => 'select * from orders',
85 getOrderByPkey => 'select * from orders where id = ?',
86 getOrderItemByOrderId => 'select * from order_item where orderId = ?',
87 addOrder => 'insert orders values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
88 replaceOrder => 'replace orders values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
89 addOrderItem => 'insert order_item values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
90 getOrderByUserId => 'select * from orders where userId = ?',
91 deleteOrdersItems => 'delete from order_item where orderId = ?',
93 getOrderItemByProductId => 'select * from order_item where productId = ?',
95 OtherParents => 'select * from other_parents',
96 getOtherParentByChildId => <<EOS,
97 select * from other_parents where childId = ? order by childDisplayOrder desc
99 getOtherParentByParentId => <<EOS,
100 select * from other_parents where parentId = ? order by parentDisplayOrder desc
102 getOtherParentByParentIdAndChildId =>
103 'select * from other_parents where parentId = ? and childId = ?',
104 addOtherParent=>'insert other_parents values(null,?,?,?,?,?,?)',
105 deleteOtherParent => 'delete from other_parents where id = ?',
106 replaceOtherParent=>'replace other_parents values(?,?,?,?,?,?,?)',
107 'OtherParents.anylinks' =>
108 'select * from other_parents where childId = ? or parentId = ?',
111 'insert into article_files values (null,?,?,?,?,?,?,?,?,?,?,?)',
112 replaceArticleFile =>
113 'replace article_files values (?,?,?,?,?,?,?,?,?,?,?,?)',
114 deleteArticleFile => 'delete from article_files where id = ?',
115 getArticleFileByArticleId =>
116 'select * from article_files where articleId = ? order by displayOrder desc',
117 getArticleFileByPkey => 'select * from article_files where id = ?',
120 select distinct af.*, oi.id as item_id
121 from article_files af, order_item oi
122 where af.articleId = oi.productId and oi.orderId = ?
123 order by oi.id, af.displayOrder desc
126 getSiteUserByUserId =>
127 'select * from site_users where userId = ?',
129 'select * from site_users where id = ?',
130 getSiteUserByAffiliate_name =>
131 'select * from site_users where affiliate_name = ?',
132 addSiteUser => 'insert site_users values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
133 replaceSiteUser => 'replace site_users values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
134 'SiteUsers.removeSubscriptions'=>
135 'delete from subscribed_users where userId = ?',
136 'SiteUsers.removeSub'=>
137 'delete from subscribed_users where userId = ? and subId = ?',
138 'SiteUsers.subRecipients' => <<EOS,
139 select si.* from site_users si, subscribed_users su
140 where confirmed <> 0 and disabled = 0 and si.id = su.userId and su.subId = ?
142 SiteUsers => 'select * from site_users',
143 'SiteUsers.allSubscribers' => <<SQL,
145 from site_users su, orders od, order_item oi
146 where su.id = od.siteuser_id and od.id = oi.orderId
147 and oi.subscription_id <> -1
149 getBSESiteuserImage => <<SQL,
150 select * from bse_siteuser_images
151 where siteuser_id = ? and image_id = ?
153 getBSESiteuserImages => <<SQL,
154 select * from bse_siteuser_images where siteuser_id = ?
156 addBSESiteuserImage => <<SQL,
157 insert bse_siteuser_images values(?,?,?,?,?,?,?,?)
159 replaceBSESiteuserImage => <<SQL,
160 replace bse_siteuser_images values(?,?,?,?,?,?,?,?)
162 deleteBSESiteuserImage=> <<SQL,
163 delete from bse_siteuser_images where siteuser_id = ? and image_id = ?
167 'select * from subscription_types',
168 addSubscriptionType=>
169 'insert subscription_types values(null,?,?,?,?,?,?,?,?,?,?,?,?)',
170 replaceSubscriptionType=>
171 'replace subscription_types values(?,?,?,?,?,?,?,?,?,?,?,?,?)',
172 getSubscriptionTypeByPkey =>
173 'select * from subscription_types where id = ? order by name',
174 deleteSubscriptionType =>
175 'delete from subscription_types where id = ?',
176 subRecipientCount => <<EOS,
177 select count(*) as "count" from site_users si, subscribed_users su
178 where confirmed <> 0 and disabled = 0 and si.id = su.userId and su.subId = ?
180 'SubscriptionTypes.userSubscribedTo' => <<'EOS',
181 select su.* from subscription_types su, subscribed_users us
182 where us.userId = ? and us.subId = su.id
186 'insert subscribed_users values(null,?,?)',
187 getSubscribedUserByUserId =>
188 'select * from subscribed_users where userId = ?',
190 # the following don't work with the row/table classes
192 'select id, title from article order by level, displayOrder desc',
194 getEmailBlackEntryByEmail =>
195 'select * from email_blacklist where email = ?',
196 addEmailBlackEntry =>
197 'insert email_blacklist values(null,?,?)',
200 'insert email_requests values(null,?,?,?,?)',
201 replaceEmailRequest =>
202 'replace email_requests values(?,?,?,?,?)',
203 deleteEmailRequest =>
204 'delete from email_requests where id = ?',
205 getEmailRequestByGenEmail =>
206 'select * from email_requests where genEmail = ?',
208 addAdminBase => 'insert into admin_base values(null, ?)',
209 replaceAdminBase => 'replace into admin_base values(?, ?)',
210 deleteAdminBase => 'delete from admin_base where id = ?',
211 getAdminBaseByPkey => 'select * from admin_base where id=?',
214 select bs.*, us.* from admin_base bs, admin_users us
215 where bs.id = us.base_id
218 getAdminUserByLogon => <<SQL,
219 select bs.*, us.* from admin_base bs, admin_users us
220 where bs.id = us.base_id and us.logon = ?
222 getAdminUserByPkey => <<SQL,
223 select bs.*, us.* from admin_base bs, admin_users us
224 where bs.id = us.base_id and bs.id = ?
226 addAdminUser => 'insert into admin_users values(?,?,?,?,?)',
227 replaceAdminUser => 'replace into admin_users values(?,?,?,?,?)',
228 deleteAdminUser => 'delete from admin_users where base_id = ?',
229 adminUsersGroups => <<SQL,
231 from admin_base bs, admin_groups gr, admin_membership am
232 where bs.id = gr.base_id && am.user_id = ? and am.group_id = bs.id
235 userGroups => 'select * from admin_membership where user_id = ?',
236 deleteUserGroups => 'delete from admin_membership where user_id = ?',
238 AdminGroups => <<SQL,
240 from admin_base bs, admin_groups gr
241 where bs.id = gr.base_id
244 adminGroupsUsers => <<SQL,
246 from admin_base bs, admin_users us, admin_membership am
247 where bs.id = us.base_id && am.group_id = ? and am.user_id = bs.id
250 getAdminGroupByName => <<SQL,
251 select bs.*, gr.* from admin_base bs, admin_groups gr
252 where bs.id = gr.base_id and gr.name = ?
254 getAdminGroupByPkey => <<SQL,
255 select bs.*, gr.* from admin_base bs, admin_groups gr
256 where bs.id = gr.base_id and bs.id = ?
258 addAdminGroup => 'insert into admin_groups values(?,?,?,?)',
259 replaceAdminGroup => 'replace into admin_groups values(?,?,?,?)',
260 deleteAdminGroup => 'delete from admin_groups where base_id = ?',
261 groupUsers => 'select * from admin_membership where group_id = ?',
262 'AdminGroups.userPermissionGroups' => <<SQL,
263 select bs.*, ag.* from admin_base bs, admin_groups ag, admin_membership am
264 where bs.id = ag.base_id
265 and ( (ag.base_id = am.group_id and am.user_id = ?)
266 or ag.name = 'everyone' )
269 addUserToGroup => 'insert into admin_membership values(?,?)',
270 delUserFromGroup => <<SQL,
271 delete from admin_membership where user_id = ? and group_id = ?
273 deleteGroupUsers => 'delete from admin_membership where group_id = ?',
275 articleObjectPerm => <<SQL,
276 select * from admin_perms where object_id = ? and admin_id = ?
278 addArticleObjectPerm => 'insert into admin_perms values(?,?,?)',
279 replaceArticleObjectPerm => 'replace into admin_perms values(?,?,?)',
283 where ap.admin_id = ?
287 from admin_perms ap, admin_membership am
288 where ap.admin_id = am.group_id and am.user_id = ?
290 commonPerms => <<SQL,
292 from admin_perms ap, admin_groups ag
293 where ap.admin_id = ag.base_id and ag.name = 'everyone'
295 Subscriptions => 'select * from bse_subscriptions',
296 addSubscription => 'insert bse_subscriptions values(null,?,?,?,?)',
297 replaceSubscription => 'replace bse_subscriptions values(?,?,?,?,?)',
298 deleteSubscription => <<SQL,
299 delete from bse_subscriptions where subscription_id = ?
301 getSubscriptionByPkey => <<SQL,
302 select * from bse_subscriptions where subscription_id = ?
304 getSubscriptionByText_id => <<SQL,
305 select * from bse_subscriptions where text_id = ?
307 subscriptionOrderItemCount => <<SQL,
308 select count(*) as "count" from order_item where subscription_id = ?
310 subscriptionOrderSummary => <<SQL,
311 select od.id, od.userId, od.orderDate, od.siteuser_id,
312 sum(oi.subscription_period * oi.units) as "subscription_period"
313 from orders od, order_item oi
314 where oi.subscription_id = ? and od.id = oi.orderId and od.complete <> 0
315 group by od.id, od.userId, od.orderDate, od.siteuser_id
316 order by od.orderDate desc
318 subscriptionUserSummary => <<SQL,
320 from site_users su, bse_user_subscribed us
321 where su.id = us.siteuser_id and us.subscription_id = ?
323 subscriptionProductCount => <<SQL,
324 select count(*) as "count" from product
325 where subscription_id = ? or subscription_required = ?
327 removeUserSubscribed => <<SQL,
328 delete from bse_user_subscribed where subscription_id = ? and siteuser_id = ?
330 addUserSubscribed => <<SQL,
331 insert bse_user_subscribed values (?,?,?,?,?)
333 subscriptionUserBought => <<SQL,
335 oi.subscription_period * oi.units as "subscription_period",
337 od.id as "order_id", oi.id as "item_id", oi.productId as "product_id"
338 from orders od, order_item oi
339 where oi.subscription_id = ? and od.id = oi.orderId and od.siteuser_id = ?
342 userSubscribedEntry => <<SQL,
343 select * from bse_user_subscribed
344 where siteuser_id = ? and subscription_id = ?
346 siteuserSubscriptions => <<SQL,
347 select su.*, us.started_at, us.ends_at, us.max_lapsed
348 from bse_subscriptions su, bse_user_subscribed us
349 where us.siteuser_id = ? and us.subscription_id = su.subscription_id
350 and us.ends_at >= curdate()
353 addLocation => <<SQL,
354 insert bse_locations values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
356 replaceLocation => <<SQL,
357 replace bse_locations values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
359 getLocationByPkey => 'select * from bse_locations where id = ?',
360 deleteLocation => 'delete from bse_locations where id = ?',
361 Locations => 'select * from bse_locations order by description',
364 select ar.*, pr.*, se.*
365 from article ar, product pr, bse_seminars se
366 where ar.id = pr.articleId and ar.id = se.articleId
368 addSeminar => 'insert bse_seminars values(?,?)',
369 replaceSeminar => 'replace bse_seminars values(?,?)',
370 getSeminarByPkey => <<SQL,
371 select ar.*, pr.*, se.*
372 from article ar, product pr, bse_seminars se
373 where id = ? and ar.id = pr.articleId and ar.id = se.seminar_id
380 warn "Incorrect number of parameters passed to DatabaseHandle::single\n" unless @_ == 0;
382 unless ( defined $self ) {
383 my $dbh = DBI->connect_cached( $DSN, $UN, $PW)
384 or die "Cannot connect to database: $DBI::errstr";
386 $self = bless { dbh => $dbh }, $class;
391 my $get_sql_by_name = 'select sql_statement from sql_statements where name=?';
394 my ($self, $name) = @_;
396 $name =~ s/BSE.*:://;
398 my $sql = $statements{$name};
400 my @row = $self->{dbh}->selectrow_array($get_sql_by_name, {}, $name);
403 #print STDERR "Found SQL '$sql'\n";
406 print STDERR "SQL statment $name not found in sql_statements table\n";
409 $sql or confess "Statement named '$name' not found";
410 my $sth = $self->{dbh}->prepare($sql)
411 or croak "Cannot prepare $name statment: ",$self->{dbh}->errstr;
417 my ($self, $sth) = @_;
419 my $id = $sth->{"mysql_insertid"};
428 # this is wierd - we only need to reset this on 5.6.x (for x == 0 so
430 # Works fine without the reset for 5.005_03
432 $self->{dbh}->disconnect;