dutils.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. (*
  2. Duall Sistemas, Utilities Unit
  3. Copyright (C) 2014 Silvio Clecio
  4. See the file LICENSE.txt, included in this distribution,
  5. for details about the copyright.
  6. This library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. *)
  10. unit dUtils;
  11. {$i dopf.inc}
  12. interface
  13. uses
  14. dClasses, DB, SysUtils, TypInfo;
  15. const
  16. dNullParam: string = 'null';
  17. dNullStr: string = '';
  18. dNullChar: Char = #0;
  19. dNullInt: Integer = 0;
  20. dNullInt64: Int64 = 0;
  21. dNullFloat: Double = 0;
  22. dNullBoolean: Boolean = False;
  23. dNullDate: TDate = 0;
  24. dNullTime: TTime = 0;
  25. dNullDateTime: TDateTime = 0;
  26. dNullEnum: string = '';
  27. dNullSet: string = '';
  28. procedure dParameterizeSQL(var ASql: string; AParams: TParams;
  29. const ANulls: Boolean = False);
  30. procedure dGetFields(AObject: TObject; AFields: TFields;
  31. const ANulls: Boolean = False; const AUseUtf8: Boolean = False);
  32. procedure dSetFields(APropList: PPropList; const APropCount: Integer;
  33. AObject: TObject; AFields: TFields; const ANulls: Boolean = False;
  34. const AUseUtf8: Boolean = False); overload;
  35. procedure dSetFields(AObject: TObject; AFields: TFields;
  36. const ANulls: Boolean = False; const AUseUtf8: Boolean = False); overload;
  37. procedure dGetParams(AObject: TObject; AParams: TParams;
  38. const ANulls: Boolean = False; const AUseUtf8: Boolean = False);
  39. procedure dSetParams(APropList: PPropList; const APropCount: Integer;
  40. AObject: TObject; AParams: TParams; const ANulls: Boolean = False;
  41. const AUseUtf8: Boolean = False); overload;
  42. procedure dSetParams(AObject: TObject; AParams: TParams;
  43. const ANulls: Boolean = False; const AUseUtf8: Boolean = False); overload;
  44. implementation
  45. procedure dParameterizeSQL(var ASql: string; AParams: TParams;
  46. const ANulls: Boolean);
  47. var
  48. V: string;
  49. P: TParam;
  50. I: Integer;
  51. procedure Replace;
  52. begin
  53. { TODO: use exactly replace instead of StringReplace. }
  54. ASql := StringReplace(ASql, ':' + P.Name, V, [rfIgnoreCase]);
  55. end;
  56. begin
  57. if not Assigned(AParams) then
  58. raise EdException.Create('AParams must not be nil.');
  59. if ANulls then
  60. for I := 0 to Pred(AParams.Count) do
  61. begin
  62. P := AParams[I];
  63. case P.DataType of
  64. ftString, ftDate, ftTime, ftDateTime, ftMemo, ftFixedChar, ftGuid:
  65. if P.IsNull then
  66. V := dNullParam
  67. else
  68. V := QuotedStr(P.AsString);
  69. ftFloat, ftCurrency, ftBCD:
  70. if P.IsNull then
  71. V := dNullParam
  72. else
  73. begin
  74. V := FloatToStr(P.AsFloat);
  75. V := StringReplace(V, ',', '.', []);
  76. end;
  77. else
  78. if P.IsNull then
  79. V := dNullParam
  80. else
  81. V := P.AsString;
  82. end;
  83. Replace;
  84. end
  85. else
  86. for I := 0 to Pred(AParams.Count) do
  87. begin
  88. P := AParams[I];
  89. case P.DataType of
  90. ftString, ftDate, ftTime, ftDateTime, ftMemo, ftFixedChar, ftGuid:
  91. V := QuotedStr(P.AsString);
  92. ftFloat, ftCurrency, ftBCD:
  93. begin
  94. V := FloatToStr(P.AsFloat);
  95. V := StringReplace(V, ',', '.', []);
  96. end
  97. else
  98. V := P.AsString;
  99. end;
  100. Replace;
  101. end;
  102. end;
  103. procedure dGetFields(AObject: TObject; AFields: TFields; const ANulls: Boolean;
  104. const AUseUtf8: Boolean);
  105. var
  106. I: Integer;
  107. F: TField;
  108. PI: PPropInfo;
  109. begin
  110. if not Assigned(AObject) then
  111. raise EdException.Create('AObject must not be nil.');
  112. if not Assigned(AFields) then
  113. raise EdException.Create('AFields must not be nil.');
  114. if ANulls then
  115. for I := 0 to Pred(AFields.Count) do
  116. begin
  117. F := AFields[I];
  118. PI := GetPropInfo(PTypeInfo(AObject.ClassInfo), F.FieldName);
  119. if not Assigned(PI) then
  120. Continue;
  121. case PI^.PropType^.Kind of
  122. tkAString:
  123. if AUseUtf8 then
  124. begin
  125. if F.IsNull then
  126. SetStrProp(AObject, PI, dNullStr)
  127. else
  128. SetStrProp(AObject, PI, UTF8Encode(F.AsString));
  129. end
  130. else
  131. begin
  132. if F.IsNull then
  133. SetStrProp(AObject, PI, dNullStr)
  134. else
  135. SetStrProp(AObject, PI, F.AsString);
  136. end;
  137. tkChar:
  138. if F.IsNull then
  139. SetOrdProp(AObject, PI, Ord(dNullChar))
  140. else
  141. SetOrdProp(AObject, PI, Ord(PChar(F.AsString)^));
  142. tkInteger:
  143. if F.IsNull then
  144. SetOrdProp(AObject, PI, dNullInt)
  145. else
  146. SetOrdProp(AObject, PI, F.AsInteger);
  147. tkInt64, tkQWord:
  148. if F.IsNull then
  149. SetInt64Prop(AObject, PI, dNullInt64)
  150. else
  151. SetInt64Prop(AObject, PI, F.AsLargeInt);
  152. tkBool:
  153. if F.IsNull then
  154. SetOrdProp(AObject, PI, Ord(dNullBoolean))
  155. else
  156. SetOrdProp(AObject, PI, Ord(F.AsBoolean));
  157. tkFloat:
  158. case PI^.PropType^.Name of
  159. 'TDate':
  160. if F.IsNull then
  161. SetFloatProp(AObject, PI, dNullDate)
  162. else
  163. SetFloatProp(AObject, PI, Trunc(F.AsDateTime));
  164. 'TTime':
  165. if F.IsNull then
  166. SetFloatProp(AObject, PI, dNullTime)
  167. else
  168. SetFloatProp(AObject, PI, Frac(F.AsDateTime));
  169. 'TDateTime':
  170. if F.IsNull then
  171. SetFloatProp(AObject, PI, dNullDateTime)
  172. else
  173. SetFloatProp(AObject, PI, F.AsDateTime)
  174. else
  175. if F.IsNull then
  176. SetFloatProp(AObject, PI, dNullFloat)
  177. else
  178. SetFloatProp(AObject, PI, F.AsFloat);
  179. end;
  180. tkEnumeration:
  181. if F.IsNull then
  182. SetEnumProp(AObject, PI, dNullEnum)
  183. else
  184. SetEnumProp(AObject, PI, F.AsString);
  185. tkSet:
  186. if F.IsNull then
  187. SetSetProp(AObject, PI, dNullSet)
  188. else
  189. SetSetProp(AObject, PI, F.AsString);
  190. end;
  191. end
  192. else
  193. for I := 0 to Pred(AFields.Count) do
  194. begin
  195. F := AFields[I];
  196. PI := GetPropInfo(PTypeInfo(AObject.ClassInfo), F.FieldName);
  197. if not Assigned(PI) then
  198. Continue;
  199. case PI^.PropType^.Kind of
  200. tkAString:
  201. if AUseUtf8 then
  202. SetStrProp(AObject, PI, UTF8Encode(F.AsString))
  203. else
  204. SetStrProp(AObject, PI, F.AsString);
  205. tkChar: SetOrdProp(AObject, PI, Ord(PChar(F.AsString)^));
  206. tkInteger: SetOrdProp(AObject, PI, F.AsInteger);
  207. tkInt64, tkQWord: SetInt64Prop(AObject, PI, F.AsLargeInt);
  208. tkBool: SetOrdProp(AObject, PI, Ord(F.AsBoolean));
  209. tkFloat:
  210. case PI^.PropType^.Name of
  211. 'TDate': SetFloatProp(AObject, PI, Trunc(F.AsDateTime));
  212. 'TTime': SetFloatProp(AObject, PI, Frac(F.AsDateTime));
  213. 'TDateTime': SetFloatProp(AObject, PI, F.AsDateTime)
  214. else
  215. SetFloatProp(AObject, PI, F.AsFloat);
  216. end;
  217. tkEnumeration: SetEnumProp(AObject, PI, F.AsString);
  218. tkSet: SetSetProp(AObject, PI, F.AsString);
  219. end;
  220. end;
  221. end;
  222. procedure dSetFields(APropList: PPropList; const APropCount: Integer;
  223. AObject: TObject; AFields: TFields; const ANulls: Boolean;
  224. const AUseUtf8: Boolean);
  225. var
  226. F: TField;
  227. I: Integer;
  228. PI: PPropInfo;
  229. begin
  230. if not Assigned(AObject) then
  231. raise EdException.Create('AObject must not be nil.');
  232. if APropCount < 1 then
  233. raise EdException.CreateFmt(
  234. 'APropCount must be greater than zero. Probably, you need to publish ' +
  235. 'the properties in the "%s" class.', [AObject.ClassName]);
  236. if not Assigned(APropList) then
  237. raise EdException.Create('APropList must not be nil.');
  238. if not Assigned(AFields) then
  239. raise EdException.Create('AFields must not be nil.');
  240. if ANulls then
  241. for I := 0 to Pred(APropCount) do
  242. begin
  243. PI := APropList^[I];
  244. F := AFields.FindField(PI^.Name);
  245. if not Assigned(F) then
  246. Continue;
  247. case PI^.PropType^.Kind of
  248. tkAString:
  249. if AUseUtf8 then
  250. begin
  251. F.AsString := UTF8Decode(GetStrProp(AObject, PI));
  252. if F.AsString = dNullStr then
  253. F.Clear;
  254. end
  255. else
  256. begin
  257. F.AsString := GetStrProp(AObject, PI);
  258. if F.AsString = dNullStr then
  259. F.Clear;
  260. end;
  261. tkChar:
  262. begin
  263. F.AsString := Char(GetOrdProp(AObject, PI));
  264. if F.AsString = dNullChar then
  265. F.Clear;
  266. end;
  267. tkInteger:
  268. begin
  269. F.AsInteger := GetOrdProp(AObject, PI);
  270. if F.AsInteger = dNullInt then
  271. F.Clear;
  272. end;
  273. tkInt64, tkQWord:
  274. begin
  275. F.AsLargeInt := GetInt64Prop(AObject, PI);
  276. if F.AsLargeInt = dNullInt64 then
  277. F.Clear;
  278. end;
  279. tkBool:
  280. begin
  281. F.AsBoolean := GetOrdProp(AObject, PI) <> 0;
  282. if F.AsBoolean = dNullBoolean then
  283. F.Clear;
  284. end;
  285. tkFloat:
  286. case PI^.PropType^.Name of
  287. 'TDate':
  288. begin
  289. F.AsDateTime := Trunc(GetFloatProp(AObject, PI));
  290. if F.AsDateTime = dNullDate then
  291. F.Clear;
  292. end;
  293. 'TTime':
  294. begin
  295. F.AsDateTime := Frac(GetFloatProp(AObject, PI));
  296. if F.AsDateTime = dNullTime then
  297. F.Clear;
  298. end;
  299. 'TDateTime':
  300. begin
  301. F.AsDateTime := GetFloatProp(AObject, PI);
  302. if F.AsDateTime = dNullDateTime then
  303. F.Clear;
  304. end
  305. else
  306. F.AsFloat := GetFloatProp(AObject, PI);
  307. if F.AsFloat = dNullFloat then
  308. F.Clear;
  309. end;
  310. tkEnumeration:
  311. begin
  312. F.AsString := GetEnumProp(AObject, PI);
  313. if F.AsString = dNullEnum then
  314. F.Clear;
  315. end;
  316. tkSet:
  317. begin
  318. F.AsString := GetSetProp(AObject, PI, False);
  319. if F.AsString = dNullSet then
  320. F.Clear;
  321. end;
  322. end;
  323. end
  324. else
  325. for I := 0 to Pred(APropCount) do
  326. begin
  327. PI := APropList^[I];
  328. F := AFields.FindField(PI^.Name);
  329. if not Assigned(F) then
  330. Continue;
  331. case PI^.PropType^.Kind of
  332. tkAString:
  333. if AUseUtf8 then
  334. F.AsString := UTF8Decode(GetStrProp(AObject, PI))
  335. else
  336. F.AsString := GetStrProp(AObject, PI);
  337. tkChar: F.AsString := Char(GetOrdProp(AObject, PI));
  338. tkInteger: F.AsInteger := GetOrdProp(AObject, PI);
  339. tkInt64, tkQWord: F.AsLargeInt := GetInt64Prop(AObject, PI);
  340. tkBool: F.AsBoolean := GetOrdProp(AObject, PI) <> 0;
  341. tkFloat:
  342. case PI^.PropType^.Name of
  343. 'TDate': F.AsDateTime := Trunc(GetFloatProp(AObject, PI));
  344. 'TTime': F.AsDateTime := Frac(GetFloatProp(AObject, PI));
  345. 'TDateTime': F.AsDateTime := GetFloatProp(AObject, PI);
  346. else
  347. F.AsFloat := GetFloatProp(AObject, PI);
  348. end;
  349. tkEnumeration: F.AsString := GetEnumProp(AObject, PI);
  350. tkSet: F.AsString := GetSetProp(AObject, PI, False);
  351. end;
  352. end;
  353. end;
  354. procedure dSetFields(AObject: TObject; AFields: TFields; const ANulls: Boolean;
  355. const AUseUtf8: Boolean);
  356. var
  357. C: Integer;
  358. PL: PPropList = nil;
  359. begin
  360. if not Assigned(AObject) then
  361. raise EdException.Create('AObject must not be nil.');
  362. C := GetPropList(PTypeInfo(AObject.ClassInfo), PL);
  363. if Assigned(PL) then
  364. try
  365. dUtils.dSetFields(PL, C, AObject, AFields, ANulls, AUseUtf8);
  366. finally
  367. FreeMem(PL);
  368. end;
  369. end;
  370. procedure dGetParams(AObject: TObject; AParams: TParams; const ANulls: Boolean;
  371. const AUseUtf8: Boolean);
  372. var
  373. I: Integer;
  374. P: TParam;
  375. PI: PPropInfo;
  376. begin
  377. if not Assigned(AObject) then
  378. raise EdException.Create('AObject must not be nil.');
  379. if not Assigned(AParams) then
  380. raise EdException.Create('AParams must not be nil.');
  381. if ANulls then
  382. for I := 0 to Pred(AParams.Count) do
  383. begin
  384. P := AParams[I];
  385. PI := GetPropInfo(PTypeInfo(AObject.ClassInfo), P.Name);
  386. if not Assigned(PI) then
  387. Continue;
  388. case PI^.PropType^.Kind of
  389. tkAString:
  390. if AUseUtf8 then
  391. begin
  392. if P.IsNull then
  393. SetStrProp(AObject, PI, dNullStr)
  394. else
  395. SetStrProp(AObject, PI, UTF8Encode(P.AsString));
  396. end
  397. else
  398. begin
  399. if P.IsNull then
  400. SetStrProp(AObject, PI, dNullStr)
  401. else
  402. SetStrProp(AObject, PI, P.AsString);
  403. end;
  404. tkChar:
  405. if P.IsNull then
  406. SetOrdProp(AObject, PI, Ord(dNullChar))
  407. else
  408. SetOrdProp(AObject, PI, Ord(PChar(P.AsString)^));
  409. tkInteger:
  410. if P.IsNull then
  411. SetOrdProp(AObject, PI, dNullInt)
  412. else
  413. SetOrdProp(AObject, PI, P.AsInteger);
  414. tkInt64, tkQWord:
  415. if P.IsNull then
  416. SetInt64Prop(AObject, PI, dNullInt64)
  417. else
  418. SetInt64Prop(AObject, PI, P.AsLargeInt);
  419. tkBool:
  420. if P.IsNull then
  421. SetOrdProp(AObject, PI, Ord(dNullBoolean))
  422. else
  423. SetOrdProp(AObject, PI, Ord(P.AsBoolean));
  424. tkFloat:
  425. case PI^.PropType^.Name of
  426. 'TDate':
  427. if P.IsNull then
  428. SetFloatProp(AObject, PI, dNullDate)
  429. else
  430. SetFloatProp(AObject, PI, P.AsDate);
  431. 'TTime':
  432. if P.IsNull then
  433. SetFloatProp(AObject, PI, dNullTime)
  434. else
  435. SetFloatProp(AObject, PI, P.AsTime);
  436. 'TDateTime':
  437. if P.IsNull then
  438. SetFloatProp(AObject, PI, dNullDateTime)
  439. else
  440. SetFloatProp(AObject, PI, P.AsDateTime)
  441. else
  442. if P.IsNull then
  443. SetFloatProp(AObject, PI, dNullFloat)
  444. else
  445. SetFloatProp(AObject, PI, P.AsFloat);
  446. end;
  447. tkEnumeration:
  448. if P.IsNull then
  449. SetEnumProp(AObject, PI, dNullEnum)
  450. else
  451. SetEnumProp(AObject, PI, P.AsString);
  452. tkSet:
  453. if P.IsNull then
  454. SetSetProp(AObject, PI, dNullSet)
  455. else
  456. SetSetProp(AObject, PI, P.AsString);
  457. end;
  458. end
  459. else
  460. for I := 0 to Pred(AParams.Count) do
  461. begin
  462. P := AParams[I];
  463. PI := GetPropInfo(PTypeInfo(AObject.ClassInfo), P.Name);
  464. if not Assigned(PI) then
  465. Continue;
  466. case PI^.PropType^.Kind of
  467. tkAString:
  468. if AUseUtf8 then
  469. SetStrProp(AObject, PI, UTF8Encode(P.AsString))
  470. else
  471. SetStrProp(AObject, PI, P.AsString);
  472. tkChar: SetOrdProp(AObject, PI, Ord(PChar(P.AsString)^));
  473. tkInteger: SetOrdProp(AObject, PI, P.AsInteger);
  474. tkInt64, tkQWord: SetInt64Prop(AObject, PI, P.AsLargeInt);
  475. tkBool: SetOrdProp(AObject, PI, Ord(P.AsBoolean));
  476. tkFloat:
  477. case PI^.PropType^.Name of
  478. 'TDate': SetFloatProp(AObject, PI, P.AsDate);
  479. 'TTime': SetFloatProp(AObject, PI, P.AsTime);
  480. 'TDateTime': SetFloatProp(AObject, PI, P.AsDateTime)
  481. else
  482. SetFloatProp(AObject, PI, P.AsFloat);
  483. end;
  484. tkEnumeration: SetEnumProp(AObject, PI, P.AsString);
  485. tkSet: SetSetProp(AObject, PI, P.AsString);
  486. end;
  487. end;
  488. end;
  489. procedure dSetParams(APropList: PPropList; const APropCount: Integer;
  490. AObject: TObject; AParams: TParams; const ANulls: Boolean;
  491. const AUseUtf8: Boolean);
  492. var
  493. P: TParam;
  494. I: Integer;
  495. PI: PPropInfo;
  496. begin
  497. if not Assigned(AObject) then
  498. raise EdException.Create('AObject must not be nil.');
  499. if APropCount < 1 then
  500. raise EdException.CreateFmt(
  501. 'APropCount must be greater than zero. Probably, you need to publish ' +
  502. 'the properties in the "%s" class.', [AObject.ClassName]);
  503. if not Assigned(APropList) then
  504. raise EdException.Create('APropList must not be nil.');
  505. if not Assigned(AParams) then
  506. raise EdException.Create('AParams must not be nil.');
  507. if ANulls then
  508. for I := 0 to Pred(APropCount) do
  509. begin
  510. PI := APropList^[I];
  511. P := AParams.FindParam(PI^.Name);
  512. if not Assigned(P) then
  513. Continue;
  514. case PI^.PropType^.Kind of
  515. tkAString:
  516. if AUseUtf8 then
  517. begin
  518. P.AsString := UTF8Decode(GetStrProp(AObject, PI));
  519. if P.AsString = dNullStr then
  520. P.Clear;
  521. end
  522. else
  523. begin
  524. P.AsString := GetStrProp(AObject, PI);
  525. if P.AsString = dNullStr then
  526. P.Clear;
  527. end;
  528. tkChar:
  529. begin
  530. P.AsString := Char(GetOrdProp(AObject, PI));
  531. if P.AsString = dNullChar then
  532. P.Clear;
  533. end;
  534. tkInteger:
  535. begin
  536. P.AsInteger := GetOrdProp(AObject, PI);
  537. if P.AsInteger = dNullInt then
  538. P.Clear;
  539. end;
  540. tkInt64, tkQWord:
  541. begin
  542. P.AsLargeInt := GetInt64Prop(AObject, PI);
  543. if P.AsLargeInt = dNullInt64 then
  544. P.Clear;
  545. end;
  546. tkBool:
  547. begin
  548. P.AsBoolean := GetOrdProp(AObject, PI) <> 0;
  549. if P.AsBoolean = dNullBoolean then
  550. P.Clear;
  551. end;
  552. tkFloat:
  553. case PI^.PropType^.Name of
  554. 'TDate':
  555. begin
  556. P.AsDate := Trunc(GetFloatProp(AObject, PI));
  557. if P.AsDate = dNullDate then
  558. P.Clear;
  559. end;
  560. 'TTime':
  561. begin
  562. P.AsTime := Frac(GetFloatProp(AObject, PI));
  563. if P.AsTime = dNullTime then
  564. P.Clear;
  565. end;
  566. 'TDateTime':
  567. begin
  568. P.AsDateTime := GetFloatProp(AObject, PI);
  569. if P.AsDateTime = dNullDateTime then
  570. P.Clear;
  571. end
  572. else
  573. P.AsFloat := GetFloatProp(AObject, PI);
  574. if P.AsFloat = dNullFloat then
  575. P.Clear;
  576. end;
  577. tkEnumeration:
  578. begin
  579. P.AsString := GetEnumProp(AObject, PI);
  580. if P.AsString = dNullEnum then
  581. P.Clear;
  582. end;
  583. tkSet:
  584. begin
  585. P.AsString := GetSetProp(AObject, PI, False);
  586. if P.AsString = dNullSet then
  587. P.Clear;
  588. end;
  589. end;
  590. end
  591. else
  592. for I := 0 to Pred(APropCount) do
  593. begin
  594. PI := APropList^[I];
  595. P := AParams.FindParam(PI^.Name);
  596. if not Assigned(P) then
  597. Continue;
  598. case PI^.PropType^.Kind of
  599. tkAString:
  600. if AUseUtf8 then
  601. P.AsString := UTF8Decode(GetStrProp(AObject, PI))
  602. else
  603. P.AsString := GetStrProp(AObject, PI);
  604. tkChar: P.AsString := Char(GetOrdProp(AObject, PI));
  605. tkInteger: P.AsInteger := GetOrdProp(AObject, PI);
  606. tkInt64, tkQWord: P.AsLargeInt := GetInt64Prop(AObject, PI);
  607. tkBool: P.AsBoolean := GetOrdProp(AObject, PI) <> 0;
  608. tkFloat:
  609. case PI^.PropType^.Name of
  610. 'TDate': P.AsDate := Trunc(GetFloatProp(AObject, PI));
  611. 'TTime': P.AsTime := Frac(GetFloatProp(AObject, PI));
  612. 'TDateTime': P.AsDateTime := GetFloatProp(AObject, PI);
  613. else
  614. P.AsFloat := GetFloatProp(AObject, PI);
  615. end;
  616. tkEnumeration: P.AsString := GetEnumProp(AObject, PI);
  617. tkSet: P.AsString := GetSetProp(AObject, PI, False);
  618. end;
  619. end;
  620. end;
  621. procedure dSetParams(AObject: TObject; AParams: TParams; const ANulls: Boolean;
  622. const AUseUtf8: Boolean);
  623. var
  624. C: Integer;
  625. PL: PPropList = nil;
  626. begin
  627. if not Assigned(AObject) then
  628. raise EdException.Create('AObject must not be nil.');
  629. C := GetPropList(PTypeInfo(AObject.ClassInfo), PL);
  630. if Assigned(PL) then
  631. try
  632. dUtils.dSetParams(PL, C, AObject, AParams, ANulls, AUseUtf8);
  633. finally
  634. FreeMem(PL);
  635. end;
  636. end;
  637. end.