TSQLScript/ja: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: TSQLScript/ja
| Old | New | Line | |
|---|---|---|---|
| 1 | + | **TSQLScript**  は1度の実行で、ひとまとまりのSQLステートメント / 複数の SQLステートメントを発行する。 | |
| 2 | + | ||
| 3 | + | TSQLScriptは FPC と Lazarusの双方で、[[SQLdb]] をサポートするどのデータベースでも用いることができる。[[Component Palette/ja]] の [[SQLdb tab]] にある。 | |
| 4 | + | ||
| 5 | + | ## 例 | |
| 6 | + | ||
| 7 | + | 以下のような、FlameRobinツール(Database Properties/DDL)でFirebirdデータベーススクリプトのDDLダンプのセットがあったとする: | |
| 8 | + | ||
| 9 | + |  | |
| 10 | + | ||
| 11 | + | [[TSQLQuery|TSQLQueryと]]異なり、TSQLScriptはそれぞれのブロックコマンドの後にセミコロン(**;**)を必要とすることに注意。 | |
| 12 | + | ||
| 13 | + | この例ではSQLコネクションとトランザクションがセットアップされていると仮定する。 | |
| 14 | + | ||
| 15 | + | ``` pascal | |
| 16 | + | uses | |
| 17 | + | ...sqldb, TIBConnection,... | |
| 18 | + | const | |
| 19 | + | DBSchemaFile='dbreporter.sql'; | |
| 20 | + | var | |
| 21 | + | FBScript:TSQLScript; | |
| 22 | + | ScriptText:TStringList; | |
| 23 | + | TranWasStarted: boolean; | |
| 24 | + | begin | |
| 25 | + | TranWasStarted:=FTran.Active; //Ftranはこのクラスのどこかで定義されているトランザクション | |
| 26 | + | if not TranWasStarted then FTran.StartTransaction; | |
| 27 | + | FBScript:=TSQLScript.Create(nil); | |
| 28 | + | ScriptText:=TStringList.Create; | |
| 29 | + | try | |
| 30 | + | if not fileexists(DBSchemaFile) then | |
| 31 | + | raise Exception.CreateFmt('dbreporter: could not load database schema file %s',[DBSchemaFile]); | |
| 32 | + | ScriptText.LoadFromFile(DBSchemaFile); | |
| 33 | + | FBScript.DataBase:=(FConn as TIBConnection); | |
| 34 | + | FBScript.Transaction:=FTran; | |
| 35 | + | FBScript.Script:=ScriptText; | |
| 36 | + | // いま、すべてが読み込まれ、すべてのコマンドが1度に実行される: | |
| 37 | + | FBScript.Execute; | |
| 38 | + | //... そしてコミットしてそれらを固定させ、 | |
| 39 | + | // コミットの後に来るSQLに表示します | |
| 40 | + | FTran.Commit; | |
| 41 | + | finally | |
| 42 | + | FBScript.Free; | |
| 43 | + | ScriptText.Free; | |
| 44 | + | end; | |
| 45 | + | // トランザクション状態を元あった状態に戻すこと、切り替えに便利 | |
| 46 | + | // 明示的な開始/コミットトランザクションとコミット保持の間: | |
| 47 | + | if TranWasStarted then FTran.StartTransaction; | |
| 48 | + | ``` | |
| 49 | + | ||
| 50 | + | Lazarusディレクトリ、\examples\database\tsqlscriptのTSQLScriptのデモプログラム例も参照のこと。 | |
| 51 | + | ||
| 52 | + | > **Warning:** (At least)FPC 2.6.4 以前: TSQLScript はすべての Firebird DDL を正しく解析しないため、事前にスクリプトをテストする必要がある場合がある。TSQLScript は FPC トランク (2.7.1) で改善された。 別の方法としては、たとえば次のコマンドを使用して Firebird の[[TProcess|TProcessを]]用いる isql 実行可能ファイルを呼び出すこともできる。 | |
| 53 | + | ||
| 54 | + | > **Note:** CommentsInSQL プロパティを false に設定すると、ストアド プロシージャ宣言内にコメントがある場合、TSQLScript の信頼性が向上する可能性がある。 | |
| 55 | + | ||
| 56 | + | --- | |
| 57 | + | *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](/wiki-contact).* |
