sqldbwebdata.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. unit sqldbwebdata;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fphttp, fpwebdata, DB, SQLDB;
  6. Type
  7. { TCustomSQLDBWebDataProvider }
  8. TNewIDEvent = Procedure(Sender : TObject; Out AID : String) of object;
  9. TGetParamTypeEvent = Procedure (Sender : TObject; Const ParamName,AValue : String; Var AType : TFieldtype) of object;
  10. TGetParamValueEvent = Procedure (Sender : TObject; P : TParam; Var Handled : Boolean) of object;
  11. TCustomSQLDBWebDataProvider = Class(TFPCustomWebDataProvider)
  12. private
  13. FIDFieldName: String;
  14. FOnGetNewID: TNewIDEvent;
  15. FOnGetParamValue: TGetParamValueEvent;
  16. FParams: TParams;
  17. FSQLS : Array[0..3] of TStringList;
  18. FConnection: TSQLConnection;
  19. FQuery : TSQLQuery;
  20. FLastNewID : String;
  21. FOnGetParamType : TGetParamTypeEvent;
  22. function GetS(AIndex: integer): TStrings;
  23. procedure RegenerateParams;
  24. procedure SetConnection(const AValue: TSQLConnection);
  25. procedure SetParams(const AValue: TParams);
  26. procedure SetS(AIndex: integer; const AValue: TStrings);
  27. Protected
  28. function CheckDataset : Boolean; virtual;
  29. function CreateQuery(AOwner: TComponent; ATransaction: TSQLTransaction; ASQL: Tstrings): TSQLQuery;
  30. function GetParamType(P: TParam; const AValue: String): TFieldType; virtual;
  31. procedure SetTypedParam(P: TParam; Const AValue: String); virtual;
  32. procedure ExecuteSQL(ASQL: TStrings; Msg: String=''; DoNewID : Boolean = False); virtual;
  33. procedure ApplySQLParams(AQuery: TSQLQuery; DoNewID : Boolean = False); virtual;
  34. Procedure SQLChanged(Sender : TObject); virtual;
  35. Procedure DoUpdate; override;
  36. Procedure DoDelete; override;
  37. Procedure DoInsert; override;
  38. Procedure DoApplyParams; override;
  39. Function SQLQuery : TSQLQuery;
  40. Function GetDataset : TDataset; override;
  41. Function GetNewID : String; virtual;
  42. Function IDFieldValue : String; override;
  43. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  44. Property SelectSQL : TStrings Index 0 Read GetS Write SetS;
  45. Property UpdateSQL : TStrings Index 1 Read GetS Write SetS;
  46. Property DeleteSQL : TStrings Index 2 Read GetS Write SetS;
  47. Property InsertSQL : TStrings Index 3 Read GetS Write SetS;
  48. Property Connection : TSQLConnection Read FConnection Write SetConnection;
  49. Property OnGetNewID : TNewIDEvent Read FOnGetNewID Write FOnGetNewID;
  50. property OnGetParameterType : TGetParamTypeEvent Read FOnGetParamType Write FOnGetParamType;
  51. property OnGetParameterValue : TGetParamValueEvent Read FOnGetParamValue Write FOnGetParamValue;
  52. Property Params : TParams Read FParams Write SetParams;
  53. Public
  54. Constructor Create(AOwner : TComponent); override;
  55. Destructor Destroy; override;
  56. end;
  57. TSQLDBWebDataProvider = Class(TCustomSQLDBWebDataProvider)
  58. Published
  59. Property SelectSQL;
  60. Property UpdateSQL;
  61. Property DeleteSQL;
  62. Property InsertSQL;
  63. Property Connection;
  64. Property IDFieldName;
  65. Property OnGetNewID;
  66. property OnGetParameterType;
  67. property OnGetParameterValue;
  68. Property Options;
  69. Property Params;
  70. end;
  71. implementation
  72. { $define wmdebug}
  73. {$ifdef wmdebug}
  74. uses dbugintf;
  75. {$endif}
  76. resourcestring
  77. SErrNoSelectSQL = '%s: No select SQL statement provided.';
  78. SErrNoUpdateSQL = '%s: No update SQL statement provided.';
  79. SErrNoInsertSQL = '%s: No insert SQL statement provided.';
  80. SErrNoDeleteSQL = '%s: No delete SQL statement provided.';
  81. SErrUpdating = '%s: An error occurred during the update operation: %s';
  82. SErrDeleting = '%s: An error occurred during the delete operation: %s';
  83. SErrInserting = '%s: An error occurred during the insert operation: %s';
  84. SErrNoNewIDEvent = '%s : Cannot generate ID: No OnGetNewID event assigned.';
  85. { TCustomSQLDBWebDataProvider }
  86. function TCustomSQLDBWebDataProvider.GetS(AIndex: integer): TStrings;
  87. begin
  88. Result:=FSQLS[AIndex];
  89. end;
  90. procedure TCustomSQLDBWebDataProvider.SetConnection(const AValue: TSQLConnection
  91. );
  92. begin
  93. if (FConnection=AValue) then exit;
  94. If Assigned(FConnection) then
  95. FConnection.RemoveFreeNotification(Self);
  96. FConnection:=AValue;
  97. If Assigned(FConnection) then
  98. FConnection.FreeNotification(Self);
  99. end;
  100. procedure TCustomSQLDBWebDataProvider.SetParams(const AValue: TParams);
  101. begin
  102. if FParams=AValue then exit;
  103. FParams.Assign(AValue);
  104. end;
  105. procedure TCustomSQLDBWebDataProvider.SetS(AIndex: integer;
  106. const AValue: TStrings);
  107. begin
  108. FSQLS[AIndex].Assign(AValue);
  109. end;
  110. procedure TCustomSQLDBWebDataProvider.SQLChanged(Sender: TObject);
  111. begin
  112. If (Sender=SelectSQL) then
  113. begin
  114. if Assigned(FQuery) then
  115. begin
  116. FQuery.Close;
  117. FQuery.SQL.Assign(SelectSQL);
  118. end;
  119. If Not (csLoading in ComponentState) then
  120. RegenerateParams;
  121. end;
  122. end;
  123. procedure TCustomSQLDBWebDataProvider.RegenerateParams;
  124. Var
  125. S : String;
  126. begin
  127. S:=SelectSQL.Text;
  128. Params.Clear;
  129. Params.ParseSQL(S,True);
  130. end;
  131. procedure TCustomSQLDBWebDataProvider.ExecuteSQL(ASQL : TStrings; Msg : String = ''; DoNewID : Boolean = False);
  132. Var
  133. Q : TSQLQuery;
  134. begin
  135. {$ifdef wmdebug}SendDebug('Entering TCustomSQLDBWebDataProvider.ExecuteSQL');{$endif}
  136. Q:=CreateQuery(Nil,Nil,ASQL);
  137. try
  138. Q.Transaction.Active:=True;
  139. try
  140. ApplySQLParams(Q,DoNewID);
  141. Q.ExecSQL;
  142. (Q.Transaction as TSQLTransaction).Commit;
  143. except
  144. On E : Exception do
  145. begin
  146. (Q.Transaction as TSQLTransaction).Rollback;
  147. If (Msg<>'') then
  148. E.Message:=Format(Msg,[Self.Name,E.Message]);
  149. Raise;
  150. end;
  151. end
  152. finally
  153. Q.Free;
  154. end;
  155. {$ifdef wmdebug}SendDebug('Exiting TCustomSQLDBWebDataProvider.ExecuteSQL');{$endif}
  156. end;
  157. procedure TCustomSQLDBWebDataProvider.DoUpdate;
  158. begin
  159. {$ifdef wmdebug}SendDebug('Entering TCustomSQLDBWebDataProvider.DoUpdate');{$endif}
  160. If (Trim(UpdateSQL.Text)='') then
  161. Raise EFPHTTPError.CreateFmt(SErrNoUpdateSQL,[Self.Name]);
  162. FLastNewID:='';
  163. ExecuteSQL(UpdateSQL,SErrUpdating);
  164. {$ifdef wmdebug}SendDebug('Exiting TCustomSQLDBWebDataProvider.DoUpdate');{$endif}
  165. end;
  166. procedure TCustomSQLDBWebDataProvider.DoDelete;
  167. begin
  168. {$ifdef wmdebug}SendDebug('Entering TCustomSQLDBWebDataProvider.DoDelete');{$endif}
  169. If (Trim(DeleteSQL.Text)='') then
  170. Raise EFPHTTPError.CreateFmt(SErrNoDeleteSQL,[Self.Name]);
  171. FLastNewID:='';
  172. ExecuteSQL(DeleteSQL,SErrDeleting);
  173. {$ifdef wmdebug}SendDebug('Exiting TCustomSQLDBWebDataProvider.DoDelete');{$endif}
  174. end;
  175. procedure TCustomSQLDBWebDataProvider.DoInsert;
  176. begin
  177. {$ifdef wmdebug}SendDebug('Entering TCustomSQLDBWebDataProvider.DoInsert');{$endif}
  178. If (Trim(InsertSQL.Text)='') then
  179. Raise EFPHTTPError.CreateFmt(SErrNoInsertSQL,[Self.Name]);
  180. FLastNewID:='';
  181. ExecuteSQL(InsertSQL,SErrInserting,(IDFieldName<>''));
  182. {$ifdef wmdebug}SendDebug('Exiting TCustomSQLDBWebDataProvider.DoInsert');{$endif}
  183. end;
  184. procedure TCustomSQLDBWebDataProvider.Notification(AComponent: TComponent;
  185. Operation: TOperation);
  186. begin
  187. inherited;
  188. If (Operation=opRemove) then
  189. begin
  190. If (AComponent=FQuery) then
  191. FQuery:=Nil
  192. else if (AComponent=FConnection) then
  193. FConnection:=Nil;
  194. end;
  195. end;
  196. Function TCustomSQLDBWebDataProvider.CreateQuery(AOwner : TComponent; ATransaction : TSQLTransaction; ASQL : Tstrings) : TSQLQuery;
  197. begin
  198. Result:=TSQLQuery.Create(AOwner);
  199. If (AOwner<>Self) then
  200. Result.FreeNotification(Self);
  201. Result.DataBase:=Connection;
  202. If ATransaction=Nil then
  203. begin
  204. ATransaction:=TSQLTransaction.Create(Result);
  205. ATransaction.DataBase:=Connection;
  206. end;
  207. Result.Transaction:=ATransaction;
  208. Result.SQL.Assign(ASQL);
  209. end;
  210. Function TCustomSQLDBWebDataProvider.CheckDataset : boolean;
  211. begin
  212. {$ifdef wmdebug}SendDebug('Entering CheckDataset');{$endif}
  213. If (Trim(SelectSQL.Text)='') then
  214. Raise EFPHTTPError.CreateFmt(SErrNoSelectSQL,[Self.Name]);
  215. Result:=FQuery=Nil;
  216. If (Result) then
  217. FQuery:=CreateQuery(Nil,Nil,SelectSQL)
  218. else if not FQuery.Active then
  219. FQuery.SQL.Assign(SelectSQL);
  220. {$ifdef wmdebug}SendDebug('Exiting CheckDataset');{$endif}
  221. end;
  222. Function TCustomSQLDBWebDataProvider.GetParamType(P : TParam; Const AValue : String) : TFieldType;
  223. begin
  224. Result:=ftunknown;
  225. If Assigned(FOnGetParamType) then
  226. FOnGetParamType(Self,P.Name,AValue,Result);
  227. end;
  228. procedure TCustomSQLDBWebDataProvider.SetTypedParam(P : TParam; Const AValue : String);
  229. Var
  230. I : Integer;
  231. Q : Int64;
  232. D : TDateTime;
  233. ft : TFieldType;
  234. F : Double;
  235. B : Boolean;
  236. C : Currency;
  237. begin
  238. ft:=GetParamtype(P,AValue);
  239. If ft<>ftUnknown then
  240. begin
  241. try
  242. case ft of
  243. ftInteger,
  244. ftword,
  245. ftsmallint : I:=StrToInt(AValue);
  246. ftDate : D:=StrToDate(AValue);
  247. ftDateTime,
  248. ftTimestamp : D:=StrToDateTime(AValue);
  249. ftBoolean : B:=StrToBool(AValue);
  250. ftTime : D:=StrToTime(AValue);
  251. ftLargeint : Q:=StrToInt64(AValue);
  252. ftCurrency : C:=StrToCurr(Avalue);
  253. else
  254. ft:=ftString
  255. end
  256. except
  257. ft:=ftUnknown
  258. end;
  259. end;
  260. If (ft=ftUnknown) and (Length(AValue)<30) then
  261. begin
  262. if TryStrToInt(Avalue,I) then
  263. ft:=ftInteger
  264. else if TryStrToInt64(Avalue,Q) then
  265. ft:=ftInteger
  266. else if (Pos(DateSeparator,AValue)<>0) then
  267. begin
  268. if (Pos(TimeSeparator,AValue)<>0) and TryStrToDateTime(Avalue,D) then
  269. ft:=ftDateTime
  270. else if TryStrToDate(Avalue,D) then
  271. ft:=ftDate
  272. end
  273. else If (Pos(TimeSeparator,AValue)<>0) and TryStrToTime(Avalue,D) then
  274. ft:=ftTime
  275. else if (Pos(DecimalSeparator,AValue)<>0) then
  276. begin
  277. if trystrtofloat(AValue,F) then
  278. ft:=ftFloat
  279. else if TryStrToCurr(Avalue,C) then
  280. ft:=ftCurrency
  281. end
  282. else if TryStrToBool(Avalue,B) then
  283. ft:=ftBoolean
  284. end;
  285. Case ft of
  286. ftInteger,
  287. ftword,
  288. ftsmallint : P.AsInteger:=I;
  289. ftBoolean : P.AsBoolean:=B;
  290. ftLargeInt : P.AsLargeInt:=Q;
  291. ftDate : P.AsDate:=D;
  292. ftDateTime,
  293. ftTimestamp : P.AsDateTime:=D;
  294. ftTime : P.AsTime:=D;
  295. ftFloat,
  296. ftBCD,
  297. ftFMTBCD : P.AsFloat:=F;
  298. ftCurrency : P.AsCurrency:=F;
  299. else
  300. P.AsString:=AValue;
  301. end;
  302. end;
  303. procedure TCustomSQLDBWebDataProvider.ApplySQLParams(AQuery : TSQLQuery; DoNewID : Boolean = False);
  304. var
  305. I: Integer;
  306. P : TParam;
  307. S : String;
  308. B : Boolean;
  309. begin
  310. {$ifdef wmdebug}SendDebug('Entering ApplySQLPArams');{$endif}
  311. For I:=0 to AQuery.Params.Count-1 do
  312. begin
  313. P:=AQuery.Params[i];
  314. B:=Assigned(FOnGetParamValue);
  315. if B then
  316. FOnGetParamValue(Self,P,B);
  317. if not B then
  318. begin
  319. If (P.Name=IDFieldName) and DoNewID then
  320. SetTypedParam(P,GetNewID)
  321. else If Adaptor.TryFieldValue(P.Name,S) then
  322. SetTypedParam(P,S)
  323. else If Adaptor.TryParamValue(P.Name,S) then
  324. SetTypedParam(P,S)
  325. else
  326. P.Clear;
  327. end;
  328. end;
  329. {$ifdef wmdebug}SendDebug('Exiting ApplySQLPArams');{$endif}
  330. end;
  331. procedure TCustomSQLDBWebDataProvider.DoApplyParams;
  332. begin
  333. CheckDataset;
  334. ApplySQLParams(FQuery);
  335. end;
  336. function TCustomSQLDBWebDataProvider.SQLQuery: TSQLQuery;
  337. begin
  338. Result:=FQuery;
  339. end;
  340. function TCustomSQLDBWebDataProvider.GetDataset: TDataset;
  341. begin
  342. {$ifdef wmdebug}SendDebug('Get dataset: checking dataset');{$endif}
  343. CheckDataset;
  344. FLastNewID:='';
  345. Result:=FQuery;
  346. {$ifdef wmdebug}SendDebug('Get dataset: activating transaction');{$endif}
  347. If Not FQuery.Transaction.Active then
  348. FQuery.Transaction.Active:=True;
  349. {$ifdef wmdebug}SendDebug('Get dataset: done');{$endif}
  350. end;
  351. function TCustomSQLDBWebDataProvider.GetNewID: String;
  352. begin
  353. If Not Assigned(FOnGetNewID) then
  354. Raise EFPHTTPError.CreateFmt(SErrNoNewIDEvent,[Self.Name]);
  355. FOnGetNewID(Self,Result);
  356. FLastNewID:=Result;
  357. end;
  358. function TCustomSQLDBWebDataProvider.IDFieldValue: String;
  359. begin
  360. {$ifdef wmdebug}SendDebug('Entering IDFieldValue');{$endif}
  361. If (FLastNewID<>'') then
  362. Result:=FLastNewID
  363. else If (IDFieldName<>'') then
  364. begin
  365. If not Adaptor.TryParamValue(IDFieldName,Result) then
  366. If not Adaptor.TryFieldValue(IDFieldName,Result) then
  367. Result:=inherited IDFieldValue;
  368. end
  369. else
  370. Result:=inherited IDFieldValue;
  371. {$ifdef wmdebug}SendDebug('Exiting IDFieldValue : '+Result);{$endif}
  372. end;
  373. constructor TCustomSQLDBWebDataProvider.Create(AOwner: TComponent);
  374. Var
  375. I : Integer;
  376. L : TStringList;
  377. begin
  378. inherited Create(AOwner);
  379. For I:=0 to 3 do
  380. begin
  381. L:=TStringList.Create;
  382. L.OnChange:=@SQLChanged;
  383. FSQLS[i]:=L;
  384. end;
  385. FParams:=TParams.Create(TParam);
  386. end;
  387. destructor TCustomSQLDBWebDataProvider.Destroy;
  388. Var
  389. I: Integer;
  390. begin
  391. For I:=0 to 3 do
  392. FreeAndNil(FSQLS[i]);
  393. Connection:=Nil;
  394. FreeAndNil(FQuery);
  395. FreeAndNil(FParams);
  396. inherited Destroy;
  397. end;
  398. end.