]> git.imager.perl.org - bse.git/blob - site/cgi-bin/modules/BSE/DB/Mysql.pm
0.14_11 commit
[bse.git] / site / cgi-bin / modules / BSE / DB / Mysql.pm
1 package BSE::DB::Mysql;
2 use strict;
3 use DBI;
4 use vars qw/@ISA/;
5 @ISA = qw(BSE::DB);
6
7 use vars qw($VERSION);
8
9 use Constants 0.1 qw/$DSN $UN $PW/;
10
11 use Carp;
12
13 my $self;
14
15 $VERSION = 1.01;
16
17 my %statements =
18   (
19    Articles => 'select * from article',
20    replaceArticle =>
21      'replace article values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
22    addArticle =>  
23      'insert article values (null, ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
24    deleteArticle => 'delete from article where id = ?',
25    getArticleByPkey => 'select * from article where id = ?',
26    
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
33 EOS
34    'Articles.stepKids' => <<EOS,
35 select ar.* from article ar, other_parents op
36    where op.childId = ar.id and op.parentId = ?
37 EOS
38    'Articles.visibleStepKids' => <<EOS,
39 select ar.* from article ar, other_parents op
40    where op.childId = ar.id 
41      and op.parentId = ? and ? between op.release and op.expire
42 EOS
43    'Articles.ids'=>'select id from article',
44
45    Images => 'select * from image',
46    replaceImage =>
47      'replace image values (?,?,?,?,?,?,?,?,?)',
48    addImage => 'insert image values(null, ?, ?, ?, ?, ?, ?, ?, ?)',
49    deleteImage => 'delete from image where id = ?',
50    getImageByArticleId => 'select * from image where articleId = ? order by displayOrder',
51    
52    dropIndex => 'delete from searchindex',
53    insertIndex => 'insert searchindex values(?, ?, ?, ?)',
54    searchIndex => 'select * from searchindex where id = ?',
55    searchIndexWC => 'select * from searchindex where id like ?',
56    
57    Products=> 'select article.*, product.* from article, product where id = articleId',
58    addProduct => 'insert product values(?,?,?,?,?,?,?)',
59    getProductByPkey => 'select article.*, product.* from article, product where id=? and articleId = id',
60    replaceProduct => 'replace product values(?,?,?,?,?,?,?)',
61    'Products.stepProducts' => <<EOS,
62 select ar.*, pr.* from article ar, product pr, other_parents op
63    where ar.id = pr.articleId and op.childId = ar.id and op.parentId = ?
64 EOS
65    'Products.visibleStep' => <<EOS,
66 select ar.*, pr.* from article ar, product pr, other_parents op
67    where ar.id = pr.articleId and op.childId = ar.id 
68      and op.parentId = ? and ? between op.release and op.expire
69 EOS
70    deleteProduct => 'delete from product where articleId = ?',
71    Orders => 'select * from orders',
72    getOrderByPkey => 'select * from orders where id = ?',
73    getOrderItemByOrderId => 'select * from order_item where orderId = ?',
74    addOrder => 'insert orders values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
75    replaceOrder => 'replace orders values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
76    addOrderItem => 'insert order_item values(null,?,?,?,?,?,?,?,?,?,?,?,?,?)',
77    getOrderByUserId => 'select * from orders where userId = ?',
78
79    getOrderItemByProductId => 'select * from order_item where productId = ?',
80
81    OtherParents => 'select * from other_parents',
82    getOtherParentByChildId => <<EOS,
83 select * from other_parents where childId = ? order by childDisplayOrder desc
84 EOS
85    getOtherParentByParentId => <<EOS,
86 select * from other_parents where parentId = ? order by parentDisplayOrder desc
87 EOS
88    getOtherParentByParentIdAndChildId =>
89    'select * from other_parents where parentId = ? and childId = ?',
90    addOtherParent=>'insert other_parents values(null,?,?,?,?,?,?)',
91    deleteOtherParent => 'delete from other_parents where id = ?',
92    replaceOtherParent=>'replace other_parents values(?,?,?,?,?,?,?)',
93    'OtherParents.anylinks' => 
94    'select * from other_parents where childId = ? or parentId = ?',
95
96    addArticleFile =>
97    'insert into article_files values (null,?,?,?,?,?,?,?,?,?,?,?)',
98    replaceArticleFile =>
99    'replace article_files values (?,?,?,?,?,?,?,?,?,?,?,?)',
100    deleteArticleFile => 'delete from article_files where id = ?',
101    getArticleFileByArticleId =>
102    'select * from article_files where articleId = ? order by displayOrder desc',
103    getArticleFileByPkey => 'select * from article_files where id = ?',
104
105    orderFiles =><<SQL,
106 select distinct af.*, oi.id as item_id
107 from article_files af, order_item oi
108 where af.articleId = oi.productId and oi.orderId = ?
109 order by af.description
110 SQL
111    
112    getSiteUserByUserId =>
113    'select * from site_users where userId = ?',
114    getSiteUserByPkey =>
115    'select * from site_users where id = ?',
116    addSiteUser => 'insert site_users values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
117    replaceSiteUser => 'replace site_users values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
118    'SiteUsers.removeSubscriptions'=>
119    'delete from subscribed_users where userId = ?',
120    'SiteUsers.removeSub'=>
121    'delete from subscribed_users where userId = ? and subId = ?',
122    'SiteUsers.subRecipients' => <<EOS,
123 select si.* from site_users si, subscribed_users su
124   where confirmed <> 0 and disabled = 0 and si.id = su.userId and su.subId = ?
125 EOS
126    SiteUsers => 'select * from site_users',
127
128    SubscriptionTypes =>
129    'select * from subscription_types',
130    addSubscriptionType=>
131    'insert subscription_types values(null,?,?,?,?,?,?,?,?,?,?,?,?)',
132    replaceSubscriptionType=>
133    'replace subscription_types values(?,?,?,?,?,?,?,?,?,?,?,?,?)',
134    getSubscriptionTypeByPkey =>
135    'select * from subscription_types where id = ? order by name',
136    deleteSubscriptionType =>
137    'delete from subscription_types where id = ?',
138    subRecipientCount => <<EOS,
139 select count(*) as "count" from site_users si, subscribed_users su
140   where confirmed <> 0 and disabled = 0 and si.id = su.userId and su.subId = ?
141 EOS
142    'SubscriptionTypes.userSubscribedTo' => <<'EOS',
143 select su.* from subscription_types su, subscribed_users us
144   where us.userId = ? and us.subId = su.id
145 EOS
146
147    addSubscribedUser=>
148    'insert subscribed_users values(null,?,?)',
149    getSubscribedUserByUserId =>
150    'select * from subscribed_users where userId = ?',
151
152    # the following don't work with the row/table classes
153    articlesList =>
154    'select id, title from article order by level, displayOrder desc',
155
156    getEmailBlackEntryByEmail =>
157    'select * from email_blacklist where email = ?',
158    addEmailBlackEntry =>
159    'insert email_blacklist values(null,?,?)',
160
161    addEmailRequest =>
162    'insert email_requests values(null,?,?,?,?)',
163    replaceEmailRequest =>
164    'replace email_requests values(?,?,?,?,?)',
165    deleteEmailRequest =>
166    'delete from email_requests where id = ?',
167    getEmailRequestByGenEmail =>
168    'select * from email_requests where genEmail = ?',
169
170    addAdminBase => 'insert into admin_base values(null, ?)',
171    replaceAdminBase => 'replace into admin_base values(?, ?)',
172    deleteAdminBase => 'delete from admin_base where id = ?',
173    getAdminBaseByPkey => 'select * from admin_base where id=?',
174    
175    AdminUsers => <<SQL,
176 select bs.*, us.* from admin_base bs, admin_users us
177   where bs.id = us.base_id
178    order by logon
179 SQL
180    getAdminUserByLogon => <<SQL,
181 select bs.*, us.* from admin_base bs, admin_users us
182   where bs.id = us.base_id and us.logon = ?
183 SQL
184    getAdminUserByPkey => <<SQL,
185 select bs.*, us.* from admin_base bs, admin_users us
186   where bs.id = us.base_id and bs.id = ?
187 SQL
188    addAdminUser => 'insert into admin_users values(?,?,?,?,?)',
189    replaceAdminUser => 'replace into admin_users values(?,?,?,?,?)',
190    deleteAdminUser => 'delete from admin_users where base_id = ?',
191    adminUsersGroups => <<SQL,
192 select bs.*, gr.*
193   from admin_base bs, admin_groups gr, admin_membership am
194   where bs.id = gr.base_id && am.user_id = ? and am.group_id = bs.id
195   order by gr.name
196 SQL
197    userGroups => 'select * from admin_membership where user_id = ?',
198    deleteUserGroups => 'delete from admin_membership where user_id = ?',
199
200    AdminGroups => <<SQL,
201 select bs.*, gr.* 
202   from admin_base bs, admin_groups gr
203   where bs.id = gr.base_id
204   order by name
205 SQL
206    adminGroupsUsers => <<SQL,
207 select bs.*, us.*
208   from admin_base bs, admin_users us, admin_membership am
209   where bs.id = us.base_id && am.group_id = ? and am.user_id = bs.id
210   order by logon
211 SQL
212    getAdminGroupByName => <<SQL,
213 select bs.*, gr.* from admin_base bs, admin_groups gr
214   where bs.id = gr.base_id and gr.name = ?
215 SQL
216    getAdminGroupByPkey => <<SQL,
217 select bs.*, gr.* from admin_base bs, admin_groups gr
218   where bs.id = gr.base_id and bs.id = ?
219 SQL
220    addAdminGroup => 'insert into admin_groups values(?,?,?,?)',
221    replaceAdminGroup => 'replace into admin_groups values(?,?,?,?)',
222    deleteAdminGroup => 'delete from admin_groups where base_id = ?',
223    groupUsers => 'select * from admin_membership where group_id = ?',
224    'AdminGroups.userPermissionGroups' => <<SQL,
225 select bs.*, ag.* from admin_base bs, admin_groups ag, admin_membership am
226 where bs.id = ag.base_id
227   and ( (ag.base_id = am.group_id and am.user_id = ?) 
228         or ag.name = 'everyone' )
229 SQL
230
231    addUserToGroup => 'insert into admin_membership values(?,?)',
232    delUserFromGroup => <<SQL,
233 delete from admin_membership where user_id = ? and group_id = ?
234 SQL
235    deleteGroupUsers => 'delete from admin_membership where group_id = ?',
236
237    articleObjectPerm => <<SQL,
238 select * from admin_perms where object_id = ? and admin_id = ?
239 SQL
240    addArticleObjectPerm => 'insert into admin_perms values(?,?,?)',
241    replaceArticleObjectPerm => 'replace into admin_perms values(?,?,?)',
242    userPerms => <<SQL,
243 select distinct ap.* 
244 from admin_perms ap
245 where ap.admin_id = ?
246 SQL
247    groupPerms => <<SQL,
248 select distinct ap.* 
249 from admin_perms ap, admin_membership am
250 where ap.admin_id = am.group_id and am.user_id = ?
251 SQL
252    commonPerms => <<SQL,
253 select distinct ap.* 
254 from admin_perms ap, admin_groups ag
255 where ap.admin_id = ag.base_id and ag.name = 'everyone'
256 SQL
257   );
258
259 sub _single
260 {
261   my $class = shift;
262   warn "Incorrect number of parameters passed to DatabaseHandle::single\n" unless @_ == 0;
263   
264   unless ( defined $self ) {
265     my $dbh = DBI->connect_cached( $DSN, $UN, $PW)
266       or die "Cannot connect to database: $DBI::errstr";
267     
268     $self = bless { dbh => $dbh }, $class;
269   }
270   $self;
271 }
272
273 sub stmt {
274   my ($self, $name) = @_;
275
276   $name =~ s/BSE.*:://;
277
278   $statements{$name} or confess "Statement named '$name' not found";
279   my $sth = $self->{dbh}->prepare($statements{$name})
280     or croak "Cannot prepare $name statment: ",$self->{dbh}->errstr;
281
282   $sth;
283 }
284
285 sub insert_id {
286   my ($self, $sth) = @_;
287
288   my $id = $sth->{"mysql_insertid"};
289
290   return $id;
291 }
292
293 # gotta love this
294 sub DESTROY
295 {
296   my ($self) = @_;
297   # this is wierd - we only need to reset this on 5.6.x (for x == 0 so
298   # far)
299   # Works fine without the reset for 5.005_03
300   if ($self->{dbh}) {
301     $self->{dbh}->disconnect;
302     delete $self->{dbh};
303   }
304 }
305
306 1;
307