database.inc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Michael Van Canneyt, member of the
  5. Free Pascal development team
  6. TDatabase and related objects implementation
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. { ---------------------------------------------------------------------
  14. TDatabase
  15. ---------------------------------------------------------------------}
  16. Procedure TDatabase.CheckConnected;
  17. begin
  18. If Not Connected Then
  19. DatabaseError(SNotConnected,Self);
  20. end;
  21. Procedure TDatabase.CheckDisConnected;
  22. begin
  23. If Connected Then
  24. DatabaseError(SConnected,Self);
  25. end;
  26. procedure TDataBase.Loaded;
  27. begin
  28. inherited;
  29. if FOpenAfterRead then SetConnected(true);
  30. end;
  31. procedure TDataBase.SetConnected (Value : boolean);
  32. begin
  33. If Value<>FConnected then
  34. begin
  35. If Value then
  36. begin
  37. if csLoading in ComponentState then
  38. begin
  39. FOpenAfterRead := true;
  40. exit;
  41. end
  42. else
  43. DoInternalConnect;
  44. end
  45. else
  46. begin
  47. Closedatasets;
  48. Closetransactions;
  49. DoInternalDisConnect;
  50. if csloading in ComponentState then
  51. FOpenAfterRead := false;
  52. end;
  53. FConnected:=Value;
  54. end;
  55. end;
  56. procedure TDatabase.Notification(AComponent: TComponent; Operation: TOperation);
  57. begin
  58. //!! To be implemented.
  59. end;
  60. constructor TDatabase.Create(AOwner: TComponent);
  61. begin
  62. Inherited Create(AOwner);
  63. FParams:=TStringlist.Create;
  64. FDatasets:=TList.Create;
  65. FTransactions:=TList.Create;
  66. end;
  67. destructor TDatabase.Destroy;
  68. begin
  69. Connected:=False;
  70. RemoveDatasets;
  71. RemoveTransactions;
  72. FDatasets.Free;
  73. FTransactions.Free;
  74. FParams.Free;
  75. Inherited Destroy;
  76. end;
  77. procedure TDatabase.Close;
  78. begin
  79. Connected:=False;
  80. end;
  81. procedure TDatabase.CloseDataSets;
  82. Var I : longint;
  83. begin
  84. If Assigned(FDatasets) then
  85. begin
  86. For I:=FDatasets.Count-1 downto 0 do
  87. TDBDataset(FDatasets[i]).Close;
  88. end;
  89. end;
  90. procedure TDatabase.CloseTransactions;
  91. Var I : longint;
  92. begin
  93. If Assigned(FTransactions) then
  94. begin
  95. For I:=FTransactions.Count-1 downto 0 do
  96. TDBTransaction(FTransactions[i]).EndTransaction;
  97. end;
  98. end;
  99. procedure TDatabase.RemoveDataSets;
  100. Var I : longint;
  101. begin
  102. If Assigned(FDatasets) then
  103. For I:=FDataSets.Count-1 downto 0 do
  104. TDBDataset(FDataSets[i]).Database:=Nil;
  105. end;
  106. procedure TDatabase.RemoveTransactions;
  107. Var I : longint;
  108. begin
  109. If Assigned(FTransactions) then
  110. For I:=FTransactions.Count-1 downto 0 do
  111. TDBTransaction(FTransactions[i]).Database:=Nil;
  112. end;
  113. procedure TDatabase.Open;
  114. begin
  115. Connected:=True;
  116. end;
  117. Function TDatabase.GetDataSetCount : Longint;
  118. begin
  119. If Assigned(FDatasets) Then
  120. Result:=FDatasets.Count
  121. else
  122. Result:=0;
  123. end;
  124. Function TDatabase.GetTransactionCount : Longint;
  125. begin
  126. If Assigned(FTransactions) Then
  127. Result:=FTransactions.Count
  128. else
  129. Result:=0;
  130. end;
  131. Function TDatabase.GetDataset(Index : longint) : TDBDataset;
  132. begin
  133. If Assigned(FDatasets) then
  134. Result:=TDBDataset(FDatasets[Index])
  135. else
  136. DatabaseError(SNoDatasets);
  137. end;
  138. Function TDatabase.GetTransaction(Index : longint) : TDBtransaction;
  139. begin
  140. If Assigned(FTransactions) then
  141. Result:=TDBTransaction(FTransactions[Index])
  142. else
  143. DatabaseError(SNoTransactions);
  144. end;
  145. procedure TDatabase.RegisterDataset (DS : TDBDataset);
  146. Var I : longint;
  147. begin
  148. I:=FDatasets.IndexOf(DS);
  149. If I=-1 then
  150. FDatasets.Add(DS)
  151. else
  152. DatabaseErrorFmt(SDatasetRegistered,[DS.Name]);
  153. end;
  154. procedure TDatabase.RegisterTransaction (TA : TDBTransaction);
  155. Var I : longint;
  156. begin
  157. I:=FTransactions.IndexOf(TA);
  158. If I=-1 then
  159. FTransactions.Add(TA)
  160. else
  161. DatabaseErrorFmt(STransactionRegistered,[TA.Name]);
  162. end;
  163. procedure TDatabase.UnRegisterDataset (DS : TDBDataset);
  164. Var I : longint;
  165. begin
  166. I:=FDatasets.IndexOf(DS);
  167. If I<>-1 then
  168. FDatasets.Delete(I)
  169. else
  170. DatabaseErrorFmt(SNoDatasetRegistered,[DS.Name]);
  171. end;
  172. procedure TDatabase.UnRegisterTransaction (TA : TDBTransaction);
  173. Var I : longint;
  174. begin
  175. I:=FTransactions.IndexOf(TA);
  176. If I<>-1 then
  177. FTransactions.Delete(I)
  178. else
  179. DatabaseErrorFmt(SNoTransactionRegistered,[TA.Name]);
  180. end;
  181. { ---------------------------------------------------------------------
  182. TDBdataset
  183. ---------------------------------------------------------------------}
  184. Procedure TDBDataset.SetDatabase (Value : TDatabase);
  185. begin
  186. CheckInactive;
  187. If Value<>FDatabase then
  188. begin
  189. If Assigned(FDatabase) then
  190. FDatabase.UnregisterDataset(Self);
  191. If Value<>Nil Then
  192. Value.RegisterDataset(Self);
  193. FDatabase:=Value;
  194. end;
  195. end;
  196. Procedure TDBDataset.CheckDatabase;
  197. begin
  198. If (FDatabase=Nil) then
  199. DatabaseError(SErrNoDatabaseAvailable,Self)
  200. end;
  201. Destructor TDBDataset.Destroy;
  202. begin
  203. Database:=Nil;
  204. Inherited;
  205. end;
  206. { ---------------------------------------------------------------------
  207. TDBTransaction
  208. ---------------------------------------------------------------------}
  209. Procedure TDBTransaction.SetDatabase (Value : TDatabase);
  210. begin
  211. // CheckInactive;
  212. If Value<>FDatabase then
  213. begin
  214. If Assigned(FDatabase) then
  215. FDatabase.UnregisterTransaction(Self);
  216. If Value<>Nil Then
  217. Value.RegisterTransaction(Self);
  218. FDatabase:=Value;
  219. end;
  220. end;
  221. Procedure TDBTransaction.CheckDatabase;
  222. begin
  223. If (FDatabase=Nil) then
  224. DatabaseError(SErrNoDatabaseAvailable,Self)
  225. end;
  226. Destructor TDBTransaction.Destroy;
  227. begin
  228. Database:=Nil;
  229. Inherited;
  230. end;
  231. {
  232. $Log$
  233. Revision 1.6 2004-09-26 16:55:24 michael
  234. * big patch from Joost van der Sluis
  235. bufdataset.inc:
  236. fix getrecord (prior)
  237. getcanmodify default false
  238. database.inc / db.inc:
  239. Added transactions
  240. dataset.inc:
  241. raise error if trying to insert into an readonly dataset
  242. db.inc:
  243. remove published properties from bufdataset
  244. changed ancestor of tbufdataset to tdbdataset
  245. Revision 1.5 2004/07/25 11:32:40 michael
  246. * Patches from Joost van der Sluis
  247. interbase.pp:
  248. * Removed unused Fprepared
  249. * Changed the error message 'database connect string not filled
  250. in' to 'database connect string (databasename) not filled in'
  251. * Preparestatement and execute now checks if transaction is
  252. assigned (in stead of crashing if it isn't) and if the
  253. transaction isn't started, it calls starttransaction.
  254. dataset.inc:
  255. * In DoInternalOpen the buffers are now initialised before the
  256. dataset is set into browse-state
  257. database.inc and db.pp:
  258. * If the dataset is created from a stream, the database is opened
  259. after the dataset is read completely
  260. Revision 1.4 2003/08/16 16:42:21 michael
  261. + Fixes in TDBDataset etc. Changed MySQLDb to use database as well
  262. Revision 1.3 2002/09/07 15:15:22 peter
  263. * old logs removed and tabs fixed
  264. }