Controller/SQL/MySQL.pm

filter: Statementstmt 43.5% 17 / 39 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 5 / 6 Podpod 100% 1 / 1 totaltotal 42.5% 23 / 54 CC 5 SCAR 27.4
CC
5
Cov
36%
insert_hash
30.0
BEGIN
1.0
BEGIN
1.0
CRAP 15.5
↑ Summary Controller/Directives.pm »
1 package Apache2::Controller::SQL::MySQL;
2  
3 =head1 NAME
4  
5 Apache2::Controller::SQL::MySQL - useful database methods for MySQL
6  
7 =head1 VERSION
8  
9 Version 1.001.001
10  
11 =cut
12  
13 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 our $VERSION = version->new('1.001.001');
15  
16 =head1 SYNOPSIS
17  
18 package UFP::SFC::Controller::Tools;
19 use base qw(
20 Apache2::Controller
21 Apache2::Controller::SQL::MySQL
22 );
23 # ...
24  
25 =head1 DESCRIPTION
26  
27 Provides some useful methods for interacting with a MySQL database.
28  
29 This isn't really standard or a necessary part of A2C, I just find it handy.
30  
31 =head1 DEPRECATED
32  
33 Don't depend on this. I intend to remove it in a future
34 release because it is not relevant.
35  
36 =head1 METHODS
37  
38 =head2 insert_hash
39  
40 insert_hash( \%hashref )
41  
42 Insert data into the database.
43  
44 # http://sfc.ufp/tools/register_crew/enterprise?captain=kirk&sci=spock&med=mccoy
45 sub register_crew {
46 my ($self, $ship) = @_;
47 my $crew = $self->param();
48 $self->insert_hash({
49 table => "crew_$ship",
50 data => $crew,
51 });
52 $self->print("Warp factor 5, engage.\n");
53 return Apache2::Const::HTTP_OK;
54 }
55  
56 Requires a database handle be assigned to C<< $self->{dbh} >>.
57 See L<Apache2::Controller::DBI::Connector>.
58  
59 Hashref argument supports these fields:
60  
61 =over 4
62  
63 =item table
64  
65 The SQL table to insert into.
66  
67 =item data
68  
69 The hash ref of field data to insert.
70  
71 =item on_dup_sql
72  
73 Optional string of SQL for after 'ON DUPLICATE KEY UPDATE'.
74 Format it yourself.
75  
76 =item on_dup_bind
77  
78 Array ref of bind values for extra C<?> characters in C<on_dup_sql>.
79  
80 =back
81  
82 =cut
83  
84 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
86 2 ▶use English '-no_match_vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
87 2 ▶use Apache2::Controller::X;
Subroutine BEGIN: called CC=1 SCAR=0.0
88  
89 0 ▶sub insert_hash {
Subroutine insert_hash: not called CC=5 SCAR=34.0
Pod documented
90 0 my ($self, $p) = @_;
91  
92 0 my ($table, $data, $on_dup_sql, $on_dup_bind) = @{$p}{qw(
93 table data on_dup_sql on_dup_bind
94 )};
95  
96 0 my @bind = values %{$data};
97  
98 my $sql
99 = "INSERT INTO $table SET\n"
100 0 ▶ . join(",\n", map {" $_ = ".(ref $_ ? $_ : '?')} keys %{$data});
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
101  
102 0 ▶ if ($on_dup_sql) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_dup_sql) 0 0 0
103 0 $sql .= "\nON DUPLICATE KEY UPDATE\n$on_dup_sql\n";
104 0 ▶ push @bind, @{$on_dup_bind} if $on_dup_bind;
Branch: 1 branch
BranchTrueFalseTotal
if $on_dup_bind 0 0 0
105 }
106  
107 0 my $dbh = $self->{dbh};
108 0 my $id;
109 0 eval {
110 0 DEBUG("preparing handle for sql:\n$sql\n---\n");
111 0 my $sth = $dbh->prepare_cached($sql);
112 0 $sth->execute(@bind);
113 0 ($id) = $dbh->selectrow_array(q{ SELECT LAST_INSERT_ID() });
114 };
115 0 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Apache2::Controller::SQL::MySQL::EVAL_ERROR) 0 0 0
116 0 a2cx message => "database error: $EVAL_ERROR",
117 dump => { sql => $sql, bind => \@bind, };
118 }
119 0 return $id;
120 }
121  
122 =head1 SEE ALSO
123  
124 L<Apache2::Controller::DBI::Connector>
125  
126 L<Apache2::Controller>
127  
128 =head1 AUTHOR
129  
130 Mark Hedges, C<hedges +(a t)- formdata.biz>
131  
132 =head1 COPYRIGHT AND LICENSE
133  
134 Copyright 2008-2010 Mark Hedges. CPAN: markle
135  
136 This library is free software; you can redistribute it and/or modify
137 it under the same terms as Perl itself.
138  
139 This software is provided as-is, with no warranty
140 and no guarantee of fitness
141 for any particular purpose.
142  
143 =cut
144  
145 2 1;
146  
↑ Summary Controller/Directives.pm »