line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::Schema::Data; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
62744
|
use strict; |
|
4
|
|
|
|
|
20
|
|
|
4
|
|
|
|
|
95
|
|
4
|
4
|
|
|
4
|
|
17
|
use warnings; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
82
|
|
5
|
|
|
|
|
|
|
|
6
|
4
|
|
|
4
|
|
1763
|
use English; |
|
4
|
|
|
|
|
12685
|
|
|
4
|
|
|
|
|
19
|
|
7
|
4
|
|
|
4
|
|
3070
|
use Error::Pure qw(err); |
|
4
|
|
|
|
|
25529
|
|
|
4
|
|
|
|
|
84
|
|
8
|
4
|
|
|
4
|
|
7341
|
use Getopt::Std; |
|
4
|
|
|
|
|
202
|
|
|
4
|
|
|
|
|
214
|
|
9
|
4
|
|
|
4
|
|
4166
|
use Unicode::UTF8 qw(decode_utf8 encode_utf8); |
|
4
|
|
|
|
|
3235
|
|
|
4
|
|
|
|
|
2774
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = 0.03; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# Constructor. |
14
|
|
|
|
|
|
|
sub new { |
15
|
5
|
|
|
5
|
1
|
5067
|
my ($class, @params) = @_; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Create object. |
18
|
5
|
|
|
|
|
13
|
my $self = bless {}, $class; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Object. |
21
|
5
|
|
|
|
|
17
|
return $self; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# Run. |
25
|
|
|
|
|
|
|
sub run { |
26
|
4
|
|
|
4
|
1
|
5
|
my $self = shift; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# Process arguments. |
29
|
4
|
|
|
|
|
18
|
$self->{'_opts'} = { |
30
|
|
|
|
|
|
|
'h' => 0, |
31
|
|
|
|
|
|
|
'l' => undef, |
32
|
|
|
|
|
|
|
'p' => '', |
33
|
|
|
|
|
|
|
'u' => '', |
34
|
|
|
|
|
|
|
'v' => undef, |
35
|
|
|
|
|
|
|
}; |
36
|
4
|
100
|
100
|
|
|
13
|
if (! getopts('hl:p:u:v:', $self->{'_opts'}) |
|
|
|
100
|
|
|
|
|
37
|
|
|
|
|
|
|
|| $self->{'_opts'}->{'h'} |
38
|
|
|
|
|
|
|
|| @ARGV < 2) { |
39
|
|
|
|
|
|
|
|
40
|
3
|
|
|
|
|
291
|
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
|
|
|
|
|
35
|
print STDERR "\t-h\t\t\tPrint help.\n"; |
43
|
3
|
|
|
|
|
26
|
print STDERR "\t-l plugin:...\t\tLoad data from plugin.\n"; |
44
|
3
|
|
|
|
|
26
|
print STDERR "\t-p password\t\tDatabase password.\n"; |
45
|
3
|
|
|
|
|
25
|
print STDERR "\t-u user\t\t\tDatabase user.\n"; |
46
|
3
|
|
|
|
|
26
|
print STDERR "\t-v schema_version\tSchema version (default is ". |
47
|
|
|
|
|
|
|
"latest version).\n"; |
48
|
3
|
|
|
|
|
26
|
print STDERR "\t--version\t\tPrint version.\n"; |
49
|
3
|
|
|
|
|
26
|
print STDERR "\tdsn\t\t\tDatabase DSN. e.g. dbi:SQLite:dbname=ex1.db\n"; |
50
|
3
|
|
|
|
|
33
|
print STDERR "\tschema_data_module\tName of Schema data module.\n"; |
51
|
3
|
|
|
|
|
26
|
print STDERR "\tvar_key=var_value\tVariable keys with values for insert.\n"; |
52
|
3
|
|
|
|
|
20
|
return 1; |
53
|
|
|
|
|
|
|
} |
54
|
1
|
|
|
|
|
25
|
$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
|
|
|
|
|
59
|
eval "require $self->{'_schema_data_module'}"; |
64
|
1
|
50
|
|
|
|
8
|
if ($EVAL_ERROR) { |
65
|
|
|
|
|
|
|
err 'Cannot load Schema data module.', |
66
|
1
|
|
|
|
|
8
|
'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 = split m/:/ms, $self->{'_opts'}->{'l'}; |
113
|
0
|
|
|
|
|
|
foreach my $plugin (@plugins) { |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# Load plugin object. |
116
|
0
|
|
|
|
|
|
my $plugin_module = "$self->{'_schema_data_module'}::Plugin::$plugin"; |
117
|
0
|
|
|
|
|
|
eval "require $plugin_module"; |
118
|
0
|
0
|
|
|
|
|
if ($EVAL_ERROR) { |
119
|
0
|
|
|
|
|
|
err 'Cannot load Schema data plugin module.', |
120
|
|
|
|
|
|
|
'Module name', $plugin_module, |
121
|
|
|
|
|
|
|
'Error', $EVAL_ERROR, |
122
|
|
|
|
|
|
|
; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# Create plugin object. |
126
|
0
|
|
|
|
|
|
my $plugin = eval { |
127
|
|
|
|
|
|
|
$plugin_module->new( |
128
|
|
|
|
|
|
|
'schema' => $data->schema, |
129
|
|
|
|
|
|
|
'verbose_cb' => sub { |
130
|
0
|
|
|
0
|
|
|
my $message = shift; |
131
|
0
|
|
|
|
|
|
print encode_utf8($message)."\n"; |
132
|
0
|
|
|
|
|
|
return; |
133
|
|
|
|
|
|
|
}, |
134
|
0
|
|
|
|
|
|
); |
135
|
|
|
|
|
|
|
}; |
136
|
0
|
0
|
|
|
|
|
if ($EVAL_ERROR) { |
137
|
0
|
|
|
|
|
|
err "Cannot create '$plugin_module' object.", |
138
|
|
|
|
|
|
|
'Error', $EVAL_ERROR, |
139
|
|
|
|
|
|
|
; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
# Load plugin data. |
143
|
0
|
|
|
|
|
|
$plugin->load($self->{'_variables'}); |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
|
return 0; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
1; |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
__END__ |