TSQLScript/fr: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: TSQLScript/fr
| Old | New | Line | |
|---|---|---|---|
| 1 | + | **TSQLScript**  vous laisse passer un ensemble de commandes SQL en une seule exécution. C'est utile si vous voulez mettre en place une nouvelle base de données ou mettre à jour une base existante. | |
| 2 | + | ||
| 3 | + | TSQLScript est disponible pour FPC et Lazarus et marche pour toute base de données que [[SQLdb/fr|SQLdb]] prend en charge. Il peut être trouvé dans [[SQLdb tab/fr|l'onglet SQLdb]] de la [[Component Palette/fr|palette de composants]]. | |
| 4 | + | ||
| 5 | + | ## Exemple | |
| 6 | + | ||
| 7 | + | Supposez que vous avez un ensemble de commandes SQL telles que la sortie SQL suivante d'un script pour Firebird dans l'outil FlameRobin (dans Properties/DDL) : | |
| 8 | + | ||
| 9 | + |  | |
| 10 | + | ||
| 11 | + | Remarquez qu'au contraire de [[TSQLQuery/fr|TSQLQuery]], TSQLScript nécessite un point-virgule après chaque ordre SQL. | |
| 12 | + | ||
| 13 | + | Cet exemple suppose que vous avez des objets connexion SQL et transaction définis. | |
| 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 is the transaction, defined somewhere in our class | |
| 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 | + | // Now everything is loaded in, run all commands at once: | |
| 37 | + | FBScript.Execute; | |
| 38 | + | //... and then commit to make them stick and show them to the SQL that comes | |
| 39 | + | // after the commit | |
| 40 | + | FTran.Commit; | |
| 41 | + | finally | |
| 42 | + | FBScript.Free; | |
| 43 | + | ScriptText.Free; | |
| 44 | + | end; | |
| 45 | + | // Make sure we leave the transaction state as we found it, handy for switchnig | |
| 46 | + | // between explicit start/commit transaction and commitretaining: | |
| 47 | + | if TranWasStarted then FTran.StartTransaction; | |
| 48 | + | ``` | |
| 49 | + | ||
| 50 | + | Voir aussi \examples\database\tsqlscript dans votre dossier Lazarus pour un exemple de programme faisant la démonstration de TSQLScript. | |
| 51 | + | ||
| 52 | + | > **Warning:** (au moins) FPC 2.6.4 et antérieur : TSQLScript n'analysera pas tous les ordres DDL de Firebird, vous aurez donc besoin de tester votre script en avance. TSQLScript a été amélioré dans le tronc FPC (2.7.1). Une alternative serait d'appeler l'exécutable ISQL de Firebird en utilisant par exemple [[TProcess/fr|TProcess]]. | |
| 53 | + | ||
| 54 | + | > **Note:** Définir la propriété CommentsInSQL à 'False' peut aider à améliorer la fiabilité de TSQLScript si, p.ex., vous avez des commentaires dans des déclarations de procédures stockées. | |
| 55 | + | ||
| 56 | + | --- | |
| 57 | + | *From the Free Pascal wiki, page "TSQLScript/fr", as it stood on 2020-03-02 after a change by Trev, 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).* |
