Write Utility Code (dbExpress Tutorial)

From RAD Studio
Jump to: navigation, search

Go Up to Tutorial: Using dbExpress to View and Update Databases in an Application

A couple of utility routines are needed to complete the application's processing.

Delphi

First, add two functions to the end of the type part of the interface section, declaring them public:

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

After adding these routines to the interface section, you can use class completion to create skeleton code for them in the implementation section.

C++

Add these two member functions to cdsmain.h:

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

MatchFilter

PopulateListBox calls MatchFilter to determine whether a table should be listed or not, i.e., whether the table name matches the pattern in the TEdit containing the filter.

Delphi

Add this function in the implementation section.

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++

Add this member function to 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 adds table names to the TListBox from the list of tables, which must already be in the AllTables TStringList. PopulateListBox uses MatchFilter to test if the table name matches the filter text.

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;
}

Previous

Write Code to Handle Minor Events

Next

Add Cell Viewing Form