dsparams.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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. var pb : TParamBinding;
  126. begin
  127. Result := ParseSQL(SQL,DoCreate,psInterbase, pb);
  128. end;
  129. Function TParams.ParseSQL(SQL: String; DoCreate: Boolean; ParameterStyle : TParamStyle): String;
  130. var pb : TParamBinding;
  131. begin
  132. Result := ParseSQL(SQL,DoCreate,ParameterStyle,pb);
  133. end;
  134. Function TParams.ParseSQL(SQL: String; DoCreate: Boolean; ParameterStyle : TParamStyle; var ParamBinding: TParambinding): String;
  135. type
  136. // used for ParamPart
  137. TStringPart = record
  138. Start,Stop:integer;
  139. end;
  140. const
  141. ParamAllocStepSize = 8;
  142. var
  143. p,ParamNameStart,BufStart:PChar;
  144. ParamName:string;
  145. QuestionMarkParamCount,ParameterIndex,NewLength:integer;
  146. ParamCount:integer; // actual number of parameters encountered so far;
  147. // always <= Length(ParamPart) = Length(Parambinding)
  148. // Parambinding will have length ParamCount in the end
  149. ParamPart:array of TStringPart; // describe which parts of buf are parameters
  150. NewQueryLength:integer;
  151. NewQuery:string;
  152. NewQueryIndex,BufIndex,CopyLen,i:integer; // Parambinding will have length ParamCount in the end
  153. begin
  154. if DoCreate then Clear;
  155. // Parse the SQL and build ParamBinding
  156. ParamCount:=0;
  157. NewQueryLength:=Length(SQL);
  158. SetLength(ParamPart,ParamAllocStepSize);
  159. SetLength(Parambinding,ParamAllocStepSize);
  160. QuestionMarkParamCount:=0; // number of ? params found in query so far
  161. p:=PChar(SQL);
  162. BufStart:=p; // used to calculate ParamPart.Start values
  163. repeat
  164. case p^ of
  165. '''': // single quote delimited string
  166. begin
  167. Inc(p);
  168. while not (p^ in [#0, '''']) do
  169. begin
  170. if p^='\' then Inc(p,2) // make sure we handle \' and \\ correct
  171. else Inc(p);
  172. end;
  173. if p^='''' then Inc(p); // skip final '
  174. end;
  175. '"': // double quote delimited string
  176. begin
  177. Inc(p);
  178. while not (p^ in [#0, '"']) do
  179. begin
  180. if p^='\' then Inc(p,2) // make sure we handle \" and \\ correct
  181. else Inc(p);
  182. end;
  183. if p^='"' then Inc(p); // skip final "
  184. end;
  185. '-': // possible start of -- comment
  186. begin
  187. Inc(p);
  188. if p='-' then // -- comment
  189. begin
  190. repeat // skip until at end of line
  191. Inc(p);
  192. until p^ in [#10, #0];
  193. end
  194. end;
  195. '/': // possible start of /* */ comment
  196. begin
  197. Inc(p);
  198. if p^='*' then // /* */ comment
  199. begin
  200. repeat
  201. Inc(p);
  202. if p^='*' then // possible end of comment
  203. begin
  204. Inc(p);
  205. if p^='/' then Break; // end of comment
  206. end;
  207. until p^=#0;
  208. if p^='/' then Inc(p); // skip final /
  209. end;
  210. end;
  211. ':','?': // parameter
  212. begin
  213. Inc(ParamCount);
  214. if ParamCount>Length(ParamPart) then
  215. begin
  216. NewLength:=Length(ParamPart)+ParamAllocStepSize;
  217. SetLength(ParamPart,NewLength);
  218. SetLength(ParamBinding,NewLength);
  219. end;
  220. if p^=':' then
  221. begin // find parameter name
  222. Inc(p);
  223. ParamNameStart:=p;
  224. while not (p^ in (SQLDelimiterCharacters+[#0])) do
  225. Inc(p);
  226. ParamName:=Copy(ParamNameStart,1,p-ParamNameStart);
  227. end
  228. else
  229. begin
  230. Inc(p);
  231. ParamNameStart:=p;
  232. ParamName:='';
  233. end;
  234. // create Parameter and assign ParameterIndex
  235. if DoCreate then
  236. ParameterIndex := CreateParam(ftUnknown, ParamName, ptInput).Index
  237. // else find ParameterIndex
  238. else
  239. begin
  240. if ParamName<>'' then
  241. ParameterIndex:=ParamByName(ParamName).Index
  242. else
  243. begin
  244. ParameterIndex:=QuestionMarkParamCount;
  245. Inc(QuestionMarkParamCount);
  246. end;
  247. end;
  248. // store ParameterIndex in FParamIndex, ParamPart data
  249. ParamBinding[ParamCount-1]:=ParameterIndex;
  250. ParamPart[ParamCount-1].Start:=ParamNameStart-BufStart;
  251. ParamPart[ParamCount-1].Stop:=p-BufStart+1;
  252. // update NewQueryLength
  253. Dec(NewQueryLength,p-ParamNameStart);
  254. end;
  255. #0:Break;
  256. else
  257. Inc(p);
  258. end;
  259. until false;
  260. SetLength(ParamPart,ParamCount);
  261. SetLength(ParamBinding,ParamCount);
  262. if ParamCount>0 then
  263. begin
  264. // replace :ParamName by ? (using ParamPart array and NewQueryLength)
  265. if ParameterStyle = psPostgreSQL then
  266. if paramcount < 10 then
  267. inc(NewQueryLength,paramcount)
  268. else
  269. inc(NewQueryLength,(paramcount-9)*2+9);
  270. SetLength(NewQuery,NewQueryLength);
  271. NewQueryIndex:=1;
  272. BufIndex:=1;
  273. for i:=0 to High(ParamPart) do
  274. begin
  275. CopyLen:=ParamPart[i].Start-BufIndex;
  276. Move(SQL[BufIndex],NewQuery[NewQueryIndex],CopyLen);
  277. Inc(NewQueryIndex,CopyLen);
  278. case ParameterStyle of
  279. psInterbase : NewQuery[NewQueryIndex]:='?';
  280. psPostgreSQL: begin
  281. ParamName := IntToStr(i+1);
  282. NewQuery[NewQueryIndex]:='$';
  283. Inc(NewQueryIndex);
  284. NewQuery[NewQueryIndex]:= paramname[1];
  285. if i>10 then
  286. begin
  287. Inc(NewQueryIndex);
  288. NewQuery[NewQueryIndex]:= paramname[2]
  289. end;
  290. end;
  291. end;
  292. Inc(NewQueryIndex);
  293. BufIndex:=ParamPart[i].Stop;
  294. end;
  295. CopyLen:=Length(SQL)+1-BufIndex;
  296. Move(SQL[BufIndex],NewQuery[NewQueryIndex],CopyLen);
  297. end
  298. else
  299. NewQuery:=SQL;
  300. Result := NewQuery;
  301. end;
  302. Procedure TParams.RemoveParam(Value: TParam);
  303. begin
  304. Value.Collection:=Nil;
  305. end;
  306. { TParam }
  307. Function TParam.GetDataSet: TDataSet;
  308. begin
  309. If Assigned(Collection) and (Collection is TParams) then
  310. Result:=TParams(Collection).GetDataset
  311. else
  312. Result:=Nil;
  313. end;
  314. Function TParam.IsParamStored: Boolean;
  315. begin
  316. Result:=Bound;
  317. end;
  318. Procedure TParam.AssignParam(Param: TParam);
  319. begin
  320. if Not Assigned(Param) then
  321. begin
  322. Clear;
  323. FDataType:=ftunknown;
  324. FParamType:=ptUnknown;
  325. Name:='';
  326. Size:=0;
  327. Precision:=0;
  328. NumericScale:=0;
  329. end
  330. else
  331. begin
  332. FDataType:=Param.DataType;
  333. if Param.IsNull then
  334. Clear
  335. else
  336. FValue:=Param.FValue;
  337. FBound:=Param.Bound;
  338. Name:=Param.Name;
  339. if (ParamType=ptUnknown) then
  340. ParamType:=Param.ParamType;
  341. Size:=Param.Size;
  342. Precision:=Param.Precision;
  343. NumericScale:=Param.NumericScale;
  344. end;
  345. end;
  346. Procedure TParam.AssignTo(Dest: TPersistent);
  347. begin
  348. if (Dest is TField) then
  349. AssignToField(TField(Dest))
  350. else
  351. inherited AssignTo(Dest);
  352. end;
  353. Function TParam.GetAsBoolean: Boolean;
  354. begin
  355. If IsNull then
  356. Result:=False
  357. else
  358. Result:=FValue;
  359. end;
  360. Function TParam.GetAsCurrency: Currency;
  361. begin
  362. If IsNull then
  363. Result:=0.0
  364. else
  365. Result:=FValue;
  366. end;
  367. Function TParam.GetAsDateTime: TDateTime;
  368. begin
  369. If IsNull then
  370. Result:=0.0
  371. else
  372. Result:=FValue;
  373. end;
  374. Function TParam.GetAsFloat: Double;
  375. begin
  376. If IsNull then
  377. Result:=0.0
  378. else
  379. Result:=FValue;
  380. end;
  381. Function TParam.GetAsInteger: Longint;
  382. begin
  383. If IsNull then
  384. Result:=0
  385. else
  386. Result:=FValue;
  387. end;
  388. Function TParam.GetAsMemo: string;
  389. begin
  390. If IsNull then
  391. Result:=''
  392. else
  393. Result:=FValue;
  394. end;
  395. Function TParam.GetAsString: string;
  396. begin
  397. If IsNull then
  398. Result:=''
  399. else
  400. Result:=FValue;
  401. end;
  402. Function TParam.GetAsVariant: Variant;
  403. begin
  404. if IsNull then
  405. Result:=Null
  406. else
  407. Result:=FValue;
  408. end;
  409. Function TParam.GetDisplayName: string;
  410. begin
  411. if (FName<>'') then
  412. Result:=FName
  413. else
  414. Result:=inherited GetDisplayName
  415. end;
  416. Function TParam.GetIsNull: Boolean;
  417. begin
  418. Result:= VarIsNull(FValue) or VarIsClear(FValue);
  419. end;
  420. Function TParam.IsEqual(AValue: TParam): Boolean;
  421. begin
  422. Result:=(Name=AValue.Name)
  423. and (IsNull=AValue.IsNull)
  424. and (Bound=AValue.Bound)
  425. and (DataType=AValue.DataType)
  426. and (ParamType=AValue.ParamType)
  427. and (VarType(FValue)=VarType(AValue.FValue))
  428. and (FValue=AValue.FValue);
  429. end;
  430. Procedure TParam.SetAsBlob(const AValue: TBlobData);
  431. begin
  432. FValue:=AValue;
  433. FDataType:=ftBlob;
  434. end;
  435. Procedure TParam.SetAsBoolean(AValue: Boolean);
  436. begin
  437. FValue:=AValue;
  438. FDataType:=ftBoolean;
  439. end;
  440. Procedure TParam.SetAsCurrency(const AValue: Currency);
  441. begin
  442. FValue:=Avalue;
  443. FDataType:=ftCurrency;
  444. end;
  445. Procedure TParam.SetAsDate(const AValue: TDateTime);
  446. begin
  447. FValue:=Avalue;
  448. FDataType:=ftDate;
  449. end;
  450. Procedure TParam.SetAsDateTime(const AValue: TDateTime);
  451. begin
  452. FValue:=AValue;
  453. FDataType:=ftDateTime;
  454. end;
  455. Procedure TParam.SetAsFloat(const AValue: Double);
  456. begin
  457. FValue:=AValue;
  458. FDataType:=ftFloat;
  459. end;
  460. Procedure TParam.SetAsInteger(AValue: Longint);
  461. begin
  462. FValue:=AValue;
  463. FDataType:=ftInteger;
  464. end;
  465. Procedure TParam.SetAsMemo(const AValue: string);
  466. begin
  467. FValue:=AValue;
  468. FDataType:=ftMemo;
  469. end;
  470. Procedure TParam.SetAsSmallInt(AValue: LongInt);
  471. begin
  472. FValue:=AValue;
  473. FDataType:=ftSmallInt;
  474. end;
  475. Procedure TParam.SetAsString(const AValue: string);
  476. begin
  477. FValue:=AValue;
  478. FDataType:=ftString;
  479. end;
  480. Procedure TParam.SetAsTime(const AValue: TDateTime);
  481. begin
  482. FValue:=AValue;
  483. FDataType:=ftTime;
  484. end;
  485. Procedure TParam.SetAsVariant(const AValue: Variant);
  486. begin
  487. FValue:=AValue;
  488. FBound:=not VarIsClear(Value);
  489. if FDataType = ftUnknown then
  490. case VarType(Value) of
  491. varBoolean : FDataType:=ftBoolean;
  492. varSmallint,
  493. varShortInt,
  494. varByte : FDataType:=ftSmallInt;
  495. varWord,
  496. varInteger : FDataType:=ftInteger;
  497. varCurrency : FDataType:=ftCurrency;
  498. varLongWord,
  499. varSingle,
  500. varDouble : FDataType:=ftFloat;
  501. varDate : FDataType:=ftDateTime;
  502. varString,
  503. varOleStr : if (FDataType<>ftFixedChar) then
  504. FDataType:=ftString;
  505. varInt64 : FDataType:=ftLargeInt;
  506. else
  507. FDataType:=ftUnknown;
  508. end;
  509. end;
  510. Procedure TParam.SetAsWord(AValue: LongInt);
  511. begin
  512. FValue:=AValue;
  513. FDataType:=ftWord;
  514. end;
  515. Procedure TParam.SetDataType(AValue: TFieldType);
  516. Var
  517. VT : Integer;
  518. begin
  519. FDataType:=AValue;
  520. VT:=FieldTypetoVariantMap[AValue];
  521. If (VT=varError) then
  522. clear
  523. else
  524. Try
  525. FValue:=VarAsType(AValue,VT)
  526. except
  527. Clear;
  528. end;
  529. end;
  530. Procedure TParam.SetText(const AValue: string);
  531. begin
  532. Value:=AValue;
  533. end;
  534. constructor TParam.Create(ACollection: TCollection);
  535. begin
  536. inherited Create(ACollection);
  537. ParamType:=ptUnknown;
  538. DataType:=ftUnknown;
  539. FValue:=Unassigned;
  540. end;
  541. constructor TParam.Create(AParams: TParams; AParamType: TParamType);
  542. begin
  543. Create(AParams);
  544. ParamType:=AParamType;
  545. end;
  546. Procedure TParam.Assign(Source: TPersistent);
  547. begin
  548. if (Source is TParam) then
  549. AssignParam(TParam(Source))
  550. else if (Source is TField) then
  551. AssignField(TField(Source))
  552. else if (source is TStrings) then
  553. AsMemo:=TStrings(Source).Text
  554. else
  555. inherited Assign(Source);
  556. end;
  557. Procedure TParam.AssignField(Field: TField);
  558. begin
  559. if Assigned(Field) then
  560. begin
  561. // Need TField.Value
  562. // AssignFieldValue(Field,Field.Value);
  563. Name:=Field.FieldName;
  564. end
  565. else
  566. begin
  567. Clear;
  568. Name:='';
  569. end
  570. end;
  571. procedure TParam.AssignToField(Field : TField);
  572. begin
  573. if Assigned(Field) then
  574. case FDataType of
  575. ftUnknown : DatabaseErrorFmt(SUnknownParamFieldType,[Name],DataSet);
  576. // Need TField.AsSmallInt
  577. ftSmallint : Field.AsInteger:=AsSmallInt;
  578. // Need TField.AsWord
  579. ftWord : Field.AsInteger:=AsWord;
  580. ftInteger,
  581. ftAutoInc : Field.AsInteger:=AsInteger;
  582. // Need TField.AsCurrency
  583. ftCurrency : Field.asFloat:=AsCurrency;
  584. ftFloat : Field.asFloat:=AsFloat;
  585. ftBoolean : Field.AsBoolean:=AsBoolean;
  586. ftBlob,
  587. ftGraphic..ftTypedBinary,
  588. ftOraBlob,
  589. ftOraClob,
  590. ftString,
  591. ftMemo,
  592. ftAdt,
  593. ftFixedChar: Field.AsString:=AsString;
  594. ftTime,
  595. ftDate,
  596. ftDateTime : Field.AsDateTime:=AsDateTime;
  597. ftBytes,
  598. ftVarBytes : ; // Todo.
  599. else
  600. If not (DataType in [ftCursor, ftArray, ftDataset,ftReference]) then
  601. DatabaseErrorFmt(SBadParamFieldType, [Name], DataSet);
  602. end;
  603. end;
  604. Procedure TParam.AssignFieldValue(Field: TField; const AValue: Variant);
  605. begin
  606. If Assigned(Field) then
  607. begin
  608. // Need TField.FixedChar property.
  609. if (Field.DataType = ftString) {and TStringField(Field).FixedChar} then
  610. DataType:=ftFixedChar
  611. else if (Field.DataType = ftMemo) and (Field.Size > 255) then
  612. DataType:=ftString
  613. else
  614. DataType:=Field.DataType;
  615. if VarIsNull(AValue) then
  616. Clear
  617. else
  618. Value:=AValue;
  619. Size:=Field.DataSize;
  620. FBound:=True;
  621. end;
  622. end;
  623. Procedure TParam.Clear;
  624. begin
  625. FValue:=UnAssigned;
  626. end;
  627. Procedure TParam.GetData(Buffer: Pointer);
  628. Var
  629. P : Pointer;
  630. S : String;
  631. begin
  632. case FDataType of
  633. ftUnknown : DatabaseErrorFmt(SUnknownParamFieldType,[Name],DataSet);
  634. ftSmallint : PSmallint(Buffer)^:=AsSmallInt;
  635. ftWord : PWord(Buffer)^:=AsWord;
  636. ftInteger,
  637. ftAutoInc : PInteger(Buffer)^:=AsInteger;
  638. ftCurrency : PDouble(Buffer)^:=AsCurrency;
  639. ftFloat : PDouble(Buffer)^:=AsFloat;
  640. ftBoolean : PWordBool(Buffer)^:=AsBoolean;
  641. ftString,
  642. ftMemo,
  643. ftAdt,
  644. ftFixedChar:
  645. begin
  646. S:=AsString;
  647. StrMove(PChar(Buffer),Pchar(S),Length(S)+1);
  648. end;
  649. ftTime : PInteger(Buffer)^:=DateTimeToTimeStamp(AsTime).Time;
  650. ftDate : PInteger(Buffer)^:=DateTimeToTimeStamp(AsTime).Date;
  651. ftDateTime : PDouble(Buffer)^:=TimeStampToMSecs(DateTimeToTimeStamp(AsDateTime));
  652. ftBlob,
  653. ftGraphic..ftTypedBinary,
  654. ftOraBlob,
  655. ftOraClob :
  656. begin
  657. S:=GetAsString;
  658. Move(PChar(S)^, Buffer^, Length(S));
  659. end;
  660. ftBytes, ftVarBytes:
  661. begin
  662. if VarIsArray(FValue) then
  663. begin
  664. P:=VarArrayLock(FValue);
  665. try
  666. Move(P^, Buffer^, VarArrayHighBound(FValue, 1) + 1);
  667. finally
  668. VarArrayUnlock(FValue);
  669. end;
  670. end;
  671. end;
  672. else
  673. If not (DataType in [ftCursor, ftArray, ftDataset,ftReference]) then
  674. DatabaseErrorFmt(SBadParamFieldType, [Name], DataSet);
  675. end;
  676. end;
  677. Function TParam.GetDataSize: Integer;
  678. begin
  679. Result:=0;
  680. case DataType of
  681. ftUnknown : DatabaseErrorFmt(SUnknownParamFieldType,[Name],DataSet);
  682. ftBoolean : Result:=SizeOf(WordBool);
  683. ftInteger,
  684. ftAutoInc : Result:=SizeOf(Integer);
  685. ftSmallint : Result:=SizeOf(SmallInt);
  686. ftWord : Result:=SizeOf(Word);
  687. ftTime,
  688. ftDate : Result:=SizeOf(Integer);
  689. ftDateTime,
  690. ftCurrency,
  691. ftFloat : Result:=SizeOf(Double);
  692. ftString,
  693. ftFixedChar,
  694. ftMemo,
  695. ftADT : Result:=Length(AsString)+1;
  696. ftBytes,
  697. ftVarBytes : if VarIsArray(FValue) then
  698. Result:=VarArrayHighBound(FValue,1)+1
  699. else
  700. Result:=0;
  701. ftBlob,
  702. ftGraphic..ftTypedBinary,
  703. ftOraClob,
  704. ftOraBlob : Result:=Length(AsString);
  705. ftArray,
  706. ftDataSet,
  707. ftReference,
  708. ftCursor : Result:=0;
  709. else
  710. DatabaseErrorFmt(SBadParamFieldType,[Name],DataSet);
  711. end;
  712. end;
  713. Procedure TParam.LoadFromFile(const FileName: string; BlobType: TBlobType);
  714. Var
  715. S : TFileStream;
  716. begin
  717. S:=TFileStream.Create(FileName,fmOpenRead);
  718. Try
  719. LoadFromStream(S,BlobType);
  720. Finally
  721. FreeAndNil(S);
  722. end;
  723. end;
  724. Procedure TParam.LoadFromStream(Stream: TStream; BlobType: TBlobType);
  725. Var
  726. Temp : String;
  727. begin
  728. FDataType:=BlobType;
  729. With Stream do
  730. begin
  731. Position:=0;
  732. SetLength(Temp,Size);
  733. ReadBuffer(Pointer(Temp)^,Size);
  734. FValue:=Temp;
  735. end;
  736. end;
  737. Procedure TParam.SetBlobData(Buffer: Pointer; ASize: Integer);
  738. Var
  739. Temp : String;
  740. begin
  741. SetLength(Temp,ASize);
  742. Move(Buffer^,Temp,ASize);
  743. AsBlob:=Temp;
  744. end;
  745. Procedure TParam.SetData(Buffer: Pointer);
  746. Function FromTimeStamp(T,D : Integer) : TDateTime;
  747. Var TS : TTimeStamp;
  748. begin
  749. TS.Time:=T;
  750. TS.Date:=D;
  751. Result:=TimeStampToDateTime(TS);
  752. end;
  753. begin
  754. case FDataType of
  755. ftUnknown : DatabaseErrorFmt(SUnknownParamFieldType,[Name],DataSet);
  756. ftSmallint : AsSmallInt:=PSmallint(Buffer)^;
  757. ftWord : AsWord:=PWord(Buffer)^;
  758. ftInteger,
  759. ftAutoInc : AsInteger:=PInteger(Buffer)^;
  760. ftCurrency : AsCurrency:= PDouble(Buffer)^;
  761. ftFloat : AsFloat:=PDouble(Buffer)^;
  762. ftBoolean : AsBoolean:=PWordBool(Buffer)^;
  763. ftString,
  764. ftFixedChar: AsString:=StrPas(Buffer);
  765. ftMemo : AsMemo:=StrPas(Buffer);
  766. ftTime : AsTime:=FromTimeStamp(PInteger(Buffer)^,DateDelta);
  767. ftDate : Asdate:=FromTimeStamp(0,PInteger(Buffer)^);
  768. ftDateTime : AsDateTime:=TimeStampToDateTime(MSecsToTimeStamp(trunc(PDouble(Buffer)^)));
  769. ftCursor : FValue:=0;
  770. ftBlob,
  771. ftGraphic..ftTypedBinary,
  772. ftOraBlob,
  773. ftOraClob : SetBlobData(Buffer, StrLen(PChar(Buffer)));
  774. else
  775. DatabaseErrorFmt(SBadParamFieldType,[Name],DataSet);
  776. end;
  777. end;