sqldbexampleunit.pp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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, IBConnection, ODBCConn,
  17. mysql40conn, mysql41conn, mysql50conn;
  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. // I know, this results in bogus-values...
  35. FPdevBirthDates : Array[1..8] of TDateTime = (1-1-1991,2-2-1992,
  36. 3-3-1993, 4-4-1994, 5-5-1995,
  37. 6-6-1996, 7-7-1997, 8-8-1998
  38. );
  39. procedure ExitWithError(s : string);
  40. procedure ReadIniFile;
  41. procedure CreateFConnection;
  42. procedure CreateFTransaction;
  43. procedure CreateFQuery;
  44. implementation
  45. uses
  46. inifiles;
  47. procedure ExitWithError(s : string);
  48. begin
  49. writeln(s);
  50. writeln('Execution aborted');
  51. halt;
  52. end;
  53. procedure ReadIniFile;
  54. var IniFile : TIniFile;
  55. begin
  56. IniFile := TIniFile.Create('database.ini');
  57. dbtype := IniFile.ReadString('Database','Type','');
  58. dbname := IniFile.ReadString('Database','Name','');
  59. dbuser := IniFile.ReadString('Database','User','');
  60. dbpassword := IniFile.ReadString('Database','Password','');
  61. IniFile.Free;
  62. end;
  63. procedure CreateFConnection;
  64. begin
  65. if dbtype = 'mysql40' then Fconnection := tMySQL40Connection.Create(nil);
  66. if dbtype = 'mysql41' then Fconnection := tMySQL41Connection.Create(nil);
  67. if dbtype = 'mysql50' then Fconnection := tMySQL50Connection.Create(nil);
  68. if dbtype = 'postgresql' then Fconnection := tpqConnection.Create(nil);
  69. if dbtype = 'interbase' then Fconnection := tIBConnection.Create(nil);
  70. if dbtype = 'odbc' then Fconnection := tODBCConnection.Create(nil);
  71. if not assigned(Fconnection) then ExitWithError('Invalid database-type, check if a valid database-type was provided in the file ''database.ini''');
  72. with Fconnection do
  73. begin
  74. DatabaseName := dbname;
  75. UserName := dbuser;
  76. Password := dbpassword;
  77. open;
  78. end;
  79. end;
  80. procedure CreateFTransaction;
  81. begin
  82. Ftransaction := tsqltransaction.create(nil);
  83. with Ftransaction do
  84. begin
  85. database := Fconnection;
  86. StartTransaction;
  87. end;
  88. end;
  89. procedure CreateFQuery;
  90. begin
  91. Fquery := TSQLQuery.create(nil);
  92. with Fquery do
  93. begin
  94. database := Fconnection;
  95. transaction := Ftransaction;
  96. end;
  97. end;
  98. end.