sqldbexampleunit.pp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. (******************************************************************************
  2. * *
  3. * (c) 2005 CNOC v.o.f. *
  4. * *
  5. * File: SqldbExampleUnit.pp *
  6. * Author: Joost van der Sluis ([email protected]) *
  7. * Description: This unit is used by the SQLdb examples *
  8. * License: GPL *
  9. * *
  10. ******************************************************************************)
  11. unit SqldbExampleUnit;
  12. {$mode objfpc}{$H+}
  13. interface
  14. uses
  15. Classes,
  16. sqldb, pqconnection, mysql4conn, IBConnection;
  17. var dbtype,
  18. dbname,
  19. dbuser,
  20. dbpassword : string;
  21. Fconnection : tSQLConnection;
  22. Ftransaction : tSQLTransaction;
  23. Fquery : tSQLQuery;
  24. const
  25. FPdevNames : Array[1..8] of string = ('Florian Klämpfl', 'Carl Eric Codère',
  26. 'Daniël Mantione', 'Jonas Maebe', 'Michael Van Canneyt',
  27. 'Peter Vreman', 'Pierre Muller', 'Marco van de Voort'
  28. );
  29. FPdevEmails : Array[1..8] of string = ('[email protected]', '[email protected]',
  30. '[email protected]', '[email protected]', '[email protected]',
  31. '[email protected]', '[email protected]', '[email protected]'
  32. );
  33. // I know, this results in bogus-values...
  34. FPdevBirthDates : Array[1..8] of TDateTime = (1-1-1991,2-2-1992,
  35. 3-3-1993, 4-4-1994, 5-5-1995,
  36. 6-6-1996, 7-7-1997, 8-8-1998
  37. );
  38. procedure ExitWithError(s : string);
  39. procedure ReadIniFile;
  40. procedure CreateFConnection;
  41. procedure CreateFTransaction;
  42. procedure CreateFQuery;
  43. implementation
  44. uses
  45. inifiles;
  46. procedure ExitWithError(s : string);
  47. begin
  48. writeln(s);
  49. writeln('Execution aborted');
  50. halt;
  51. end;
  52. procedure ReadIniFile;
  53. var IniFile : TIniFile;
  54. begin
  55. IniFile := TIniFile.Create('database.ini');
  56. dbtype := IniFile.ReadString('Database','Type','');
  57. dbname := IniFile.ReadString('Database','Name','');
  58. dbuser := IniFile.ReadString('Database','User','');
  59. dbpassword := IniFile.ReadString('Database','Password','');
  60. IniFile.Free;
  61. end;
  62. procedure CreateFConnection;
  63. begin
  64. if dbtype = 'mysql' then Fconnection := tMySQLConnection.Create(nil);
  65. if dbtype = 'postgresql' then Fconnection := tpqConnection.Create(nil);
  66. if dbtype = 'interbase' then Fconnection := tIBConnection.Create(nil);
  67. if not assigned(Fconnection) then ExitWithError('Invalid database-type, check if a valid database-type was provided in the file ''database.ini''');
  68. with Fconnection do
  69. begin
  70. DatabaseName := dbname;
  71. UserName := dbuser;
  72. Password := dbpassword;
  73. open;
  74. end;
  75. end;
  76. procedure CreateFTransaction;
  77. begin
  78. Ftransaction := tsqltransaction.create(nil);
  79. with Ftransaction do
  80. begin
  81. database := Fconnection;
  82. StartTransaction;
  83. end;
  84. end;
  85. procedure CreateFQuery;
  86. begin
  87. Fquery := TSQLQuery.create(nil);
  88. with Fquery do
  89. begin
  90. database := Fconnection;
  91. transaction := Ftransaction;
  92. end;
  93. end;
  94. end.