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