line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SPOPS::Import::DBI::Data; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# $Id: Data.pm,v 3.6 2004/06/02 00:48:22 lachoy Exp $ |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
857
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
40
|
|
6
|
1
|
|
|
1
|
|
5
|
use base qw( SPOPS::Import ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
549
|
|
7
|
|
|
|
|
|
|
use SPOPS::Exception qw( spops_error ); |
8
|
|
|
|
|
|
|
use SPOPS::SQLInterface; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
$SPOPS::Import::DBI::Data::VERSION = sprintf("%d.%02d", q$Revision: 3.6 $ =~ /(\d+)\.(\d+)/); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my @FIELDS = qw( table fields db ); |
13
|
|
|
|
|
|
|
SPOPS::Import::DBI::Data->mk_accessors( @FIELDS ); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
######################################## |
16
|
|
|
|
|
|
|
# Core API |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub get_fields { return ( $_[0]->SUPER::get_fields(), @FIELDS ) } |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub run { |
21
|
|
|
|
|
|
|
my ( $self ) = @_; |
22
|
|
|
|
|
|
|
unless ( $self->db ) { spops_error "Cannot run without a database handle available" } |
23
|
|
|
|
|
|
|
unless ( $self->table ) { spops_error "Cannot run without table defined" } |
24
|
|
|
|
|
|
|
unless ( $self->fields ) { spops_error "Cannot run without fields defined" } |
25
|
|
|
|
|
|
|
unless ( $self->data ) { spops_error "Cannot run without data defined" } |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my %insert_args = ( db => $self->db, |
28
|
|
|
|
|
|
|
table => $self->table, |
29
|
|
|
|
|
|
|
field => $self->fields, ); |
30
|
|
|
|
|
|
|
my @status = (); |
31
|
|
|
|
|
|
|
foreach my $data ( @{ $self->data } ) { |
32
|
|
|
|
|
|
|
$insert_args{value} = $data; |
33
|
|
|
|
|
|
|
my $rv = eval { SPOPS::SQLInterface->db_insert( \%insert_args ) }; |
34
|
|
|
|
|
|
|
if ( $@ ) { |
35
|
|
|
|
|
|
|
push @status, [ undef, $data, $@ ]; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
else { |
38
|
|
|
|
|
|
|
push @status, [ 1, $data, undef ]; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
return \@status; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
######################################## |
45
|
|
|
|
|
|
|
# Property manipulation |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub fields_as_hashref { |
48
|
|
|
|
|
|
|
my ( $self ) = @_; |
49
|
|
|
|
|
|
|
my $field_list = $self->fields; |
50
|
|
|
|
|
|
|
unless ( ref $field_list eq 'ARRAY' and scalar @{ $field_list } ) { |
51
|
|
|
|
|
|
|
spops_error "Before using this method, please set the fields in the " . |
52
|
|
|
|
|
|
|
"importer object using:\n\$importer->fields( \\\@fields )"; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
my $count = 0; |
55
|
|
|
|
|
|
|
return { map { $_ => $count++ } @{ $field_list } }; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
######################################## |
59
|
|
|
|
|
|
|
# I/O and property assignment |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub data_from_file { |
62
|
|
|
|
|
|
|
my ( $self, $filename ) = @_; |
63
|
|
|
|
|
|
|
$self->assign_raw_data( $self->raw_data_from_file( $filename ) ); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub data_from_fh { |
68
|
|
|
|
|
|
|
my ( $self, $fh ) = @_; |
69
|
|
|
|
|
|
|
$self->assign_raw_data( $self->raw_data_from_fh( $fh ) ); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub assign_raw_data { |
74
|
|
|
|
|
|
|
my ( $self, $raw_data ) = @_; |
75
|
|
|
|
|
|
|
my $meta = shift @{ $raw_data }; |
76
|
|
|
|
|
|
|
$self->table( $meta->{table} || $meta->{sql_table} ); |
77
|
|
|
|
|
|
|
$self->fields( $meta->{fields} || $meta->{field_order} ); |
78
|
|
|
|
|
|
|
$self->data( $raw_data ); |
79
|
|
|
|
|
|
|
delete $meta->{ $_ } for ( qw( table sql_table fields field_order ) ); |
80
|
|
|
|
|
|
|
$self->extra_metadata( $meta ); |
81
|
|
|
|
|
|
|
return $self; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
1; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
__END__ |