| 1 | #------------------------------------------------------------------------------ | ||
| 2 | # DBO::Visitor::Check - check record for validity | ||
| 3 | # | ||
| 4 | # DESCRIPTION | ||
| 5 | # A visitor class that checks a record from a database table | ||
| 6 | # (represented as a hash mapping column name to value) for validity. | ||
| 7 | # | ||
| 8 | # AUTHOR | ||
| 9 | # Gareth Rees | ||
| 10 | # | ||
| 11 | # COPYRIGHT | ||
| 12 | # Copyright (c) 1999 Canon Research Centre Europe Ltd/ | ||
| 13 | # | ||
| 14 | # $Id: Check.pm,v 1.3 1999/06/21 15:11:24 garethr Exp $ | ||
| 15 | #------------------------------------------------------------------------------ | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | package DBO::Visitor::Check; | ||
| 19 | use base qw(DBO::Visitor); | ||
| 20 | use Class::Multimethods; | ||
| 21 | |||
| 22 | multimethod visit_table => | ||
| 23 | qw(DBO::Visitor::Check DBO::Table DBO::Handle) => | ||
| 24 | sub { | ||
| 25 | my ($vis, $table, $handle) = @_; | ||
| 26 | foreach my $col (@{$table->{columns}}) { | ||
| 27 | visit_column($vis, $col, $handle) or return 0; | ||
| 28 | } | ||
| 29 | return 1; | ||
| 30 | }; | ||
| 31 | |||
| 32 | multimethod visit_column => | ||
| 33 | qw(DBO::Visitor::Check DBO::Column::Base DBO::Handle) => | ||
| 34 | sub { | ||
| 35 | my ($vis, $col, $handle) = @_; | ||
| 36 | if ($col->{not_null} and not defined $vis->{record}{$col->{name}}) { | ||
| 37 | $vis->{error} = DBO::Exception->new | ||
| 38 | (NULL_COLUMN => "You must supply a value for %s.", $col->{name}); | ||
| 39 | return 0; | ||
| 40 | } | ||
| 41 | return 1; | ||
| 42 | }; | ||
| 43 | |||
| 44 | multimethod visit_column => | ||
| 45 | qw(DBO::Visitor::Check DBO::Column::Number DBO::Handle) => | ||
| 46 | sub { | ||
| 47 | my ($vis, $col, $handle) = @_; | ||
| 48 | # visit_column($vis, superclass($col), $handle) or return 0; | ||
| 49 | call_next_method() or return 0; | ||
| 50 | my $value = $vis->{record}{$col->{name}}; | ||
| 51 | if (defined $value and do { local $^W; $value + 0 ne $value }) { | ||
| 52 | $vis->{error} = DBO::Exception->new | ||
| 53 | (NUMERIC => "You must supply a number for %s.", $col->{name}); | ||
| 54 | return 0; | ||
| 55 | } | ||
| 56 | return 1; | ||
| 57 | }; | ||
| 58 | |||
| 59 | multimethod visit_column => | ||
| 60 | qw(DBO::Visitor::Check DBO::Column::Integer DBO::Handle) => | ||
| 61 | sub { | ||
| 62 | my ($vis, $col, $handle) = @_; | ||
| 63 | # visit_column($vis, superclass($col), $handle) or return 0; | ||
| 64 | call_next_method() or return 0; | ||
| 65 | my $value = $vis->{record}{$col->{name}}; | ||
| 66 | if (defined $value and $value != int $value) { | ||
| 67 | $vis->{error} = DBO::Exception->new | ||
| 68 | (INTEGER => "You must supply an integer for %s.", $col->{name}); | ||
| 69 | return 0; | ||
| 70 | } | ||
| 71 | return 1; | ||
| 72 | }; | ||
| 73 | |||
| 74 | multimethod visit_column => | ||
| 75 | qw(DBO::Visitor::Check DBO::Column::Unsigned DBO::Handle) => | ||
| 76 | sub { | ||
| 77 | my ($vis, $col, $handle) = @_; | ||
| 78 | # visit_column($vis, superclass($col), $handle) or return 0; | ||
| 79 | call_next_method() or return 0; | ||
| 80 | my $value = $vis->{record}{$col->{name}}; | ||
| 81 | if (defined $value and $value < 0) { | ||
| 82 | $vis->{error} = DBO::Exception->new | ||
| 83 | (UNSIGNED => "The value for %s must be non-negative.", $col->{name}); | ||
| 84 | return 0; | ||
| 85 | } | ||
| 86 | return 1; | ||
| 87 | }; | ||
| 88 | |||
| 89 | multimethod visit_column => | ||
| 90 | qw(DBO::Visitor::Check DBO::Column::Option DBO::Handle) => | ||
| 91 | sub { | ||
| 92 | my ($vis, $col, $handle) = @_; | ||
| 93 | # visit_column($vis, superclass($col), $handle) or return 0; | ||
| 94 | call_next_method() or return 0; | ||
| 95 | my $value = $vis->{record}{$col->{name}}; | ||
| 96 | unless (grep { $_ eq $value } @{$col->{values}}) { | ||
| 97 | $vis->{error} = DBO::Exception->new | ||
| 98 | (OPTION => "The value for %s must be one of %s.", | ||
| 99 | $col->{name}, join ", ", @{$col->{values}}); | ||
| 100 | return 0; | ||
| 101 | } | ||
| 102 | return 1; | ||
| 103 | }; | ||
| 104 | |||
| 105 | multimethod visit_column => | ||
| 106 | qw(DBO::Visitor::Check DBO::Column::ForeignKey DBO::Handle) => | ||
| 107 | sub { | ||
| 108 | my ($vis, $col, $handle) = @_; | ||
| 109 | # visit_column($vis, superclass($col), $handle) or return 0; | ||
| 110 | call_next_method() or return 0; | ||
| 111 | my $value = $vis->{record}{$col->{name}}; | ||
| 112 | |||
| 113 | # TODO: Fetch values from foreign table and check. | ||
| 114 | |||
| 115 | return 1; | ||
| 116 | }; | ||
| 117 | |||
| 118 | multimethod visit_column => | ||
| 119 | qw(DBO::Visitor::Check DBO::Column::Char DBO::Handle) => | ||
| 120 | sub { | ||
| 121 | my ($vis, $col, $handle) = @_; | ||
| 122 | # visit_column($vis, superclass($col), $handle) or return 0; | ||
| 123 | call_next_method() or return 0; | ||
| 124 | my $value = $vis->{record}{$col->{name}}; | ||
| 125 | if (defined $value and $col->{max_length} < length $value) { | ||
| 126 | $vis->{error} = DBO::Exception->new | ||
| 127 | (LENGTH => "The value for %s is %d characters long (must be at most %d characters long).", | ||
| 128 | $col->{name}, length $value, $col->{max_length}); | ||
| 129 | return 0; | ||
| 130 | } | ||
| 131 | return 1; | ||
| 132 | }; | ||
| 133 | |||
| 134 | multimethod visit_column => | ||
| 135 | qw(DBO::Visitor::Check DBO::Column::Time DBO::Handle) => | ||
| 136 | sub { | ||
| 137 | my ($vis, $col, $handle) = @_; | ||
| 138 | # visit_column($vis, superclass($col), $handle) or return 0; | ||
| 139 | call_next_method() or return 0; | ||
| 140 | my $value = $vis->{record}{$col->{name}}; | ||
| 141 | if ($value !~ /^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/) { | ||
| 142 | $vis->{error} = DBO::Exception->new | ||
| 143 | (TIME => "The value for %s must look like '1999-05-15 23:46:00'.", | ||
| 144 | $col->{name}, $value); | ||
| 145 | return 0; | ||
| 146 | } | ||
| 147 | return 1; | ||
| 148 | }; | ||
| 149 | |||
| 150 | 1; |