| 1 | # $Id: BooleanField.pm,v 1.5 2005/05/26 15:01:03 mjb47 Exp $ | ||
| 2 | |||
| 3 | package DBIx::XMLServer::BooleanField; | ||
| 4 | use XML::LibXML; | ||
| 5 | our @ISA = ('DBIx::XMLServer::Field'); | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | DBIx::XMLServer::BooleanField - Boolean field type | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | This class implements the built-in Boolean field type of | ||
| 14 | DBIx::XMLServer. The B<where> and B<value> methods are overridden | ||
| 15 | from the base class. | ||
| 16 | |||
| 17 | =head2 B<where> method | ||
| 18 | |||
| 19 | $sql_expression = $boolean_field->where($condition); | ||
| 20 | |||
| 21 | The condition must either be empty, or be equal to one of the following: | ||
| 22 | |||
| 23 | ! | ||
| 24 | =1 | ||
| 25 | =y | ||
| 26 | =yes | ||
| 27 | =true | ||
| 28 | =0 | ||
| 29 | =n | ||
| 30 | =no | ||
| 31 | =false . | ||
| 32 | |||
| 33 | If the condition is empty, then the SQL expression 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 | Otherwise, the SQL expression returned is equal to | ||
| 42 | |||
| 43 | <field> = 'Y' or <field> = 'N' | ||
| 44 | |||
| 45 | accordingly. | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | sub where { | ||
| 50 | my $self = shift; | ||
| 51 | my $cond = shift; | ||
| 52 | my $column = $self->select; | ||
| 53 | return "$column IS NOT NULL" if $cond eq ''; | ||
| 54 | return "$column IS NULL" if $cond eq '!'; | ||
| 55 | $cond or return "$column = 'Y'"; | ||
| 56 | $cond =~ s/^=// or die "Unrecognised Boolean condition: $cond"; | ||
| 57 | $cond =~ /^(1|y(es)?|true)$/i && return "$column = 'Y'"; | ||
| 58 | $cond =~ /^(0|n(o)?|false)$/i && return "$column = 'N'"; | ||
| 59 | die "Unrecognised Boolean condition: $cond"; | ||
| 60 | } | ||
| 61 | |||
| 62 | =head2 B<value> method | ||
| 63 | |||
| 64 | The value is either 'true' or 'false', as required by the B<xsi:boolean> | ||
| 65 | type in XML Schema. | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | our %values = ( 'Y' => 'true', 'N' => 'false' ); | ||
| 70 | |||
| 71 | sub value { | ||
| 72 | shift; | ||
| 73 | return $values{shift @{shift()}}; | ||
| 74 | } | ||
| 75 | |||
| 76 | 1; | ||
| 77 | |||
| 78 | __END__ |