… | |
… | |
27 | |
27 | |
28 | This module provides you with easy-to-use functions to execute sql |
28 | This module provides you with easy-to-use functions to execute sql |
29 | commands (using DBI). Despite being easy to use, they are also quite |
29 | commands (using DBI). Despite being easy to use, they are also quite |
30 | efficient and allow you to write faster programs in less lines of code. It |
30 | efficient and allow you to write faster programs in less lines of code. It |
31 | should work with anything from perl-5.004_01 onwards, but I only support |
31 | should work with anything from perl-5.004_01 onwards, but I only support |
32 | 5.005+. |
32 | 5.005+. UTF8 handling (the C<sql_u*> family of functions) will only be |
|
|
33 | effective with perl version 5.006 and beyond. |
33 | |
34 | |
34 | If the descriptions here seem terse or if you always wanted to know |
35 | If the descriptions here seem terse or if you always wanted to know |
35 | what PApp is then have a look at the PApp module which uses this module |
36 | what PApp is then have a look at the PApp module which uses this module |
36 | extensively but also provides you with a lot more gimmicks to play around |
37 | extensively but also provides you with a lot more gimmicks to play around |
37 | with to help you create cool applications ;) |
38 | with to help you create cool applications ;) |
… | |
… | |
43 | use DBI (); |
44 | use DBI (); |
44 | |
45 | |
45 | BEGIN { |
46 | BEGIN { |
46 | use base qw(Exporter DynaLoader); |
47 | use base qw(Exporter DynaLoader); |
47 | |
48 | |
48 | $VERSION = 0.12; |
49 | $VERSION = 0.123; |
49 | @EXPORT = qw( |
50 | @EXPORT = qw( |
50 | sql_exec sql_fetch sql_fetchall sql_exists sql_insertid $sql_exec |
51 | sql_exec sql_fetch sql_fetchall sql_exists sql_insertid $sql_exec |
|
|
52 | sql_ufetch sql_ufetchall sql_uexists |
51 | ); |
53 | ); |
52 | @EXPORT_OK = qw( |
54 | @EXPORT_OK = qw( |
53 | connect_cached |
55 | connect_cached |
54 | ); |
56 | ); |
55 | |
57 | |
… | |
… | |
143 | $dbcache{$id}; |
145 | $dbcache{$id}; |
144 | } |
146 | } |
145 | |
147 | |
146 | =item $sth = sql_exec [dbh,] [bind-vals...,] "sql-statement", [arguments...] |
148 | =item $sth = sql_exec [dbh,] [bind-vals...,] "sql-statement", [arguments...] |
147 | |
149 | |
|
|
150 | =item $sth = sql_uexec <see sql_exec> |
|
|
151 | |
148 | C<sql_exec> is the most important and most-used function in this module. |
152 | C<sql_exec> is the most important and most-used function in this module. |
149 | |
153 | |
150 | Runs the given sql command with the given parameters and returns the |
154 | Runs the given sql command with the given parameters and returns the |
151 | statement handle. The command and the statement handle will be cached |
155 | statement handle. The command and the statement handle will be cached |
152 | (with the database handle and the sql string as key), so prepare will be |
156 | (with the database handle and the sql string as key), so prepare will be |
… | |
… | |
163 | The actual return value from the C<$sth->execute> call is stored in the |
167 | The actual return value from the C<$sth->execute> call is stored in the |
164 | package-global (and exported) variable C<$sql_exec>. |
168 | package-global (and exported) variable C<$sql_exec>. |
165 | |
169 | |
166 | If any error occurs C<sql_exec> will throw an exception. |
170 | If any error occurs C<sql_exec> will throw an exception. |
167 | |
171 | |
|
|
172 | C<sql_uexec> is similar to C<sql_exec> but upgrades all input arguments to |
|
|
173 | utf8 before calling the C<execute> method. |
|
|
174 | |
168 | Examples: |
175 | Examples: |
169 | |
176 | |
170 | # easy one |
177 | # easy one |
171 | my $st = sql_exec "select name, id from table where id = ?", $id; |
178 | my $st = sql_exec "select name, id from table where id = ?", $id; |
172 | while (my ($name, $id) = $st->fetchrow_array) { ... }; |
179 | while (my ($name, $id) = $st->fetchrow_array) { ... }; |
… | |
… | |
181 | sql_exec $dbh, "update file set name = ?", "oops.txt"; |
188 | sql_exec $dbh, "update file set name = ?", "oops.txt"; |
182 | |
189 | |
183 | |
190 | |
184 | =item sql_fetch <see sql_exec> |
191 | =item sql_fetch <see sql_exec> |
185 | |
192 | |
|
|
193 | =item sql_ufetch <see sql_uexec> |
|
|
194 | |
186 | Execute a sql-statement and fetch the first row of results. Depending on |
195 | Execute an sql-statement and fetch the first row of results. Depending on |
187 | the caller context the row will be returned as a list (array context), or |
196 | the caller context the row will be returned as a list (array context), or |
188 | just the first columns. In table form: |
197 | just the first columns. In table form: |
189 | |
198 | |
190 | CONTEXT RESULT |
199 | CONTEXT RESULT |
191 | void () |
200 | void () |
… | |
… | |
202 | |
211 | |
203 | my($name, $amount) = sql_fetch "select ...", args... |
212 | my($name, $amount) = sql_fetch "select ...", args... |
204 | |
213 | |
205 | ... and it's still quite fast unless you fetch large amounts of data. |
214 | ... and it's still quite fast unless you fetch large amounts of data. |
206 | |
215 | |
|
|
216 | C<sql_ufetch> is similar to C<sql_fetch> but upgrades all input values to |
|
|
217 | utf8 and forces all result values to utf8. |
|
|
218 | |
207 | =item sql_fetchall <see sql_exec> |
219 | =item sql_fetchall <see sql_exec> |
|
|
220 | |
|
|
221 | =item sql_ufetchall <see sql_uexec> |
208 | |
222 | |
209 | Similarly to C<sql_fetch>, but all result rows will be fetched (this is |
223 | Similarly to C<sql_fetch>, but all result rows will be fetched (this is |
210 | of course inefficient for large results!). The context is ignored (only |
224 | of course inefficient for large results!). The context is ignored (only |
211 | list context makes sense), but the result still depends on the number of |
225 | list context makes sense), but the result still depends on the number of |
212 | columns in the result: |
226 | columns in the result: |
… | |
… | |
224 | |
238 | |
225 | for (sql_fetchall "select name, age, place from user") { |
239 | for (sql_fetchall "select name, age, place from user") { |
226 | my ($name, $age, $place) = @$_; |
240 | my ($name, $age, $place) = @$_; |
227 | } |
241 | } |
228 | |
242 | |
|
|
243 | C<sql_ufetchall> is similar to C<sql_fetchall> but upgrades all input |
|
|
244 | values to utf8 and forces all result values to utf8. |
|
|
245 | |
229 | =item sql_exists "<table> where ...", args... |
246 | =item sql_exists "<table> where ...", args... |
|
|
247 | |
|
|
248 | =item sql_uexists <see sql_exists> |
230 | |
249 | |
231 | Check wether the result of the sql-statement "select xxx from |
250 | Check wether the result of the sql-statement "select xxx from |
232 | $first_argument" would be empty or not (that is, imagine the string |
251 | $first_argument" would be empty or not (that is, imagine the string |
233 | "select from" were prepended to your statement (it isn't)). Should work |
252 | "select * from" were prepended to your statement (it isn't)). Should work |
234 | with every database but can be quite slow, except on mysql, where this |
253 | with every database but can be quite slow, except on mysql, where this |
235 | should be quite fast. |
254 | should be quite fast. |
|
|
255 | |
|
|
256 | C<sql_uexists> is similar to C<sql_exists> but upgrades all parameters to |
|
|
257 | utf8. |
236 | |
258 | |
237 | Examples: |
259 | Examples: |
238 | |
260 | |
239 | print "user 7 exists!\n" |
261 | print "user 7 exists!\n" |
240 | if sql_exists "user where id = ?", 7; |
262 | if sql_exists "user where id = ?", 7; |
… | |
… | |
314 | |
336 | |
315 | package PApp::SQL::Database; |
337 | package PApp::SQL::Database; |
316 | |
338 | |
317 | =head2 THE DATABASE CLASS |
339 | =head2 THE DATABASE CLASS |
318 | |
340 | |
319 | Again (sigh) the problem of persistency. What do you do when you have to serialize on object |
341 | Again (sigh) the problem of persistency. What do you do when you have |
320 | that contains (or should contain) a database handle? Short answer: you don't. Long answer: |
342 | to serialize on object that contains (or should contain) a database |
|
|
343 | handle? Short answer: you don't. Long answer: you can embed the necessary |
321 | you can embed the necessary information to recreate the dbh when needed. |
344 | information to recreate the dbh when needed. |
322 | |
345 | |
323 | The C<PApp::SQL::Database> class does that, in a relatively efficient |
346 | The C<PApp::SQL::Database> class does that, in a relatively efficient |
324 | fashion: the overhead is currently a single method call per access (you |
347 | fashion: the overhead is currently a single method call per access (you |
325 | can cache the real dbh if you want). |
348 | can cache the real dbh if you want). |
326 | |
349 | |
… | |
… | |
366 | |
389 | |
367 | =item $db->dsn |
390 | =item $db->dsn |
368 | |
391 | |
369 | Return the DSN (L<DBI>) fo the database object (e.g. for error messages). |
392 | Return the DSN (L<DBI>) fo the database object (e.g. for error messages). |
370 | |
393 | |
|
|
394 | =item $db->login |
|
|
395 | |
|
|
396 | Return the login name. |
|
|
397 | |
|
|
398 | =item $db->password |
|
|
399 | |
|
|
400 | Return the password (emphasizing the fact that the apssword is stored plaintext ;) |
|
|
401 | |
371 | =cut |
402 | =cut |
372 | |
403 | |
373 | sub dsn($) { |
404 | sub dsn($) { |
374 | my $self = shift; |
405 | my $self = shift; |
375 | (split /\x00/, $self->[0])[1]; |
406 | (split /\x00/, $self->[0])[1]; |
376 | } |
407 | } |
377 | |
408 | |
|
|
409 | sub login($) { |
|
|
410 | my $self = shift; |
|
|
411 | (split /\x00/, $self->[0])[2]; |
|
|
412 | } |
|
|
413 | |
|
|
414 | sub password($) { |
|
|
415 | my $self = shift; |
|
|
416 | (split /\x00/, $self->[0])[3]; |
|
|
417 | } |
|
|
418 | |
378 | =back |
419 | =back |
379 | |
420 | |
380 | =cut |
421 | =cut |
381 | |
422 | |
382 | 1; |
423 | 1; |