sqldbwebdata.pp 13 KB

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