File Coverage

blib/lib/Schema/Data/Data.pm
Criterion Covered Total %
statement 18 70 25.7
branch 0 18 0.0
condition n/a
subroutine 6 12 50.0
pod 0 5 0.0
total 24 105 22.8


line stmt bran cond sub pod time code
1             package Schema::Data::Data;
2              
3 2     2   73401 use strict;
  2         10  
  2         58  
4 2     2   10 use warnings;
  2         4  
  2         54  
5              
6 2     2   938 use Class::Utils qw(set_params);
  2         61640  
  2         37  
7 2     2   134 use English;
  2         4  
  2         12  
8 2     2   874 use Error::Pure qw(err);
  2         4  
  2         99  
9 2     2   13 use List::Util qw(none);
  2         4  
  2         1496  
10              
11             our $VERSION = 0.05;
12              
13             sub new {
14 0     0 0   my ($class, @params) = @_;
15              
16             # Create object.
17 0           my $self = bless {}, $class;
18              
19             # db debug.
20 0           $self->{'db_debug'} = 0;
21              
22             # db user password.
23 0           $self->{'db_password'} = undef;
24              
25             # db user name.
26 0           $self->{'db_user'} = undef;
27              
28             # db options.
29 0           $self->{'db_options'} = {};
30              
31             # DSN.
32 0           $self->{'dsn'} = undef;
33              
34             # Schema module.
35 0           $self->{'schema_module'} = undef;
36              
37             # Process parameters.
38 0           set_params($self, @params);
39              
40 0 0         if (! defined $self->{'schema_module'}) {
41 0           err "Parameter 'schema_module' is required.";
42             }
43              
44             # Load schema.
45 0           eval 'require '.$self->{'schema_module'};
46 0 0         if ($EVAL_ERROR) {
47             err 'Cannot load Schema module.',
48 0           'Module name', $self->{'schema_module'},
49             'Error', $EVAL_ERROR,
50             ;
51             }
52              
53 0           $self->{'_schema'} = eval {
54             $self->{'schema_module'}->connect(
55             $self->{'dsn'},
56             $self->{'db_user'},
57             $self->{'db_password'},
58 0           $self->{'db_options'},
59             );
60             };
61 0 0         if ($EVAL_ERROR) {
62 0           err 'Cannot connect to Schema database.',
63             'Error', $EVAL_ERROR,
64             ;
65             }
66 0 0         if (! $self->{'_schema'}->isa('DBIx::Class::Schema')) {
67             err "Instance of schema must be a 'DBIx::Class::Schema' object.",
68 0           'Reference', $self->{'_schema'}->isa,
69             ;
70             }
71              
72 0 0         if ($self->{'db_debug'}) {
73 0           $self->{'_schema'}->storage->debug(1);
74             }
75              
76 0           return $self;
77             }
78              
79             sub data {
80 0     0 0   my ($self, $variables_hr) = @_;
81              
82 0           err 'Package __PACKAGE__ is abstract class. data() method must be '.
83             'defined in inherited class.';
84             }
85              
86             sub insert {
87 0     0 0   my ($self, $variables_hr) = @_;
88              
89             # Check required variables.
90 0           foreach my $required_variable ($self->required_variables) {
91 0 0         if (! exists $variables_hr->{$required_variable}) {
92 0           err "Variable '$required_variable' is required.";
93             }
94             }
95              
96 0           my $data_hr = $self->data($variables_hr);
97 0           my %sources = map { ($self->{'_schema'}->source($_)->name => $_ ) }
98 0           $self->{'_schema'}->sources;
99              
100             # Explicit order.
101 0           my @order;
102 0 0         if ($data_hr->{'_order'}) {
103 0           @order = @{$data_hr->{'_order'}};
  0            
104 0           foreach my $db_name (keys %sources) {
105 0 0   0     if (none { $_ eq $db_name } @order) {
  0            
106 0           push @order, $db_name;
107             }
108             }
109             # Random DB names as order.
110             } else {
111 0           @order = keys %sources;
112             }
113              
114             # Insert.
115 0           foreach my $db_name (@order) {
116              
117             # Skip db table without data.
118 0 0         if (! exists $data_hr->{$db_name}) {
119 0           next;
120             }
121              
122 0           foreach my $data_hr (@{$data_hr->{$db_name}}) {
  0            
123 0           $self->{'_schema'}->resultset($sources{$db_name})->create($data_hr);
124             }
125             }
126              
127 0           return;
128             }
129              
130             sub required_variables {
131 0     0 0   my $self = shift;
132              
133 0           err 'Package __PACKAGE__ is abstract class. required_variables() method must be '.
134             'defined in inherited class.';
135             }
136              
137             sub schema {
138 0     0 0   my $self = shift;
139              
140 0           return $self->{'_schema'};
141             }
142              
143             1;
144              
145             __END__