Lite.pm

filter: Statementstmt 68.0% 64 / 94 Branchbran 36.8% 14 / 38 Conditioncond 45.4% 5 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 72.7% 16 / 22 Podpod 75.0% 6 / 8 totaltotal 59.4% 107 / 180 CC 24 SCAR 19.7
CC
24
Cov
58%
_autopk
30.0
txn
12.0
new
7.8
CRAP 7.2
« Lite/ResultSet.pm ↑ Summary Lite/Row.pm »
1 package DBIx::Lite;
2 5 $DBIx::Lite::VERSION = '0.39';
3 # ABSTRACT: Chained and minimal ORM
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use DBIx::Connector;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use DBIx::Lite::ResultSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use DBIx::Lite::Row;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use DBIx::Lite::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use SQL::Abstract::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 $Carp::Internal{$_}++ for __PACKAGE__, qw(DBIx::Connector);
15  
16 sub new {
17 4 ▶ my $class = shift;
Subroutine new: called CC=7 SCAR=20.5
Pod documented
18 4 my (%params) = @_;
19
20 my $self = {
21 schema => delete $params{schema} || DBIx::Lite::Schema->new,
22 abstract => SQL::Abstract::More->new(
23 4 ▶ %{ delete $params{abstract} || {} },
Branch: 1 branch
BranchTrueFalseTotal
delete $params{"abstract"} or {} 4 0 4
24 ),
25 connector => delete $params{connector},
26 dbh => delete $params{dbh},
27 4 ▶ };
Condition33%1/3
delete $params{'schema'} || 'DBIx::Lite::Schema'->new
l!l&&r!l&&!r
040
MC/DC0%0/2
delete $params{'schema'} || 'DBIx::Lite::Schema'->new
delete $params{'schema'} 'DBIx::Lite::Schema'->new
Truth table33%1/3
A || B
A= delete $params{'schema'}B= 'DBIx::Lite::Schema'->new
ABresult
1X1
011
000
28  
29 4 ▶ if (!$params{connector} && $params{driver_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $params{"connector"} and $params{"driver_name"}) 1 3 4
Condition66%2/3
not $params{"connector"} and $params{"driver_name"}
!ll&&!rl&&r
031
MC/DC50%1/2
not $params{"connector"} and $params{"driver_name"}
not $params{"connector"} $params{"driver_name"}
Truth table66%2/3
not(A) and B
A= not $params{"connector"}B= $params{"driver_name"}
ABresult
0X0
100
111
30 1 $self->{connector} = DBIx::Connector->new('DBI:' . delete $params{driver_name});
31 }
32
33 4 ▶ !%params
Branch: 1 branch
BranchTrueFalseTotal
%params and croak('Unknown options: ' . join(", ", keys %params)) 0 4 4
34 or croak "Unknown options: " . join(', ', keys %params);
35
36 4 ▶ ref $self->{schema} eq 'DBIx::Lite::Schema'
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{"schema"} eq "DBIx::Lite::Schema" or croak("schema must be a DBIx::Lite::Schema object") 0 4 4
37 or croak "schema must be a DBIx::Lite::Schema object";
38
39 4 bless $self, $class;
40 4 $self;
41 }
42  
43 sub connect {
44 2 ▶ my $class = shift;
Subroutine connect: called CC=3 SCAR=11.4
Pod documented
45 2 ▶ my $self = ref $class ? $class : $class->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 2 0 2
46
47 2 $self->{connector} = DBIx::Connector->new(@_);
48 2 $self->{dbh} = undef;
49 2 ▶ $self->dbh(1) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->dbh(1) or return undef 0 2 2
50 2 ▶ $self->dbh->{HandleError} = sub { croak $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
51
52 2 $self;
53 }
54  
55 sub schema {
56 41 ▶ my $self = shift;
Subroutine schema: called CC=2 SCAR=8.4
Pod documented
57 41 ▶ if (ref $_[0] eq 'DBIx::Lite::Schema') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "DBIx::Lite::Schema") 0 41 41
58 0 $self->{schema} = $_[0];
59 0 return $self;
60 }
61 41 $self->{schema};
62 }
63  
64 sub table {
65 34 ▶ my $self = shift;
Subroutine table: called CC=3 SCAR=11.0
Pod documented
66 34 ▶ my $table_name = shift or croak "Table name missing";
Branch: 1 branch
BranchTrueFalseTotal
my $table_name = shift() or croak("Table name missing") 0 34 34
67
68 34 my $table = $self->schema->table($table_name);
69 34 ▶ my $package = $table->resultset_class || 'DBIx::Lite::ResultSet';
Condition100%2/2
$table->resultset_class || 'DBIx::Lite::ResultSet'
l!l
133
MC/DC100%1/1
$table->resultset_class || 'DBIx::Lite::ResultSet'
$table->resultset_class
Truth table100%2/2
A
A= $table->resultset_class
Aresult
11
00
70 34 $package->_new(
71 dbix_lite => $self,
72 table => $table,
73 );
74 }
75  
76 sub dbh {
77 33 ▶ my $self = shift;
Subroutine dbh: called CC=5 SCAR=19.2
Pod documented
78 33 my ($dont_die) = @_;
79
80 my $dbh = $self->{dbh} ? $self->{dbh}
81 : $self->{connector} ? $self->{connector}->dbh
82 33 ▶ : undef;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'dbh'} ? : 0 33 33
$self->{'connector'} ? : 33 0 33
83 33 ▶ return $dbh ? $dbh
Branch: 2 branches
BranchTrueFalseTotal
$dbh ? : 33 0 33
$dont_die ? : 0 0 0
84 : $dont_die ? undef
85 : croak "No database handle or DBIx::Connector object provided";
86 }
87  
88 sub dbh_do {
89 23 ▶ my $self = shift;
Subroutine dbh_do: called CC=2 SCAR=7.9
Pod undocumented
90 23 my $code = shift;
91
92 23 ▶ if ($self->{connector}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'connector'}) { } 23 0 23
93 23 return $self->{connector}->run($code);
94 } else {
95 0 $_ = $self->dbh;
96 0 return $code->();
97 }
98 }
99  
100 sub txn {
101 0 ▶ my $self = shift;
Subroutine txn: not called CC=3 SCAR=24.8
Pod documented
102 0 my $code = shift;
103
104 0 ▶ if ($self->{connector}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'connector'}) { } 0 0 0
105 0 return $self->{connector}->txn($code);
106 } else {
107 0 $self->dbh->begin_work;
108 0 eval { $code->() };
109 0 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
110 0 eval { $self->dbh->rollback };
111 0 croak $err;
112 }
113 0 $self->dbh->commit;
114 }
115 }
116  
117 sub driver_name {
118 39 ▶ my $self = shift;
Subroutine driver_name: called CC=2 SCAR=7.2
Pod undocumented
119
120 39 ▶ return $self->{connector} ? $self->{connector}->driver_name : $self->dbh->{Driver}->{Name};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'connector'} ? : 39 0 39
121 }
122  
123 sub _autopk {
124 0 ▶ my $self = shift;
Subroutine _autopk: not called CC=5 SCAR=34.0
125 0 my $table_name = shift;
126
127 0 my $driver_name = $self->driver_name;
128
129 0 ▶ if ($driver_name eq 'mysql') {
Branch: 3 branches
BranchTrueFalseTotal
if ($driver_name eq 'mysql') { } 0 0 0
elsif ($driver_name eq 'SQLite') { } 0 0 0
elsif ($driver_name eq 'Pg') { } 0 0 0
130 0 ▶ return $self->dbh_do(sub { +($_->selectrow_array('SELECT LAST_INSERT_ID()'))[0] });
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 } elsif ($driver_name eq 'SQLite') {
132 0 ▶ return $self->dbh_do(sub { +($_->selectrow_array('SELECT LAST_INSERT_ROWID()'))[0] });
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 } elsif ($driver_name eq 'Pg') {
134 0 my ( $schema, $name ) = $table_name =~ /^(?:([^.]+)[.])?([^.]+)$/;
135 # if RE fails to match, just pass in the original name and let DBD::Pg handle it
136 0 ▶ $name //= $table_name;
Condition0%0/3
$name //= $table_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$name //= $table_name
$name $table_name
Truth table0%0/3
A //= B
A= $nameB= $table_name
ABresult
1X1
011
000
137 0 ▶ return $self->dbh_do( sub { $_->last_insert_id( undef, $schema, $name, undef ) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
138  
139 } else {
140 0 croak "Autoincrementing ID is not supported on $driver_name";
141 }
142 }
143  
144 sub _quote {
145 5 ▶ my $self = shift;
Subroutine _quote: called CC=1 SCAR=0.0
146
147 5 return $self->{abstract}->_quote(@_);
148 }
149  
150 5 1;
151  
152 __END__
« Lite/ResultSet.pm ↑ Summary Lite/Row.pm »