/packages/fcl-db/src/sqldb/examples/sqldbexampleunit.pp

https://github.com/slibre/freepascal · Puppet · 131 lines · 103 code · 28 blank · 0 comment · 9 complexity · b9246c85f8f6cd05eaceb6d6fb5ded8e MD5 · raw file

  1. (******************************************************************************
  2. * *
  3. * (c) 2005 CNOC v.o.f. *
  4. * *
  5. * File: SqldbExampleUnit.pp *
  6. * Author: Joost van der Sluis (joost@cnoc.nl) *
  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, sysutils,
  16. sqldb, pqconnection, IBConnection, ODBCConn,
  17. mysql40conn, mysql41conn, mysql50conn, oracleconnection;
  18. var dbtype,
  19. dbname,
  20. dbuser,
  21. dbhost,
  22. dbpassword : string;
  23. Fconnection : tSQLConnection;
  24. Ftransaction : tSQLTransaction;
  25. Fquery : tSQLQuery;
  26. const
  27. FPdevNames : Array[1..8] of string = ('Florian Klämpfl', 'Carl Eric Codère',
  28. 'Daniël Mantione', 'Jonas Maebe', 'Michael Van Canneyt',
  29. 'Peter Vreman', 'Pierre Muller', 'Marco van de Voort'
  30. );
  31. FPdevEmails : Array[1..8] of string = ('florian@freepascal.org', 'ccodere@spamidontlike.ieee.org',
  32. 'd.s.p.mantione@twi.tudelft.nl', 'jonas@SPAM.freepascal.ME.org.NOT', 'michael@freepascal.org',
  33. 'peter@freepascal.org', 'muller@cerbere.u-strasbg.fr', 'marcov@stack.nl'
  34. );
  35. var
  36. FPdevBirthDates : Array[1..8] of TDateTime;
  37. procedure ExitWithError(s : string);
  38. procedure ReadIniFile;
  39. procedure CreateFConnection;
  40. procedure CreateFTransaction;
  41. procedure CreateFQuery;
  42. implementation
  43. uses
  44. inifiles;
  45. procedure ExitWithError(s : string);
  46. begin
  47. writeln(s);
  48. writeln('Execution aborted');
  49. halt;
  50. end;
  51. procedure ReadIniFile;
  52. var IniFile : TIniFile;
  53. I : integer;
  54. begin
  55. IniFile := TIniFile.Create('database.ini');
  56. dbtype := IniFile.ReadString('Database','Type','');
  57. dbhost := IniFile.ReadString('Database','Host','');
  58. dbname := IniFile.ReadString('Database','Name','');
  59. dbuser := IniFile.ReadString('Database','User','');
  60. dbpassword := IniFile.ReadString('Database','Password','');
  61. IniFile.Free;
  62. For I:=1 to 8 do
  63. FPdevBirthDates[i] := EncodeDate(1990+i,i,i);
  64. end;
  65. procedure CreateFConnection;
  66. begin
  67. if dbtype = 'mysql40' then Fconnection := tMySQL40Connection.Create(nil);
  68. if dbtype = 'mysql41' then Fconnection := tMySQL41Connection.Create(nil);
  69. if dbtype = 'mysql50' then Fconnection := tMySQL50Connection.Create(nil);
  70. if dbtype = 'postgresql' then Fconnection := tpqConnection.Create(nil);
  71. if dbtype = 'interbase' then Fconnection := tIBConnection.Create(nil);
  72. if dbtype = 'odbc' then Fconnection := tODBCConnection.Create(nil);
  73. if dbtype = 'oracle' then Fconnection := TOracleConnection.Create(nil);
  74. if not assigned(Fconnection) then ExitWithError('Invalid database-type, check if a valid database-type was provided in the file ''database.ini''');
  75. with Fconnection do
  76. begin
  77. if dbhost<>'' then
  78. Hostname:=dbhost;
  79. DatabaseName := dbname;
  80. UserName := dbuser;
  81. Password := dbpassword;
  82. open;
  83. end;
  84. end;
  85. procedure CreateFTransaction;
  86. begin
  87. Ftransaction := tsqltransaction.create(nil);
  88. with Ftransaction do
  89. begin
  90. database := Fconnection;
  91. StartTransaction;
  92. end;
  93. end;
  94. procedure CreateFQuery;
  95. begin
  96. Fquery := TSQLQuery.create(nil);
  97. with Fquery do
  98. begin
  99. database := Fconnection;
  100. transaction := Ftransaction;
  101. end;
  102. end;
  103. end.