AutoCRUD/DataSource.pm

filter: Statementstmt 83.7% 144 / 172 Branchbran 42.1% 27 / 64 Conditioncond 28.5% 6 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 96.3% 26 / 27 Podpod 100% 5 / 5 totaltotal 69.1% 208 / 301 CC 48 SCAR 24.6
CC
48
Cov
69%
_dbh
31.6
colgroups
16.5
_tablegroups
13.4
CRAP 11.7
« AutoCRUD/Controller/Schema.pm ↑ Summary AutoCRUD/Controller/Table.pm »
1 package App::AutoCRUD::DataSource;
2
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Clone qw/clone/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use List::MoreUtils qw/part/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Scalar::Does qw/does/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Data::Reach qw/reach/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use SQL::Abstract::FromQuery 0.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
14
15 1 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
16
17 1 has 'app' => (is => 'ro', isa => 'App::AutoCRUD', required => 1,
18 weak_ref => 1, handles => [qw/ dir/]);
19 1 has 'name' => (is => 'ro', isa => 'Str',
20 builder => '_name', lazy => 1);
21 1 has 'config' => (is => 'ro', isa => 'HashRef', reader => 'config_data',
22 builder => '_config', lazy => 1);
23 1 has 'dbh' => (is => 'ro', isa => 'DBI::db',
24 builder => '_dbh', lazy => 1);
25 1 has 'schema' => (is => 'ro', isa => 'Str|Object',
26 builder => '_schema', lazy => 1);
27 1 has 'query_parser' => (is => 'ro', isa => 'SQL::Abstract::FromQuery',
28 builder => '_query_parser', lazy => 1);
29 1 has 'tablegroups' => (is => 'ro', isa => 'ArrayRef',
30 builder => '_tablegroups', lazy => 1);
31
32 # indirectly generated through the _schema builder method
33 1 has 'generated_schema' => (is => 'ro', isa => 'Str', init_arg => undef);
34 1 has 'loaded_class' => (is => 'ro', isa => 'Str', init_arg => undef);
35
36
37
38 #======================================================================
39 # ATTRIBUTE BUILDERS
40 #======================================================================
41
42 1 ▶sub _dbh {
Subroutine _dbh: called CC=10 SCAR=34.5
43 1 my $self = shift;
44 1 my $dbh;
45
46 # create a connection from specifications found in config
47 1 ▶ if (my $connect_spec = $self->config(qw/dbh connect/)) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $connect_spec = $self->config('dbh', 'connect')) { } 1 0 1
elsif (my $schema = $self->{'schema'}) { } 0 0 0
48 1 ▶ if (does($connect_spec, 'ARRAY')) {
Branch: 3 branches
BranchTrueFalseTotal
if (&does($connect_spec, 'ARRAY')) { } 0 1 1
elsif (&does($connect_spec, 'CODE')) { } 1 0 1
elsif (&does($connect_spec, '""')) { } 0 0 0
49 # regular DBI connect using the given list of arguments
50 0 ▶ $dbh = DBI->connect(@$connect_spec)
Branch: 1 branch
BranchTrueFalseTotal
$dbh = "DBI"->connect(@$connect_spec) or die q[can't connect to ] . join(", ", @$connect_spec) 0 0 0
51 or die "can't connect to " . join(", ", @$connect_spec);
52 }
53 elsif (does($connect_spec, 'CODE')) {
54 1 ▶ $dbh = $connect_spec->()
Branch: 1 branch
BranchTrueFalseTotal
$dbh = &$connect_spec() or die 'coderef connection to ' . "self"->name . ' failed' 1 0 1
55 or die "coderef connection to " . self->name . " failed";
56 }
57 elsif (does($connect_spec, '""')) {
58 # config was a string : treat it as a line of Perl code
59 0 local $@;
60 0 ▶ $dbh = eval $connect_spec
Branch: 1 branch
BranchTrueFalseTotal
$dbh = eval $connect_spec or die $@ 0 0 0
61 or die $@;
62 }
63 else {
64 0 die "can't connect to " . $self->name . " (wrong config/dbh info)";
65 }
66 }
67
68 # or recover existing connection in schema
69 elsif (my $schema = $self->{schema}) { # bypass encapsulation to avoid
70 # circular calls with ->_schema()
71 0 $dbh = $schema->dbh;
72 }
73
74 # report failure if no connection found
75 $dbh
76 1 ▶ or die "no DBI/connect information in config for " . $self->name;
Branch: 1 branch
BranchTrueFalseTotal
$dbh or die 'no DBI/connect information in config for ' . $self->name 1 0 1
77
78 1 return $dbh;
79 }
80
81 1 ▶sub _schema {
Subroutine _schema: called CC=9 SCAR=25.5
82 1 my $self = shift;
83
84 1 my $required_class = $self->config('require');
85 1 ▶ my $schema_class = $self->config('schema_class') || $required_class;
Condition33%1/3
$self->config('schema_class') || $required_class
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->config('schema_class') || $required_class
$self->config('schema_class') $required_class
Truth table33%1/3
A || B
A= $self->config('schema_class')B= $required_class
ABresult
1X1
011
000
86
87 # if external code is required, load it
88 1 ▶ if ($required_class &&
Branch: 1 branch
BranchTrueFalseTotal
if ($required_class and not $schema_class && $self->app->is_class_loaded($schema_class)) 0 1 1
Condition16%1/6
$required_class and not $schema_class && $self->app->is_class_loaded($schema_class)
!ll&&!rl&&r
100
$schema_class && $self->app->is_class_loaded($schema_class)
!ll&&!rl&&r
000
MC/DC0%0/3
$required_class and not $schema_class && $self->app->is_class_loaded($schema_class)
$required_class $schema_class $self->app->is_class_loaded($schema_class)
Truth table25%1/4
A and not(B && C)
A= $required_classB= $schema_classC= $self->app->is_class_loaded($schema_class)
ABCresult
0XX0
1110
10X1
1101
89 !($schema_class && $self->app->is_class_loaded($schema_class))) {
90 0 ▶ $self->{loaded_class} = $self->app->try_load_class($required_class)
Branch: 1 branch
BranchTrueFalseTotal
$self->{"loaded_class"} = $self->app->try_load_class($required_class) or die "Can't locate $required_class" 0 0 0
91 or die "Can't locate $required_class";
92 0 ▶ $schema_class = $self->config('schema_class') || $self->{loaded_class};
Condition0%0/3
$self->config('schema_class') || $self->{'loaded_class'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->config('schema_class') || $self->{'loaded_class'}
$self->config('schema_class') $self->{'loaded_class'}
Truth table0%0/3
A || B
A= $self->config('schema_class')B= $self->{'loaded_class'}
ABresult
1X1
011
000
93 }
94
95 # generate class on the fly if needed
96 1 ▶ if (!$schema_class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema_class) 1 0 1
97 1 $schema_class = (ref $self) . "::_Auto_Schema::" . $self->name;
98
99 1 ▶ if (! $self->app->is_class_loaded($schema_class)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->app->is_class_loaded($schema_class)) 1 0 1
100 # build a schema generator from the DBI connection
101 1 my $dbh = $self->dbh;
102 1 require DBIx::DataModel::Schema::Generator;
103 1 my $generator = DBIx::DataModel::Schema::Generator->new(
104 -schema => $schema_class,
105 );
106 1 my @args = map {$self->config('dbh', $_)} qw/db_catalog db_schema db_type/;
107 1 $generator->parse_DBI($self->dbh, @args);
108
109 # generate and store perl code
110 1 $self->{generated_schema} = $generator->perl_code;
111
112 # eval source code on the fly
113 1 ▶ eval $self->{generated_schema};
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
114 }
115 }
116
117 1 return $schema_class;
118 }
119
120
121
122
123 1 ▶sub _query_parser {
Subroutine _query_parser: called CC=1 SCAR=0.0
124 1 my $self = shift;
125
126 1 return SQL::Abstract::FromQuery->new;
127 }
128
129
130 1 ▶sub _tablegroups {
Subroutine _tablegroups: called CC=10 SCAR=26.0
131 1 my ($self) = @_;
132
133 # get table info from database
134 1 my $dbh = $self->dbh;
135 1 ▶ my $sth = $dbh->table_info($self->config(qw/dbh db_catalog/),
Condition50%1/2
$self->config('dbh', 'db_type') || 'TABLE'
l!l
01
MC/DC0%0/1
$self->config('dbh', 'db_type') || 'TABLE'
$self->config('dbh', 'db_type')
Truth table50%1/2
A
A= $self->config('dbh', 'db_type')
Aresult
11
00
136 $self->config(qw/dbh db_schema/),
137 undef,
138 $self->config(qw/dbh db_type/) || 'TABLE',
139 );
140 1 my $tables = $sth->fetchall_hashref('TABLE_NAME');
141
142 # merge with descriptions from config
143 1 foreach my $table (keys %$tables) {
144 12 my $descr = $self->config(tables => $table => 'descr');
145 12 ▶ $tables->{$table}{descr} = $descr if $descr;
Branch: 1 branch
BranchTrueFalseTotal
if $descr 0 12 12
146 }
147
148 # grouping: merge with table info from config
149 1 ▶ my $tablegroups = clone $self->config('tablegroups') || [];
Condition50%1/2
$self->config('tablegroups') || []
l!l
01
MC/DC0%0/1
$self->config('tablegroups') || []
$self->config('tablegroups')
Truth table50%1/2
A
A= $self->config('tablegroups')
Aresult
11
00
150 1 foreach my $group (@$tablegroups) {
151 # tables declared in this group are removed from the global %$tables ..
152 0 my @declared_table_names = @{$group->{tables}};
153 0 my @extracted_tables = map {delete $tables->{$_}} @declared_table_names;
154
155 # .. and their full definitions take place of the declared names
156 0 $group->{tables} = [ grep {$_} @extracted_tables ];
157 }
158
159 # deal with remaining tables (
160 1 ▶ if (my @other_tables = sort keys %$tables) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@other_tables) = sort(keys %$tables)) 1 0 1
161
162 # Filter out based on the regexps in filters include & exclude
163 1 ▶ if (my $filter_include = $self->config(qw/filters include/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $filter_include = $self->config("filters", "include")) 1 0 1
164 1 @other_tables = grep { $_ =~ /$filter_include/ } @other_tables;
165 }
166 1 ▶ if (my $filter_exclude = $self->config(qw/filters exclude/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $filter_exclude = $self->config("filters", "exclude")) 1 0 1
167 1 @other_tables = grep { $_ !~ /$filter_exclude/ } @other_tables;
168 }
169
170 # if some unclassified tables remain after the filtering
171 1 ▶ if (@other_tables) {
Branch: 1 branch
BranchTrueFalseTotal
if (@other_tables) 1 0 1
172 push @$tablegroups, {
173 name => 'Unclassified tables',
174 descr => 'Present in database but unlisted in config',
175 1 tables => [ @{$tables}{@other_tables} ],
176 };
177 }
178 }
179
180 1 return $tablegroups;
181 }
182
183
184 1 ▶sub _config {
Subroutine _config: called CC=5 SCAR=20.1
185 1 my $self = shift;
186 1 ▶ my $config = $self->app->config(datasources => $self->name)
Branch: 1 branch
BranchTrueFalseTotal
my $config = $self->app->config("datasources", $self->name) or die 'no config for datasource ' . $self->name 1 0 1
187 or die "no config for datasource " . $self->name;
188
189 # shallow copy
190 1 $config = { %$config };
191
192 1 ▶ if (my $struct = $config->{structure}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $struct = $config->{"structure"}) 0 1 1
193 # get the structure config
194 0 ▶ my $struct_config = $self->app->config(structures => $struct)
Branch: 1 branch
BranchTrueFalseTotal
my $struct_config = $self->app->config("structures", $struct) or die "no config for structure $struct" 0 0 0
195 or die "no config for structure $struct";
196
197 # copy structure into datasource config
198 0 $config->{$_} = $struct_config->{$_} foreach keys %$struct_config;
199
200 }
201
202 1 return $config;
203 }
204
205
206
207 #======================================================================
208 # METHODS
209 #======================================================================
210
211 40 ▶sub config {
Subroutine config: called CC=1 SCAR=0.0
Pod documented
212 40 my ($self, @path) = @_;
213 40 return reach $self->config_data, @path;
214 }
215
216
217 1 ▶sub descr {
Subroutine descr: called CC=1 SCAR=0.0
Pod documented
218 1 my ($self) = @_;
219 1 return $self->config('descr');
220 }
221
222 16 ▶sub prepare_for_request {
Subroutine prepare_for_request: called CC=2 SCAR=7.1
Pod documented
223 16 my ($self, $req) = @_;
224
225 # if schema is in single-schema mode, make sure it is connected to
226 # the proper database
227 16 my $schema = $self->schema;
228 16 ▶ $schema->dbh($self->dbh) unless ref $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema 16 0 16
229 }
230
231
232 9 ▶sub primary_key {
Subroutine primary_key: called CC=1 SCAR=0.0
Pod documented
233 9 my ($self, $table) = @_;
234
235 9 return $self->_meta_table($table)->primary_key;
236 }
237
238
239 9 ▶sub colgroups {
Subroutine colgroups: called CC=13 SCAR=28.1
Pod documented
240 9 my ($self, $table) = @_;
241
242 # if info already in cache, return it
243 9 my $colgroups = $self->{colgroups}{$table};
244 9 ▶ return $colgroups if $colgroups;
Branch: 1 branch
BranchTrueFalseTotal
if $colgroups 7 2 9
245
246 # paths from this table
247 2 my $meta_table = $self->_meta_table($table);
248 2 my %paths = $meta_table->path;
249
250 # primary_key
251 2 my @pk = $meta_table->primary_key;
252
253 # get column info from database
254 2 my $db_catalog = $self->config(qw/dbh db_catalog/);
255 2 my $db_schema = $self->config(qw/dbh db_schema/);
256 2 my $sth = $self->dbh->column_info($db_catalog, $db_schema,
257 $table, undef);
258 2 my $columns = $sth->fetchall_hashref('COLUMN_NAME');
259
260 # TMP HACK, Oracle-specific. Q: How to design a good abstraction for this ?
261 $columns = $self->_columns_from_Oracle_synonym($db_schema, $table)
262 2 ▶ if ! keys %$columns and $self->dbh->{Driver}{Name} eq 'Oracle';
Branch: 1 branch
BranchTrueFalseTotal
if not keys %$columns and $self->dbh->{"Driver"}{"Name"} eq "Oracle" 0 2 2
Condition33%1/3
not keys %$columns and $self->dbh->{"Driver"}{"Name"} eq "Oracle"
!ll&&!rl&&r
200
MC/DC0%0/2
not keys %$columns and $self->dbh->{"Driver"}{"Name"} eq "Oracle"
not keys %$columns $self->dbh->{"Driver"}{"Name"} eq "Oracle"
Truth table33%1/3
not(A) and B
A= not keys %$columnsB= $self->dbh->{"Driver"}{"Name"} eq "Oracle"
ABresult
0X0
100
111
263
264 # mark primary keys
265 2 $columns->{$_}{is_pk} = 1 foreach @pk;
266
267 # attach paths (in alphabetic order) to relevant columns
268 2 foreach my $path (map {$paths{$_}} sort keys %paths) {
269 # name of column(s) from which this path starts
270 3 my %path_on = $path->on;
271 3 my ($col_name, @others) = keys %path_on;
272
273 # for the moment, don't handle assoc on multiple columns (TODO)
274 3 ▶ next if @others;
Branch: 1 branch
BranchTrueFalseTotal
if @others 0 3 3
275
276 3 ▶ my $col = $columns->{$col_name} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $col = $columns->{$col_name} or next 3 0 3
277 my $path_subdata = { name => $path->name,
278 to_table => $path->to->db_from,
279 3 foreign_key => $path_on{$col_name} };
280 3 push @{$col->{paths}}, $path_subdata;
281 }
282
283 # grouping: merge with column info from config
284 2 ▶ $colgroups = clone $self->config(tables => $table => 'colgroups') || [];
Condition50%1/2
$self->config('tables', $table, 'colgroups') || []
l!l
02
MC/DC0%0/1
$self->config('tables', $table, 'colgroups') || []
$self->config('tables', $table, 'colgroups')
Truth table50%1/2
A
A= $self->config('tables', $table, 'colgroups')
Aresult
11
00
285 2 foreach my $group (@$colgroups) {
286 0 my @columns;
287 0 foreach my $column (@{$group->{columns}}) {
288 0 my $col_name = $column->{name};
289 0 ▶ my $db_col = delete $columns->{$col_name} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $db_col = delete $columns->{$col_name} or next 0 0 0
290 0 push @columns, {%$db_col, %$column};
291 }
292 0 $group->{columns} = \@columns;
293 }
294
295 # deal with remaining columns (present in database but unlisted in
296 # config); sorted with primary keys first, then alphabetically.
297 4 ▶ my $sort_pk = sub { $columns->{$a}{is_pk} ? -1
Subroutine __ANON__: called CC=3 SCAR=11.0
298 4 ▶ : $columns->{$b}{is_pk} ? 1
Branch: 2 branches
BranchTrueFalseTotal
$columns->{$a}{'is_pk'} ? : 1 3 4
$columns->{$b}{'is_pk'} ? : 2 1 3
299 2 : $a cmp $b};
300 2 ▶ if (my @other_cols = sort $sort_pk keys %$columns) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@other_cols) = (sort $sort_pk keys %$columns)) 2 0 2
301 # build colgroup
302 push @$colgroups, {name => 'Unclassified columns',
303 2 columns => [ @{$columns}{@other_cols} ]};
304 }
305
306 # cache result and return
307 2 $self->{colgroups}{$table} = $colgroups;
308 2 return $colgroups;
309 }
310
311
312
313
314 0 ▶sub _columns_from_Oracle_synonym {
Subroutine _columns_from_Oracle_synonym: not called CC=2 SCAR=17.9
315 0 my ($self, $db_schema, $syn_name) = @_;
316
317 0 my $dbh = $self->dbh;
318 0 my $sql = "SELECT TABLE_OWNER, TABLE_NAME FROM ALL_SYNONYMS "
319 . "WHERE OWNER=? AND SYNONYM_NAME=?";
320 0 ▶ my ($owner, $table) = $dbh->selectrow_array($sql, {}, $db_schema, $syn_name)
Branch: 1 branch
BranchTrueFalseTotal
() = $dbh->selectrow_array($sql, {}, $db_schema, $syn_name) or return {} 0 0 0
321 or return {};
322
323 0 my $sth = $dbh->column_info(undef, $owner, $table, undef);
324 0 return $sth->fetchall_hashref('COLUMN_NAME')
325 }
326
327
328
329 11 ▶sub _meta_table {
Subroutine _meta_table: called CC=2 SCAR=7.1
330 11 my ($self, $table) = @_;
331
332 11 ▶ my $meta_table = $self->schema->metadm->db_table($table)
Branch: 1 branch
BranchTrueFalseTotal
my $meta_table = $self->schema->metadm->db_table($table) or die "no table in schema corresponds to '${table}'" 11 0 11
333 or die "no table in schema corresponds to '$table'";
334 11 return $meta_table;
335 }
336
337
338
339
340 1 1;
341
342 __END__
« AutoCRUD/Controller/Schema.pm ↑ Summary AutoCRUD/Controller/Table.pm »