| 1 | package Catmandu::Fix::lookup_in_storescip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/; |
| 2 | |
| 3 | use Catmandu::Sanescip-perl cpan Catmandu 1.2025 Catmandu/Sane/; |
| 4 | |
| 5 | our $VERSIONscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/$VERSION. = '1.2025'; |
| 6 | |
| 7 | use Catmanduscip-perl cpan Catmandu 1.2025 Catmandu/; |
| 8 | use Catmandu::Util::Pathscip-perl cpan Catmandu 1.2025 Catmandu/Util/Path/ qw(as_path); |
| 9 | use Catmandu::Utilscip-perl cpan Catmandu 1.2025 Catmandu/Util/ qw(is_value); |
| 10 | use Mooscip-perl cpan Catmandu 1.2025 Moo/; |
| 11 | use namespace::cleanscip-perl cpan Catmandu 1.2025 namespace/clean/; |
| 12 | use Catmandu::Fix::Hasscip-perl cpan Catmandu 1.2025 Catmandu/Fix/Has/; |
| 13 | |
| 14 | withscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/with(). 'Catmandu::Fix::Builder'; |
| 15 | |
| 16 | hasscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/has(). path => (fix_arg => 1); |
| 17 | hasscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/has(). store_name => (fix_arg => 1); |
| 18 | hasscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/has(). bag_name => (fix_opt => 1, init_arg => 'bag'); |
| 19 | has default => (fix_opt => 1, predicate => 1); |
| 20 | has delete => (fix_opt => 1); |
| 21 | hasscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/has(). store_args => (fix_opt => 'collect'); |
| 22 | hasscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/has(). store => (is => 'lazy', init_arg => undef); |
| 23 | hasscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/has(). bag => (is => 'lazy', init_arg => undef); |
| 24 | |
| 25 | sub _build_storescip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/_build_store(). { |
| 26 | my ($self) = @_scip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/`@_`.; |
| 27 | Catmandu->storescip-perl cpan Catmandu 1.2025 Catmandu/store().($self->store_namescip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/store_name()., %{$self->store_argsscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/store_args().}); |
| 28 | } |
| 29 | |
| 30 | sub _build_bagscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/_build_bag(). { |
| 31 | my ($self) = @_scip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/`@_`.; |
| 32 | definedscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/defined(). $self->bag_namescip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/bag_name(). |
| 33 | ? $self->storescip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/store().->bagscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/bag().($self->bag_namescip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/bag_name().) |
| 34 | : $self->storescip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/store().->bagscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/bag().; |
| 35 | } |
| 36 | |
| 37 | sub _build_fixerscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/_build_fixer(). { |
| 38 | my ($self) = @_scip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/`@_`.; |
| 39 | |
| 40 | my $bag = $self->bagscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/bag().; |
| 41 | my $cb; |
| 42 | |
| 43 | if ($self->deletescip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/delete().) { |
| 44 | $cb = sub { |
| 45 | my $val = $_scip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/`@_`.[0]; |
| 46 | if (is_valuescip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/is_value().($val) && definedscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/defined().($val = $bag->getscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/get().($val))) { |
| 47 | return $val; |
| 48 | } |
| 49 | return undef, 1, 1; |
| 50 | }; |
| 51 | } |
| 52 | elsif ($self->has_defaultscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/has_default().) { |
| 53 | my $default = $self->defaultscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/default().; |
| 54 | $cb = sub { |
| 55 | my $val = $_scip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/`@_`.[0]; |
| 56 | if (is_valuescip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/is_value().($val) && definedscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/defined().($val = $bag->getscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/get().($val))) { |
| 57 | return $val; |
| 58 | } |
| 59 | $default; |
| 60 | }; |
| 61 | } |
| 62 | else { |
| 63 | $cb = sub { |
| 64 | my $val = $_scip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/`@_`.[0]; |
| 65 | if (is_valuescip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/is_value().($val) && definedscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/defined().($val = $bag->getscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/get().($val))) { |
| 66 | return $val; |
| 67 | } |
| 68 | return undef, 1, 0; |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | as_pathscip-perl cpan Catmandu 1.2025 Catmandu/Util/Path/as_path().($self->pathscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/path().)->updaterscip-perl cpan Catmandu 1.2025 Catmandu/Fix/lookup_in_store/updater().($cb); |
| 73 | } |
| 74 | |
| 75 | 1; |
| 76 | |
| 77 | __END__ |
| 78 | |
| 79 | =pod |
| 80 | |
| 81 | =head1 NAME |
| 82 | |
| 83 | Catmandu::Fix::lookup_in_store - change the value of a HASH key or ARRAY index |
| 84 | by looking up its value in a store |
| 85 | |
| 86 | =head1 SYNOPSIS |
| 87 | |
| 88 | # Lookup in an SQLLite database |
| 89 | lookup_in_store(foo.bar, DBI, data_source: "dbi:SQLite:path/data.sqlite") |
| 90 | |
| 91 | # Lookup in a MongoDB database |
| 92 | lookup_in_store(foo.bar, MongoDB, database_name: lookups, bag: mydata) |
| 93 | |
| 94 | # Lookup in a MongoDB database, using the default bag and a default value when nothing found |
| 95 | lookup_in_store(foo.bar, MongoDB, database_name: lookups, default: 'default value') |
| 96 | |
| 97 | # Lookup in a MongoDB database, using the default bag and delete the foo.bar field when nothing found |
| 98 | lookup_in_store(foo.bar, MongoDB, database_name: lookups, delete: 1) |
| 99 | |
| 100 | # Or, a much faster option: use a named store in a catmandu.yml file |
| 101 | # |
| 102 | # store: |
| 103 | # mydbi: |
| 104 | # package: DBI |
| 105 | # options: |
| 106 | # data_source: "dbi:SQLite:path/data.sqlite" |
| 107 | # mymongo: |
| 108 | # package: MongoDB |
| 109 | # options: |
| 110 | # database_name: lookups |
| 111 | lookup_in_store(foo.bar, mydbi) |
| 112 | lookup_in_store(foo.bar, mymongo, bag: mydata) |
| 113 | lookup_in_store(foo.bar, mymongo, default: 'default value') |
| 114 | lookup_in_store(foo.bar, mymongo, delete: 1) |
| 115 | |
| 116 | =head1 DESCRIPTION |
| 117 | |
| 118 | =head2 lookup_in_store(PATH,STORE[,store_param: store_val, ...][,bag: bag_name][,delete:1][,default:value]) |
| 119 | |
| 120 | Use the lookup_in_store fix to match a field in a record to the "_id" field in |
| 121 | a Catmandu::Store of choice. For instance, if a Catmandu::Store contains these |
| 122 | records: |
| 123 | |
| 124 | --- |
| 125 | _id: water |
| 126 | fr: l'eau |
| 127 | de: wasser |
| 128 | en: water |
| 129 | nl: water |
| 130 | --- |
| 131 | _id: tree |
| 132 | fr: arbre |
| 133 | de: baum |
| 134 | en: tree |
| 135 | nl: boom |
| 136 | |
| 137 | And you data contains these fields: |
| 138 | |
| 139 | --- |
| 140 | _id: 001 |
| 141 | tag: tree |
| 142 | --- |
| 143 | _id: 002 |
| 144 | tag: water |
| 145 | |
| 146 | Then, the fix below will lookup a tag in the Catmandu::Store and replace it |
| 147 | with the database value: |
| 148 | |
| 149 | lookup_in_store(tag, DBI, data_source: "dbi:SQLite:path/data.sqlite") |
| 150 | |
| 151 | The resulting data will contain: |
| 152 | |
| 153 | --- |
| 154 | _id: 001 |
| 155 | tag: |
| 156 | _id: tree |
| 157 | fr: arbre |
| 158 | de: baum |
| 159 | en: tree |
| 160 | nl: boom |
| 161 | --- |
| 162 | _id: 002 |
| 163 | tag: |
| 164 | _id: water |
| 165 | fr: l'eau |
| 166 | de: wasser |
| 167 | en: water |
| 168 | nl: water |
| 169 | |
| 170 | =head1 DATABASE CONNECTIONS |
| 171 | |
| 172 | For every call to a C<lookup_in_store> a new database connection is created. It |
| 173 | is much more effient to used named stores in a C<catmandu.yml> file. This file |
| 174 | needs to contain all the connection parameters to the database. E.g. |
| 175 | |
| 176 | store: |
| 177 | mystore: |
| 178 | package: MongoDB |
| 179 | options: |
| 180 | database_name: mydata |
| 181 | |
| 182 | The C<catmandu.yml> file should be available in the same directory as where the |
| 183 | C<catmandu> command is executed. Or, this directory can be set with the C<-L> option: |
| 184 | |
| 185 | $ catmandu -L /tmp/path convert ... |
| 186 | |
| 187 | =head1 SEE ALSO |
| 188 | |
| 189 | L<Catmandu::Fix>, L<Catmandu::Store> , L<Catmandu::Fix::add_to_store> |
| 190 | |
| 191 | =cut |