blib/lib/Queue/DBI/Admin.pm

filter: Statementstmt 93.9% 156 / 166 Branchbran 60.4% 58 / 96 Conditioncond 61.2% 19 / 31 MC/DCmcdc 40.0% 8 / 20 Subroutinesub 100% 31 / 31 Podpod 100% 18 / 18 totaltotal 80.1% 290 / 362 CC 60 SCAR 24.9
CC
60
Cov
80%
create_tables
51.9
has_tables
9.0
drop_tables
7.2
CRAP 12.0
« blib/lib/Queue/DBI.pm ↑ Summary Build »
1 package Queue::DBI::Admin;
2  
3 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 21 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 21 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 21 ▶use Data::Validate::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 21 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 21 ▶use Queue::DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 =head1 NAME
15  
16 Queue::DBI::Admin - Manage Queue::DBI queues.
17  
18  
19 =head1 VERSION
20  
21 Version 2.6.2
22  
23 =cut
24  
25 21 our $VERSION = '2.6.2';
26  
27  
28 =head1 SYNOPSIS
29  
30 use Queue::DBI::Admin;
31  
32 # Create the object which will allow managing the queues.
33 my $queues_admin = Queue::DBI::Admin->new(
34 database_handle => $dbh,
35 );
36  
37 # Check if the tables required by Queue::DBI exist.
38 if ( !$queues_admin->has_tables() )
39 {
40 # Create the tables required by Queue::DBI to store the queues and data.
41 $queues_admin->create_tables();
42 }
43  
44 # Create a new queue.
45 my $queue = $queues_admin->create_queue( $queue_name );
46  
47 # Test if a queue exists.
48 if ( $queues_admin->has_queue( $queue_name ) )
49 {
50 ...
51 }
52  
53 # Retrieve a queue.
54 my $queue = $queues_admin->retrieve_queue( $queue_name );
55  
56 # Delete a queue.
57 $queues_admin->delete_queue( $queue_name );
58  
59  
60 =head1 SUPPORTED DATABASES
61  
62 This distribution currently supports:
63  
64 =over 4
65  
66 =item * SQLite
67  
68 =item * MySQL
69  
70 =item * PostgreSQL
71  
72 =back
73  
74 Please contact me if you need support for another database type, I'm always
75 glad to add extensions if you can help me with testing.
76  
77  
78 =head1 QUEUES ADMINISTRATION METHODS
79  
80 =head2 new()
81  
82 Create a new Queue::DBI::Admin object.
83  
84 my $queues_admin = Queue::DBI::Admin->new(
85 database_handle => $database_handle,
86 );
87  
88 The 'database_handle' parameter is mandatory and must correspond to a
89 DBI connection handle object.
90  
91 Optional parameters:
92  
93 =over 4
94  
95 =item * queues_table_name
96  
97 By default, Queue::DBI uses a table named I<queues> to store the queue
98 definitions. This allows using your own name, if you want to support separate
99 queuing systems or legacy systems.
100  
101 =item * queue_elements_table_name
102  
103 By default, Queue::DBI uses a table named I<queue_elements> to store the queued
104 data. This allows using your own name, if you want to support separate queuing
105 systems or legacy systems.
106  
107 =back
108  
109 my $queues_admin = Queue::DBI::Admin->new(
110 database_handle => $database_handle,
111 queues_table_name => $custom_queues_table_name,
112 queue_elements_table_name => $custom_queue_elements_table_name,
113 );
114  
115 =cut
116  
117 sub new
118 {
119 40 ▶ my ( $class, %args ) = @_;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
120 40 my $database_handle = delete( $args{'database_handle'} );
121 40 my $queues_table_name = delete( $args{'queues_table_name'} );
122 40 my $queue_elements_table_name = delete( $args{'queue_elements_table_name'} );
123  
124 40 ▶ croak 'Unrecognized arguments: ' . join( ', ', keys %args )
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %args != 0 1 39 40
125 if scalar( keys %args ) != 0;
126  
127 # Verify arguments.
128 39 ▶ croak 'The argument "database_handle" must be a DBI connection handle object'
Branch: 1 branch
BranchTrueFalseTotal
unless Data::Validate::Type::is_instance($database_handle, "class", "DBI::db") 2 37 39
129 if !Data::Validate::Type::is_instance( $database_handle, class => 'DBI::db' );
130  
131 37 my $self = bless(
132 {
133 database_handle => $database_handle,
134 table_names =>
135 {
136 'queues' => $queues_table_name,
137 'queue_elements' => $queue_elements_table_name,
138 },
139 tables_verified => 0,
140 },
141 $class
142 );
143  
144 37 return $self;
145 }
146  
147  
148 =head2 create_queue()
149  
150 Create a new queue.
151  
152 $queues_admin->create_queue( $queue_name );
153  
154 =cut
155  
156 sub create_queue
157 {
158 2 ▶ my ( $self, $queue_name ) = @_;
Subroutine create_queue: called CC=4 SCAR=14.8
Pod documented
159 2 my $database_handle = $self->get_database_handle();
160  
161 # Verify parameters.
162 2 ▶ croak 'The first parameter must be a queue name'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $queue_name or $queue_name eq "" 0 2 2
Condition33%1/3
not defined $queue_name or $queue_name eq ""
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $queue_name or $queue_name eq ""
not defined $queue_name $queue_name eq ""
Truth table33%1/3
not(A) or B
A= not defined $queue_nameB= $queue_name eq ""
ABresult
1X1
011
000
163 if !defined( $queue_name ) || ( $queue_name eq '' );
164  
165 # Make sure the tables are correctly set up.
166 2 $self->assert_tables_verified();
167  
168 2 my $queues_table_name = $database_handle->quote_identifier(
169 $self->get_queues_table_name()
170 );
171  
172 # Create the queue.
173 2 ▶ $database_handle->do(
Branch: 1 branch
BranchTrueFalseTotal
$database_handle->do(sprintf("\n\t\t\t\tINSERT INTO %s ( name )\n\t\t\t\tVALUES ( ? )\n\t\t\t", $queues_table_name), {}, $queue_name) or croak('Cannot execute SQL: ' . $database_handle->errstr) 0 2 2
174 sprintf(
175 q|
176 INSERT INTO %s ( name )
177 VALUES ( ? )
178 |,
179 $queues_table_name,
180 ),
181 {},
182 $queue_name,
183 ) || croak 'Cannot execute SQL: ' . $database_handle->errstr();
184  
185 2 return;
186 }
187  
188  
189 =head2 has_queue()
190  
191 Test if a queue exists.
192  
193 if ( $queues_admin->has_queue( $queue_name ) )
194 {
195 ...
196 }
197  
198 =cut
199  
200 sub has_queue
201 {
202 4 ▶ my ( $self, $queue_name ) = @_;
Subroutine has_queue: called CC=3 SCAR=11.4
Pod documented
203 4 my $database_handle = $self->get_database_handle();
204  
205 # Verify parameters.
206 4 ▶ croak 'The first parameter must be a queue name'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $queue_name or $queue_name eq "" 0 4 4
Condition33%1/3
not defined $queue_name or $queue_name eq ""
l!l&&r!l&&!r
004
MC/DC0%0/2
not defined $queue_name or $queue_name eq ""
not defined $queue_name $queue_name eq ""
Truth table33%1/3
not(A) or B
A= not defined $queue_nameB= $queue_name eq ""
ABresult
1X1
011
000
207 if !defined( $queue_name ) || ( $queue_name eq '' );
208  
209 # Make sure the tables are correctly set up.
210 4 $self->assert_tables_verified();
211  
212 return try
213 {
214 4 ▶ my $queue = $self->retrieve_queue( $queue_name );
Subroutine __ANON__: called CC=2 SCAR=7.1
215  
216 2 ▶ croak 'The queue does not exist'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $queue 0 2 2
217 if !defined( $queue );
218  
219 2 return 1;
220 }
221 catch
222 {
223 2 ▶ return 0;
Subroutine __ANON__: called CC=1 SCAR=0.0
224 4 };
225 }
226  
227  
228 =head2 retrieve_queue()
229  
230 Retrieve a queue.
231  
232 my $queue = $queues_admin->retrieve_queue( $queue_name );
233  
234 # See Queue::DBI->new() for all the available options.
235 my $queue = $queues_admin->retrieve_queue(
236 $queue_name,
237 'cleanup_timeout' => 3600,
238 'verbose' => 1,
239 'max_requeue_count' => 5,
240 );
241  
242 =cut
243  
244 sub retrieve_queue
245 {
246 12 ▶ my ( $self, $queue_name, %args ) = @_;
Subroutine retrieve_queue: called CC=3 SCAR=11.6
Pod documented
247 12 my $database_handle = $self->get_database_handle();
248  
249 # Verify parameters.
250 12 ▶ croak 'The first parameter must be a queue name'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $queue_name or $queue_name eq "" 0 12 12
Condition33%1/3
not defined $queue_name or $queue_name eq ""
l!l&&r!l&&!r
0012
MC/DC0%0/2
not defined $queue_name or $queue_name eq ""
not defined $queue_name $queue_name eq ""
Truth table33%1/3
not(A) or B
A= not defined $queue_nameB= $queue_name eq ""
ABresult
1X1
011
000
251 if !defined( $queue_name ) || ( $queue_name eq '' );
252  
253 # Make sure the tables are correctly set up.
254 12 $self->assert_tables_verified();
255  
256 # Instantiate a Queue::DBI object.
257 12 my $queue = Queue::DBI->new(
258 database_handle => $database_handle,
259 queue_name => $queue_name,
260 queues_table_name => $self->get_queues_table_name(),
261 queue_elements_table_name => $self->get_queue_elements_table_name(),
262 %args
263 );
264  
265 6 return $queue;
266 }
267  
268  
269 =head2 delete_queue()
270  
271 Delete a queue and all associated data, permanently. Use this function at your
272 own risk!
273  
274 $queues_admin->delete_queue( $queue_name );
275  
276 =cut
277  
278 sub delete_queue
279 {
280 2 ▶ my ( $self, $queue_name ) = @_;
Subroutine delete_queue: called CC=5 SCAR=17.0
Pod documented
281 2 my $database_handle = $self->get_database_handle();
282  
283 # Verify parameters.
284 2 ▶ croak 'The first parameter must be a queue name'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $queue_name or $queue_name eq "" 0 2 2
Condition33%1/3
not defined $queue_name or $queue_name eq ""
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $queue_name or $queue_name eq ""
not defined $queue_name $queue_name eq ""
Truth table33%1/3
not(A) or B
A= not defined $queue_nameB= $queue_name eq ""
ABresult
1X1
011
000
285 if !defined( $queue_name ) || ( $queue_name eq '' );
286  
287 # Make sure the tables are correctly set up.
288 2 $self->assert_tables_verified();
289  
290 # Retrieve the queue object, to get the queue ID.
291 2 my $queue = $self->retrieve_queue( $queue_name );
292  
293 # Delete queue elements.
294 2 my $queue_elements_table_name = $database_handle->quote_identifier(
295 $self->get_queue_elements_table_name()
296 );
297  
298 2 ▶ $database_handle->do(
Branch: 1 branch
BranchTrueFalseTotal
$database_handle->do(sprintf("\n\t\t\t\tDELETE\n\t\t\t\tFROM %s\n\t\t\t\tWHERE queue_id = ?\n\t\t\t", $queue_elements_table_name), {}, $queue->get_queue_id) or croak('Cannot execute SQL: ' . $database_handle->errstr) 0 2 2
299 sprintf(
300 q|
301 DELETE
302 FROM %s
303 WHERE queue_id = ?
304 |,
305 $queue_elements_table_name,
306 ),
307 {},
308 $queue->get_queue_id(),
309 ) || croak 'Cannot execute SQL: ' . $database_handle->errstr();
310  
311 # Delete the queue.
312 2 my $queues_table_name = $database_handle->quote_identifier(
313 $self->get_queues_table_name()
314 );
315  
316 2 ▶ $database_handle->do(
Branch: 1 branch
BranchTrueFalseTotal
$database_handle->do(sprintf("\n\t\t\t\tDELETE\n\t\t\t\tFROM %s\n\t\t\t\tWHERE queue_id = ?\n\t\t\t", $queues_table_name), {}, $queue->get_queue_id) or croak('Cannot execute SQL: ' . $database_handle->errstr) 0 2 2
317 sprintf(
318 q|
319 DELETE
320 FROM %s
321 WHERE queue_id = ?
322 |,
323 $queues_table_name,
324 ),
325 {},
326 $queue->get_queue_id(),
327 ) || croak 'Cannot execute SQL: ' . $database_handle->errstr();
328  
329 2 return;
330 }
331  
332  
333 =head1 DATABASE SETUP METHODS
334  
335 =head2 has_tables()
336  
337 Determine if the tables required for L<Queue::DBI> to operate exist.
338  
339 my $tables_exist = $queues_admin->has_tables();
340  
341 This method returns 1 if all tables exist, 0 if none exist, and croaks with
342 more information if some tables are missing or if the mandatory fields on some
343 of the tables are missing.
344  
345 =cut
346  
347 sub has_tables
348 {
349 18 ▶ my ( $self ) = @_;
Subroutine has_tables: called CC=9 SCAR=22.0
Pod documented
350 18 my $database_handle = $self->get_database_handle();
351  
352 # Check the database type.
353 18 $self->assert_database_type_supported();
354  
355 # Check if the queues table exists.
356 18 my $queues_table_exists = $self->has_table( 'queues' );
357  
358 # Check if the queue elements table exists.
359 18 my $queue_elements_table_exists = $self->has_table( 'queue_elements' );
360  
361 # If both tables don't exist, return 0.
362 18 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless $queues_table_exists or $queue_elements_table_exists 4 14 18
Condition100%3/3
$queues_table_exists or $queue_elements_table_exists
l!l&&r!l&&!r
1314
MC/DC100%2/2
$queues_table_exists or $queue_elements_table_exists
$queues_table_exists $queue_elements_table_exists
Truth table100%3/3
A or B
A= $queues_table_existsB= $queue_elements_table_exists
ABresult
1X1
011
000
363 if !$queues_table_exists && !$queue_elements_table_exists;
364  
365 # If one of the tables is missing, we want the user to know that there is
366 # a problem to fix and that create_table() won't work.
367 14 ▶ croak "The table '" . $self->get_queues_table_name() . "' exists, but '" . $self->get_queue_elements_table_name() . "' is missing"
Branch: 1 branch
BranchTrueFalseTotal
if $queues_table_exists and not $queue_elements_table_exists 1 13 14
Condition100%3/3
$queues_table_exists and not $queue_elements_table_exists
!ll&&!rl&&r
1121
MC/DC100%2/2
$queues_table_exists and not $queue_elements_table_exists
$queues_table_exists not $queue_elements_table_exists
Truth table100%3/3
A and not(B)
A= $queues_table_existsB= not $queue_elements_table_exists
ABresult
0X0
100
111
368 if $queues_table_exists && !$queue_elements_table_exists;
369 13 ▶ croak "The table '" . $self->get_queue_elements_table_name() . "' exists, but '" . $self->get_queues_table_name() . "' is missing"
Branch: 1 branch
BranchTrueFalseTotal
if not $queues_table_exists and $queue_elements_table_exists 1 12 13
Condition66%2/3
not $queues_table_exists and $queue_elements_table_exists
!ll&&!rl&&r
1201
MC/DC50%1/2
not $queues_table_exists and $queue_elements_table_exists
not $queues_table_exists $queue_elements_table_exists
Truth table66%2/3
not(A) and B
A= not $queues_table_existsB= $queue_elements_table_exists
ABresult
0X0
100
111
370 if !$queues_table_exists && $queue_elements_table_exists;
371  
372 # Check if the queues table has the mandatory fields.
373 12 my $queues_table_has_fields = $self->has_mandatory_fields( 'queues' );
374 12 ▶ croak "The table '" . $self->get_queues_table_name() . "' exists, but is missing mandatory fields"
Branch: 1 branch
BranchTrueFalseTotal
unless $queues_table_has_fields 1 11 12
375 if !$queues_table_has_fields;
376  
377 # Check if the queue elements table has the mandatory fields.
378 11 my $queue_elements_table_has_fields = $self->has_mandatory_fields( 'queue_elements' );
379 11 ▶ croak "The table '" . $self->get_queue_elements_table_name() . "' exists, but is missing mandatory fields"
Branch: 1 branch
BranchTrueFalseTotal
unless $queue_elements_table_has_fields 1 10 11
380 if !$queue_elements_table_has_fields;
381  
382 # Both tables exist and have the mandatory fields for Queue::DBI to
383 # work, we can safely return 1.
384 10 return 1;
385 }
386  
387  
388 =head2 create_tables()
389  
390 Create the tables required by L<Queue::DBI> to store the queues and data.
391  
392 $queues_admin->create_tables(
393 drop_if_exist => $boolean,
394 );
395  
396 By default, it won't drop any table but you can force that by setting
397 'drop_if_exist' to 1. See C<drop_tables()> for more information on how tables
398 are dropped.
399  
400 =cut
401  
402 sub create_tables
403 {
404 3 ▶ my ( $self, %args ) = @_;
Subroutine create_tables: called CC=15 SCAR=39.5
Pod documented
405 3 ▶ my $drop_if_exist = delete( $args{'drop_if_exist'} ) || 0;
Condition100%2/2
delete $args{'drop_if_exist'} || 0
l!l
12
MC/DC100%1/1
delete $args{'drop_if_exist'} || 0
delete $args{'drop_if_exist'}
Truth table100%2/2
A
A= delete $args{'drop_if_exist'}
Aresult
11
00
406 3 ▶ croak 'Unrecognized arguments: ' . join( ', ', keys %args )
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %args != 0 0 3 3
407 if scalar( keys %args ) != 0;
408  
409 3 my $database_handle = $self->get_database_handle();
410  
411 # Check the database type.
412 3 my $database_type = $self->assert_database_type_supported();
413  
414 # Prepare the name of the tables.
415 3 my $queues_table_name = $self->get_queues_table_name();
416 3 my $quoted_queues_table_name = $self->get_quoted_queues_table_name();
417  
418 3 my $queue_elements_table_name = $self->get_queue_elements_table_name();
419 3 my $quoted_queue_elements_table_name = $self->get_quoted_queue_elements_table_name();
420  
421 # Drop the tables, if requested.
422 3 ▶ $self->drop_tables()
Branch: 1 branch
BranchTrueFalseTotal
if $drop_if_exist 1 2 3
423 if $drop_if_exist;
424  
425 # Create the list of queues.
426 3 ▶ if ( $database_type eq 'SQLite' )
Branch: 2 branches
BranchTrueFalseTotal
if ($database_type eq 'SQLite') { } 3 0 3
elsif ($database_type eq 'Pg') { } 0 0 0
427 {
428 3 ▶ $database_handle->do(
Branch: 1 branch
BranchTrueFalseTotal
$database_handle->do(sprintf("\n\t\t\t\t\tCREATE TABLE %s\n\t\t\t\t\t(\n\t\t\t\t\t\tqueue_id INTEGER PRIMARY KEY AUTOINCREMENT,\n\t\t\t\t\t\tname VARCHAR(255) NOT NULL UNIQUE\n\t\t\t\t\t)\n\t\t\t\t", $quoted_queues_table_name)) or croak('Cannot execute SQL: ' . $database_handle->errstr) 0 3 3
429 sprintf(
430 q|
431 CREATE TABLE %s
432 (
433 queue_id INTEGER PRIMARY KEY AUTOINCREMENT,
434 name VARCHAR(255) NOT NULL UNIQUE
435 )
436 |,
437 $quoted_queues_table_name,
438 )
439 ) || croak 'Cannot execute SQL: ' . $database_handle->errstr();
440 }
441 elsif ( $database_type eq 'Pg' )
442 {
443 0 my $unique_index_name = $database_handle->quote_identifier(
444 'unq_' . $queues_table_name . '_name',
445 );
446  
447 0 ▶ $database_handle->do(
Branch: 1 branch
BranchTrueFalseTotal
$database_handle->do(sprintf("\n\t\t\t\t\tCREATE TABLE %s\n\t\t\t\t\t(\n\t\t\t\t\t\tqueue_id SERIAL,\n\t\t\t\t\t\tname VARCHAR(255) NOT NULL,\n\t\t\t\t\t\tPRIMARY KEY (queue_id),\n\t\t\t\t\t\tCONSTRAINT %s UNIQUE (name)\n\t\t\t\t\t)\n\t\t\t\t", $quoted_queues_table_name, $unique_index_name)) or croak('Cannot execute SQL: ' . $database_handle->errstr) 0 0 0
448 sprintf(
449 q|
450 CREATE TABLE %s
451 (
452 queue_id SERIAL,
453 name VARCHAR(255) NOT NULL,
454 PRIMARY KEY (queue_id),
455 CONSTRAINT %s UNIQUE (name)
456 )
457 |,
458 $quoted_queues_table_name,
459 $unique_index_name,
460 )
461 ) || croak 'Cannot execute SQL: ' . $database_handle->errstr();
462 }
463 else
464 {
465 0 my $unique_index_name = $database_handle->quote_identifier(
466 'unq_' . $queues_table_name . '_name',
467 );
468  
469 0 ▶ $database_handle->do(
Branch: 1 branch
BranchTrueFalseTotal
$database_handle->do(sprintf("\n\t\t\t\t\tCREATE TABLE %s\n\t\t\t\t\t(\n\t\t\t\t\t\tqueue_id INT(11) NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t\tname VARCHAR(255) NOT NULL,\n\t\t\t\t\t\tPRIMARY KEY (queue_id),\n\t\t\t\t\t\tUNIQUE KEY %s (name)\n\t\t\t\t\t)\n\t\t\t\t\tENGINE=InnoDB\n\t\t\t\t", $quoted_queues_table_name, $unique_index_name)) or croak('Cannot execute SQL: ' . $database_handle->errstr) 0 0 0
470 sprintf(
471 q|
472 CREATE TABLE %s
473 (
474 queue_id INT(11) NOT NULL AUTO_INCREMENT,
475 name VARCHAR(255) NOT NULL,
476 PRIMARY KEY (queue_id),
477 UNIQUE KEY %s (name)
478 )
479 ENGINE=InnoDB
480 |,
481 $quoted_queues_table_name,
482 $unique_index_name,
483 )
484 ) || croak 'Cannot execute SQL: ' . $database_handle->errstr();
485 }
486  
487 # Create the table that will hold the queue elements.
488 3 ▶ if ( $database_type eq 'SQLite' )
Branch: 2 branches
BranchTrueFalseTotal
if ($database_type eq 'SQLite') { } 3 0 3
elsif ($database_type eq 'Pg') { } 0 0 0
489 {
490 3 ▶ $database_handle->do(
Branch: 1 branch
BranchTrueFalseTotal
$database_handle->do(sprintf("\n\t\t\t\t\tCREATE TABLE %s\n\t\t\t\t\t(\n\t\t\t\t\t\tqueue_element_id INTEGER PRIMARY KEY AUTOINCREMENT,\n\t\t\t\t\t\tqueue_id INTEGER NOT NULL,\n\t\t\t\t\t\tdata TEXT,\n\t\t\t\t\t\tlock_time INT(10) DEFAULT NULL,\n\t\t\t\t\t\trequeue_count INT(3) DEFAULT '0',\n\t\t\t\t\t\tcreated INT(10) NOT NULL DEFAULT '0'\n\t\t\t\t\t)\n\t\t\t\t", $quoted_queue_elements_table_name)) or croak('Cannot execute SQL: ' . $database_handle->errstr) 0 3 3
491 sprintf(
492 q|
493 CREATE TABLE %s
494 (
495 queue_element_id INTEGER PRIMARY KEY AUTOINCREMENT,
496 queue_id INTEGER NOT NULL,
497 data TEXT,
498 lock_time INT(10) DEFAULT NULL,
499 requeue_count INT(3) DEFAULT '0',
500 created INT(10) NOT NULL DEFAULT '0'
501 )
502 |,
503 $quoted_queue_elements_table_name,
504 )
505 ) || croak 'Cannot execute SQL: ' . $database_handle->errstr();
506 }
507 elsif ( $database_type eq 'Pg' )
508 {
509 0 ▶ $database_handle->do(
Branch: 1 branch
BranchTrueFalseTotal
$database_handle->do(sprintf("\n\t\t\t\t\tCREATE TABLE %s\n\t\t\t\t\t(\n\t\t\t\t\t\tqueue_element_id SERIAL,\n\t\t\t\t\t\tqueue_id INTEGER NOT NULL REFERENCES %s (queue_id),\n\t\t\t\t\t\tdata TEXT,\n\t\t\t\t\t\tlock_time INTEGER DEFAULT NULL,\n\t\t\t\t\t\trequeue_count SMALLINT DEFAULT 0,\n\t\t\t\t\t\tcreated INTEGER NOT NULL DEFAULT 0,\n\t\t\t\t\t\tPRIMARY KEY (queue_element_id)\n\t\t\t\t\t)\n\t\t\t\t", $quoted_queue_elements_table_name, $quoted_queues_table_name)) or croak('Cannot execute SQL: ' . $database_handle->errstr) 0 0 0
510 sprintf(
511 q|
512 CREATE TABLE %s
513 (
514 queue_element_id SERIAL,
515 queue_id INTEGER NOT NULL REFERENCES %s (queue_id),
516 data TEXT,
517 lock_time INTEGER DEFAULT NULL,
518 requeue_count SMALLINT DEFAULT 0,
519 created INTEGER NOT NULL DEFAULT 0,
520 PRIMARY KEY (queue_element_id)
521 )
522 |,
523 $quoted_queue_elements_table_name,
524 $quoted_queues_table_name,
525 )
526 ) || croak 'Cannot execute SQL: ' . $database_handle->errstr();
527  
528 0 my $queue_id_index_name = $database_handle->quote_identifier(
529 'idx_' . $queue_elements_table_name . '_queue_id'
530 );
531  
532 0 ▶ $database_handle->do(
Branch: 1 branch
BranchTrueFalseTotal
$database_handle->do(sprintf("\n\t\t\t\t\tCREATE INDEX %s\n\t\t\t\t\tON %s (queue_id)\n\t\t\t\t", $queue_id_index_name, $quoted_queue_elements_table_name)) or croak('Cannot execute SQL: ' . $database_handle->errstr) 0 0 0
533 sprintf(
534 q|
535 CREATE INDEX %s
536 ON %s (queue_id)
537 |,
538 $queue_id_index_name,
539 $quoted_queue_elements_table_name,
540 )
541 ) || croak 'Cannot execute SQL: ' . $database_handle->errstr();
542 }
543 else
544 {
545 0 my $queue_id_index_name = $database_handle->quote_identifier(
546 'idx_' . $queue_elements_table_name . '_queue_id'
547 );
548 0 my $queue_id_foreign_key_name = $database_handle->quote_identifier(
549 'fk_' . $queue_elements_table_name . '_queue_id'
550 );
551  
552 0 ▶ $database_handle->do(
Branch: 1 branch
BranchTrueFalseTotal
$database_handle->do(sprintf("\n\t\t\t\t\tCREATE TABLE %s\n\t\t\t\t\t(\n\t\t\t\t\t\tqueue_element_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t\tqueue_id INT(11) NOT NULL,\n\t\t\t\t\t\tdata TEXT,\n\t\t\t\t\t\tlock_time INT(10) UNSIGNED DEFAULT NULL,\n\t\t\t\t\t\trequeue_count INT(3) UNSIGNED DEFAULT '0',\n\t\t\t\t\t\tcreated INT(10) UNSIGNED NOT NULL DEFAULT '0',\n\t\t\t\t\t\tPRIMARY KEY (queue_element_id),\n\t\t\t\t\t\tKEY %s (queue_id),\n\t\t\t\t\t\tCONSTRAINT %s FOREIGN KEY (queue_id) REFERENCES %s (queue_id)\n\t\t\t\t\t)\n\t\t\t\t\tENGINE=InnoDB\n\t\t\t\t", $quoted_queue_elements_table_name, $queue_id_index_name, $queue_id_foreign_key_name, $quoted_queues_table_name)) or croak('Cannot execute SQL: ' . $database_handle->errstr) 0 0 0
553 sprintf(
554 q|
555 CREATE TABLE %s
556 (
557 queue_element_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
558 queue_id INT(11) NOT NULL,
559 data TEXT,
560 lock_time INT(10) UNSIGNED DEFAULT NULL,
561 requeue_count INT(3) UNSIGNED DEFAULT '0',
562 created INT(10) UNSIGNED NOT NULL DEFAULT '0',
563 PRIMARY KEY (queue_element_id),
564 KEY %s (queue_id),
565 CONSTRAINT %s FOREIGN KEY (queue_id) REFERENCES %s (queue_id)
566 )
567 ENGINE=InnoDB
568 |,
569 $quoted_queue_elements_table_name,
570 $queue_id_index_name,
571 $queue_id_foreign_key_name,
572 $quoted_queues_table_name,
573 )
574 ) || croak 'Cannot execute SQL: ' . $database_handle->errstr();
575 }
576  
577 3 return;
578 }
579  
580  
581 =head2 drop_tables()
582  
583 Drop the tables used to store the queues and queue data.
584  
585 Warning: there is no undo for this operation. Make sure you really want to drop
586 the tables before using this method.
587  
588 $queues_admin->drop_tables();
589  
590 Note: due to foreign key constraints, the tables are dropped in the reverse
591 order in which they are created.
592  
593 =cut
594  
595 sub drop_tables
596 {
597 3 ▶ my ( $self ) = @_;
Subroutine drop_tables: called CC=7 SCAR=19.7
Pod documented
598 3 my $database_handle = $self->get_database_handle();
599  
600 # Check the database type.
601 3 $self->assert_database_type_supported();
602  
603 # If the tables exist, make sure that they have the mandatory fields. This
604 # prevents a user from deleting a random table using this function.
605 3 ▶ if ( $self->has_table( 'queues' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_table("queues")) 2 1 3
606 {
607 2 my $queues_table_name = $self->get_queues_table_name();
608 2 ▶ croak "The table '$queues_table_name' is missing some or all mandatory fields, so we cannot safely determine that it is used by Queue::DBI and delete it"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_mandatory_fields("queues") 0 2 2
609 if !$self->has_mandatory_fields( 'queues' );
610 }
611 3 ▶ if ( $self->has_table( 'queue_elements' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_table("queue_elements")) 2 1 3
612 {
613 2 my $queue_elements_table_name = $self->get_queue_elements_table_name();
614 2 ▶ croak "The table '$queue_elements_table_name' is missing some or all mandatory fields, so we cannot safely determine that it is used by Queue::DBI and delete it"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_mandatory_fields("queue_elements") 0 2 2
615 if !$self->has_mandatory_fields( 'queue_elements' );
616 }
617  
618 # Prepare the name of the tables.
619 3 my $quoted_queues_table_name = $self->get_quoted_queues_table_name();
620 3 my $quoted_queue_elements_table_name = $self->get_quoted_queue_elements_table_name();
621  
622 # Drop the tables.
623 # Note: due to foreign key constraints, we need to drop the tables in the
624 # reverse order in which they are created.
625 3 ▶ $database_handle->do(
Branch: 1 branch
BranchTrueFalseTotal
$database_handle->do(sprintf('DROP TABLE IF EXISTS %s', $quoted_queue_elements_table_name)) or croak('Cannot execute SQL: ' . $database_handle->errstr) 0 3 3
626 sprintf(
627 q|DROP TABLE IF EXISTS %s|,
628 $quoted_queue_elements_table_name,
629 )
630 ) || croak 'Cannot execute SQL: ' . $database_handle->errstr();
631  
632 3 ▶ $database_handle->do(
Branch: 1 branch
BranchTrueFalseTotal
$database_handle->do(sprintf('DROP TABLE IF EXISTS %s', $quoted_queues_table_name)) or croak('Cannot execute SQL: ' . $database_handle->errstr) 0 3 3
633 sprintf(
634 q|DROP TABLE IF EXISTS %s|,
635 $quoted_queues_table_name,
636 )
637 ) || croak 'Cannot execute SQL: ' . $database_handle->errstr();
638  
639 3 return;
640 }
641  
642  
643 =head1 INTERNAL METHODS
644  
645 =head2 get_database_handle()
646  
647 Return the database handle associated with the L<Queue::DBI::Admin> object.
648  
649 my $database_handle = $queue->get_database_handle();
650  
651 =cut
652  
653 sub get_database_handle
654 {
655 245 ▶ my ( $self ) = @_;
Subroutine get_database_handle: called CC=1 SCAR=0.0
Pod documented
656  
657 245 return $self->{'database_handle'};
658 }
659  
660  
661 =head2 get_queues_table_name()
662  
663 Return the name of the table used to store queue definitions.
664  
665 my $queues_table_name = $queues_admin->get_queues_table_name();
666  
667 =cut
668  
669 sub get_queues_table_name
670 {
671 76 ▶ my ( $self ) = @_;
Subroutine get_queues_table_name: called CC=3 SCAR=11.1
Pod documented
672  
673 return defined( $self->{'table_names'}->{'queues'} ) && ( $self->{'table_names'}->{'queues'} ne '' )
674 76 ▶ ? $self->{'table_names'}->{'queues'}
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'table_names'}{'queues'} && $self->{'table_names'}{'queues'} ne '' ? : 43 33 76
Condition66%2/3
defined $self->{"table_names"}{"queues"} and $self->{"table_names"}{"queues"} ne ""
!ll&&!rl&&r
33043
MC/DC50%1/2
defined $self->{"table_names"}{"queues"} and $self->{"table_names"}{"queues"} ne ""
defined $self->{"table_names"}{"queues"} $self->{"table_names"}{"queues"} ne ""
Truth table66%2/3
A and B
A= defined $self->{"table_names"}{"queues"}B= $self->{"table_names"}{"queues"} ne ""
ABresult
0X0
100
111
675 : $Queue::DBI::DEFAULT_QUEUES_TABLE_NAME;
676 }
677  
678  
679 =head2 get_queue_elements_table_name()
680  
681 Return the name of the table used to store queue elements.
682  
683 my $queue_elements_table_name = $queues_admin->get_queue_elements_table_name();
684  
685 =cut
686  
687 sub get_queue_elements_table_name
688 {
689 71 ▶ my ( $self ) = @_;
Subroutine get_queue_elements_table_name: called CC=3 SCAR=11.1
Pod documented
690  
691 return defined( $self->{'table_names'}->{'queue_elements'} ) && ( $self->{'table_names'}->{'queue_elements'} ne '' )
692 71 ▶ ? $self->{'table_names'}->{'queue_elements'}
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'table_names'}{'queue_elements'} && $self->{'table_names'}{'queue_elements'} ne '' ? : 40 31 71
Condition66%2/3
defined $self->{"table_names"}{"queue_elements"} and $self->{"table_names"}{"queue_elements"} ne ""
!ll&&!rl&&r
31040
MC/DC50%1/2
defined $self->{"table_names"}{"queue_elements"} and $self->{"table_names"}{"queue_elements"} ne ""
defined $self->{"table_names"}{"queue_elements"} $self->{"table_names"}{"queue_elements"} ne ""
Truth table66%2/3
A and B
A= defined $self->{"table_names"}{"queue_elements"}B= $self->{"table_names"}{"queue_elements"} ne ""
ABresult
0X0
100
111
693 : $Queue::DBI::DEFAULT_QUEUE_ELEMENTS_TABLE_NAME;
694 }
695  
696  
697 =head2 get_quoted_queues_table_name()
698  
699 Return the name of the table used to store queue definitions, quoted for
700 inclusion in SQL statements.
701  
702 my $quoted_queues_table_name = $queues_admin->get_quoted_queues_table_name();
703  
704  
705 =cut
706  
707 sub get_quoted_queues_table_name
708 {
709 48 ▶ my ( $self ) = @_;
Subroutine get_quoted_queues_table_name: called CC=2 SCAR=7.0
Pod documented
710  
711 48 my $database_handle = $self->get_database_handle();
712 48 my $queues_table_name = $self->get_queues_table_name();
713  
714 48 ▶ return defined( $queues_table_name )
Branch: 1 branch
BranchTrueFalseTotal
defined $queues_table_name ? : 48 0 48
715 ? $database_handle->quote_identifier( $queues_table_name )
716 : undef;
717 }
718  
719  
720 =head2 get_quoted_queue_elements_table_name()
721  
722 Return the name of the table used to store queue elements, quoted for inclusion
723 in SQL statements.
724  
725 my $quoted_queue_elements_table_name = $queues_admin->get_quoted_queue_elements_table_name();
726  
727 =cut
728  
729 sub get_quoted_queue_elements_table_name
730 {
731 47 ▶ my ( $self ) = @_;
Subroutine get_quoted_queue_elements_table_name: called CC=2 SCAR=7.0
Pod documented
732  
733 47 my $database_handle = $self->get_database_handle();
734 47 my $queue_elements_table_name = $self->get_queue_elements_table_name();
735  
736 47 ▶ return defined( $queue_elements_table_name )
Branch: 1 branch
BranchTrueFalseTotal
defined $queue_elements_table_name ? : 47 0 47
737 ? $database_handle->quote_identifier( $queue_elements_table_name )
738 : undef;
739 }
740  
741  
742 =head2 assert_database_type_supported()
743  
744 Assert (i.e., die on failure) whether the database type specified by the
745 database handle passed to C<new()> is supported or not.
746  
747 my $database_type = $queues_admin->assert_database_type_supported();
748  
749 Note: the type of the database handle associated with the current object is
750 returned when it is supported.
751  
752 =cut
753  
754 sub assert_database_type_supported
755 {
756 25 ▶ my ( $self ) = @_;
Subroutine assert_database_type_supported: called CC=2 SCAR=7.0
Pod documented
757  
758 # Check the database type.
759 25 my $database_type = $self->get_database_type();
760 25 ▶ croak "This database type ($database_type) is not supported yet, please email the maintainer of the module for help"
Branch: 1 branch
BranchTrueFalseTotal
unless $database_type =~ /^(?:SQLite|MySQL|Pg)$/xi 0 25 25
761 if $database_type !~ m/^(?:SQLite|MySQL|Pg)$/ix;
762  
763 25 return $database_type;
764 }
765  
766  
767 =head2 get_database_type()
768  
769 Return the database type corresponding to the database handle associated
770 with the L<Queue::DBI::Admin> object.
771  
772 my $database_type = $queues_admin->get_database_type();
773  
774 =cut
775  
776 sub get_database_type
777 {
778 26 ▶ my ( $self ) = @_;
Subroutine get_database_type: called CC=2 SCAR=7.1
Pod documented
779  
780 26 my $database_handle = $self->get_database_handle();
781  
782 26 ▶ return $database_handle->{'Driver'}->{'Name'} || '';
Condition50%1/2
$database_handle->{'Driver'}{'Name'} || ''
l!l
260
MC/DC0%0/1
$database_handle->{'Driver'}{'Name'} || ''
$database_handle->{'Driver'}{'Name'}
Truth table50%1/2
A
A= $database_handle->{'Driver'}{'Name'}
Aresult
11
00
783 }
784  
785  
786 =head2 has_table()
787  
788 Return if a table required by L<Queue::DBI> to operate exists.
789  
790 my $has_table = $queues_admin->has_table( $table_type );
791  
792 Valid table types are:
793  
794 =over 4
795  
796 =item * 'queues'
797  
798 =item * 'queue_elements'
799  
800 =back
801  
802 =cut
803  
804 sub has_table
805 {
806 51 ▶ my ( $self, $table_type ) = @_;
Subroutine has_table: called CC=4 SCAR=13.9
Pod documented
807  
808 # Check the table type.
809 51 ▶ croak 'A table type must be specified'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table_type 1 50 51
810 if !defined( $table_type );
811 50 ▶ croak "The table type '$table_type' is not valid"
Branch: 1 branch
BranchTrueFalseTotal
unless $table_type =~ /\A(?:queues|queue_elements)\Z/x 0 50 50
812 if $table_type !~ /\A(?:queues|queue_elements)\Z/x;
813  
814 # Retrieve the table name.
815 50 ▶ my $table_name = $table_type eq 'queues'
Branch: 1 branch
BranchTrueFalseTotal
$table_type eq 'queues' ? : 25 25 50
816 ? $self->get_quoted_queues_table_name()
817 : $self->get_quoted_queue_elements_table_name();
818  
819 # Check if the table exists.
820 50 my $database_handle = $self->get_database_handle();
821 my $table_exists =
822 try
823 {
824 # Disable printing errors out since we expect the statement to fail.
825 50 ▶ local $database_handle->{'PrintError'} = 0;
Subroutine __ANON__: called CC=1 SCAR=0.0
826 50 local $database_handle->{'RaiseError'} = 1;
827  
828 50 $database_handle->selectrow_array(
829 sprintf(
830 q|
831 SELECT *
832 FROM %s
833 |,
834 $table_name,
835 )
836 );
837  
838 35 return 1;
839 }
840 catch
841 {
842 15 ▶ return 0;
Subroutine __ANON__: called CC=1 SCAR=0.0
843 50 };
844  
845 50 return $table_exists;
846 }
847  
848  
849 =head2 has_mandatory_fields()
850  
851 Return if a table required by L<Queue::DBI> has the mandatory fields.
852  
853 my $has_mandatory_fields = $queues_admin->has_mandatory_fields( $table_type );
854  
855 Valid table types are:
856  
857 =over 4
858  
859 =item * 'queues'
860  
861 =item * 'queue_elements'
862  
863 =back
864  
865 =cut
866  
867 sub has_mandatory_fields
868 {
869 29 ▶ my ( $self, $table_type ) = @_;
Subroutine has_mandatory_fields: called CC=5 SCAR=16.1
Pod documented
870  
871 # Check the table type.
872 29 ▶ croak 'A table type must be specified'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table_type 0 29 29
873 if !defined( $table_type );
874 29 ▶ croak "The table type '$table_type' is not valid"
Branch: 1 branch
BranchTrueFalseTotal
unless $table_type =~ /\A(?:queues|queue_elements)\Z/x 0 29 29
875 if $table_type !~ /\A(?:queues|queue_elements)\Z/x;
876  
877 # Retrieve the table name.
878 29 ▶ my $table_name = $table_type eq 'queues'
Branch: 1 branch
BranchTrueFalseTotal
$table_type eq 'queues' ? : 15 14 29
879 ? $self->get_quoted_queues_table_name()
880 : $self->get_quoted_queue_elements_table_name();
881  
882 # Retrieve the list of fields to check for.
883 29 ▶ my $mandatory_fields = $table_type eq 'queues'
Branch: 1 branch
BranchTrueFalseTotal
$table_type eq 'queues' ? : 15 14 29
884 ? 'queue_id, name'
885 : 'queue_element_id, queue_id, data, lock_time, requeue_count, created';
886  
887 # Check if the fields exist.
888 29 my $database_handle = $self->get_database_handle();
889 my $has_mandatory_fields =
890 try
891 {
892 # Disable printing errors out since we expect the statement to fail.
893 29 ▶ local $database_handle->{'PrintError'} = 0;
Subroutine __ANON__: called CC=1 SCAR=0.0
894 29 local $database_handle->{'RaiseError'} = 1;
895  
896 29 $database_handle->selectrow_array(
897 sprintf(
898 q|
899 SELECT %s
900 FROM %s
901 |,
902 $mandatory_fields,
903 $table_name,
904 )
905 );
906  
907 25 return 1;
908 }
909 catch
910 {
911 4 ▶ return 0;
Subroutine __ANON__: called CC=1 SCAR=0.0
912 29 };
913  
914 29 return $has_mandatory_fields;
915 }
916  
917  
918 =head2 assert_tables_verified()
919  
920 Assert that the tables exist and are defined correctly.
921  
922 $queues_admin->assert_tables_verified();
923  
924 Note that this will perform the check only once per L<Queue::DBI::Admin>
925 object, as this is an expensive check that would otherwise slow down the
926 methods that use it.
927  
928 =cut
929  
930 sub assert_tables_verified
931 {
932 20 ▶ my ( $self ) = @_;
Subroutine assert_tables_verified: called CC=3 SCAR=11.0
Pod documented
933  
934 20 ▶ return if $self->{'tables_verified'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tables_verified"} 12 8 20
935  
936 # If some tables are incorrectly set up, has_tables() will croak here.
937 # It however also returns 0 if no tables are defined, and we need to
938 # turn it into a croak here.
939 8 ▶ $self->has_tables()
Branch: 1 branch
BranchTrueFalseTotal
$self->has_tables or croak("The queues and queue elements tables need to be created, see Queue::DBI::Admin->create_tables()") 0 8 8
940 || croak 'The queues and queue elements tables need to be created, see Queue::DBI::Admin->create_tables()';
941  
942 8 $self->{'tables_verified'} = 1;
943  
944 8 return;
945 }
946  
947  
948 =head1 BUGS
949  
950 Please report any bugs or feature requests through the web interface at
951 L<https://github.com/guillaumeaubert/Queue-DBI/issues/new>.
952 I will be notified, and then you'll automatically be notified of progress on
953 your bug as I make changes.
954  
955  
956 =head1 SUPPORT
957  
958 You can find documentation for this module with the perldoc command.
959  
960 perldoc Queue::DBI::Admin
961  
962  
963 You can also look for information at:
964  
965 =over 4
966  
967 =item * GitHub's request tracker
968  
969 L<https://github.com/guillaumeaubert/Queue-DBI/issues>
970  
971 =item * AnnoCPAN: Annotated CPAN documentation
972  
973 L<http://annocpan.org/dist/Queue-DBI>
974  
975 =item * CPAN Ratings
976  
977 L<http://cpanratings.perl.org/d/Queue-DBI>
978  
979 =item * MetaCPAN
980  
981 L<https://metacpan.org/release/Queue-DBI>
982  
983 =back
984  
985  
986 =head1 AUTHOR
987  
988 L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
989 C<< <aubertg at cpan.org> >>.
990  
991  
992 =head1 ACKNOWLEDGEMENTS
993  
994 Thanks to Sergey Bond for suggesting this administration module to extend
995 and complete the features offered by L<Queue::DBI>.
996  
997  
998 =head1 COPYRIGHT & LICENSE
999  
1000 Copyright 2009-2015 Guillaume Aubert.
1001  
1002 This program is free software: you can redistribute it and/or modify it under
1003 the terms of the GNU General Public License version 3 as published by the Free
1004 Software Foundation.
1005  
1006 This program is distributed in the hope that it will be useful, but WITHOUT ANY
1007 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
1008 PARTICULAR PURPOSE. See the GNU General Public License for more details.
1009  
1010 You should have received a copy of the GNU General Public License along with
1011 this program. If not, see http://www.gnu.org/licenses/
1012  
1013 =cut
1014  
1015 21 1;
« blib/lib/Queue/DBI.pm ↑ Summary Build »