sqldbexampleunit.pp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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, 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 = ('[email protected]', '[email protected]',
  32. '[email protected]', '[email protected]', '[email protected]',
  33. '[email protected]', '[email protected]', '[email protected]'
  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.