
print ParseQuery("foo bar foobar"),"\n";
print ParseQuery("foo, bar, foobar"),"\n";
print ParseQuery("bla +foo,bar nix"),"\n";
print ParseQuery("foo and not bar"),"\n";
print ParseQuery("TEST and not (dies and das) or (not tit* and *tat)"),"\n";


sub ParseQuery
  {
  my $qcmd = "SELECT * FROM xxx WHERE ";  # Anfang des Ergebnis-Strings
  my($query) = @_;                        # Abfrage-String (Parameter)
  my($part, $first);                      # Hilfsvariablen
  $query =~ s|^/|\\/|;
  # $query =~ tr/A-Z/a-z/;
  $query =~ s|\(| ( |g;                   # Klammern separat stellen
  $query =~ s|\)| ) |g;
  $query =~ s|["'`]||g;                   # Quotes loeschen
  $query =~ s|\+| and |g;                 # erlaubt +wort +wort ... fuer 'AND'
  $query =~ s|,| or |g;                   # erlaubt wort,wort, ... fuer 'OR'
  $query =~ s|!| not |g;                  # erlaubt Negation
  $query =~ s|\*|\%|g;                    # erlaubt '*' als Jokerzeichen

  $first = 1;                             # 'NOT' (am Anfang) oder 'AND NOT' (woanders)
  for (split(/[ \t]+/, $query))           # for each "word", do ...
    {
    next if /^$/;                         # Leerzeile
    if ($_ eq ")")   { $qcmd .= $_; next; }
    if ($_ eq "(")   { $qcmd .= "$part$_"; $part = ""; $first = 1; next; }
    if ($_ eq "not") { $part = ($first)?" NOT ":" AND NOT "; next; }
    if ($_ eq "or" ) { $part = " OR "; next; }
    if ($_ eq "and") { $part = " AND "; next; }
    if ($_ =~ /\%/)                       # Jokerzeichen
      { $qcmd .= "$part KEY LIKE '$_'"; }
    else
      { $qcmd .= "$part KEY='$_'"; }
    $part = " AND ";                      # default: AND Verknuepfungen
    $first = 0;
    }
  $qcmd .= ";";                           # Semikolon am Schluss
  return $qcmd;
  }
