ユーティリティ コードを作成する(dbExpress チュートリアル)
チュートリアル:アプリケーションで dbExpress を使用してデータベースを表示および更新する への移動
アプリケーションの処理を完成させるには、2 つのユーティリティ ルーチンが必要です。
Delphi
まず、interface
セクションの type
の末尾に、次の 2 つの関数を追加します。これは、public
として宣言します。
public
{ Public declarations }
function MatchFilter(FilterStr, TestStr: String): Boolean;
procedure PopulateListBox;
ルーチンを interface
セクションに追加した後で、クラス補完を使って implementation
セクションにルーチンのスケルトン コードを作成することができます。
C++
次の 2 つのメンバ関数を cdsmain.h に追加します。
bool __fastcall MatchFilter(String FilterStr, String TestStr);
void __fastcall PopulateListBox(void);
MatchFilter
PopulateListBox
では、MatchFilter
を呼び出して、テーブルをリストに含めるかどうか、つまり、フィルタを保持する TEdit のパターンにテーブル名が一致するかどうかを判断します。
Delphi
次の関数を 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++
次のメンバ関数を cdsmain.cpp に追加します。
// 文字列がパターンに一致するかを判断する
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()) // 最後の文字
//EndPattern = Copy(Pattern, WildPos+1, MaxInt); // ワイルドカード以降のテキスト
EndPattern = Pattern.SubString(WildPos+1, MaxInt); // ワイルドカード以降のテキスト
//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
は、既に AllTables
(TStringList)に設定されているはずのテーブルのリストから、テーブル名を TListBox に追加します。 PopulateListBox
では、MatchFilter
を使って、テーブル名がフィルタ テキストに一致しているかどうかを確認します。
Delphi
procedure TForm2.PopulateListBox;
// AllTables にはアクティブなデータベース接続のテーブルのリストが設定されているはずである
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++
// テーブル リスト ボックスの内容を設定する
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;
}