| 1 | |||
| 2 | package DBIx::Romani::Query::Update; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | |||
| 6 | sub new | ||
| 7 | { | ||
| 8 | my $class = shift; | ||
| 9 | my $args = shift; | ||
| 10 | |||
| 11 | my $table; | ||
| 12 | |||
| 13 | if ( ref($args) eq 'HASH' ) | ||
| 14 | { | ||
| 15 | $table = $args->{table}; | ||
| 16 | } | ||
| 17 | else | ||
| 18 | { | ||
| 19 | $table = $args; | ||
| 20 | } | ||
| 21 | |||
| 22 | my $self = { | ||
| 23 | table => $table, | ||
| 24 | values => [ ], | ||
| 25 | where => undef | ||
| 26 | }; | ||
| 27 | |||
| 28 | bless $self, $class; | ||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub get_table { return shift->{table}; } | ||
| 33 | sub get_values { return shift->{values}; } | ||
| 34 | sub get_where { return shift->{where}; } | ||
| 35 | |||
| 36 | sub set_value | ||
| 37 | { | ||
| 38 | my ($self, $column, $value) = @_; | ||
| 39 | |||
| 40 | # attempt to remove the old value | ||
| 41 | for (my $i = 0; $i < scalar @{$self->{values}}; $i++ ) | ||
| 42 | { | ||
| 43 | if ( $self->{values}->[$i]->{column} eq $column ) | ||
| 44 | { | ||
| 45 | delete $self->{values}->[$i]; | ||
| 46 | last; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | push @{$self->{values}}, { column => $column, value => $value }; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub set_where | ||
| 54 | { | ||
| 55 | my ($self, $where) = @_; | ||
| 56 | $self->{where} = $where; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub visit | ||
| 60 | { | ||
| 61 | my ($self, $visitor) = @_; | ||
| 62 | $visitor->visit_update( $self ); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub clone | ||
| 66 | { | ||
| 67 | my $self = shift; | ||
| 68 | |||
| 69 | my $query = DBIx::Romani::Query::Update->new({ table => $self->get_table() }); | ||
| 70 | |||
| 71 | foreach my $value ( @{$self->{values}} ) | ||
| 72 | { | ||
| 73 | $query->set_value( $value->{column}, $value->{value} ); | ||
| 74 | } | ||
| 75 | |||
| 76 | if ( $self->get_where() ) | ||
| 77 | { | ||
| 78 | $query->set_where( $self->get_where()->clone() ); | ||
| 79 | } | ||
| 80 | |||
| 81 | return $query; | ||
| 82 | } | ||
| 83 | |||
| 84 | 1; | ||
| 85 |