sqldbwebdata.pp 13 KB

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