| 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.03; | ||
| 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 | # It might be handy for convertion tools to inherit this class and | ||
| 37 | # override this sub for tables that needs special handling: | ||
| 38 | |||
| 39 | sub construct_select_sth { | ||
| 40 | my $self=shift; | ||
| 41 | my $table=shift; | ||
| 42 | |||
| 43 | return $self->{src}->prepare | ||
| 44 | ($self->construct_select_statement($table)) | ||
| 45 | || undef; | ||
| 46 | |||
| 47 | } | ||
| 48 | |||
| 49 | sub construct_insert_sth { | ||
| 50 | my $self=shift; | ||
| 51 | my $table=shift; | ||
| 52 | my $dest=shift; | ||
| 53 | my $row=shift; | ||
| 54 | |||
| 55 | return $self->{dst}->prepare | ||
| 56 | ($self->construct_insert_statement($table, $dest, $row)) | ||
| 57 | || undef; | ||
| 58 | } | ||
| 59 | |||
| 60 | # It might be handy for convertion tools to inherit this class and | ||
| 61 | # override this sub for tables that needs special handling: | ||
| 62 | |||
| 63 | sub construct_select_statement { | ||
| 64 | my $self=shift; | ||
| 65 | my $table=shift; | ||
| 66 | |||
| 67 | if (exists $self->{opts}->{column_translation_table}->{$table}) { | ||
| 68 | my $c=$self->{opts}->{column_translation_table}->{$table}; | ||
| 69 | my $cols=join(",", keys(%{$self->{opts}->{column_translation_table}->{$table}})); | ||
| 70 | return "select $cols from $table"; | ||
| 71 | } else { | ||
| 72 | return "select * from $table"; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | sub construct_insert_statement { | ||
| 77 | my $self=shift; | ||
| 78 | my $table=shift; | ||
| 79 | my $dest=shift; | ||
| 80 | my $row=shift; | ||
| 81 | |||
| 82 | if (exists $self->{opts}->{column_translation_table}->{$table}) { | ||
| 83 | # Finding the right insert statement | ||
| 84 | # There must be a better way to do this ... suggestions? | ||
| 85 | my @qmarks; | ||
| 86 | for (keys %{$self->{opts}->{column_translation_table}->{$table}}) { | ||
| 87 | push (@qmarks, '?'); | ||
| 88 | } | ||
| 89 | my $qmarks = join(',',@qmarks); | ||
| 90 | my $cols=join(",", values(%{$self->{opts}->{column_translation_table}->{$table}})); | ||
| 91 | return "insert into $dest ($cols) values ($qmarks)" | ||
| 92 | } else { | ||
| 93 | # Finding the right insert statement | ||
| 94 | # There must be a better way to do this ... suggestions? | ||
| 95 | my @qmarks; | ||
| 96 | for (@$row) {push (@qmarks, '?')}; | ||
| 97 | my $qmarks = join(',',@qmarks); | ||
| 98 | return "insert into $dest values ($qmarks)"; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | # Copy one table | ||
| 103 | sub copy_one_table { | ||
| 104 | my $self=shift; | ||
| 105 | my $table=shift; | ||
| 106 | |||
| 107 | my $cnt=0; | ||
| 108 | |||
| 109 | my $dest = $self->{opts}->{table_translation_table}->{$table} || $table; | ||
| 110 | |||
| 111 | my $select; | ||
| 112 | my $insert_sth; | ||
| 113 | my $row; | ||
| 114 | |||
| 115 | # Selecting referenced columns | ||
| 116 | $select=$self->construct_select_sth($table); | ||
| 117 | |||
| 118 | $select->execute() || return undef; | ||
| 119 | $row=$select->fetch(); | ||
| 120 | |||
| 121 | $insert_sth=$self->construct_insert_sth($table, $dest, $row); | ||
| 122 | |||
| 123 | my $insert_row=$self->get_insert_row_sub($table, $dest); | ||
| 124 | |||
| 125 | # Deleting all the destination table | ||
| 126 | $self->{dst}->do("delete from $dest") || return undef; | ||
| 127 | |||
| 128 | while (defined $row) { | ||
| 129 | $cnt++; | ||
| 130 | $insert_row->($self, $row, $insert_sth); | ||
| 131 | $row=$select->fetch; | ||
| 132 | } | ||
| 133 | $select->finish; | ||
| 134 | return $cnt; | ||
| 135 | } | ||
| 136 | |||
| 137 | # It might be handy for convertion tools to inherit this class and | ||
| 138 | # override this sub for tables that needs special handling. See | ||
| 139 | # DBIx::Copy::UpgradeRT::get_insert_row_sub for an example. | ||
| 140 | |||
| 141 | sub get_insert_row_sub { | ||
| 142 | my $self=shift; | ||
| 143 | |||
| 144 | # ehem.. | ||
| 145 | # Honestly ... I'm not _really_ sure that I'm doing things The | ||
| 146 | # Right Way here ... but this seems to work: | ||
| 147 | # return *{ref $self . '::insert_row'}{CODE}; | ||
| 148 | # absolutely not! well.. | ||
| 149 | |||
| 150 | return \&DBIx::Copy::insert_row; | ||
| 151 | |||
| 152 | } | ||
| 153 | |||
| 154 | sub insert_row { | ||
| 155 | my $self=shift; | ||
| 156 | my $row=shift; | ||
| 157 | my $insert_sth=shift; | ||
| 158 | $insert_sth->execute(@$row); | ||
| 159 | $insert_sth->finish; | ||
| 160 | } | ||
| 161 | |||
| 162 | # Copy tables | ||
| 163 | sub copy { | ||
| 164 | my $self=shift; | ||
| 165 | my $tables=shift; | ||
| 166 | |||
| 167 | my $cnt=0; # return the number of rows copied | ||
| 168 | |||
| 169 | # Tables should be a array-ref. | ||
| 170 | # Maybe we first should test that it's an array? | ||
| 171 | for my $table (@$tables) { | ||
| 172 | $cnt+=$self->copy_one_table($table); | ||
| 173 | } | ||
| 174 | return $cnt; | ||
| 175 | } | ||
| 176 | |||
| 177 | 1; | ||
| 178 | |||
| 179 | __END__ | ||