| 1 | |||
| 2 | package DBIx::Romani::Query::Operator; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | |||
| 6 | our $ADD = '+'; | ||
| 7 | our $SUBTRACT = '-'; | ||
| 8 | our $MULTIPLY = '*'; | ||
| 9 | our $DIVIDE = '/'; | ||
| 10 | |||
| 11 | sub new | ||
| 12 | { | ||
| 13 | my $class = shift; | ||
| 14 | my $args = shift; | ||
| 15 | |||
| 16 | my $type; | ||
| 17 | my $values_max; | ||
| 18 | |||
| 19 | if ( ref($args) eq 'HASH' ) | ||
| 20 | { | ||
| 21 | $type = $args->{type}; | ||
| 22 | } | ||
| 23 | else | ||
| 24 | { | ||
| 25 | $type = $args; | ||
| 26 | } | ||
| 27 | |||
| 28 | my $self = { | ||
| 29 | type => $type, | ||
| 30 | values => [ ], | ||
| 31 | values_max => $values_max, | ||
| 32 | }; | ||
| 33 | |||
| 34 | bless $self, $class; | ||
| 35 | return $self; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub get_type { return shift->{type}; } | ||
| 39 | sub get_values { return shift->{values}; } | ||
| 40 | sub get_values_max { return shift->{values_max}; } | ||
| 41 | |||
| 42 | sub add | ||
| 43 | { | ||
| 44 | my ($self, $val) = @_; | ||
| 45 | |||
| 46 | if ( defined $self->{values_max} ) | ||
| 47 | { | ||
| 48 | if ( scalar @{$self->{values}} == $self->{values_max} ) | ||
| 49 | { | ||
| 50 | die "Cannot add more than $self->{values_max} values to the $self->{type} operator"; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | push @{$self->{values}}, $val; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub visit | ||
| 58 | { | ||
| 59 | my ($self, $visitor) = (shift, shift); | ||
| 60 | return $visitor->visit_operator( $self, @_ ); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub copy_values | ||
| 64 | { | ||
| 65 | my ($self, $other) = @_; | ||
| 66 | |||
| 67 | foreach my $value ( @{$other->get_values()} ) | ||
| 68 | { | ||
| 69 | $self->add( $value->clone() ); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | sub clone | ||
| 74 | { | ||
| 75 | my $self = shift; | ||
| 76 | my $class = ref($self); | ||
| 77 | |||
| 78 | my $clone; | ||
| 79 | $clone = $class->new(); | ||
| 80 | $clone->copy_values( $self ); | ||
| 81 | |||
| 82 | return $clone; | ||
| 83 | } | ||
| 84 | |||
| 85 | 1; | ||
| 86 |