| 1 | package TaskPipe::Task_Record_Array; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | use Log::Log4perl; | ||
| 5 | use Data::Dumper; | ||
| 6 | extends 'TaskPipe::Task_Record'; | ||
| 7 | |||
| 8 | sub action{ | ||
| 9 | my ($self) = @_; | ||
| 10 | |||
| 11 | my $logger = Log::Log4perl->get_logger; | ||
| 12 | |||
| 13 | $self->set_dt; | ||
| 14 | |||
| 15 | my $cols = $self->pinterp->{columns}; | ||
| 16 | confess "'columns' was not specified" unless $cols; | ||
| 17 | |||
| 18 | my $add_to_all = $self->pinterp->{add_to_all}; | ||
| 19 | confess "'add_to_all' was not specified. pinterp: ".Dumper( $self->pinterp ) unless $add_to_all; | ||
| 20 | |||
| 21 | my $record; | ||
| 22 | my $rows = $self->pinterp->{array}; | ||
| 23 | |||
| 24 | return [{}] unless $rows; | ||
| 25 | confess "'array' should have been an arrayref, but was type '".ref($rows)."'" unless ref $rows eq ref []; | ||
| 26 | |||
| 27 | foreach my $row (@$rows){ | ||
| 28 | |||
| 29 | my $to_record = { %$add_to_all }; | ||
| 30 | |||
| 31 | |||
| 32 | foreach my $name ( keys %$cols ){ | ||
| 33 | $to_record->{$name} = $row->{ $cols->{$name} }; | ||
| 34 | } | ||
| 35 | |||
| 36 | $record = $self->record_row( $to_record ); | ||
| 37 | |||
| 38 | } | ||
| 39 | |||
| 40 | if ( $record ){ | ||
| 41 | my $rec_hash = { $record->get_columns }; | ||
| 42 | delete $rec_hash->{modified_dt}; | ||
| 43 | delete $rec_hash->{created_dt}; | ||
| 44 | return [ $rec_hash ]; | ||
| 45 | } else { | ||
| 46 | return [{}]; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | =head1 NAME | ||
| 52 | |||
| 53 | TaskPipe::Task_Record_NameValuePairs - record a result set as a series of name/value pairs | ||
| 54 | |||
| 55 | =head1 DESCRIPTION | ||
| 56 | |||
| 57 | 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: | ||
| 58 | |||
| 59 | task: | ||
| 60 | _name: Record_NameValuePairs | ||
| 61 | table: whois_kv | ||
| 62 | key: | ||
| 63 | table_id: $this{id} | ||
| 64 | columns: | ||
| 65 | name: param | ||
| 66 | value: value | ||
| 67 | pairs: $this[1]{*} | ||
| 68 | |||
| 69 | 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. | ||
| 70 | |||
| 71 | 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. | ||
| 72 | |||
| 73 | 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. | ||
| 74 | |||
| 75 | =head1 AUTHOR | ||
| 76 | |||
| 77 | Tom Gracey <tomgracey@gmail.com> | ||
| 78 | |||
| 79 | =head1 COPYRIGHT AND LICENSE | ||
| 80 | |||
| 81 | Copyright (c) Tom Gracey 2018 | ||
| 82 | |||
| 83 | TaskPipe is free software, licensed under | ||
| 84 | |||
| 85 | The GNU Public License Version 3 | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | |||
| 90 | 1; | ||
| 91 |