| 1 | package TaskPipe::Task_Record_NameValuePairs; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | extends 'TaskPipe::Task_Record'; | ||
| 5 | |||
| 6 | sub action{ | ||
| 7 | my ($self) = @_; | ||
| 8 | |||
| 9 | $self->set_dt; | ||
| 10 | |||
| 11 | my $name_col = 'name'; | ||
| 12 | my $val_col = 'value'; | ||
| 13 | |||
| 14 | my $cols = $self->pinterp->{columns}; | ||
| 15 | if ( $cols ){ | ||
| 16 | $name_col = $cols->{name} if $cols->{name}; | ||
| 17 | $val_col = $cols->{val} if $cols->{val}; | ||
| 18 | } | ||
| 19 | |||
| 20 | my $record; | ||
| 21 | my $kvh = $self->pinterp->{pairs} || $self->input; | ||
| 22 | |||
| 23 | foreach my $k (keys %$kvh){ | ||
| 24 | next if $self->pinterp->{key}{$k}; | ||
| 25 | |||
| 26 | my $to_record = { | ||
| 27 | $name_col => $k, | ||
| 28 | $val_col => +$kvh->{$k} | ||
| 29 | }; | ||
| 30 | |||
| 31 | my $key = $self->pinterp->{key}; | ||
| 32 | if ( $key ){ | ||
| 33 | foreach my $k (keys %$key){ | ||
| 34 | $to_record->{ $k } = $key->{ $k }; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | $record = $self->record_row( $to_record ); | ||
| 39 | |||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | return [{ $record->get_columns }] if $record; | ||
| 44 | return []; | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | =head1 NAME | ||
| 49 | |||
| 50 | TaskPipe::Task_Record_NameValuePairs - record a result set as a series of name/value pairs | ||
| 51 | |||
| 52 | =head1 DESCRIPTION | ||
| 53 | |||
| 54 | This task records a list of name value pairs onto a database table. You can use it in your plan by referring to it in the following format: | ||
| 55 | |||
| 56 | task: | ||
| 57 | _name: Record_NameValuePairs | ||
| 58 | table: whois_kv | ||
| 59 | key: | ||
| 60 | table_id: $this{id} | ||
| 61 | columns: | ||
| 62 | name: param | ||
| 63 | value: value | ||
| 64 | pairs: $this[1]{*} | ||
| 65 | |||
| 66 | In this example (which is shown as part of a C<tree> mode plan, C<key> indicates the foreign key value to go on the table. ie this value will be included on all records in the set passed to the task. | ||
| 67 | |||
| 68 | C<columns> tells the task the name of the key/value columns to insert parameter names/parameter values to. This defaults to C<name> and C<value> if not provided. | ||
| 69 | |||
| 70 | C<pairs> needs to point to the hashref of key/values you want to go onto the database table. This defaults to the set of inputs to the current task if not provided. | ||
| 71 | |||
| 72 | =head1 AUTHOR | ||
| 73 | |||
| 74 | Tom Gracey <tomgracey@gmail.com> | ||
| 75 | |||
| 76 | =head1 COPYRIGHT AND LICENSE | ||
| 77 | |||
| 78 | Copyright (c) Tom Gracey 2018 | ||
| 79 | |||
| 80 | TaskPipe is free software, licensed under | ||
| 81 | |||
| 82 | The GNU Public License Version 3 | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | |||
| 87 | 1; | ||
| 88 |