TSQLScript/ja
TSQLScript
は1度の実行で、ひとまとまりのSQLステートメント / 複数の SQLステートメントを発行する。
TSQLScriptは FPC と Lazarusの双方で、SQLdb をサポートするどのデータベースでも用いることができる。Component Palette/ja の SQLdb tab にある。
例
以下のような、FlameRobinツール(Database Properties/DDL)でFirebirdデータベーススクリプトのDDLダンプのセットがあったとする:

TSQLQueryと異なり、TSQLScriptはそれぞれのブロックコマンドの後にセミコロン(;)を必要とすることに注意。
この例ではSQLコネクションとトランザクションがセットアップされていると仮定する。
uses
...sqldb, TIBConnection,...
const
DBSchemaFile='dbreporter.sql';
var
FBScript:TSQLScript;
ScriptText:TStringList;
TranWasStarted: boolean;
begin
TranWasStarted:=FTran.Active; //Ftranはこのクラスのどこかで定義されているトランザクション
if not TranWasStarted then FTran.StartTransaction;
FBScript:=TSQLScript.Create(nil);
ScriptText:=TStringList.Create;
try
if not fileexists(DBSchemaFile) then
raise Exception.CreateFmt('dbreporter: could not load database schema file %s',[DBSchemaFile]);
ScriptText.LoadFromFile(DBSchemaFile);
FBScript.DataBase:=(FConn as TIBConnection);
FBScript.Transaction:=FTran;
FBScript.Script:=ScriptText;
// いま、すべてが読み込まれ、すべてのコマンドが1度に実行される:
FBScript.Execute;
//... そしてコミットしてそれらを固定させ、
// コミットの後に来るSQLに表示します
FTran.Commit;
finally
FBScript.Free;
ScriptText.Free;
end;
// トランザクション状態を元あった状態に戻すこと、切り替えに便利
// 明示的な開始/コミットトランザクションとコミット保持の間:
if TranWasStarted then FTran.StartTransaction;Lazarusディレクトリ、\examples\database\tsqlscriptのTSQLScriptのデモプログラム例も参照のこと。
Warning: (At least)FPC 2.6.4 以前: TSQLScript はすべての Firebird DDL を正しく解析しないため、事前にスクリプトをテストする必要がある場合がある。TSQLScript は FPC トランク (2.7.1) で改善された。 別の方法としては、たとえば次のコマンドを使用して Firebird のTProcessを用いる isql 実行可能ファイルを呼び出すこともできる。
Note: CommentsInSQL プロパティを false に設定すると、ストアド プロシージャ宣言内にコメントがある場合、TSQLScript の信頼性が向上する可能性がある。
From the Free Pascal wiki, page "TSQLScript/ja", as it stood on 2024-03-31 after a change by Ariben, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.
Talk about this page
Nobody has opened a bench for this page yet. Sign in to open one.
