| 1 | =head1 NAME | ||
| 2 | |||
| 3 | HTML::FormEngine::DBSQL::Checks - collection of FormEngine::DBSQL check routines | ||
| 4 | |||
| 5 | =cut | ||
| 6 | |||
| 7 | ###################################################################### | ||
| 8 | |||
| 9 | package HTML::FormEngine::DBSQL::Checks; | ||
| 10 | |||
| 11 | use Locale::gettext; | ||
| 12 | |||
| 13 | ###################################################################### | ||
| 14 | |||
| 15 | =head1 METHODS | ||
| 16 | |||
| 17 | =head2 dbsql_unique | ||
| 18 | |||
| 19 | This method proves whether the committed field value is unique in the | ||
| 20 | tables records for this field. | ||
| 21 | |||
| 22 | When primary key values are provided, the method checks all records | ||
| 23 | except the record which belongs to the corresponding pkey. So it'll | ||
| 24 | also work when executing updates. | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | ###################################################################### | ||
| 29 | |||
| 30 | sub _dbsql_check_unique { | ||
| 31 | my ($value,$self,$caller,$namevar) = @_; | ||
| 32 | return '' unless($value ne ''); | ||
| 33 | my ($table,$where, $i); | ||
| 34 | my $field = $self->_get_var($namevar||'NAME'); | ||
| 35 | if($field =~ m/^(.+)\..+$/) { | ||
| 36 | $table = $1; | ||
| 37 | } | ||
| 38 | else { | ||
| 39 | $table = $self->{dbsql_tables}->[0]; | ||
| 40 | } | ||
| 41 | #$i = $self->_get_var('ROWNUM',1); | ||
| 42 | foreach $_ (keys(%{$self->{dbsql_pkey}->{$table}})) { | ||
| 43 | $val = $self->_get_input($_); | ||
| 44 | if(ref($val) eq 'ARRAY') { | ||
| 45 | $val = $val->[$self->{values}->{$field} || 0];# if($i); | ||
| 46 | $val = $val->[$self->{_handle_error}->{$field}-1] if(ref($val) eq 'ARRAY'); | ||
| 47 | } | ||
| 48 | if(!$val) { | ||
| 49 | undef($where); | ||
| 50 | last; | ||
| 51 | } | ||
| 52 | $where .= ' AND ' . $_ . ' != ' . $self->{dbsql}->quote($val); | ||
| 53 | } | ||
| 54 | $where = '' unless(defined($where)); | ||
| 55 | my $sql = "SELECT 1 FROM \"$table\" WHERE $field='$value'" . $where; | ||
| 56 | my $sth = $self->{dbsql}->prepare($sql); | ||
| 57 | unless($sth->execute) { | ||
| 58 | $self->_dbsql_sql_error($sql); | ||
| 59 | return 0; | ||
| 60 | } | ||
| 61 | if($sth->fetchrow_array()) { | ||
| 62 | return gettext('already exists') . '!'; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | 1; | ||
| 67 | |||
| 68 | __END__ |