| 1 | # $Id: TextField.pm,v 1.5 2005/05/26 15:01:04 mjb47 Exp $ | ||
| 2 | |||
| 3 | package DBIx::XMLServer::TextField; | ||
| 4 | use XML::LibXML; | ||
| 5 | our @ISA = ('DBIx::XMLServer::Field'); | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | DBIx::XMLServer::TextField - text field type | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | This class implements the built-in text field type of DBIx::XMLServer. | ||
| 14 | Only the B<where> method is overridden from the base class. | ||
| 15 | |||
| 16 | =head2 B<where> method | ||
| 17 | |||
| 18 | $sql_expression = $text_field->where($condition); | ||
| 19 | |||
| 20 | This field type understands three types of condition: a string comparison | ||
| 21 | with wildcards; a regular expression test; and a not-null test. | ||
| 22 | |||
| 23 | =over | ||
| 24 | |||
| 25 | =item Condition: C<=wild*card?expression> | ||
| 26 | |||
| 27 | If the first character of the condition is '=', then the rest of the | ||
| 28 | condition is interpreted as a string, possible containing wildcards, | ||
| 29 | to which the column is to be compared. First the string is escaped by | ||
| 30 | the DBI system; then any characters '%' and '_' are escaped by | ||
| 31 | prefixing with '\'; then the characters '*' and '?' are replaced by | ||
| 32 | '%' and '_' respectively. The resulting SQL expression is one of | ||
| 33 | the following: | ||
| 34 | |||
| 35 | <field> = <string> | ||
| 36 | <field> LIKE <string> | ||
| 37 | |||
| 38 | depending on whether the string contains any wildcards. | ||
| 39 | |||
| 40 | =item Condition: C<~regex> | ||
| 41 | |||
| 42 | If the first character of the condition is a tilde '~', then the rest of | ||
| 43 | the condition is interpreted as a regular expression. It is escaped by | ||
| 44 | the DBI system, and the SQL expression is | ||
| 45 | |||
| 46 | <field> RLIKE <string> . | ||
| 47 | |||
| 48 | =item Condition: C<> (empty) | ||
| 49 | |||
| 50 | If the condition is empty, then the SQL expression is | ||
| 51 | |||
| 52 | <field> IS NOT NULL . | ||
| 53 | |||
| 54 | =item Condition: C<!> | ||
| 55 | |||
| 56 | If the condition is the single character '!', then the SQL expression is | ||
| 57 | |||
| 58 | <field> IS NULL . | ||
| 59 | |||
| 60 | =back | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | sub where { | ||
| 65 | my $self = shift; | ||
| 66 | my $cond = shift; | ||
| 67 | my $column = $self->select; | ||
| 68 | return "$column IS NOT NULL" if $cond eq ''; | ||
| 69 | return "$column IS NULL" if $cond eq '!'; | ||
| 70 | for ($cond) { | ||
| 71 | s/^=// && do { | ||
| 72 | $_ = $self->{XMLServer}->{dbh}->quote($_); | ||
| 73 | s/([%_])/\\$1/g; # Escape any SQL special characters | ||
| 74 | return (tr/*?/%_/) ? # Turn wildcards into SQL ones | ||
| 75 | "$column LIKE $_" : "$column = $_"; | ||
| 76 | }; | ||
| 77 | s/^~// && return "$column RLIKE " . $self->{XMLServer}->{dbh}->quote($_); | ||
| 78 | /^$/ && return "$column IS NOT NULL"; | ||
| 79 | die "Unrecognised condition: $_\n"; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | 1; | ||
| 84 | |||
| 85 | __END__ |