Lite.pm

filter: Statementstmt 70.2% 66 / 94 Branchbran 41.6% 15 / 36 Conditioncond 46.1% 6 / 13 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 72.7% 16 / 22 Podpod 75.0% 6 / 8 totaltotal 61.3% 111 / 181 CC 24 SCAR 19.5
CC
24
Cov
61%
_autopk
30.0
txn
12.0
new
7.8
CRAP 7.1
« Lite/Row.pm ↑ Summary Lite/Schema.pm »
1 package DBIx::Lite;
2 6 $DBIx::Lite::VERSION = '0.41';
3 # ABSTRACT: Chained and minimal ORM
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use DBIx::Connector;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use DBIx::Lite::ResultSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use DBIx::Lite::Row;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use DBIx::Lite::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use SQL::Abstract::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 $Carp::Internal{$_}++ for __PACKAGE__, qw(DBIx::Connector);
15  
16 5 ▶sub new {
Subroutine new: called CC=7 SCAR=20.5
Pod documented
17 5 my $class = shift;
18 5 my (%params) = @_;
19
20 my $self = {
21 schema => delete $params{schema} || DBIx::Lite::Schema->new,
22 abstract => SQL::Abstract::More->new(
23 5 ▶ %{ delete $params{abstract} || {} },
Condition50%1/2
delete $params{'abstract'} || {}
l!l
05
MC/DC0%0/1
delete $params{'abstract'} || {}
delete $params{'abstract'}
Truth table50%1/2
A
A= delete $params{'abstract'}
Aresult
11
00
24 ),
25 connector => delete $params{connector},
26 dbh => delete $params{dbh},
27 5 ▶ };
Condition33%1/3
delete $params{'schema'} || 'DBIx::Lite::Schema'->new
l!l&&r!l&&!r
050
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 5 ▶ if (!$params{connector} && $params{driver_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $params{"connector"} and $params{"driver_name"}) 1 4 5
Condition66%2/3
not $params{"connector"} and $params{"driver_name"}
!ll&&!rl&&r
041
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 5 ▶ !%params
Branch: 1 branch
BranchTrueFalseTotal
%params and croak('Unknown options: ' . join(", ", keys %params)) 0 5 5
34 or croak "Unknown options: " . join(', ', keys %params);
35
36 5 ▶ 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") 5 0 5
37 or croak "schema must be a DBIx::Lite::Schema object";
38
39 5 bless $self, $class;
40 5 $self;
41 }
42  
43 2 ▶sub connect {
Subroutine connect: called CC=3 SCAR=11.4
Pod documented
44 2 my $class = shift;
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 2 0 2
50 0 ▶ $self->dbh->{HandleError} = sub { croak $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
51
52 2 $self;
53 }
54  
55 49 ▶sub schema {
Subroutine schema: called CC=2 SCAR=8.4
Pod documented
56 49 my $self = shift;
57 49 ▶ if (ref $_[0] eq 'DBIx::Lite::Schema') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "DBIx::Lite::Schema") 0 49 49
58 0 $self->{schema} = $_[0];
59 0 return $self;
60 }
61 49 $self->{schema};
62 }
63  
64 42 ▶sub table {
Subroutine table: called CC=3 SCAR=11.0
Pod documented
65 42 my $self = shift;
66 42 ▶ my $table_name = shift or croak "Table name missing";
Branch: 1 branch
BranchTrueFalseTotal
my $table_name = shift() or croak("Table name missing") 42 0 42
67
68 42 my $table = $self->schema->table($table_name);
69 42 ▶ my $package = $table->resultset_class || 'DBIx::Lite::ResultSet';
Condition100%2/2
$table->resultset_class || 'DBIx::Lite::ResultSet'
l!l
141
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 42 $package->_new(
71 dbix_lite => $self,
72 table => $table,
73 );
74 }
75  
76 39 ▶sub dbh {
Subroutine dbh: called CC=5 SCAR=17.8
Pod documented
77 39 my $self = shift;
78 39 my ($dont_die) = @_;
79
80 my $dbh = $self->{dbh} ? $self->{dbh}
81 : $self->{connector} ? $self->{connector}->dbh
82 39 ▶ : undef;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'dbh'} ? : 6 33 39
$self->{'connector'} ? : 33 0 33
83 39 ▶ return $dbh ? $dbh
Branch: 2 branches
BranchTrueFalseTotal
$dbh ? : 39 0 39
$dont_die ? : 0 0 0
84 : $dont_die ? undef
85 : croak "No database handle or DBIx::Connector object provided";
86 }
87  
88 26 ▶sub dbh_do {
Subroutine dbh_do: called CC=2 SCAR=6.9
Pod undocumented
89 26 my $self = shift;
90 26 my $code = shift;
91
92 26 ▶ if ($self->{connector}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'connector'}) { } 23 3 26
93 23 return $self->{connector}->run($code);
94 } else {
95 3 local $_ = $self->dbh;
96 3 return $code->();
97 }
98 }
99  
100 0 ▶sub txn {
Subroutine txn: not called CC=3 SCAR=24.8
Pod documented
101 0 my $self = shift;
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 43 ▶sub driver_name {
Subroutine driver_name: called CC=2 SCAR=7.2
Pod undocumented
118 43 my $self = shift;
119
120 43 ▶ return $self->{connector} ? $self->{connector}->driver_name : $self->dbh->{Driver}->{Name};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'connector'} ? : 43 0 43
121 }
122  
123 0 ▶sub _autopk {
Subroutine _autopk: not called CC=5 SCAR=34.0
124 0 my $self = shift;
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 5 ▶sub _quote {
Subroutine _quote: called CC=1 SCAR=0.0
145 5 my $self = shift;
146
147 5 return $self->{abstract}->_quote(@_);
148 }
149  
150 6 1;
151  
152 __END__
« Lite/Row.pm ↑ Summary Lite/Schema.pm »