Ecrire du code utilitaire (tutoriel dbExpress)

De RAD Studio
Aller à : navigation, rechercher

Remonter à Tutoriel : Utilisation de dbExpress pour visualiser et mettre à jour les bases de données d'une application

Un couple de routines utilitaires est nécessaire pour terminer le traitement de l'application.

Delphi

Ajoutez d'abord deux fonctions à la fin de la partie type de la section interface, en les déclarant public :

  public
    { Public declarations }
    function MatchFilter(FilterStr, TestStr: String): Boolean;
    procedure PopulateListBox;

Après l'ajout de ces routines à la section interface, vous pouvez utiliser l'achèvement de classe pour créer le code squelette des routines dans la section implementation.

C++

Ajoutez ces deux fonctions membres à cdsmain.h :

bool __fastcall MatchFilter(String FilterStr, String TestStr);
void __fastcall PopulateListBox(void);

MatchFilter

PopulateListBox appelle MatchFilter pour déterminer si une table doit être listée, c'est-à-dire si le nom de la table correspond au pattern du TEdit contenant le filtre.

Delphi

Ajoutez cette fonction dans la section implementation.

function TForm2.MatchFilter(FilterStr, TestStr: String): Boolean;
var
  Pattern, EndPattern, TempStr: String;
  WildPos: Integer;
begin
  Result := True;
  if FilterStr = '' then
    Exit;

  // Trim whitespace and make uppercase.
  Pattern := UpperCase(Trim(FilterStr));
  EndPattern := ''; //nothing until found

  // Check for wildcard use.
  WildPos := AnsiPos('*', Pattern);
  if WildPos <> 0 then
  begin
    if WildPos < Length(Pattern) then //last character
      EndPattern := Copy(Pattern, WildPos+1, MaxInt); //any text after wild card
    Pattern := Copy(Pattern, 1, WildPos-1);
  end;

  if (Pattern <> '') and (AnsiPos(Pattern, UpperCase(TestStr)) <> 1) then
    Result := False
  else if EndPattern <> '' then
  begin
    TempStr := AnsiRightStr(TestStr, Length(EndPattern));
    if AnsiPos(EndPattern, UpperCase(TempStr)) <> 1 then
      Result := False;
  end;
end;

C++

Ajoutez cette fonction membre à cdsmain.cpp :

// Determine whether a string matches a pattern
bool __fastcall TForm1::MatchFilter(String FilterStr, String TestStr)
{
  String Pattern, EndPattern, TempStr;
  int WildPos;

  bool Result = true;
  if (FilterStr.IsEmpty())
return false;

  // Trim whitespace and make uppercase.
  Pattern = UpperCase(Trim(FilterStr));
  EndPattern = ""; //nothing until found

  // Check for wildcard use.
  WildPos = AnsiPos("*", Pattern);
  if (WildPos != 0)
  {
if (WildPos < Pattern.Length()) //last character
//EndPattern = Copy(Pattern, WildPos+1, MaxInt); //any text after wild card
EndPattern = Pattern.SubString(WildPos+1, MaxInt); //any text after wild card
//Pattern = Copy(Pattern, 1, WildPos-1);
Pattern = Pattern.SubString(1, WildPos-1);
  }

  if (!Pattern.IsEmpty() && (AnsiPos(Pattern, UpperCase(TestStr)) != 1))
Result = false;
  else if (!EndPattern.IsEmpty())
  {
TempStr = AnsiRightStr(TestStr, EndPattern.Length());
if (AnsiPos(EndPattern, UpperCase(TempStr)) != 1)
Result = false;
  }
  return Result;
}

PopulateListBox

PopulateListBox ajoute les noms des tables dans le TListBox depuis la liste des tables, qui doivent déjà être dans le TStringListAllTables. PopulateListBox utilise MatchFilter pour tester si le nom de table correspond au texte du filtre.

Delphi

procedure TForm2.PopulateListBox;
// AllTables must be set to the list of tables for the active database connection.
var
  I: Integer;
begin
  ListBox1.Items.Clear;
  for I := 0 to AllTables.Count - 1 do
  begin
    // If there is no filter or the table matches the filter, add table to the list box.
    if (EditFilter.Text = sDefaultFilterText) or MatchFilter(EditFilter.Text, AllTables[I]) then
      ListBox1.Items.Add(FMetaDataProvider.QuoteIdentifierIfNeeded(AllTables[I]));
  end;

  // If list box has entries, enable it.
  if ListBox1.Count > 0 then
    ListBox1.Enabled := True;
end;

C++

// Populate Tables list box
void __fastcall TForm1::PopulateListBox(void)
{
  //  AllTables must be set to the list of tables for the active database connection.
  int I;

  ListBox1->Items->Clear();
  for (I = 0; I < AllTables->Count; I++)
  {
    // If there is no filter or the table matches the filter, add table to the list box.
if ((EditFilter->Text == sDefaultFilterText) || MatchFilter(EditFilter->Text, AllTables->Strings[I]))
ListBox1->Items->Add(FMetaDataProvider->QuoteIdentifierIfNeeded(AllTables->Strings[I]));
  }

  // If list box has entries, enable it.
  if (ListBox1->Count > 0)
ListBox1->Enabled = true;
}

Précédent

Ecrire du code pour gérer les événements mineurs

Suivant

Ajouter une fiche de visualisation de cellules