use utf8;
use warnings; no warnings "redefine";
use strict;
use Dbase::Help qw(qquote);
use Fehler qw(problem);

sub select_suche($$%) {
	my($res,$table,%suche) = @_;
	local $_ = $res;

	unless (s/^(${\ join '|', keys %suche })([?:-])\s*//) {
		return problem("Unbekannte Suchmethode: $_");
	}
	elsif ( ref $suche{$1} ) {
		my $x = $_;
		defined( $_ = $suche{$1}->( $x, $2 ) ) or
			return problem("Suche: $1:$x:$2 liefert kein Ergebnis");
	}
	else {
		if ( $2 eq '-' ) {
			return problem("Suche mit '-' nur ohne Text") if length $_;
			return "$table.$suche{$1} IS NULL";
		}
		if ( $2 eq '?' ) {
			$_ = "%$_" unless /^%/;
			$_ .= '%' unless /%$/;
		}
		$_ = "$table.$suche{$1} "
			. ( length() ? "LIKE ${\ qquote($_) }" : 'IS NULL' );
	}
	$_;
}

1;
