Load.pm

filter: Statementstmt 11.6% 14 / 120 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 15.7% 3 / 19 Podpod 0.0% 0 / 6 totaltotal 8.4% 17 / 202 CC 24 SCAR 29.5
CC
24
Cov
8%
init_columns
56.0
setupCdbi
30.0
getcol_postgres
20.0
CRAP 19.1
↑ Summary Outline.pm »
1 package Fry::Lib::CDBI::Load;
2 1 ▶ use strict qw/subs vars/;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 our @ISA;
4 #local data
5 1 my %db_driver = (qw/mysql dbi:mysql: postgres dbi:Pg:dbname= sqlite
6 dbi:SQLite:dbname=/);
7 1 our $cdbi_class = "Class::DBI";
8 1 our $regex_operator;
9 #methods
10 0 ▶ sub _default_data {
Subroutine _default_data: not called CC=1 SCAR=6.9
11 return {
12 vars=>{
13 qw/user bozo
14 pwd bozo
15 db postgres
16 dbname useful/,
17 table=>'junk',
18 columns=>'',
19 action_columns=>'',
20 set_db_opts=>{AutoCommit=>1},
21 db_default=>{
22 postgres=>{
23 regex=>'~',
24 },
25 mysql=>{
26 regex=>'REGEXP',
27 },
28 sqlite=>{
29 },
30  
31 },
32 table_class=>'My::CDBI',
33 #flags
34 CDBI_Loader=>1,
35 get_columns=>1,
36 },
37 opts=>{
38 table=>{qw/a t type var noreset 1 default junk/,
39 0 ▶ action=>sub {shift->Var('cmd_class')->newTable($_[0])} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 action_columns=>{qw/a C type var noreset 1/,
41 0 ▶ action=> sub {$_[0]->setVar(action_columns=>[$_[0]->sub->parseNum($_[1],@{$_[0]->Var('columns')})])}
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 }
43 },
44 0 class=>'Class::DBI',
45 }
46 }
47 0 ▶ sub _initLib {
Subroutine _initLib: not called CC=1 SCAR=2.9
48 0 my $cls = shift;
49 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 #should be done by tags=class attribute
52 0 my $table_class = $cls->Var('table_class');
53 0 $cls->sub->_require ($cdbi_class);
54 0 push (@{"$table_class\::ISA"},$cdbi_class);
55  
56 0 $regex_operator = $cls->_regex_operator;
57  
58 0 $cls->setupCdbi;
59 }
60  
61 #Setup subs
62 0 ▶ sub setupCdbi {
Subroutine setupCdbi: not called CC=5 SCAR=34.0
Pod undocumented
63 0 my $cls = shift;
64
65 0 my $table_class = $cls->Var('table_class');
66 0 my ($dbname,$db,$user,$pwd,$set_db_opts) = $cls->varMany(qw/dbname db user pwd set_db_opts/);
67 warn("Database $db doesn't have a dsn entry and thus set_db was not set up correctly")
68 0 ▶ if (! exists $db_driver{$db});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $db_driver{$db} 0 0 0
69  
70 0 eval "use Class::DBI::Loader ";
71 0 ▶ if ($Class::DBI::Loader::VERSION < 0.07) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Class::DBI::Loader::VERSION < "0.07") 0 0 0
72 0 $cls->view("Need at least version 0.07 for Class::DBI::Loader");
73 0 $cls->setFlag('CDBI_Loader'=>0);
74  
75 }
76 0 ▶ if ($@ or ! $cls->Flag('CDBI_Loader')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $cls->Flag('CDBI_Loader')) { } 0 0 0
Condition0%0/3
$@ or not $cls->Flag("CDBI_Loader")
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not $cls->Flag("CDBI_Loader")
$@ not $cls->Flag("CDBI_Loader")
Truth table0%0/3
A or not(B)
A= $@B= not $cls->Flag("CDBI_Loader")
ABresult
1X1
011
000
77  
78 0 $table_class->set_db('Main',$db_driver{$db}.$dbname,$user,$pwd,$set_db_opts);
79 0 $cls->newCdbiTable;
80 }
81 0 else { $cls->initCdbiLoader }
82 }
83 0 ▶ sub newTable {
Subroutine newTable: not called CC=3 SCAR=24.8
Pod undocumented
84 0 my ($cls,$table) = @_;
85 0 eval "require Class::DBI::Loader";
86 0 ▶ if ($@ or ! $cls->Flag('CDBI_Loader')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $cls->Flag('CDBI_Loader')) { } 0 0 0
Condition0%0/3
$@ or not $cls->Flag("CDBI_Loader")
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not $cls->Flag("CDBI_Loader")
$@ not $cls->Flag("CDBI_Loader")
Truth table0%0/3
A or not(B)
A= $@B= not $cls->Flag("CDBI_Loader")
ABresult
1X1
011
000
87 0 $cls->setVar(table=>$table);
88 0 $cls->newCdbiTable;
89 }
90 0 else { $cls->initCdbiLoader(table=>$table) }
91 }
92 0 ▶ sub newCdbiTable {
Subroutine newCdbiTable: not called CC=2 SCAR=17.9
Pod undocumented
93 #new table info coming from var
94 0 my $cls = shift;
95 0 my $table_class = $cls->Var('table_class');
96 0 $table_class->table($cls->Var('table'));
97  
98 #td: only works for 3 databases
99 0 $cls->init_columns;
100  
101 0 $table_class->columns(All => @{$cls->Var('columns')});
102  
103 #td: set sequences for any db
104 0 ▶ $table_class->sequence($cls->Var('table').'_'.$cls->Var('columns')->[0].'_seq')
Branch: 1 branch
BranchTrueFalseTotal
if $cls->Var("db") eq "postgres" 0 0 0
105 if ($cls->Var('db') eq "postgres");
106 }
107 0 ▶ sub initCdbiLoader {
Subroutine initCdbiLoader: not called CC=4 SCAR=26.4
Pod undocumented
108 0 my ($cls,%arg) = @_;
109 0 my %set = %arg;
110  
111 0 for (qw/pwd user db dbname table set_db_opts/) {
112 0 ▶ $arg{$_} = $arg{$_} || $cls->Var($_)
Condition0%0/3
$arg{$_} || $cls->Var($_)
l!l&&r!l&&!r
000
MC/DC0%0/2
$arg{$_} || $cls->Var($_)
$arg{$_} $cls->Var($_)
Truth table0%0/3
A || B
A= $arg{$_}B= $cls->Var($_)
ABresult
1X1
011
000
113 }
114
115 my $loader = Class::DBI::Loader->new(
116 dsn => $db_driver{$arg{db}}.$arg{dbname},
117 user => $arg{user},
118 password => $arg{pwd},
119 options=>$arg{set_db_opts},
120 #tables=>[$arg{table}],
121 constraint=>"^$arg{table}\$",
122 0 ▶ namespace => ucfirst($arg{db}),
Branch: 1 branch
BranchTrueFalseTotal
my $loader = "Class::DBI::Loader"->new("dsn", $db_driver{$arg{"db"}} . $arg{"dbname"}, "user", $arg{"user"}, "password", $arg{"pwd"}, "options", $arg{"set_db_opts"}, "constraint", qq[^$arg{"table"}\$], "namespace", ucfirst $arg{"db"}) or die "new CDBIL object failed: $@" 0 0 0
123 ) or die "new CDBIL object failed: $@";
124  
125 #if new definition has been successful
126 0 $cls->setVar(%set);
127  
128 #subclass latest shell and its fns into $tableclass
129 0 my $table_class = $loader->find_class($arg{table});
130 0 $cls->setVar(table_class=>$table_class);
131  
132 #hack: each Class::DBI::* loaded table first has cdbi subclass + then class::db::*
133 #as parents
134 0 ▶ { no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
135 0 my $tc = $cls->Var('table_class');
136 0 unshift(@{"$tc\::ISA"},'My::CDBI');
137 }
138  
139 #hack: don't pass existing columns b/c they're out of order
140 0 $cls->init_columns; #(columns=>[map {$_->name} $table_class->columns('All')]);
141 }
142 #Internal subs
143 0 ▶ sub _regex_operator {
Subroutine _regex_operator: not called CC=2 SCAR=17.9
144 0 ▶ $_[0]->Var('db_default')->{$_[0]->Var('db')}->{regex} || "LIKE"
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->Var("db_default")->{$_[0]->Var("db")}{"regex"} or "LIKE" 0 0 0
145 }
146 0 ▶ sub dbiSource {
Subroutine dbiSource: not called CC=1 SCAR=6.9
Pod undocumented
147 0 my $cls = shift;
148 0 my ($db,$dbname,$user,$pwd) = $cls->varMany(qw/db dbname user pwd/);
149 0 return ($db_driver{$db}.$dbname,$user,$pwd);
150 }
151 0 ▶ sub init_columns {
Subroutine init_columns: not called CC=7 SCAR=40.3
Pod undocumented
152 #d: initializes column data dependent &columns
153 0 my ($cls,%arg) = @_;
154 0 ▶ my $db = $arg{db} || $cls->Var('db');
Condition0%0/3
$arg{'db'} || $cls->Var('db')
l!l&&r!l&&!r
000
MC/DC0%0/2
$arg{'db'} || $cls->Var('db')
$arg{'db'} $cls->Var('db')
Truth table0%0/3
A || B
A= $arg{'db'}B= $cls->Var('db')
ABresult
1X1
011
000
155 0 ▶ my $table = $arg{table} || $cls->Var('table');
Condition0%0/3
$arg{'table'} || $cls->Var('table')
l!l&&r!l&&!r
000
MC/DC0%0/2
$arg{'table'} || $cls->Var('table')
$arg{'table'} $cls->Var('table')
Truth table0%0/3
A || B
A= $arg{'table'}B= $cls->Var('table')
ABresult
1X1
011
000
156 0 my $table_class = $cls->Var('table_class');
157  
158 #set &columns
159 0 ▶ if (exists $arg{columns}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $arg{'columns'}) { } 0 0 0
elsif ($cls->Flag('get_columns')) { } 0 0 0
160 $cls->setVar(columns=>$arg{columns})
161 0 }
162 #create columns
163 elsif ($cls->Flag('get_columns')) {
164 0 my $method = "getcol_$db";
165 0 ▶ if ($table_class->can($method)){
Branch: 2 branches
BranchTrueFalseTotal
if ($table_class->can($method)) { } 0 0 0
elsif (my(@columns) = map({$_->name;} $table_class->columns('All') > 0)) { } 0 0 0
166 0 $cls->setVar(columns=>[$table_class->$method($table)]);
167 }
168 #fall back on defined columns from Class::DBI, whose order isn't dependable :(
169 0 elsif (my @columns = map {$_->name} $table_class->columns('All') > 0) {
170 0 $cls->setVar(columns=>[@columns]);
171 }
172 0 else { warn "Columns aren't loaded for this table" }
173 }
174  
175 #sync &action_columns with &columns
176 0 $cls->setVar(action_columns=>$cls->Var('columns'));
177 }
178  
179 package My::CDBI;
180  
181 0 ▶ sub search_regex {
Subroutine search_regex: not called CC=1 SCAR=6.9
182 0 my $cls = shift;
183 0 $cls->_do_search($regex_operator=> @_);
184 }
185 #subclass of Class::DBI b/c these functions expect it
186 #h: the rest of the functions have been copied from their Class::DBI::*
187 #all the getcol* does is return the columns of a table in order
188 0 ▶ sub getcol_postgres {
Subroutine getcol_postgres: not called CC=4 SCAR=30.0
189 0 my ($class,$table) = @_;
190 0 my @columns;
191 0 eval {require DBD::Pg};
192  
193 0 ▶ my $catalog = ($class->pg_version >= 7.3) ? "pg_catalog." : "";
Branch: 1 branch
BranchTrueFalseTotal
$class->pg_version >= 7.3 ? : 0 0 0
194 0 my $sth = $class->db_Main->prepare("SELECT a.attname, a.attnum FROM ${catalog}pg_class c, ${catalog}pg_attribute a
195 WHERE c.relname = ? AND a.attnum > 0 AND a.attrelid = c.oid ORDER BY a.attnum");
196 0 $sth->execute($table);
197 0 my $columns = $sth->fetchall_arrayref;
198 0 $sth->finish;
199  
200 0 foreach my $col(@$columns) {
201 # skip dropped column.
202 0 ▶ next if $col->[0] =~ /^\.+pg\.dropped\.\d+\.+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $col->[0] =~ /^\.+pg\.dropped\.\d+\.+$/ 0 0 0
203 0 push @columns, $col->[0];
204 }
205 0 return @columns;
206 }
207 0 ▶ sub getcol_sqlite {
Subroutine getcol_sqlite: not called CC=2 SCAR=17.9
208 0 my ($class,$table) = @_;
209 0 my $sth = $class->db_Main->prepare("PRAGMA table_info(?)");
210 0 $sth->execute($table);
211 0 my @columns;
212 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
213 0 push @columns,$row->{name};
214 }
215 0 $sth->finish;
216 0 return @columns;
217 }
218 0 ▶ sub getcol_mysql {
Subroutine getcol_mysql: not called CC=3 SCAR=24.8
219 #d:get columns of tb
220 #t:mysql
221 0 my $class = shift;
222 0 my (@columns, @pri);
223  
224 0 $class->set_sql(desc_table => 'DESCRIBE __TABLE__');
225 0 (my $sth = $class->sql_desc_table)->execute;
226  
227 0 ▶ while (my $hash = $sth->fetch_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hash = $sth->fetch_hash) 0 0 0
228 0 my ($col) = $hash->{field} =~ /(\w+)/;
229 0 push @columns, $col;
230 0 ▶ push @pri, $col if $hash->{key} eq "PRI";
Branch: 1 branch
BranchTrueFalseTotal
if $hash->{"key"} eq "PRI" 0 0 0
231 }
232 #$class->_croak("$table has no primary key") unless @pri;
233 return @columns
234 0 }
235 #used by getcol_postgres
236 0 ▶ sub pg_version {
Subroutine pg_version: not called CC=1 SCAR=6.9
237 0 my $class = shift;
238 0 my $dbh = $class->db_Main;
239 0 my $sth = $dbh->prepare("SELECT version()");
240 0 $sth->execute;
241 0 my($ver_str) = $sth->fetchrow_array;
242 0 $sth->finish;
243 0 my($ver) = $ver_str =~ m/^PostgreSQL ([\d\.]{3})/;
244 0 return $ver;
245 }
246 1 1;
247  
248 __END__
↑ Summary Outline.pm »