Recipes/DBIx.pm

filter: Statementstmt 51.6% 31 / 60 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 64.2% 9 / 14 Podpod 80.0% 4 / 5 totaltotal 37.9% 44 / 116 CC 14 SCAR 29.3
CC
14
Cov
36%
dbh
42.0
categories
30.0
recipes
20.0
CRAP 18.7
« Recipes.pm ↑ Summary Recipes/Edit.pm »
1 package CGI::Ex::Recipes::DBIx;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use DBD::SQLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use SQL::Abstract;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp qw(carp cluck croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use vars qw(@EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 require Exporter;
12 2 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 @EXPORT_OK = qw(
14 dbh
15 sql
16 create_tables
17 categories
18 recipes
19
20 );
21 2 our $VERSION = '0.02';
22  
23 0 ▶sub dbh {
Subroutine dbh: not called CC=6 SCAR=37.4
Pod documented
24 0 my $self = shift;
25 0 ▶ if (! $self->{'dbh'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dbh"}) 0 0 0
26
27 0 ▶ my $file = ($ENV{SITE_ROOT} || $self->base_dir_abs->[0] ) . '/' . $self->conf->{'db_file'}
Condition0%0/5
($ENV{'SITE_ROOT'} || $self->base_dir_abs->[0]) . '/' . $self->conf->{'db_file'} || './data/recipes.sqlite'
l!l
00
$ENV{'SITE_ROOT'} || $self->base_dir_abs->[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
($ENV{'SITE_ROOT'} || $self->base_dir_abs->[0]) . '/' . $self->conf->{'db_file'} || './data/recipes.sqlite'
$ENV{'SITE_ROOT'} $self->base_dir_abs->[0]
Truth table0%0/3
A || B
A= $ENV{'SITE_ROOT'}B= $self->base_dir_abs->[0]
ABresult
1X1
011
000
28 || './data/recipes.sqlite';
29 0 my $exists = -e $file;
30 0 ▶ my $package = $self->{'_package'} || 'somethingsligthlylessborring';
Condition0%0/2
$self->{'_package'} || 'somethingsligthlylessborring'
l!l
00
MC/DC0%0/1
$self->{'_package'} || 'somethingsligthlylessborring'
$self->{'_package'}
Truth table0%0/2
A
A= $self->{'_package'}
Aresult
11
00
31 0 $self->{'dbh'} = DBI->connect(
32 "dbi:SQLite:dbname=$file", '', '',
33 {
34 #'private_'. $package => $package ,
35 RaiseError => 1,
36 }
37 );
38 0 ▶ $self->create_tables if !$exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $exists 0 0 0
39 0 warn 'New db connetion initiated!';
40 }
41 0 return $self->{'dbh'};
42 }
43  
44  
45 0 ▶sub create_tables {
Subroutine create_tables: not called CC=1 SCAR=6.9
Pod documented
46 0 my $self = shift;
47 #TODO:move SQL in a file
48 0 $self->dbh->do("CREATE TABLE recipes (
49 id INTEGER PRIMARY KEY AUTOINCREMENT,
50 -- pid: id of the category in which this row will be placed
51 pid INTEGER NOT NULL,
52 -- is_category: is this recipe a category or not
53 is_category INTEGER NOT NULL DEFAULT 0,
54 -- sortorder: id of the field after which this field will be showed
55 sortorder INTEGER NOT NULL,
56 -- title: title of the recipe
57 title VARCHAR(100) NOT NULL,
58 -- problem: short description of the problem which this recipe solves
59 problem VARCHAR(255) NOT NULL,
60 -- analysis: analysis of the problem (why it occured etc.)
61 analysis TEXT NOT NULL,
62 -- solution: provide one or several solutions
63 solution TEXT NOT NULL,
64 -- tstamp: last modification in unix timestamp format
65 tstamp INTEGER NOT NULL,
66 -- date_added: creation date in unix timestamp format
67 date_added INTEGER NOT NULL
68 )");
69  
70 0 $self->dbh->do("CREATE TABLE cache (
71 id VARCHAR(32) PRIMARY KEY,
72 value TEXT NOT NULL,
73 tstamp INTEGER NOT NULL,
74 expires INTEGER NOT NULL
75 )");
76 }
77  
78 0 ▶sub sql {
Subroutine sql: not called CC=2 SCAR=17.9
Pod undocumented
79 0 my $self = shift;
80 0 ▶ if (! $self->{'sql'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"sql"}) 0 0 0
81 0 $self->{'sql'} = SQL::Abstract->new;
82 }
83 0 return $self->{'sql'};
84 }
85  
86 #Suitable for preparing a left menu in some view/template
87 0 ▶sub categories {
Subroutine categories: not called CC=5 SCAR=34.0
Pod documented
88 0 my $self = shift;
89 0 ▶ my $fields = shift || [qw(id pid is_category title)];
Condition0%0/2
shift() || ['id', 'pid', 'is_category', 'title']
l!l
00
MC/DC0%0/1
shift() || ['id', 'pid', 'is_category', 'title']
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
90 0 ▶ my $where = shift || {pid=>0};
Condition0%0/2
shift() || {'pid', 0}
l!l
00
MC/DC0%0/1
shift() || {'pid', 0}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
91 #make shure we want categories
92 0 ▶ $where->{is_category} ||= 1;
Condition0%0/2
$where->{"is_category"} ||= 1
l!l
00
MC/DC0%0/1
$where->{"is_category"} ||= 1
$where->{"is_category"}
Truth table0%0/2
A
A= $where->{"is_category"}
Aresult
11
00
93 0 ▶ my $order = shift || ['sortorder'];
Condition0%0/2
shift() || ['sortorder']
l!l
00
MC/DC0%0/1
shift() || ['sortorder']
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
94 0 my ($s, @bind) = $self->sql->select('recipes',$fields,$where,$order);
95 0 return $self->dbh->selectall_arrayref($s,{Slice => {} ,MaxRows=>1000,},@bind);
96 }
97  
98 #note: this method is more general than categories. returns all recipes with given pid||0
99 0 ▶sub recipes {
Subroutine recipes: not called CC=4 SCAR=30.0
Pod documented
100 0 my $self = shift;
101 0 ▶ my $fields = shift || '*';
Condition0%0/2
shift() || '*'
l!l
00
MC/DC0%0/1
shift() || '*'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
102 0 ▶ my $where = shift || { pid => 0, id => { '!=', 0 } };
Condition0%0/2
shift() || {'pid', 0, 'id', {'!=', 0}}
l!l
00
MC/DC0%0/1
shift() || {'pid', 0, 'id', {'!=', 0}}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
103 0 ▶ my $order = shift || ['sortorder'];
Condition0%0/2
shift() || ['sortorder']
l!l
00
MC/DC0%0/1
shift() || ['sortorder']
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
104 0 my ($s, @bind) = $self->sql->select('recipes',$fields,$where,$order);
105 0 return $self->dbh->selectall_arrayref($s,{Slice => {} ,MaxRows=>1000,},@bind);
106 }
107  
108 2 1; # End of CGI::Ex::Recipes::DBIx
109  
110 __END__
« Recipes.pm ↑ Summary Recipes/Edit.pm »