QueryByName/FromXML.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« QueryByName/DbhPool.pm ↑ Summary QueryByName/SthPool.pm »
1 package DBIx::QueryByName::FromXML;
2 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 0 ▶use XML::Parser;
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use XML::SimpleObject;
7 use Data::Dumper;
8 use DBIx::QueryByName::Logger qw(get_logger);
9  
10 my $PARSER = XML::Parser->new( ErrorContext => 2, Style => 'Tree' );
11  
12 sub load {
13 my $qpool = shift;
14 my $session = shift;
15 my $xml = shift;
16 my $log = get_logger();
17  
18 $log->logcroak("undefined query pool") if (!defined $qpool);
19 $log->logcroak("undefined session") if (!defined $session);
20 $log->logcroak("undefined xml string") if (!defined $xml);
21  
22 my @queries;
23 eval {
24 @queries = XML::SimpleObject->new( $PARSER->parse($xml) )->child('queries')->children('query');
25 };
26  
27 $log->logcroak("failed to parse xml: $@")
28 if (defined $@ && $@ ne '');
29  
30 $log->logcroak("invalid xml: no <query> nodes (session_name => $session)")
31 if (scalar @queries == 1 and ref $queries[0] ne 'XML::SimpleObject');
32  
33 foreach my $query ( @queries ) {
34  
35 my $name = $query->attribute('name');
36 $log->logcroak("invalid xml: no name attribute in query node (session_name => $session)")
37 if (!defined $name);
38  
39 my $params = $query->attribute('params');
40 $log->logcroak("invalid xml: no params attribute in query node (query_name => $name, session_name => $session)")
41 if (!defined $params);
42  
43 my @params = map { $_ =~ s/^\s*//; $_ =~ s/\s*$//; $_ } split(q{,}, $params);
44 # my @params = split(q{,}, $params);
45  
46 my $result = $query->attribute('result') || 'sth';
47  
48 # The retry attribute controls how to handle network problems.
49 # We will always attempt to reconnect to the database if we lose connection.
50 # The "retry" attribute controls if we should attempt to execute the query again,
51 # if we have reasons to believe it was not executed when we last tried,
52 # such as a interrupted network call.
53 #
54 # safe : execute again if there is no risk it has already been executed.
55 # never : do not execute again
56 # always : execute again, even if it might already have been executed
57 my $retry = $query->attribute('retry') || 'safe';
58 if ($retry !~ m/^(safe|never|always)$/) {
59 $log->logcroak("invalid value of retry attribute: $retry");
60 }
61  
62 if ( $qpool->knows_query($name) ) {
63 # query is already imported, possibly from other XML file
64 $log->logcroak("query already imported (query_name => $name, session_name => $session)");
65 }
66  
67 # TODO: we might want to perform minimum sanity check on query string
68 $qpool->add_query(
69 name => $name,
70 sql => $query->value,
71 session => $session,
72 result => $result,
73 params => \@params,
74 retry => $retry
75 );
76 }
77 }
78  
79 1;
80  
81 __END__
« QueryByName/DbhPool.pm ↑ Summary QueryByName/SthPool.pm »