sqldbexampleunit.pp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. dbpassword : string;
  22. Fconnection : tSQLConnection;
  23. Ftransaction : tSQLTransaction;
  24. Fquery : tSQLQuery;
  25. const
  26. FPdevNames : Array[1..8] of string = ('Florian Klämpfl', 'Carl Eric Codère',
  27. 'Daniël Mantione', 'Jonas Maebe', 'Michael Van Canneyt',
  28. 'Peter Vreman', 'Pierre Muller', 'Marco van de Voort'
  29. );
  30. FPdevEmails : Array[1..8] of string = ('[email protected]', '[email protected]',
  31. '[email protected]', '[email protected]', '[email protected]',
  32. '[email protected]', '[email protected]', '[email protected]'
  33. );
  34. var
  35. FPdevBirthDates : Array[1..8] of TDateTime;
  36. procedure ExitWithError(s : string);
  37. procedure ReadIniFile;
  38. procedure CreateFConnection;
  39. procedure CreateFTransaction;
  40. procedure CreateFQuery;
  41. implementation
  42. uses
  43. inifiles;
  44. procedure ExitWithError(s : string);
  45. begin
  46. writeln(s);
  47. writeln('Execution aborted');
  48. halt;
  49. end;
  50. procedure ReadIniFile;
  51. var IniFile : TIniFile;
  52. begin
  53. IniFile := TIniFile.Create('database.ini');
  54. dbtype := IniFile.ReadString('Database','Type','');
  55. dbname := IniFile.ReadString('Database','Name','');
  56. dbuser := IniFile.ReadString('Database','User','');
  57. dbpassword := IniFile.ReadString('Database','Password','');
  58. IniFile.Free;
  59. FPdevBirthDates[1] := StrToDate('1-1-1991');
  60. FPdevBirthDates[2] := StrToDate('2-2-1992');
  61. FPdevBirthDates[3] := StrToDate('3-3-1993');
  62. FPdevBirthDates[4] := StrToDate('4-4-1994');
  63. FPdevBirthDates[5] := StrToDate('5-5-1995');
  64. FPdevBirthDates[6] := StrToDate('6-6-1996');
  65. FPdevBirthDates[7] := StrToDate('7-7-1997');
  66. FPdevBirthDates[8] := StrToDate('8-8-1998');
  67. end;
  68. procedure CreateFConnection;
  69. begin
  70. if dbtype = 'mysql40' then Fconnection := tMySQL40Connection.Create(nil);
  71. if dbtype = 'mysql41' then Fconnection := tMySQL41Connection.Create(nil);
  72. if dbtype = 'mysql50' then Fconnection := tMySQL50Connection.Create(nil);
  73. if dbtype = 'postgresql' then Fconnection := tpqConnection.Create(nil);
  74. if dbtype = 'interbase' then Fconnection := tIBConnection.Create(nil);
  75. if dbtype = 'odbc' then Fconnection := tODBCConnection.Create(nil);
  76. if dbtype = 'oracle' then Fconnection := TOracleConnection.Create(nil);
  77. if not assigned(Fconnection) then ExitWithError('Invalid database-type, check if a valid database-type was provided in the file ''database.ini''');
  78. with Fconnection do
  79. begin
  80. DatabaseName := dbname;
  81. UserName := dbuser;
  82. Password := dbpassword;
  83. open;
  84. end;
  85. end;
  86. procedure CreateFTransaction;
  87. begin
  88. Ftransaction := tsqltransaction.create(nil);
  89. with Ftransaction do
  90. begin
  91. database := Fconnection;
  92. StartTransaction;
  93. end;
  94. end;
  95. procedure CreateFQuery;
  96. begin
  97. Fquery := TSQLQuery.create(nil);
  98. with Fquery do
  99. begin
  100. database := Fconnection;
  101. transaction := Ftransaction;
  102. end;
  103. end;
  104. end.