| 1 | package SPOPS::Import::DBI::GenericOperation; | ||
| 2 | |||
| 3 | # $Id: GenericOperation.pm,v 1.2 2004/06/02 00:33:20 lachoy Exp $ | ||
| 4 | |||
| 5 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use base qw( SPOPS::Import ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | use SPOPS::Exception qw( spops_error ); | ||
| 8 | use SPOPS::SQLInterface; | ||
| 9 | |||
| 10 | $SPOPS::Import::DBI::GenericOperation::VERSION = sprintf("%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/); | ||
| 11 | |||
| 12 | my @FIELDS = qw( table db where ); | ||
| 13 | SPOPS::Import::DBI::GenericOperation->mk_accessors( @FIELDS ); | ||
| 14 | |||
| 15 | sub add_where_params { | ||
| 16 | my ( $self, @args ) = @_; | ||
| 17 | push @{ $self->{where_params} }, @args; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub where_params { | ||
| 21 | my ( $self, $all_args ) = @_; | ||
| 22 | if ( $all_args ) { | ||
| 23 | $self->{where_params} = ( ref $all_args ) | ||
| 24 | ? $all_args : [ $all_args ]; | ||
| 25 | } | ||
| 26 | return $self->{where_params}; | ||
| 27 | } | ||
| 28 | |||
| 29 | ######################################## | ||
| 30 | # Core API | ||
| 31 | |||
| 32 | sub get_fields { return ( $_[0]->SUPER::get_fields(), @FIELDS ) } | ||
| 33 | |||
| 34 | sub run { | ||
| 35 | my ( $self ) = @_; | ||
| 36 | |||
| 37 | unless ( $self->db ) { spops_error "Cannot run without a database handle available" } | ||
| 38 | unless ( $self->table ) { spops_error "Cannot run without table defined" } | ||
| 39 | unless ( $self->where ) { spops_error "Cannot run without where clause defined" } | ||
| 40 | |||
| 41 | my %op_args = ( | ||
| 42 | db => $self->db, | ||
| 43 | table => $self->table, | ||
| 44 | where => $self->where, | ||
| 45 | value => $self->where_params, | ||
| 46 | ); | ||
| 47 | my $rv = eval { $self->_run_operation( \%op_args ) }; | ||
| 48 | return ( $@ ) ? [ [ undef, $rv, $@ ] ] | ||
| 49 | : [ [ 1, $rv, undef ] ]; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub _run_operation { | ||
| 53 | my ( $self ) = @_; | ||
| 54 | die "'", ref( $self ), "' does not implement the required ", | ||
| 55 | "method '_run_operation()'"; | ||
| 56 | } | ||
| 57 | |||
| 58 | ######################################## | ||
| 59 | # For subclasses... | ||
| 60 | |||
| 61 | sub data_from_file { | ||
| 62 | my ( $self, $filename ) = @_; | ||
| 63 | $self->assign_data( $self->raw_data_from_file( $filename ) ); | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | sub data_from_fh { | ||
| 68 | my ( $self, $fh ) = @_; | ||
| 69 | $self->assign_data( $self->raw_data_from_fh( $fh ) ); | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | sub assign_raw_data { | ||
| 74 | my ( $self, $metadata ) = @_; | ||
| 75 | for ( qw( where table where_params ) ) { | ||
| 76 | $self->$_( $metadata->{$_} ); | ||
| 77 | delete $metadata->{ $_ }; | ||
| 78 | } | ||
| 79 | $self->extra_metadata( $metadata ); | ||
| 80 | return $self; | ||
| 81 | } | ||
| 82 | |||
| 83 | 1; | ||
| 84 | |||
| 85 | __END__ | ||