line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Fix::add_to_store; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
816
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
7
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.2020'; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
7
|
use Catmandu; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
5
|
|
8
|
1
|
|
|
1
|
|
524
|
use Catmandu::Util::Path qw(as_path); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
46
|
|
9
|
1
|
|
|
1
|
|
6
|
use Moo; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
10
|
1
|
|
|
1
|
|
337
|
use namespace::clean; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
3
|
|
11
|
1
|
|
|
1
|
|
610
|
use Catmandu::Fix::Has; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has path => (fix_arg => 1); |
14
|
|
|
|
|
|
|
has store_name => (fix_arg => 1); |
15
|
|
|
|
|
|
|
has bag_name => (fix_opt => 1, init_arg => 'bag'); |
16
|
|
|
|
|
|
|
has store_args => (fix_opt => 'collect'); |
17
|
|
|
|
|
|
|
has store => (is => 'lazy', init_arg => undef); |
18
|
|
|
|
|
|
|
has bag => (is => 'lazy', init_arg => undef); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
with 'Catmandu::Fix::Builder'; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub _build_store { |
23
|
1
|
|
|
1
|
|
11
|
my ($self) = @_; |
24
|
1
|
|
|
|
|
4
|
Catmandu->store($self->store_name, %{$self->store_args}); |
|
1
|
|
|
|
|
8
|
|
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub _build_bag { |
28
|
1
|
|
|
1
|
|
13
|
my ($self) = @_; |
29
|
1
|
50
|
|
|
|
41
|
defined $self->bag_name |
30
|
|
|
|
|
|
|
? $self->store->bag($self->bag_name) |
31
|
|
|
|
|
|
|
: $self->store->bag; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub _build_fixer { |
35
|
1
|
|
|
1
|
|
10
|
my ($self) = @_; |
36
|
1
|
|
|
|
|
14
|
my $bag = $self->bag; |
37
|
1
|
|
|
|
|
7
|
my $getter = as_path($self->path)->getter; |
38
|
|
|
|
|
|
|
sub { |
39
|
1
|
|
|
1
|
|
3
|
my $data = $_[0]; |
40
|
1
|
|
|
|
|
15
|
my $vals = $getter->($data); |
41
|
1
|
|
|
|
|
22
|
$bag->add($_) for @$vals; |
42
|
1
|
|
|
|
|
18
|
$data; |
43
|
1
|
|
|
|
|
15
|
}; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
1; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
__END__ |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=pod |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head1 NAME |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
Catmandu::Fix::add_to_store - add matching values to a store as a side effect |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head1 SYNOPSIS |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Add the current record to an SQLite database. |
59
|
|
|
|
|
|
|
add_to_store(., DBI, data_source: "dbi:SQLite:path/data.sqlite") |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# Add the journal field to a MongoDB database. |
62
|
|
|
|
|
|
|
add_to_store(journal, MongoDB, database_name: catalog) |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# Add all author values to a MongoDB database. |
65
|
|
|
|
|
|
|
add_to_store(authors.*, MongoDB, database_name: catalog, bag: authors) |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# Or, a much faster option: use a named store in a catmandu.yml file |
68
|
|
|
|
|
|
|
# |
69
|
|
|
|
|
|
|
# store: |
70
|
|
|
|
|
|
|
# mydbi: |
71
|
|
|
|
|
|
|
# package: DBI |
72
|
|
|
|
|
|
|
# options: |
73
|
|
|
|
|
|
|
# data_source: "dbi:SQLite:path/data.sqlite" |
74
|
|
|
|
|
|
|
# mymongo: |
75
|
|
|
|
|
|
|
# package: MongoDB |
76
|
|
|
|
|
|
|
# options: |
77
|
|
|
|
|
|
|
# database_name: catalog |
78
|
|
|
|
|
|
|
add_to_store(., mydbi) |
79
|
|
|
|
|
|
|
add_to_store(journal, mymongo) |
80
|
|
|
|
|
|
|
add_to_store(authors.*, mymongo, bag: authors) |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 DESCRIPTION |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head2 add_to_store(PATH,STORE[,store_param: store_val, ...][,bag: bag_name]) |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Store a record or parts of a record in a Catmandu::Store. |
87
|
|
|
|
|
|
|
The values at the PATH will be stored as-is in the database but should be hashes. |
88
|
|
|
|
|
|
|
If the value contains an '_id' field, then it will |
89
|
|
|
|
|
|
|
used as record identifier in the database. If not, then a new '_id' field will |
90
|
|
|
|
|
|
|
be generated and added to the database and original field (for later reference). |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
For instance this YAML input: |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
--- |
95
|
|
|
|
|
|
|
_id: 001 |
96
|
|
|
|
|
|
|
title: test |
97
|
|
|
|
|
|
|
name: foo |
98
|
|
|
|
|
|
|
--- |
99
|
|
|
|
|
|
|
_id: 002 |
100
|
|
|
|
|
|
|
title: test2 |
101
|
|
|
|
|
|
|
name: bar |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
with the fix: |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
add_to_store(., DBI, data_source: "dbi:SQLite:path/data.sqlite") |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
will create a path/data.sqlite SQLite database with two records. Each records contains |
108
|
|
|
|
|
|
|
the _id from the input file and all the record fields. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
For instance this YAML input: |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
--- |
113
|
|
|
|
|
|
|
title: test |
114
|
|
|
|
|
|
|
name: foo |
115
|
|
|
|
|
|
|
--- |
116
|
|
|
|
|
|
|
title: test2 |
117
|
|
|
|
|
|
|
name: bar |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
with the fix: |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
add_to_store(., DBI, data_source: "dbi:SQLite:path/data.sqlite") |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
will create a path/data.sqlite SQLite database with two records. Each records contains |
124
|
|
|
|
|
|
|
the a generated _id and all the record fields. The current input stream will be updated |
125
|
|
|
|
|
|
|
to contain the generated _id. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Use L<Catmandu::Fix::lookup_in_store> to lookup records in a Catmandu::Store based |
128
|
|
|
|
|
|
|
on an '_id' key. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 DATABASE CONNECTIONS |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
For every call to a C<add_to_store> a new database connection is created. It |
133
|
|
|
|
|
|
|
is much more effient to used named stores in a C<catmandu.yml> file. This file |
134
|
|
|
|
|
|
|
needs to contain all the connection parameters to the database. E.g. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
store: |
137
|
|
|
|
|
|
|
mystore: |
138
|
|
|
|
|
|
|
package: MongoDB |
139
|
|
|
|
|
|
|
options: |
140
|
|
|
|
|
|
|
database_name: mydata |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
The C<catmandu.yml> file should be available in the same directory as where the |
143
|
|
|
|
|
|
|
C<catmandu> command is executed. Or, this directory can be set with the C<-L> option: |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
$ catmandu -L /tmp/path convert ... |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 SEE ALSO |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
L<Catmandu::Fix> , L<Catmandu::Fix::lookup_in_store> |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=cut |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
1; |