blib/lib/DBIx/Copy.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 # Copyright (c) 2000 Tobias Brox. All rights reserved. This program
2 # is released under the GNU Public License which states that you are
3 # free to do whatever you want with the code - just as long as it
4 # keeps the same licensing. It might also be released under The
5 # Artistic License if you send me a mail why GPL is insufficient.
6  
7 package DBIx::Copy;
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 0 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=2.6
13  
14 # Remember to update the POD (yeah, even the version number)!
15 $VERSION=0.02;
16  
17 # Initiate a new object:
18 sub new {
19 my $object_or_class = shift; my $class = ref($object_or_class) || $object_or_class;
20 my ($dbh_source, $dbh_target, $options)=@_;
21 my $self={
22 src=>$dbh_source,
23 dst=>$dbh_target,
24 opts=>$options
25 };
26  
27 # The only check that is done about whether the input parameters
28 # are OK:
29 die "source and target needed"
30 unless (defined $dbh_source and defined $dbh_target);
31  
32 bless $self, $class;
33 return $self;
34 }
35  
36 # Copy the data - without taking any parameters into consideration for
37 # the time beeing. Returns number of rows copied. It might be handy
38 # for convertion tools to inherit this class and override this sub for
39 # tables that needs special handling.
40  
41 # This seems like a bad way to do it - I branch too much of the code
42 # dependent on whether column_translation_table is set or not.
43 sub copy_one_table {
44 my $self=shift;
45 my $table=shift;
46
47 my $cnt=0;
48  
49 my $dest = $self->{opts}->{table_translation_table}->{$table} || $table;
50
51 my $select;
52 my $insert;
53 my $row;
54  
55 if (exists $self->{opts}->{column_translation_table}->{$table}) {
56  
57 my $c=$self->{opts}->{column_translation_table}->{$table};
58  
59 # Selecting referenced columns
60 my $cols=join(",", keys(%$c));
61 $select=$self->{'src'}->prepare("select $cols from $table") || return undef;
62 $select->execute() || return undef;
63
64 # Deleting all the destination table
65 $self->{dst}->do("delete from $dest") || return undef;
66
67 # Finding the right insert statement
68 # There must be a better way to do this ... suggestions?
69 my @qmarks;
70 for (keys %$c) {push (@qmarks, '?')};
71  
72 $cols=join(",", values(%$c));
73  
74 my $qmarks = join(',',@qmarks);
75 $insert=$self->{dst}->prepare("insert into $dest ($cols) values ($qmarks)") || return undef;
76  
77 $row=$select->fetch();
78  
79 } else {
80  
81 # Selecting all the table
82 $select=$self->{'src'}->prepare("select * from $table") || return undef;
83 $select->execute() || return undef;
84
85 # Deleting all the destination table
86 $self->{dst}->do("delete from $dest") || return undef;
87 $row=$select->fetch();
88
89 # Finding the right insert statement
90 # There must be a better way to do this ... suggestions?
91 my @qmarks;
92 for (@$row) {push (@qmarks, '?')};
93 my $qmarks = join(',',@qmarks);
94 $insert=$self->{dst}->prepare("insert into $dest values ($qmarks)") || return undef;
95 }
96
97 while (defined $row) {
98 $cnt++;
99 $insert->execute(@$row);
100 $row=$select->fetch;
101 $insert->finish;
102 }
103 $select->finish;
104 return $cnt;
105 }
106  
107 # Copy tables
108 sub copy {
109 my $self=shift;
110 my $tables=shift;
111  
112 my $cnt=0; # return the number of rows copied
113  
114 # Tables should be a array-ref.
115 # Maybe we first should test that it's an array?
116 for my $table (@$tables) {
117 $cnt+=$self->copy_one_table($table);
118 }
119 return $cnt;
120 }
121  
122 1;
123  
124 __END__
↑ Summary