lib/Ormlette.pm

filter: Statementstmt 76.8% 722 / 940 Branchbran 53.0% 122 / 230 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 81.1% 198 / 244 Podpod 100% 2 / 2 totaltotal 73.7% 1051 / 1426 CC 220 SCAR 7.1
CC
220
Cov
72%
init
7.1
_scan_tables
6.0
_table_mutators
6.0
CRAP 2.0
↑ Summary
1 package Ormlette;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 our $VERSION = 0.005;
7  
8 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 43 ▶sub init {
Subroutine init: called CC=7 SCAR=19.5
Pod documented
11 43 my ($class, $dbh, %params) = @_;
12  
13 43 ▶ croak 'First param to Ormlette->init must be a connected database handle'
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->isa("DBI::db") 1 41 42
14 unless $dbh->isa('DBI::db');
15  
16 41 ▶ my $namespace = $params{namespace} || caller;
Condition66%2/3
$params{'namespace'} || &CORE::GLOBAL::caller()
l!l&&r!l&&!r
30100
MC/DC0%0/2
$params{'namespace'} || &CORE::GLOBAL::caller()
$params{'namespace'} &CORE::GLOBAL::caller()
Truth table66%2/3
A || B
A= $params{'namespace'}B= &CORE::GLOBAL::caller()
ABresult
1X1
011
000
17  
18 41 my $tbl_names = _scan_tables($dbh, $namespace, %params);
19  
20 my $self = bless {
21 dbh => $dbh,
22 debug => $params{debug} ? 1 : 0,
23 ignore_root => $params{ignore_root},
24 isa => $params{isa},
25 namespace => $namespace,
26 41 ▶ readonly => $params{readonly} ? 1 : 0,
Branch: 2 branches
BranchTrueFalseTotal
$params{'debug'} ? : 0 40 40
$params{'readonly'} ? : 2 38 40
27 tbl_names => $tbl_names,
28 }, $class;
29  
30 41 ▶ $self->_build_root_pkg unless $self->{ignore_root};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ignore_root"} 39 1 40
31 41 $self->_build_table_pkg($_) for keys %$tbl_names;
32  
33 41 return $self;
34 }
35  
36 231 ▶sub dbh { $_[0]->{dbh} }
Subroutine dbh: called CC=1 SCAR=0.0
Pod documented
37  
38 41 ▶sub _scan_tables {
Subroutine _scan_tables: called CC=6 SCAR=17.9
39 41 my ($dbh, $namespace, %params) = @_;
40  
41 41 my @tables = $dbh->tables(undef, undef, undef, 'TABLE');
42 41 ▶ if (my $quote_char = $dbh->get_info(29)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $quote_char = $dbh->get_info(29)) 40 0 40
43 41 for (@tables) {
44 53 s/$quote_char$//;
45 53 s/^.*$quote_char//;
46 }
47 }
48  
49 41 ▶ if ($params{tables}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($params{'tables'}) { } 2 38 40
elsif ($params{'ignore_tables'}) { } 1 37 38
50 3 my %include = map { $_ => 1 } @{$params{tables}};
51 3 @tables = grep { $include{$_} } @tables;
52 } elsif ($params{ignore_tables}) {
53 2 my %exclude = map { $_ => 1 } @{$params{ignore_tables}};
54 2 @tables = grep { !$exclude{$_} } @tables;
55 }
56  
57 41 my %tbl_names;
58 41 for (@tables) {
59 49 my $tbl = lc $_;
60 49 $tbl =~ s/__(.)/::\U$1/g;
61 49 my @words = split '_', $tbl;
62 49 $tbl_names{$_} = $namespace . '::' . (join '', map { ucfirst } @words);
63 }
64  
65 41 return \%tbl_names;
66 }
67  
68 49 ▶sub _scan_fields {
Subroutine _scan_fields: called CC=1 SCAR=0.0
69 49 my ($self, $tbl_name) = @_;
70  
71 49 my $sth = $self->dbh->prepare("SELECT * FROM $tbl_name LIMIT 0");
72 49 $sth->execute;
73 49 return $sth->{NAME};
74 }
75  
76 # Code generation methods below
77  
78 40 ▶sub _build_root_pkg {
Subroutine _build_root_pkg: called CC=2 SCAR=6.9
79 40 my $self = shift;
80 40 my $pkg_name = $self->{namespace};
81  
82 40 my $pkg_src = $self->_pkg_core($pkg_name);
83 40 $pkg_src .= $self->_root_methods;
84  
85 40 ▶ $self->_compile_pkg($pkg_src) unless $pkg_name->can('_ormlette_init');
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg_name->can("_ormlette_init") 32 7 39
86 40 $pkg_name->_ormlette_init_root($self);
87 }
88  
89 49 ▶sub _build_table_pkg {
Subroutine _build_table_pkg: called CC=2 SCAR=7.0
90 49 my ($self, $tbl_name) = @_;
91 49 my $pkg_name = $self->{tbl_names}{$tbl_name};
92  
93 49 my $field_list = $self->_scan_fields($tbl_name);
94  
95 49 my $pkg_src = $self->_pkg_core($pkg_name);
96 49 $pkg_src .= $self->_table_methods($tbl_name, $field_list);
97  
98 49 ▶ $self->_compile_pkg($pkg_src) unless $pkg_name->can('_ormlette_init');
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg_name->can("_ormlette_init") 48 0 48
99 49 $pkg_name->_ormlette_init_table($self, $tbl_name);
100 }
101  
102 81 ▶sub _compile_pkg {
Subroutine _compile_pkg: called CC=3 SCAR=11.9
103 81 my ($self, $pkg_src) = @_;
104 81 local $@;
105 81 ▶ print STDERR $pkg_src if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 80 80
106 0 ▶ eval $pkg_src;
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine __ANON__: called CC=2 SCAR=7.6
Subroutine __ANON__: called CC=1 SCAR=0.3
Subroutine __ANON__: called CC=2 SCAR=7.6
Subroutine __ANON__: called CC=1 SCAR=0.3
Subroutine __ANON__: called CC=2 SCAR=7.6
Subroutine __ANON__: called CC=1 SCAR=0.3
Subroutine __ANON__: called CC=2 SCAR=7.6
Subroutine __ANON__: called CC=1 SCAR=0.3
Subroutine __ANON__: called CC=2 SCAR=7.6
Subroutine __ANON__: called CC=1 SCAR=0.3
Subroutine __ANON__: called CC=2 SCAR=7.6
Subroutine __ANON__: not called CC=1 SCAR=0.3
Subroutine __ANON__: not called CC=2 SCAR=7.6
Subroutine __ANON__: called CC=1 SCAR=0.3
Subroutine __ANON__: not called CC=2 SCAR=7.6
Subroutine __ANON__: not called CC=1 SCAR=0.3
Subroutine __ANON__: not called CC=2 SCAR=7.6
Subroutine __ANON__: not called CC=1 SCAR=0.3
Subroutine __ANON__: not called CC=2 SCAR=7.6
Subroutine __ANON__: not called CC=1 SCAR=0.3
Subroutine _ormlette_init: called CC=1 SCAR=0.3
Subroutine _ormlette_init: called CC=1 SCAR=0.3
Subroutine _ormlette_init: called CC=1 SCAR=0.3
Subroutine _ormlette_init: called CC=1 SCAR=0.3
Subroutine _ormlette_init: called CC=1 SCAR=0.3
Subroutine _ormlette_init: called CC=1 SCAR=0.3
Subroutine _ormlette_init: called CC=1 SCAR=0.3
Subroutine _ormlette_init: called CC=1 SCAR=0.3
Subroutine _ormlette_init: called CC=1 SCAR=0.3
Subroutine _ormlette_init: called CC=1 SCAR=0.3
Subroutine _ormlette_init: called CC=1 SCAR=0.3
Subroutine _ormlette_init: called CC=1 SCAR=0.3
Subroutine _ormlette_init: called CC=1 SCAR=0.3
Subroutine _ormlette_init_root: called CC=1 SCAR=0.3
Subroutine _ormlette_init_root: called CC=1 SCAR=0.3
Subroutine _ormlette_init_root: called CC=1 SCAR=0.3
Subroutine _ormlette_init_table: called CC=1 SCAR=0.3
Subroutine _ormlette_init_table: called CC=1 SCAR=0.3
Subroutine _ormlette_init_table: called CC=1 SCAR=0.3
Subroutine _ormlette_init_table: called CC=1 SCAR=0.3
Subroutine _ormlette_init_table: called CC=1 SCAR=0.3
Subroutine _ormlette_init_table: called CC=1 SCAR=0.3
Subroutine _ormlette_init_table: called CC=1 SCAR=0.3
Subroutine _ormlette_init_table: called CC=1 SCAR=0.3
Subroutine _ormlette_init_table: called CC=1 SCAR=0.3
Subroutine _ormlette_init_table: called CC=1 SCAR=0.3
Subroutine _ormlette_load_from_sth: called CC=2 SCAR=7.6
Subroutine _ormlette_load_from_sth: called CC=2 SCAR=7.6
Subroutine _ormlette_load_from_sth: called CC=2 SCAR=7.6
Subroutine _ormlette_load_from_sth: called CC=2 SCAR=7.6
Subroutine _ormlette_load_from_sth: called CC=2 SCAR=7.6
Subroutine _ormlette_load_from_sth: called CC=2 SCAR=7.6
Subroutine _ormlette_load_from_sth: called CC=2 SCAR=7.6
Subroutine _ormlette_load_from_sth: called CC=2 SCAR=7.6
Subroutine _ormlette_load_from_sth: called CC=2 SCAR=7.6
Subroutine _ormlette_load_from_sth: called CC=2 SCAR=7.6
Subroutine _ormlette_new: called CC=1 SCAR=0.3
Subroutine _ormlette_new: called CC=1 SCAR=0.3
Subroutine _ormlette_new: called CC=1 SCAR=0.3
Subroutine _ormlette_new: called CC=1 SCAR=0.3
Subroutine _ormlette_new: called CC=1 SCAR=0.3
Subroutine _ormlette_new: called CC=1 SCAR=0.3
Subroutine _ormlette_new: called CC=1 SCAR=0.3
Subroutine _ormlette_new: called CC=1 SCAR=0.3
Subroutine _ormlette_new: called CC=1 SCAR=0.3
Subroutine _ormlette_new: called CC=1 SCAR=0.3
Subroutine create: called CC=1 SCAR=0.3
Subroutine create: called CC=1 SCAR=0.3
Subroutine create: called CC=1 SCAR=0.3
Subroutine create: called CC=1 SCAR=0.3
Subroutine create: called CC=1 SCAR=0.3
Subroutine create: called CC=1 SCAR=0.3
Subroutine create: called CC=1 SCAR=0.3
Subroutine create: not called CC=1 SCAR=0.3
Subroutine create: called CC=1 SCAR=0.3
Subroutine create: called CC=1 SCAR=0.3
Subroutine dbh: called CC=1 SCAR=0.3
Subroutine dbh: called CC=1 SCAR=0.3
Subroutine dbh: called CC=1 SCAR=0.3
Subroutine dbh: called CC=1 SCAR=0.3
Subroutine dbh: called CC=1 SCAR=0.3
Subroutine dbh: called CC=1 SCAR=0.3
Subroutine dbh: called CC=1 SCAR=0.3
Subroutine dbh: called CC=1 SCAR=0.3
Subroutine dbh: called CC=1 SCAR=0.3
Subroutine dbh: not called CC=1 SCAR=0.3
Subroutine dbh: not called CC=1 SCAR=0.3
Subroutine dbh: not called CC=1 SCAR=0.3
Subroutine dbh: called CC=1 SCAR=0.3
Subroutine delete: called CC=3 SCAR=12.0
Subroutine delete: called CC=3 SCAR=12.0
Subroutine delete: called CC=3 SCAR=12.0
Subroutine delete: called CC=3 SCAR=12.0
Subroutine delete: called CC=3 SCAR=12.0
Subroutine delete: not called CC=3 SCAR=12.0
Subroutine delete: called CC=3 SCAR=12.0
Subroutine delete: called CC=3 SCAR=12.0
Subroutine delete: called CC=3 SCAR=12.0
Subroutine delete: called CC=3 SCAR=12.0
Subroutine insert: called CC=2 SCAR=7.6
Subroutine insert: called CC=1 SCAR=0.3
Subroutine insert: called CC=2 SCAR=7.6
Subroutine insert: called CC=2 SCAR=7.6
Subroutine insert: called CC=2 SCAR=7.6
Subroutine insert: called CC=2 SCAR=7.6
Subroutine insert: called CC=1 SCAR=0.3
Subroutine insert: called CC=2 SCAR=7.6
Subroutine insert: called CC=1 SCAR=0.3
Subroutine insert: called CC=1 SCAR=0.3
Subroutine iterate: called CC=3 SCAR=12.0
Subroutine iterate: called CC=3 SCAR=12.0
Subroutine iterate: called CC=3 SCAR=12.0
Subroutine iterate: not called CC=3 SCAR=12.0
Subroutine iterate: called CC=3 SCAR=12.0
Subroutine iterate: not called CC=3 SCAR=12.0
Subroutine iterate: called CC=3 SCAR=12.0
Subroutine iterate: not called CC=3 SCAR=12.0
Subroutine iterate: not called CC=3 SCAR=12.0
Subroutine iterate: not called CC=3 SCAR=12.0
Subroutine load: called CC=4 SCAR=15.1
Subroutine load: called CC=3 SCAR=12.0
Subroutine load: called CC=4 SCAR=15.1
Subroutine load: called CC=4 SCAR=15.1
Subroutine load: not called CC=4 SCAR=15.1
Subroutine load: not called CC=4 SCAR=15.1
Subroutine load: not called CC=3 SCAR=12.0
Subroutine load: called CC=4 SCAR=15.1
Subroutine load: called CC=3 SCAR=12.0
Subroutine load: called CC=3 SCAR=12.0
Subroutine new: called CC=1 SCAR=0.3
Subroutine new: called CC=1 SCAR=0.3
Subroutine new: called CC=1 SCAR=0.3
Subroutine new: called CC=1 SCAR=0.3
Subroutine new: called CC=1 SCAR=0.3
Subroutine new: called CC=1 SCAR=0.3
Subroutine new: called CC=1 SCAR=0.3
Subroutine new: called CC=1 SCAR=0.3
Subroutine new: called CC=1 SCAR=0.3
Subroutine new: called CC=1 SCAR=0.3
Subroutine select: called CC=1 SCAR=0.3
Subroutine select: called CC=1 SCAR=0.3
Subroutine select: called CC=1 SCAR=0.3
Subroutine select: called CC=1 SCAR=0.3
Subroutine select: called CC=1 SCAR=0.3
Subroutine select: called CC=1 SCAR=0.3
Subroutine select: called CC=1 SCAR=0.3
Subroutine select: not called CC=1 SCAR=0.3
Subroutine select: not called CC=1 SCAR=0.3
Subroutine select: not called CC=1 SCAR=0.3
Subroutine table: called CC=1 SCAR=0.3
Subroutine table: called CC=1 SCAR=0.3
Subroutine table: called CC=1 SCAR=0.3
Subroutine table: not called CC=1 SCAR=0.3
Subroutine table: not called CC=1 SCAR=0.3
Subroutine table: not called CC=1 SCAR=0.3
Subroutine table: not called CC=1 SCAR=0.3
Subroutine table: not called CC=1 SCAR=0.3
Subroutine table: called CC=1 SCAR=0.3
Subroutine table: not called CC=1 SCAR=0.3
Subroutine truncate: not called CC=2 SCAR=7.6
Subroutine truncate: not called CC=2 SCAR=7.6
Subroutine truncate: not called CC=2 SCAR=7.6
Subroutine truncate: not called CC=2 SCAR=7.6
Subroutine truncate: not called CC=2 SCAR=7.6
Subroutine truncate: not called CC=2 SCAR=7.6
Subroutine truncate: not called CC=2 SCAR=7.6
Subroutine truncate: not called CC=2 SCAR=7.6
Subroutine truncate: not called CC=2 SCAR=7.6
Subroutine truncate: not called CC=2 SCAR=7.6
Subroutine BEGIN: not called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: not called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: not called CC=1 SCAR=0.3
Subroutine BEGIN: not called CC=1 SCAR=0.3
Subroutine BEGIN: not called CC=1 SCAR=0.3
Subroutine BEGIN: not called CC=1 SCAR=0.3
Branch: 94 branches
BranchTrueFalseTotal
if defined $_[1] 0 6 6
if defined $_[1] 1 9 10
if defined $_[1] 3 5 8
if defined $_[1] 0 11 11
if defined $_[1] 0 12 12
if defined $_[1] 0 0 0
if defined $_[1] 0 0 0
if defined $_[1] 0 0 0
if defined $_[1] 0 0 0
if defined $_[1] 0 0 0
unless $sth->fetch 2 4 6
unless $sth->fetch 4 6 10
unless $sth->fetch 1 3 4
unless $sth->fetch 0 1 1
unless $sth->fetch 3 5 8
unless $sth->fetch 0 1 1
unless $sth->fetch 1 1 2
unless $sth->fetch 1 2 3
unless $sth->fetch 1 4 5
unless $sth->fetch 1 3 4
if ref $class 3 6 9
unless @_ 3 7 10
if ref $class 3 6 9
unless @_ 2 15 17
if ref $class 0 0 0
unless @_ 0 0 0
if ref $class 0 0 0
unless @_ 0 0 0
if ref $class 1 0 1
unless @_ 0 0 0
if ref $class 0 0 0
unless @_ 0 0 0
if ref $class 0 0 0
unless @_ 0 0 0
if ref $class 0 0 0
unless @_ 0 0 0
if ref $class 0 0 0
unless @_ 0 0 0
if ref $class 1 2 3
unless @_ 1 1 2
if @_ 9 10 19
if ($_ = $class->_ormlette_load_from_sth($sth)) 5 3 8
if @_ 5 2 7
if ($_ = $class->_ormlette_load_from_sth($sth)) 5 5 10
if @_ 0 1 1
if ($_ = $class->_ormlette_load_from_sth($sth)) 1 1 2
if @_ 0 0 0
if ($_ = $class->_ormlette_load_from_sth($sth)) 0 0 0
if @_ 1 0 1
if ($_ = $class->_ormlette_load_from_sth($sth)) 1 1 2
if @_ 0 0 0
if ($_ = $class->_ormlette_load_from_sth($sth)) 0 0 0
if @_ 1 0 1
if ($_ = $class->_ormlette_load_from_sth($sth)) 1 1 2
if @_ 0 0 0
if ($_ = $class->_ormlette_load_from_sth($sth)) 0 0 0
if @_ 0 0 0
if ($_ = $class->_ormlette_load_from_sth($sth)) 0 0 0
if @_ 0 0 0
if ($_ = $class->_ormlette_load_from_sth($sth)) 0 0 0
unless @_ 0 2 2
unless @_ % 2 == 0 4 2 6
unless @_ 0 3 3
unless @_ % 2 == 0 5 3 8
unless @_ 0 5 5
unless @_ % 2 == 0 7 3 10
unless @_ 1 2 3
unless @_ % 2 == 0 16 2 18
unless @_ 0 0 0
unless @_ % 2 == 0 0 0 0
unless @_ 0 0 0
unless @_ % 2 == 0 0 0 0
unless @_ 0 0 0
unless @_ % 2 == 0 0 0 0
unless @_ 0 3 3
unless @_ % 2 == 0 3 0 3
unless @_ 0 6 6
unless @_ % 2 == 0 3 3 6
unless @_ 0 4 4
unless @_ % 2 == 0 1 3 4
if ref $class 0 0 0
if ref $class 0 3 3
if ref $class 0 3 3
if ref $class 0 4 4
if ref $class 1 1 2
if ref $class 0 0 0
if ref $class 0 3 3
if ref $class 2 1 3
if ref $class 0 2 2
if ref $class 2 0 2
if $_[0]{"_dirty"} 0 0 0
if $_[0]{"_dirty"} 0 0 0
if $_[0]{"_dirty"} 0 0 0
if $_[0]{"_dirty"} 0 0 0
107 80 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 80 80
108 }
109  
110 87 ▶sub _pkg_core {
Subroutine _pkg_core: called CC=3 SCAR=11.0
111 87 my ($self, $pkg_name) = @_;
112  
113 87 my $core = <<"END_CODE";
114 package $pkg_name;
115  
116 use strict;
117 use warnings;
118  
119 use Carp;
120  
121 END_CODE
122  
123 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
124 87 ▶ if ($self->{isa} && !@{ $pkg_name . '::ISA' }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"isa"} and not @{$pkg_name . '::ISA';}) 3 85 88
Condition100%3/3
$self->{"isa"} and not @{$pkg_name . '::ISA';}
!ll&&!rl&&r
8313
MC/DC100%2/2
$self->{"isa"} and not @{$pkg_name . '::ISA';}
$self->{"isa"} not @{$pkg_name . '::ISA';}
Truth table100%3/3
A and not(B)
A= $self->{"isa"}B= not @{$pkg_name . '::ISA';}
ABresult
0X0
100
111
125 3 my $isa = $self->{isa};
126 3 $core .= <<"END_CODE";
127 our \@ISA = '$isa';
128  
129 END_CODE
130 }
131 8 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
132  
133 87 $core .= <<"END_CODE";
134 my \$_ormlette_dbh;
135  
136 sub dbh { \$_ormlette_dbh }
137  
138 sub _ormlette_init {
139 my (\$class, \$ormlette) = \@_;
140 \$_ormlette_dbh = \$ormlette->dbh;
141 }
142  
143 END_CODE
144  
145 87 return $core;
146 }
147  
148 39 ▶sub _root_methods {
Subroutine _root_methods: called CC=1 SCAR=0.0
149 39 my $self = shift;
150  
151 39 return <<"END_CODE";
152 sub _ormlette_init_root {
153 my (\$class, \$ormlette) = \@_;
154 \$class->_ormlette_init(\$ormlette);
155 }
156  
157 END_CODE
158 }
159  
160 48 ▶sub _table_methods {
Subroutine _table_methods: called CC=5 SCAR=16.1
161 48 my ($self, $tbl_name, $field_list) = @_;
162 48 my $pkg_name = $self->{tbl_names}{$tbl_name};
163  
164 48 my $select_fields = join ', ', map { "$tbl_name.$_" } @$field_list;
165 48 my $field_vars = '$' . join ', $', @$field_list;
166 48 my $inflate_fields; $inflate_fields .= "$_ => \$$_, " for @$field_list;
167  
168 48 my @accessor_fields = grep { !$pkg_name->can($_) } @$field_list;
169 49 my $code;
170 48 ▶ $code = $self->_add_accessors($pkg_name, @accessor_fields)
Branch: 1 branch
BranchTrueFalseTotal
if @accessor_fields 49 0 49
171 if @accessor_fields;
172  
173 48 $code .= <<"END_CODE";
174 sub table { '$tbl_name' }
175  
176 sub _ormlette_init_table {
177 my (\$class, \$ormlette, \$table_name) = \@_;
178 \$class->_ormlette_init(\$ormlette);
179 }
180  
181 sub iterate {
182 my \$class = shift;
183 my \$callback = shift;
184  
185 my \$sql = 'SELECT $select_fields FROM $tbl_name';
186 \$sql .= ' ' . shift if \@_;
187 my \$sth = \$class->dbh->prepare_cached(\$sql);
188 \$sth->execute(\@_);
189  
190 local \$_;
191 while (\$_ = \$class->_ormlette_load_from_sth(\$sth)) {
192 \$callback->();
193 }
194 }
195  
196 sub select {
197 my \$class = shift;
198 my \@results;
199 \$class->iterate(sub { push \@results, \$_ }, \@_);
200 return \\\@results;
201 }
202  
203 sub _ormlette_load_from_sth {
204 my (\$class, \$sth) = \@_;
205  
206 \$sth->bind_columns(\\(my ($field_vars)));
207 return unless \$sth->fetch;
208  
209 return bless { $inflate_fields }, \$class;
210 }
211  
212 END_CODE
213  
214 48 $code .= <<"END_CODE";
215 sub load {
216 my \$class = shift;
217  
218 croak '->load requires at least one argument' unless \@_;
219  
220 my \$sql = 'SELECT $select_fields FROM $tbl_name WHERE ';
221 my \@criteria;
222  
223 END_CODE
224  
225 48 my @key = $self->dbh->primary_key(undef, undef, $tbl_name);
226 49 ▶ if (@key == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@key == 1) { } 12 36 48
227 13 my $key_criteria = $key[0] . ' = ?';
228  
229 13 $code .= <<"END_CODE";
230  
231 if (\@_ == 1) {
232 \$sql .= '$key_criteria';
233 \@criteria = \@_;
234 } else {
235 croak 'if not using a single-field key, ->load requires a hash of criteria'
236 unless \@_ % 2 == 0;
237  
238 my \%params = \@_;
239 \$sql .= join ' AND ', map { "\$_ = ?" } keys \%params;
240 \@criteria = values \%params;
241 }
242 END_CODE
243 } else { # primary key absent or multi-field
244 36 $code .= <<"END_CODE";
245 croak 'if not using a single-field key, ->load requires a hash of criteria'
246 unless \@_ % 2 == 0;
247  
248 my \%params = \@_;
249 \$sql .= join ' AND ', map { "\$_ = ?" } keys \%params;
250 \$sql .= ' LIMIT 1';
251 \@criteria = values \%params;
252 END_CODE
253 }
254  
255 48 $code .= <<"END_CODE";
256 my \$sth = \$class->dbh->prepare_cached(\$sql);
257 \$sth->execute(\@criteria);
258  
259 my \$obj = \$class->_ormlette_load_from_sth(\$sth);
260 \$sth->finish;
261  
262 return \$obj;
263 }
264 END_CODE
265  
266 $code .= $self->_table_mutators($tbl_name, $field_list)
267 48 ▶ unless $self->{readonly};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"readonly"} 46 2 48
268  
269 50 return $code;
270 }
271  
272 48 ▶sub _add_accessors {
Subroutine _add_accessors: called CC=2 SCAR=6.9
273 50 my ($self, $pkg_name, @accessor_fields) = @_;
274  
275 49 my $accessor_sub;
276 49 ▶ if ($self->{readonly}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'readonly'}) { } 2 46 48
277 3 $accessor_sub = '$_[0]->{$attr}';
278 } else {
279 46 $accessor_sub = '
280 $_[0]->{$attr} = $_[1] if defined $_[1];
281 $_[0]->{$attr};'
282 }
283  
284 48 my $field_list = join ' ', @accessor_fields;
285 48 return <<"END_CODE";
286 {
287 no strict 'refs';
288 for my \$attr (qw( $field_list )) {
289 *\$attr = sub {
290 $accessor_sub
291 };
292 }
293 }
294 END_CODE
295 }
296  
297 46 ▶sub _table_mutators {
Subroutine _table_mutators: called CC=6 SCAR=17.9
298 46 my ($self, $tbl_name, $field_list) = @_;
299 46 my $pkg_name = $self->{tbl_names}{$tbl_name};
300  
301 46 my $insert_fields = join ', ', @$field_list;
302 50 my $insert_params = join ', ', ('?') x @$field_list;
303 50 my $insert_values = join ', ', map { "\$self->{$_}" } @$field_list;
304 50 my $handle_autoincrement = '';
305 my $init_all_attribs = join ",\n ",
306 50 map { "'$_' => \$params{'$_'}" } @$field_list;
307  
308 47 my @key = $self->dbh->primary_key(undef, undef, $tbl_name);
309 47 ▶ if (@key == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@key == 1) 12 34 46
310 13 my $key_field = $key[0];
311 13 $handle_autoincrement = qq(
312 \$self->{$key_field} =
313 \$self->dbh->last_insert_id(undef, undef, qw( $tbl_name $key_field ))
314 unless defined \$self->{$key_field};);
315 }
316  
317 47 my $code = <<"END_CODE";
318  
319 sub _ormlette_new {
320 my (\$class, \%params) = \@_;
321 bless {
322 $init_all_attribs
323 }, \$class;
324 }
325  
326 sub create {
327 my \$class = shift;
328 \$class->new(\@_)->insert;
329 }
330  
331 sub insert {
332 my \$self = shift;
333 my \$sql =
334 'INSERT INTO $tbl_name ( $insert_fields ) VALUES ( $insert_params )';
335 my \$sth = \$self->dbh->prepare_cached(\$sql);
336 \$sth->execute($insert_values);
337 $handle_autoincrement
338 return \$self;
339 }
340  
341 sub truncate {
342 my \$class = shift;
343 croak '->truncate must be called as a class method' if ref \$class;
344 my \$sql = 'DELETE FROM $tbl_name';
345 my \$sth = dbh->prepare_cached(\$sql);
346 \$sth->execute;
347 }
348  
349 END_CODE
350  
351 47 ▶ if (@key) {
Branch: 1 branch
BranchTrueFalseTotal
if (@key) { } 16 32 48
352 15 my $key_criteria = join ' AND ', map { "$_ = ?" } @key;
353 15 my $key_values = join ', ', map { "\$self->{$_}" } @key;
354 15 my $update_fields = join ', ', map { "$_ = ?" } @$field_list;
355  
356 15 $code .= <<"END_CODE";
357 sub update {
358 my \$self = shift;
359 my \$sql = 'UPDATE $tbl_name SET $update_fields WHERE $key_criteria';
360 my \$sth = \$self->dbh->prepare_cached(\$sql);
361 my \$changed = \$sth->execute($insert_values, $key_values);
362 \$self->insert unless \$changed > 0;
363  
364 return \$self;
365 }
366  
367 sub delete {
368 my \$self = shift;
369 my \$sql = 'DELETE FROM $tbl_name';
370 if (ref \$self) {
371 \$sql .= ' WHERE $key_criteria';
372 \@_ = ( $key_values );
373 } else {
374 return unless \@_;
375 \$sql .= ' ' . shift;
376 }
377 my \$sth = \$self->dbh->prepare_cached(\$sql);
378 \$sth->execute(\@_);
379 }
380 END_CODE
381 } else { # no primary key
382 31 $code .= <<"END_CODE";
383 sub delete {
384 my \$class = shift;
385 croak '->delete may not be called as an instance method for an unkeyed table'
386 if ref \$class;
387 return unless \@_;
388 my \$sql = 'DELETE FROM $tbl_name ' . shift;
389 my \$sth = \$class->dbh->prepare_cached(\$sql);
390 \$sth->execute(\@_);
391 }
392 END_CODE
393 }
394  
395 46 ▶ unless ($pkg_name->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pkg_name->can("new")) 45 1 46
396 45 $code .= '
397 sub new { my $class = shift; $class->_ormlette_new(@_); }
398 ';
399 }
400  
401 46 ▶ if (@key) {
Branch: 1 branch
BranchTrueFalseTotal
if (@key) 16 34 50
402 15 ▶ my $destroy_name =
Branch: 1 branch
BranchTrueFalseTotal
$pkg_name->can('DESTROY') ? : 1 15 16
403 $pkg_name->can('DESTROY') ? '_ormlette_DESTROY' : 'DESTROY';
404 15 $code .= "
405 sub mark_dirty { \$_[0]->{_dirty} = 1 }
406 sub mark_clean { \$_[0]->{_dirty} = 0 }
407 sub dirty { return \$_[0]->{_dirty} }
408  
409 sub $destroy_name { \$_[0]->update if \$_[0]->{_dirty} }"
410 }
411  
412 46 return $code;
413 }
414  
415 6 1;
416  
417 =pod
418  
419 =head1 NAME
420  
421 Ormlette - Light and fluffy object persistence
422  
423 =head1 VERSION
424  
425 version 0.005
426  
427 =head1 SYNOPSIS
428  
429 my $dbh = DBI->connect(...);
430 Ormlette->init($dbh, tables => [ 'my_table' ], namespace => Test);
431  
432 my $obj = Test::MyTable->create(foo => 1, bar => 3);
433 print Test::MyTable->load(foo => 1)->bar; # 3
434  
435 =head1 DESCRIPTION
436  
437 Ormlette is a simple object persistence mechanism which is specifically
438 designed to avoid imposing any requirements on how your code is organized or
439 what base classes you use.
440  
441 Rather than requiring your classes to inherit from it, Ormlette is initialized
442 by passing an open database handle to C<< Ormlette->init >>, at which point
443 Ormlette will identify the tables in that database and, for each one, derive a
444 package name by camel-casing the table name using C<_> in the table name as a
445 word break (e.g., C<foo_bar> becomes C<FooBar>) and prepending the current
446 package's name. It will then inject its methods (see below) into the resulting
447 package. It will also create accessors corresponding to each of the table's
448 fields unless these accessors already exist.
449  
450 Note that, if you want to define your own methods within a package which will
451 have methods injected by Ormlette, you should load the corresponding module
452 (with C<use> or C<require>) before doing any Ormlette initializations affecting
453 that package so that Ormlette will know what methods you've defined yourself
454 and avoid interfering with them.
455  
456 =head1 Ormlette Core Methods
457  
458 =head2 init ($dbh, %params)
459  
460 Attaches Ormlette methods to classes corresponding to tables in the database
461 connected to $dbh. Recognized parameters:
462  
463 =over 4
464  
465 =head3 debug
466  
467 The C<debug> option will cause additional debugging information to be printed
468 to STDERR as Ormlette does its initialization. At this point, this consists
469 solely of the generated source code for each package affected by Ormlette.
470  
471 =head3 isa
472  
473 Specifies that all Ormlette-generated classes should be made subclasses of
474 the C<isa> package. This is done by directly setting C<@ISA>, not via C<use
475 base> or C<use parent>, so you are responsible for ensuring that the
476 specified class is available to use as a parent.
477  
478 If Ormlette is adding to a class which already exists and already has a
479 parent in C<@ISA>, the existing parent will be untouched and the C<isa> option
480 will have no effect on that class.
481  
482 =head3 ignore_root
483  
484 Ormlette will normally inject a C<dbh> method into the base namespace of its
485 generated code, providing access to the source database. This is not always
486 desirable. In such cases, setting C<ignore_root> will prevent Ormlette from
487 making any modifications to that package.
488  
489 =head3 ignore_tables
490  
491 Ormlette will normally generate classes corresponding to all tables found
492 in the database. If there are tables which should be skipped over, a
493 reference to an array of table names to skip can be passed in the
494 C<ignore_tables> parameter.
495  
496 If you prefer to list the tables to include rather than the tables to
497 exclude, use C<tables>. There should never be a reason to specify both
498 C<tables> and C<ignore_tables>, but, if this is done, C<tables> will take
499 precedence and C<ignore_tables> will be silently ignored.
500  
501 =head3 namespace
502  
503 By default, Ormlette will use the name of the package which calls C<init> as
504 the base namespace for its generated code. If you want the code to be placed
505 into a different namespace, use the C<namespace> parameter to override this
506 default.
507  
508 =head3 readonly
509  
510 If C<readonly> is set to a true value, no constructors or database-altering
511 methods will be created and generated accessors will be read-only.
512  
513 =head3 tables
514  
515 If you only require Ormlette code to be generated for some of the tables in
516 your database, providing a reference to an array of table names in the
517 C<tables> parameter will cause all other tables to be ignored.
518  
519 If you prefer to list the tables to exclude rather than those to include,
520 use C<ignore_tables>.
521  
522 =back
523  
524 =head2 dbh
525  
526 Returns the internal database handle used for database interaction. Can be
527 called on the core Ormlette object, the root namespace of its generated code
528 (unless the C<ignore_root> option is set when calling C<init>), or any of the
529 persistent classes generated in that namespace.
530  
531 =head1 Root Namespace Methods
532  
533 If the C<ignore_root> option is set when calling C<init>, no methods will
534 be generated in the root namespace.
535  
536 =head2 dbh
537  
538 Returns the database handle attached by Ormlette to the root namespace. If
539 multiple Ormlette objects have been instantiated with the same C<namespace>,
540 this will return the handle corresponding to the first Ormlette initialized
541 there.
542  
543 =head1 Table Class Methods
544  
545 In addition to the methods listed below, accessors will be generated for each
546 field found in the table unless an accessor for that field already exists,
547 providing the convenience of not having to create all the accessors yourself
548 while also allowing for custom accessors to be used where needed. Generated
549 accessors will be read-only if C<readonly> is set or writable using the
550 C<< $obj->attr('new value') >> convention otherwise.
551  
552 Note that the generated accessors are extremely simple and make no attempt at
553 performing any form of data validation, so you may wish to use another fine
554 CPAN module to generate accessors before initializing Ormlette.
555  
556 =head2 create
557  
558 Constructs an object by calling C<new>, then uses C<insert> to immediately
559 store it to the database.
560  
561 This method will not be generated if C<readonly> is set.
562  
563 =head2 dbh
564  
565 Returns the database handle used by Ormlette operations on this class.
566  
567 =head2 delete
568  
569 =head2 delete('WHERE name = ?', 'John Doe')
570  
571 As a class method, deletes all objects matching the criteria specified in the
572 parameters. In an attempt to avoid data loss from accidentally calling
573 C<delete> as a class method when intending to use it as an instance method,
574 nothing will be done if no criteria are provided.
575  
576 As an instance method, deletes the object from the database. In this case, any
577 parameters will be ignored. The in-memory object is unaffected and remains
578 available for further use, including re-saving it to the database.
579  
580 This method will not be generated if C<readonly> is set. The instance method
581 variant will only be generated for tables which have a primary key.
582  
583 =head2 dirty
584  
585 Read-only flag indicating whether an object is "dirty" (i.e., has unsaved
586 changes). This flag is not maintained automatically; code using the object
587 must use its C<mark_dirty> method to set the flag and C<mark_clean> to clear
588 it.
589  
590 If an object is destroyed while dirty, a C<DESTROY> handler will automatically
591 call its C<update> method to write changes to the database. If the class
592 already has a C<DESTROY> handler prior to Ormlette initialization, this check
593 is instead placed into an C<_ormlette_DESTROY> method, which the other
594 C<DESTROY> should call if autoupdate functionality is desired.
595  
596 The C<dirty> attribute, C<mark_dirty> and C<mark_clean> methods, and C<DESTROY>
597 handler will not be generated if C<readonly> is set or for tables which do
598 not have a primary key.
599  
600 =head2 insert
601  
602 Inserts the object into the database as a new record. This method will fail if
603 the record cannot be inserted. If the table uses an autoincrement/serial
604 primary key and no value for that key is set in the object, the in-memory
605 object will be updated with the id assigned by the database.
606  
607 This method will not be generated if C<readonly> is set.
608  
609 =head2 iterate(sub { print $_->id })
610  
611 =head2 iterate(sub { print $_->name }, 'WHERE age > ?', 18)
612  
613 Takes a sub reference as the first parameter and passes each object returned by
614 the subsequent query to the referenced sub in C<$_> for processing. The
615 primary difference between this method and C<select> is that C<iterate> only
616 loads one record into memory at a time, while C<select> loads all records at
617 once, which may require unacceptable amounts of memory when dealing with larger
618 data sets.
619  
620 =head2 mark_clean
621  
622 Clears an object's C<dirty> flag.
623  
624 =head2 mark_dirty
625  
626 Sets an object's C<dirty> flag.
627  
628 =head2 new
629  
630 Basic constructor which accepts a hash of values and blesses them into the
631 class. If a ->new method has already been defined, it will not be replaced.
632 If you wish to retain the default constructor functionality within your
633 custom ->new method, you can call $class->_ormlette_new to do so.
634  
635 This method will not be generated if C<readonly> is set.
636  
637 =head2 load(1)
638  
639 =head2 load(foo => 1, bar => 2)
640  
641 Retrieves a single object from the database based on the specified criteria.
642  
643 If the table has a single-field primary key, passing a single argument will
644 retrieve the record with that value as its primary key.
645  
646 Lookups on non-key fields or multiple-field primary keys can be performed by
647 passing a hash of field => value pairs. If more than one record matches the
648 given criteria, only one will be returned; which one is returned may or may
649 not be consistent from one call to the next.
650  
651 Returns undef if no matching record exists.
652  
653 =head2 select
654  
655 =head2 select('WHERE id = 42');
656  
657 =head2 select('WHERE id > ? ORDER BY name LIMIT 5', 3);
658  
659 Returns a reference to an array containing all objects matching the query
660 specified in the parameters, in the order returned by that query. If no
661 parameters are provided, returns objects for all records in the database's
662 natural sort order.
663  
664 As this method simply appends its parameters to "SELECT (fields) FROM (table)",
665 arbitrarily-complex queries can be built up in the parameters, including joins
666 and subqueries.
667  
668 =head2 table
669  
670 Returns the table name in which Ormlette stores this class's data.
671  
672 =head2 update
673  
674 Updates the object's existing database record. This method will implicitly
675 call C<insert> if the object does not already exist in the database.
676  
677 This method will not be generated if C<readonly> is set or for tables which
678 do not have a primary key.
679  
680 =head1 TO DO
681  
682 =over 4
683  
684 =item *
685  
686 Verify functionality with DBD back-ends other than SQLite
687  
688 =item *
689  
690 Add support for multi-level package hierarchies
691  
692 =item *
693  
694 Allow all Ormlette functions to be overridden, not just C<new> and accessors
695  
696 =item *
697  
698 Add transaction support
699  
700 =item *
701  
702 Cache loaded objects to prevent duplication
703  
704 =back
705  
706 =head1 CREDITS
707  
708 Although it is not intended as a drop-in replacement, Ormlette's API and
709 general coding style are heavily influenced by Adam Kennedy's L<ORLite>.
710  
711 =head1 AUTHOR
712  
713 Dave Sherohman <dsheroh@cpan.org>
714  
715 =head1 COPYRIGHT AND LICENSE
716  
717 This software is copyright (c) 2012 by Dave Sherohman.
718  
719 This is free software; you can redistribute it and/or modify it under
720 the same terms as the Perl 5 programming language system itself.
721  
722 =cut
723  
724 __END__
↑ Summary