File Coverage

blib/lib/App/Schema/Data.pm
Criterion Covered Total %
statement 41 77 53.2
branch 3 20 15.0
condition 6 6 100.0
subroutine 8 9 88.8
pod 2 2 100.0
total 60 114 52.6


line stmt bran cond sub pod time code
1             package App::Schema::Data;
2              
3 4     4   76238 use strict;
  4         25  
  4         112  
4 4     4   20 use warnings;
  4         9  
  4         92  
5              
6 4     4   2043 use English;
  4         14921  
  4         22  
7 4     4   3671 use Error::Pure qw(err);
  4         30481  
  4         89  
8 4     4   8515 use Getopt::Std;
  4         213  
  4         242  
9 4     4   5036 use Unicode::UTF8 qw(decode_utf8 encode_utf8);
  4         3469  
  4         3268  
10              
11             our $VERSION = 0.05;
12              
13             # Constructor.
14             sub new {
15 5     5 1 6169 my ($class, @params) = @_;
16              
17             # Create object.
18 5         14 my $self = bless {}, $class;
19              
20             # Object.
21 5         28 return $self;
22             }
23              
24             # Run.
25             sub run {
26 4     4 1 8 my $self = shift;
27              
28             # Process arguments.
29 4         22 $self->{'_opts'} = {
30             'h' => 0,
31             'l' => undef,
32             'p' => '',
33             'u' => '',
34             'v' => undef,
35             };
36 4 100 100     29 if (! getopts('hl:p:u:v:', $self->{'_opts'})
      100        
37             || $self->{'_opts'}->{'h'}
38             || @ARGV < 2) {
39              
40 3         356 print STDERR "Usage: $0 [-h] [-l plugin:...] [-p password] [-u user] [-v schema_version] [--version] dsn ".
41             "schema_data_module var_key=var_value ..\n";
42 3         40 print STDERR "\t-h\t\t\tPrint help.\n";
43 3         32 print STDERR "\t-l plugin:...\t\tLoad data from plugin.\n";
44 3         68 print STDERR "\t-p password\t\tDatabase password.\n";
45 3         35 print STDERR "\t-u user\t\t\tDatabase user.\n";
46 3         31 print STDERR "\t-v schema_version\tSchema version (default is ".
47             "latest version).\n";
48 3         35 print STDERR "\t--version\t\tPrint version.\n";
49 3         30 print STDERR "\tdsn\t\t\tDatabase DSN. e.g. dbi:SQLite:dbname=ex1.db\n";
50 3         30 print STDERR "\tschema_data_module\tName of Schema data module.\n";
51 3         39 print STDERR "\tvar_key=var_value\tVariable keys with values for insert.\n";
52 3         30 return 1;
53             }
54 1         30 $self->{'_dsn'} = shift @ARGV;
55 1         3 $self->{'_schema_data_module'} = shift @ARGV;
56             $self->{'_variables'} = {
57             map {
58 1         3 my ($k, $v) = split m/=/ms, decode_utf8($_), 2;
  0         0  
59 0         0 ($k => $v);
60             } @ARGV
61             };
62              
63 1         66 eval "require $self->{'_schema_data_module'}";
64 1 50       9 if ($EVAL_ERROR) {
65             err 'Cannot load Schema data module.',
66 1         7 'Module name', $self->{'_schema_data_module'},
67             'Error', $EVAL_ERROR,
68             ;
69             }
70              
71 0           my $data_module;
72             my $data_version;
73 0 0         if ($self->{'_schema_data_module'}->can('new')) {
74             my $versioned_data = $self->{'_schema_data_module'}->new(
75             $self->{'_opts'}->{'v'} ? (
76 0 0         'version' => $self->{'_opts'}->{'v'},
77             ) : (),
78             );
79 0           $data_module = $versioned_data->schema_data;
80 0           $data_version = $versioned_data->version;
81             } else {
82 0           $data_module = $self->{'_schema_data_module'};
83             }
84 0           my $data = eval {
85             $data_module->new(
86             'db_options' => {},
87             'db_password' => $self->{'_opts'}->{'p'},
88             'db_user' => $self->{'_opts'}->{'u'},
89 0           'dsn' => $self->{'_dsn'},
90             );
91             };
92 0 0         if ($EVAL_ERROR) {
93 0           err 'Cannot connect to Schema database.',
94             'Error', $EVAL_ERROR,
95             ;
96             }
97              
98             # Check Schema::Data::Data instance.
99 0 0         if (! $data->isa('Schema::Data::Data')) {
100 0           err "Schema data module must be a 'Schema::Data::Data' instance.";
101             }
102              
103 0           $data->insert($self->{'_variables'});
104              
105 0           my $print_version = '';
106 0 0         if (defined $data_version) {
107 0           $print_version = '(v'.$data_version.') ';
108             }
109 0           print "Schema data ${print_version}from '$self->{'_schema_data_module'}' was ".
110             "inserted to '$self->{'_dsn'}'.\n";
111              
112 0           my @plugins;
113 0 0         if (defined $self->{'_opts'}->{'l'}) {
114 0           @plugins = split m/:/ms, $self->{'_opts'}->{'l'};
115             }
116 0           foreach my $plugin (@plugins) {
117              
118             # Load plugin object.
119 0           my $plugin_module = "$self->{'_schema_data_module'}::Plugin::$plugin";
120 0           eval "require $plugin_module";
121 0 0         if ($EVAL_ERROR) {
122 0           err 'Cannot load Schema data plugin module.',
123             'Module name', $plugin_module,
124             'Error', $EVAL_ERROR,
125             ;
126             }
127              
128             # Create plugin object.
129 0           my $plugin = eval {
130             $plugin_module->new(
131             'schema' => $data->schema,
132             'verbose_cb' => sub {
133 0     0     my $message = shift;
134 0           print encode_utf8($message)."\n";
135 0           return;
136             },
137 0           );
138             };
139 0 0         if ($EVAL_ERROR) {
140 0           err "Cannot create '$plugin_module' object.",
141             'Error', $EVAL_ERROR,
142             ;
143             }
144              
145             # Load plugin data.
146 0           $plugin->load($self->{'_variables'});
147             }
148              
149 0           return 0;
150             }
151              
152             1;
153              
154              
155             __END__