dsparams.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. { TParams }
  2. Function TParams.GetItem(Index: Integer): TParam;
  3. begin
  4. Result:=(Inherited GetItem(Index)) as TParam;
  5. end;
  6. Function TParams.GetParamValue(const ParamName: string): Variant;
  7. begin
  8. Result:=ParamByName(ParamName).Value;
  9. end;
  10. Procedure TParams.SetItem(Index: Integer; Value: TParam);
  11. begin
  12. Inherited SetItem(Index,Value);
  13. end;
  14. Procedure TParams.SetParamValue(const ParamName: string; const Value: Variant);
  15. begin
  16. ParamByName(ParamName).Value:=Value;
  17. end;
  18. Procedure TParams.AssignTo(Dest: TPersistent);
  19. begin
  20. if (Dest is TParams) then
  21. TParams(Dest).Assign(Self)
  22. else
  23. inherited AssignTo(Dest);
  24. end;
  25. Function TParams.GetDataSet: TDataSet;
  26. begin
  27. If (FOwner is TDataset) Then
  28. Result:=TDataset(FOwner)
  29. else
  30. Result:=Nil;
  31. end;
  32. Function TParams.GetOwner: TPersistent;
  33. begin
  34. Result:=FOwner;
  35. end;
  36. constructor TParams.Create(AOwner: TPersistent);
  37. begin
  38. Inherited Create(TParam);
  39. Fowner:=AOwner;
  40. end;
  41. constructor TParams.Create;
  42. begin
  43. Create(TPersistent(Nil));
  44. end;
  45. Procedure TParams.AddParam(Value: TParam);
  46. begin
  47. Value.Collection:=Self;
  48. end;
  49. Procedure TParams.AssignValues(Value: TParams);
  50. Var
  51. I : Integer;
  52. P,PS : TParam;
  53. begin
  54. For I:=0 to Value.Count-1 do
  55. begin
  56. PS:=Value[i];
  57. P:=FindParam(PS.Name);
  58. If Assigned(P) then
  59. P.Assign(PS);
  60. end;
  61. end;
  62. Function TParams.CreateParam(FldType: TFieldType; const ParamName: string;
  63. ParamType: TParamType): TParam;
  64. begin
  65. Result:=Add as TParam;
  66. With Result do
  67. begin
  68. Name:=ParamName;
  69. DataType:=FldType;
  70. ParamType:=ParamType;
  71. end;
  72. end;
  73. Function TParams.FindParam(const Value: string): TParam;
  74. Var
  75. I : Integer;
  76. begin
  77. Result:=Nil;
  78. I:=Count-1;
  79. While (Result=Nil) and (I>=0) do
  80. If (CompareText(Value,Items[i].Name)=0) then
  81. Result:=Items[i]
  82. else
  83. Dec(i);
  84. end;
  85. Procedure TParams.GetParamList(List: TList; const ParamNames: string);
  86. Function NextName(Var S : String) : String;
  87. Var
  88. P : Integer;
  89. begin
  90. P:=Pos(';',S);
  91. If (P=0) then
  92. P:=Length(S)+1;
  93. Result:=Copy(S,1,P-1);
  94. system.Delete(S,1,P);
  95. end;
  96. Var
  97. L,N : String;
  98. begin
  99. L:=ParamNames;
  100. While (Length(L)>0) do
  101. begin
  102. N:=NextName(L);
  103. List.Add(ParamByName(N));
  104. end;
  105. end;
  106. Function TParams.IsEqual(Value: TParams): Boolean;
  107. Var
  108. I : Integer;
  109. begin
  110. Result:=(Value.Count=Count);
  111. I:=Count-1;
  112. While Result and (I>=0) do
  113. begin
  114. Result:=Items[I].IsEqual(Value[i]);
  115. Dec(I);
  116. end;
  117. end;
  118. Function TParams.ParamByName(const Value: string): TParam;
  119. begin
  120. Result:=FindParam(Value);
  121. If (Result=Nil) then
  122. DatabaseErrorFmt(SParameterNotFound,[Value],Dataset);
  123. end;
  124. Function TParams.ParseSQL(SQL: String; DoCreate: Boolean): String;
  125. begin
  126. end;
  127. Procedure TParams.RemoveParam(Value: TParam);
  128. begin
  129. Value.Collection:=Nil;
  130. end;
  131. { TParam }
  132. Function TParam.GetDataSet: TDataSet;
  133. begin
  134. If Assigned(Collection) and (Collection is TParams) then
  135. Result:=TParams(Collection).GetDataset
  136. else
  137. Result:=Nil;
  138. end;
  139. Function TParam.IsParamStored: Boolean;
  140. begin
  141. Result:=Bound;
  142. end;
  143. Procedure TParam.AssignParam(Param: TParam);
  144. begin
  145. if Not Assigned(Param) then
  146. begin
  147. Clear;
  148. FDataType:=ftunknown;
  149. FParamType:=ptUnknown;
  150. Name:='';
  151. Size:=0;
  152. Precision:=0;
  153. NumericScale:=0;
  154. end
  155. else
  156. begin
  157. FDataType:=Param.DataType;
  158. if Param.IsNull then
  159. Clear
  160. else
  161. FValue:=Param.FValue;
  162. FBound:=Param.Bound;
  163. Name:=Param.Name;
  164. if (ParamType=ptUnknown) then
  165. ParamType:=Param.ParamType;
  166. Size:=Param.Size;
  167. Precision:=Param.Precision;
  168. NumericScale:=Param.NumericScale;
  169. end;
  170. end;
  171. Procedure TParam.AssignTo(Dest: TPersistent);
  172. begin
  173. if (Dest is TField) then
  174. AssignToField(TField(Dest))
  175. else
  176. inherited AssignTo(Dest);
  177. end;
  178. Function TParam.GetAsBoolean: Boolean;
  179. begin
  180. If IsNull then
  181. Result:=False
  182. else
  183. Result:=FValue;
  184. end;
  185. Function TParam.GetAsCurrency: Currency;
  186. begin
  187. If IsNull then
  188. Result:=0.0
  189. else
  190. Result:=FValue;
  191. end;
  192. Function TParam.GetAsDateTime: TDateTime;
  193. begin
  194. If IsNull then
  195. Result:=0.0
  196. else
  197. Result:=FValue;
  198. end;
  199. Function TParam.GetAsFloat: Double;
  200. begin
  201. If IsNull then
  202. Result:=0.0
  203. else
  204. Result:=FValue;
  205. end;
  206. Function TParam.GetAsInteger: Longint;
  207. begin
  208. If IsNull then
  209. Result:=0
  210. else
  211. Result:=FValue;
  212. end;
  213. Function TParam.GetAsMemo: string;
  214. begin
  215. If IsNull then
  216. Result:=''
  217. else
  218. Result:=FValue;
  219. end;
  220. Function TParam.GetAsString: string;
  221. begin
  222. If IsNull then
  223. Result:=''
  224. else
  225. Result:=FValue;
  226. end;
  227. Function TParam.GetAsVariant: Variant;
  228. begin
  229. if IsNull then
  230. Result:=Null
  231. else
  232. Result:=FValue;
  233. end;
  234. Function TParam.GetDisplayName: string;
  235. begin
  236. if (FName<>'') then
  237. Result:=FName
  238. else
  239. Result:=inherited GetDisplayName
  240. end;
  241. Function TParam.GetIsNull: Boolean;
  242. begin
  243. Result:= VarIsNull(FValue) or VarIsClear(FValue);
  244. end;
  245. Function TParam.IsEqual(AValue: TParam): Boolean;
  246. begin
  247. Result:=(Name=AValue.Name)
  248. and (IsNull=AValue.IsNull)
  249. and (Bound=AValue.Bound)
  250. and (DataType=AValue.DataType)
  251. and (ParamType=AValue.ParamType)
  252. and (VarType(FValue)=VarType(AValue.FValue))
  253. and (FValue=AValue.FValue);
  254. end;
  255. Procedure TParam.SetAsBlob(const AValue: TBlobData);
  256. begin
  257. FValue:=AValue;
  258. FDataType:=ftBlob;
  259. end;
  260. Procedure TParam.SetAsBoolean(AValue: Boolean);
  261. begin
  262. FValue:=AValue;
  263. FDataType:=ftBoolean;
  264. end;
  265. Procedure TParam.SetAsCurrency(const AValue: Currency);
  266. begin
  267. FValue:=Avalue;
  268. FDataType:=ftCurrency;
  269. end;
  270. Procedure TParam.SetAsDate(const AValue: TDateTime);
  271. begin
  272. FValue:=Avalue;
  273. FDataType:=ftDate;
  274. end;
  275. Procedure TParam.SetAsDateTime(const AValue: TDateTime);
  276. begin
  277. FValue:=AValue;
  278. FDataType:=ftDateTime;
  279. end;
  280. Procedure TParam.SetAsFloat(const AValue: Double);
  281. begin
  282. FValue:=AValue;
  283. FDataType:=ftFloat;
  284. end;
  285. Procedure TParam.SetAsInteger(AValue: Longint);
  286. begin
  287. FValue:=AValue;
  288. FDataType:=ftInteger;
  289. end;
  290. Procedure TParam.SetAsMemo(const AValue: string);
  291. begin
  292. FValue:=AValue;
  293. FDataType:=ftMemo;
  294. end;
  295. Procedure TParam.SetAsSmallInt(AValue: LongInt);
  296. begin
  297. FValue:=AValue;
  298. FDataType:=ftSmallInt;
  299. end;
  300. Procedure TParam.SetAsString(const AValue: string);
  301. begin
  302. FValue:=AValue;
  303. FDataType:=ftString;
  304. end;
  305. Procedure TParam.SetAsTime(const AValue: TDateTime);
  306. begin
  307. FValue:=AValue;
  308. FDataType:=ftTime;
  309. end;
  310. Procedure TParam.SetAsVariant(const AValue: Variant);
  311. begin
  312. FValue:=AValue;
  313. FBound:=not VarIsClear(Value);
  314. if FDataType = ftUnknown then
  315. case VarType(Value) of
  316. varBoolean : FDataType:=ftBoolean;
  317. varSmallint,
  318. varShortInt,
  319. varByte : FDataType:=ftSmallInt;
  320. varWord,
  321. varInteger : FDataType:=ftInteger;
  322. varCurrency : FDataType:=ftCurrency;
  323. varLongWord,
  324. varSingle,
  325. varDouble : FDataType:=ftFloat;
  326. varDate : FDataType:=ftDateTime;
  327. varString,
  328. varOleStr : if (FDataType<>ftFixedChar) then
  329. FDataType:=ftString;
  330. varInt64 : FDataType:=ftLargeInt;
  331. else
  332. FDataType:=ftUnknown;
  333. end;
  334. end;
  335. Procedure TParam.SetAsWord(AValue: LongInt);
  336. begin
  337. FValue:=AValue;
  338. FDataType:=ftWord;
  339. end;
  340. Procedure TParam.SetDataType(AValue: TFieldType);
  341. Var
  342. VT : Integer;
  343. begin
  344. FDataType:=AValue;
  345. VT:=FieldTypetoVariantMap[AValue];
  346. If (VT=varError) then
  347. clear
  348. else
  349. Try
  350. FValue:=VarAsType(AValue,VT)
  351. except
  352. Clear;
  353. end;
  354. end;
  355. Procedure TParam.SetText(const AValue: string);
  356. begin
  357. Value:=AValue;
  358. end;
  359. constructor TParam.Create(ACollection: TCollection);
  360. begin
  361. inherited Create(ACollection);
  362. ParamType:=ptUnknown;
  363. DataType:=ftUnknown;
  364. FValue:=Unassigned;
  365. end;
  366. constructor TParam.Create(AParams: TParams; AParamType: TParamType);
  367. begin
  368. Create(AParams);
  369. ParamType:=AParamType;
  370. end;
  371. Procedure TParam.Assign(Source: TPersistent);
  372. begin
  373. if (Source is TParam) then
  374. AssignParam(TParam(Source))
  375. else if (Source is TField) then
  376. AssignField(TField(Source))
  377. else if (source is TStrings) then
  378. AsMemo:=TStrings(Source).Text
  379. else
  380. inherited Assign(Source);
  381. end;
  382. Procedure TParam.AssignField(Field: TField);
  383. begin
  384. if Assigned(Field) then
  385. begin
  386. // Need TField.Value
  387. // AssignFieldValue(Field,Field.Value);
  388. Name:=Field.FieldName;
  389. end
  390. else
  391. begin
  392. Clear;
  393. Name:='';
  394. end
  395. end;
  396. procedure TParam.AssignToField(Field : TField);
  397. begin
  398. if Assigned(Field) then
  399. case FDataType of
  400. ftUnknown : DatabaseErrorFmt(SUnknownParamFieldType,[Name],DataSet);
  401. // Need TField.AsSmallInt
  402. ftSmallint : Field.AsInteger:=AsSmallInt;
  403. // Need TField.AsWord
  404. ftWord : Field.AsInteger:=AsWord;
  405. ftInteger,
  406. ftAutoInc : Field.AsInteger:=AsInteger;
  407. // Need TField.AsCurrency
  408. ftCurrency : Field.asFloat:=AsCurrency;
  409. ftFloat : Field.asFloat:=AsFloat;
  410. ftBoolean : Field.AsBoolean:=AsBoolean;
  411. ftBlob,
  412. ftGraphic..ftTypedBinary,
  413. ftOraBlob,
  414. ftOraClob,
  415. ftString,
  416. ftMemo,
  417. ftAdt,
  418. ftFixedChar: Field.AsString:=AsString;
  419. ftTime,
  420. ftDate,
  421. ftDateTime : Field.AsDateTime:=AsDateTime;
  422. ftBytes,
  423. ftVarBytes : ; // Todo.
  424. else
  425. If not (DataType in [ftCursor, ftArray, ftDataset,ftReference]) then
  426. DatabaseErrorFmt(SBadParamFieldType, [Name], DataSet);
  427. end;
  428. end;
  429. Procedure TParam.AssignFieldValue(Field: TField; const AValue: Variant);
  430. begin
  431. If Assigned(Field) then
  432. begin
  433. // Need TField.FixedChar property.
  434. if (Field.DataType = ftString) {and TStringField(Field).FixedChar} then
  435. DataType:=ftFixedChar
  436. else if (Field.DataType = ftMemo) and (Field.Size > 255) then
  437. DataType:=ftString
  438. else
  439. DataType:=Field.DataType;
  440. if VarIsNull(AValue) then
  441. Clear
  442. else
  443. Value:=AValue;
  444. Size:=Field.DataSize;
  445. FBound:=True;
  446. end;
  447. end;
  448. Procedure TParam.Clear;
  449. begin
  450. FValue:=UnAssigned;
  451. end;
  452. Procedure TParam.GetData(Buffer: Pointer);
  453. Var
  454. P : Pointer;
  455. S : String;
  456. begin
  457. case FDataType of
  458. ftUnknown : DatabaseErrorFmt(SUnknownParamFieldType,[Name],DataSet);
  459. ftSmallint : PSmallint(Buffer)^:=AsSmallInt;
  460. ftWord : PWord(Buffer)^:=AsWord;
  461. ftInteger,
  462. ftAutoInc : PInteger(Buffer)^:=AsInteger;
  463. ftCurrency : PDouble(Buffer)^:=AsCurrency;
  464. ftFloat : PDouble(Buffer)^:=AsFloat;
  465. ftBoolean : PWordBool(Buffer)^:=AsBoolean;
  466. ftString,
  467. ftMemo,
  468. ftAdt,
  469. ftFixedChar:
  470. begin
  471. S:=AsString;
  472. StrMove(PChar(Buffer),Pchar(S),Length(S)+1);
  473. end;
  474. ftTime : PInteger(Buffer)^:=DateTimeToTimeStamp(AsTime).Time;
  475. ftDate : PInteger(Buffer)^:=DateTimeToTimeStamp(AsTime).Date;
  476. ftDateTime : PDouble(Buffer)^:=TimeStampToMSecs(DateTimeToTimeStamp(AsDateTime));
  477. ftBlob,
  478. ftGraphic..ftTypedBinary,
  479. ftOraBlob,
  480. ftOraClob :
  481. begin
  482. S:=GetAsString;
  483. Move(PChar(S)^, Buffer^, Length(S));
  484. end;
  485. ftBytes, ftVarBytes:
  486. begin
  487. if VarIsArray(FValue) then
  488. begin
  489. P:=VarArrayLock(FValue);
  490. try
  491. Move(P^, Buffer^, VarArrayHighBound(FValue, 1) + 1);
  492. finally
  493. VarArrayUnlock(FValue);
  494. end;
  495. end;
  496. end;
  497. else
  498. If not (DataType in [ftCursor, ftArray, ftDataset,ftReference]) then
  499. DatabaseErrorFmt(SBadParamFieldType, [Name], DataSet);
  500. end;
  501. end;
  502. Function TParam.GetDataSize: Integer;
  503. begin
  504. Result:=0;
  505. case DataType of
  506. ftUnknown : DatabaseErrorFmt(SUnknownParamFieldType,[Name],DataSet);
  507. ftBoolean : Result:=SizeOf(WordBool);
  508. ftInteger,
  509. ftAutoInc : Result:=SizeOf(Integer);
  510. ftSmallint : Result:=SizeOf(SmallInt);
  511. ftWord : Result:=SizeOf(Word);
  512. ftTime,
  513. ftDate : Result:=SizeOf(Integer);
  514. ftDateTime,
  515. ftCurrency,
  516. ftFloat : Result:=SizeOf(Double);
  517. ftString,
  518. ftFixedChar,
  519. ftMemo,
  520. ftADT : Result:=Length(AsString)+1;
  521. ftBytes,
  522. ftVarBytes : if VarIsArray(FValue) then
  523. Result:=VarArrayHighBound(FValue,1)+1
  524. else
  525. Result:=0;
  526. ftBlob,
  527. ftGraphic..ftTypedBinary,
  528. ftOraClob,
  529. ftOraBlob : Result:=Length(AsString);
  530. ftArray,
  531. ftDataSet,
  532. ftReference,
  533. ftCursor : Result:=0;
  534. else
  535. DatabaseErrorFmt(SBadParamFieldType,[Name],DataSet);
  536. end;
  537. end;
  538. Procedure TParam.LoadFromFile(const FileName: string; BlobType: TBlobType);
  539. Var
  540. S : TFileStream;
  541. begin
  542. S:=TFileStream.Create(FileName,fmOpenRead);
  543. Try
  544. LoadFromStream(S,BlobType);
  545. Finally
  546. FreeAndNil(S);
  547. end;
  548. end;
  549. Procedure TParam.LoadFromStream(Stream: TStream; BlobType: TBlobType);
  550. Var
  551. Temp : String;
  552. begin
  553. FDataType:=BlobType;
  554. With Stream do
  555. begin
  556. Position:=0;
  557. SetLength(Temp,Size);
  558. ReadBuffer(Pointer(Temp)^,Size);
  559. FValue:=Temp;
  560. end;
  561. end;
  562. Procedure TParam.SetBlobData(Buffer: Pointer; Size: Integer);
  563. Var
  564. Temp : String;
  565. begin
  566. SetLength(Temp,Size);
  567. Move(Buffer^,Temp,Size);
  568. AsBlob:=Temp;
  569. end;
  570. Procedure TParam.SetData(Buffer: Pointer);
  571. Function FromTimeStamp(T,D : Integer) : TDateTime;
  572. Var TS : TTimeStamp;
  573. begin
  574. TS.Time:=T;
  575. TS.Date:=D;
  576. Result:=TimeStampToDateTime(TS);
  577. end;
  578. begin
  579. case FDataType of
  580. ftUnknown : DatabaseErrorFmt(SUnknownParamFieldType,[Name],DataSet);
  581. ftSmallint : AsSmallInt:=PSmallint(Buffer)^;
  582. ftWord : AsWord:=PWord(Buffer)^;
  583. ftInteger,
  584. ftAutoInc : AsInteger:=PInteger(Buffer)^;
  585. ftCurrency : AsCurrency:= PDouble(Buffer)^;
  586. ftFloat : AsFloat:=PDouble(Buffer)^;
  587. ftBoolean : AsBoolean:=PWordBool(Buffer)^;
  588. ftString,
  589. ftFixedChar: AsString:=StrPas(Buffer);
  590. ftMemo : AsMemo:=StrPas(Buffer);
  591. ftTime : AsTime:=FromTimeStamp(PInteger(Buffer)^,DateDelta);
  592. ftDate : Asdate:=FromTimeStamp(0,PInteger(Buffer)^);
  593. ftDateTime : AsDateTime:=TimeStampToDateTime(MSecsToTimeStamp(trunc(PDouble(Buffer)^)));
  594. ftCursor : FValue:=0;
  595. ftBlob,
  596. ftGraphic..ftTypedBinary,
  597. ftOraBlob,
  598. ftOraClob : SetBlobData(Buffer, StrLen(PChar(Buffer)));
  599. else
  600. DatabaseErrorFmt(SBadParamFieldType,[Name],DataSet);
  601. end;
  602. end;
  603. {
  604. $Log$
  605. Revision 1.6 2005-04-10 18:27:39 joost
  606. - removed TParam.FNull
  607. Revision 1.5 2005/03/24 20:54:53 michael
  608. + Fix in params from Luk Vandelaer
  609. Revision 1.4 2005/02/14 17:13:12 peter
  610. * truncate log
  611. Revision 1.3 2005/02/01 09:05:52 marco
  612. * delete fix
  613. }