blib/lib/ClearPress/driver.pm

filter: Statementstmt 93.8% 76 / 81 Branchbran 100% 6 / 6 Conditioncond 75.0% 9 / 12 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 89.4% 17 / 19 Podpod 100% 10 / 10 totaltotal 89.6% 121 / 135 CC 10 SCAR 8.3
CC
10
Cov
92%
create_table
5.0
type_map
3.0
DESTROY
3.0
CRAP 2.3
« blib/lib/ClearPress/authenticator/db.pm ↑ Summary blib/lib/ClearPress/authenticator/session.pm »
1 # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*-
2 # vim:ts=8:sw=2:et:sta:sts=2
3 #########
4 # Author: rmp
5 # Created: 2006-10-31
6 #
7 package ClearPress::driver;
8 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use ClearPress::driver::mysql;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use ClearPress::driver::SQLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 15 our $VERSION = q[2026.07.25];
18  
19 39 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
20 39 my ($class, $ref) = @_;
21 39 ▶ $ref ||= {};
Condition100%2/2
$ref ||= {}
l!l
345
MC/DC100%1/1
$ref ||= {}
$ref
Truth table100%2/2
A
A= $ref
Aresult
11
00
22 39 bless $ref, $class;
23 39 return $ref;
24 }
25  
26 2 ▶sub dbh {
Subroutine dbh: called CC=1 SCAR=0.0
Pod documented
27 2 my $self = shift;
28 2 carp q[dbh unimplemented];
29 2 return;
30 }
31  
32 29 ▶sub new_driver {
Subroutine new_driver: called CC=1 SCAR=0.0
Pod documented
33 29 my ($self, $drivername, $ref) = @_;
34  
35 29 my $drvpkg = "ClearPress::driver::$drivername";
36 return $drvpkg->new({
37 drivername => $drivername,
38 29 %{$ref},
39 });
40 }
41  
42 31 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
43 31 my $self = shift;
44  
45 31 ▶ if($self->{dbh} && $self->{dbh}->ping()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dbh"} and $self->{"dbh"}->ping) 24 7 31
Condition66%2/3
$self->{"dbh"} and $self->{"dbh"}->ping
!ll&&!rl&&r
7024
MC/DC50%1/2
$self->{"dbh"} and $self->{"dbh"}->ping
$self->{"dbh"} $self->{"dbh"}->ping
Truth table66%2/3
A and B
A= $self->{"dbh"}B= $self->{"dbh"}->ping
ABresult
0X0
100
111
46 #########
47 # flush down any uncommitted transactions & locks
48 #
49 24 $self->{dbh}->rollback();
50 24 $self->{dbh}->disconnect();
51 }
52  
53 31 return 1;
54 }
55  
56 144 ▶sub create_table {
Subroutine create_table: called CC=5 SCAR=16.1
Pod documented
57 144 my ($self, $t_name, $ref, $t_attrs) = @_;
58 144 my $dbh = $self->dbh();
59 144 ▶ $t_attrs ||= {};
Condition50%1/2
$t_attrs ||= {}
l!l
0144
MC/DC0%0/1
$t_attrs ||= {}
$t_attrs
Truth table50%1/2
A
A= $t_attrs
Aresult
11
00
60 144 ▶ $ref ||= {};
Condition100%2/2
$ref ||= {}
l!l
1431
MC/DC100%1/1
$ref ||= {}
$ref
Truth table100%2/2
A
A= $ref
Aresult
11
00
61  
62 144 my %values = reverse %{$ref};
63 144 my $pk = $values{'primary key'};
64  
65 144 ▶ if(!$pk) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pk) 2 142 144
66 2 croak qq[Could not determine primary key for table $t_name];
67 }
68  
69 142 my @fields = (qq[$pk @{[$self->type_map('primary key')]}]);
70  
71 142 for my $f (grep { $_ ne $pk } keys %{$ref}) {
72 286 push @fields, qq[$f @{[$self->type_map($ref->{$f})]}];
73 }
74  
75 142 my $desc = join q[, ], @fields;
76 0 my $attrs = join q[ ], map { "$_=$t_attrs->{$_}" } keys %{$t_attrs};
77  
78 142 $dbh->do(qq[CREATE TABLE $t_name($desc) $attrs]);
79 142 $dbh->commit();
80  
81 142 return 1;
82 }
83  
84 142 ▶sub drop_table {
Subroutine drop_table: called CC=1 SCAR=0.0
Pod documented
85 142 my ($self, $table_name) = @_;
86 142 my $dbh = $self->dbh();
87  
88 142 $dbh->do(qq[DROP TABLE IF EXISTS $table_name]);
89 142 $dbh->commit();
90  
91 142 return 1;
92 }
93  
94 2 ▶sub types {
Subroutine types: called CC=1 SCAR=0.0
Pod documented
95 2 return {};
96 }
97  
98 430 ▶sub type_map {
Subroutine type_map: called CC=3 SCAR=11.0
Pod documented
99 430 my ($self, $type) = @_;
100 430 ▶ if(!defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 1 429 430
101 1 return;
102 }
103 429 ▶ return $self->types->{$type} || $type;
Condition66%2/3
$self->types->{$type} || $type
l!l&&r!l&&!r
1632660
MC/DC0%0/2
$self->types->{$type} || $type
$self->types->{$type} $type
Truth table66%2/3
A || B
A= $self->types->{$type}B= $type
ABresult
1X1
011
000
104 }
105  
106 0 ▶sub create {
Subroutine create: not called CC=1 SCAR=6.9
Pod documented
107 0 return;
108 }
109  
110 0 ▶sub bounded_select {
Subroutine bounded_select: not called CC=1 SCAR=6.9
Pod documented
111 0 my ($self, $query, $start, $len) = @_;
112 0 carp q[bounded_select unimplemented by driver ], ref $self;
113 0 return q[];
114 }
115  
116 8 ▶sub sth_has_warnings {
Subroutine sth_has_warnings: called CC=1 SCAR=0.0
Pod documented
117 8 my ($self, $sth) = @_;
118 8 return;
119 }
120  
121 15 1;
122 __END__
« blib/lib/ClearPress/authenticator/db.pm ↑ Summary blib/lib/ClearPress/authenticator/session.pm »