| 1 | # $Id: NumberField.pm,v 1.6 2005/05/26 15:01:04 mjb47 Exp $ | ||
| 2 | |||
| 3 | package DBIx::XMLServer::NumberField; | ||
| 4 | use XML::LibXML; | ||
| 5 | our @ISA = ('DBIx::XMLServer::Field'); | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | DBIx::XMLServer::NumberField - integer field type | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | This class implements the built-in integer field type of | ||
| 14 | DBIx::XMLServer. Only the B<where> method is overridden from the base | ||
| 15 | class. | ||
| 16 | |||
| 17 | =head2 B<where> method | ||
| 18 | |||
| 19 | $sql_expression = $number_field->where($condition); | ||
| 20 | |||
| 21 | The condition may consist of one of the numeric comparison operators '=', | ||
| 22 | '>', '<', '>=' or '<=', followed by an integer. The integer must match the | ||
| 23 | regular expression '-?\d+'. The resulting SQL expression is simply | ||
| 24 | |||
| 25 | <field> <condition> <value> . | ||
| 26 | |||
| 27 | If the operator is '=', then instead of a single integer a comma-separated | ||
| 28 | list of integers may be given. Then the SQL expression is | ||
| 29 | |||
| 30 | <field> IN (<value1>, <value2>, ...) . | ||
| 31 | |||
| 32 | Alternatively, the condition may be empty, in which case the SQL expression | ||
| 33 | is | ||
| 34 | |||
| 35 | <field> IS NOT NULL . | ||
| 36 | |||
| 37 | If the condition is the character '!', then the SQL expression is | ||
| 38 | |||
| 39 | <field> IS NULL . | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | sub where { | ||
| 44 | my $self = shift; | ||
| 45 | my $cond = shift; | ||
| 46 | my $column = $self->select; | ||
| 47 | return "$column IS NOT NULL" if $cond eq ''; | ||
| 48 | return "$column IS NULL" if $cond eq '!'; | ||
| 49 | my ($comp, $value) = ($cond =~ /([=<>]+)(.*)/); | ||
| 50 | defined($comp) or die "Unrecognised number condition: $cond\n"; | ||
| 51 | $comp =~ /^(=|[<>]=?)$/ or die "Unrecognised number comparison: $comp\n"; | ||
| 52 | if($comp eq '=' && $value =~ /^-?\d+(\s*,\s*-?\d+)+$/) { | ||
| 53 | return "$column IN ($value)"; | ||
| 54 | } | ||
| 55 | $value =~ /^-?\d+$/ or die "Unrecognised number: $value\n"; | ||
| 56 | return "$column $comp $value"; | ||
| 57 | } | ||
| 58 | |||
| 59 | 1; | ||
| 60 | |||
| 61 | __END__ |