fields.inc 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Michael Van Canneyt, member of the
  4. Free Pascal development team
  5. TFields and related components implementations.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {Procedure DumpMem (P : Pointer;Size : Longint);
  13. Var i : longint;
  14. begin
  15. Write ('Memory dump : ');
  16. For I:=0 to Size-1 do
  17. Write (Pbyte(P)[i],' ');
  18. Writeln;
  19. end;}
  20. { ---------------------------------------------------------------------
  21. TFieldDef
  22. ---------------------------------------------------------------------}
  23. Constructor TFieldDef.Create(AOwner: TFieldDefs; const AName: string;
  24. ADataType: TFieldType; ASize: Word; ARequired: Boolean; AFieldNo: Longint);
  25. begin
  26. Inherited Create(AOwner);
  27. {$ifdef dsdebug }
  28. Writeln('TFieldDef.Create : ',Aname,'(',AFieldNo,')');
  29. {$endif}
  30. FName:=Aname;
  31. FDisplayName := '';
  32. FDatatype:=ADatatype;
  33. FSize:=ASize;
  34. FRequired:=ARequired;
  35. FPrecision:=-1;
  36. FFieldNo:=AFieldNo;
  37. end;
  38. Destructor TFieldDef.Destroy;
  39. begin
  40. Inherited destroy;
  41. end;
  42. procedure TFieldDef.Assign(APersistent: TPersistent);
  43. var fd: TFieldDef;
  44. begin
  45. fd := nil;
  46. if APersistent is TFieldDef then
  47. fd := APersistent as TFieldDef;
  48. if Assigned(fd) then begin
  49. Collection.BeginUpdate;
  50. try
  51. Name := fd.Name;
  52. DataType := fd.DataType;
  53. Size := fd.Size;
  54. Precision := fd.Precision;
  55. FRequired := fd.Required;
  56. finally
  57. Collection.EndUpdate;
  58. end;
  59. end else
  60. inherited Assign(APersistent);
  61. end;
  62. Function TFieldDef.CreateField(AOwner: TComponent): TField;
  63. Var TheField : TFieldClass;
  64. begin
  65. {$ifdef dsdebug}
  66. Writeln ('Creating field '+FNAME);
  67. {$endif dsdebug}
  68. TheField:=GetFieldClass;
  69. if TheField=Nil then
  70. DatabaseErrorFmt(SUnknownFieldType,[FName]);
  71. Result:=Thefield.Create(AOwner);
  72. Try
  73. Result.Size:=FSize;
  74. Result.Required:=FRequired;
  75. Result.FFieldName:=FName;
  76. Result.FDisplayLabel:=FDisplayName;
  77. Result.FFieldNo:=Self.FieldNo;
  78. Result.SetFieldType(DataType);
  79. Result.FReadOnly:= (faReadOnly in Attributes);
  80. {$ifdef dsdebug}
  81. Writeln ('TFieldDef.CReateField : Trying to set dataset');
  82. {$endif dsdebug}
  83. {$ifdef dsdebug}
  84. Writeln ('TFieldDef.CReateField : Result Fieldno : ',Result.FieldNo,' Self : ',FieldNo);
  85. {$endif dsdebug}
  86. Result.Dataset:=TFieldDefs(Collection).Dataset;
  87. If Result is TFloatField then
  88. TFloatField(Result).Precision:=FPrecision;
  89. except
  90. Result.Free;
  91. Raise;
  92. end;
  93. end;
  94. procedure TFieldDef.SetAttributes(AValue: TFieldAttributes);
  95. begin
  96. FAttributes := AValue;
  97. Changed(False);
  98. end;
  99. procedure TFieldDef.SetDataType(AValue: TFieldType);
  100. begin
  101. FDataType := AValue;
  102. Changed(False);
  103. end;
  104. procedure TFieldDef.SetPrecision(const AValue: Longint);
  105. begin
  106. FPrecision := AValue;
  107. Changed(False);
  108. end;
  109. procedure TFieldDef.SetSize(const AValue: Word);
  110. begin
  111. FSize := AValue;
  112. Changed(False);
  113. end;
  114. procedure TFieldDef.SetRequired(const AValue: Boolean);
  115. begin
  116. FRequired := AValue;
  117. Changed(False);
  118. end;
  119. function TFieldDef.GetDisplayName: string;
  120. begin
  121. Result := FDisplayName;
  122. if Result = '' then
  123. Result := Fname;
  124. end;
  125. procedure TFieldDef.SetDisplayName(const AValue: string);
  126. begin
  127. if (AValue <> '') and (AnsiCompareText(AValue, DisplayName) <> 0) and
  128. (Collection is TOwnedCollection) and
  129. (TFieldDefs(Collection).IndexOf(AValue) >= 0) then
  130. DatabaseErrorFmt(SDuplicateName, [AValue, Collection.ClassName]);
  131. FName := AValue;
  132. end;
  133. Function TFieldDef.GetFieldClass : TFieldClass;
  134. begin
  135. //!! Should be owner as tdataset but that doesn't work ??
  136. If Assigned(Collection) And
  137. (Collection is TFieldDefs) And
  138. Assigned(TFieldDefs(Collection).Dataset) then
  139. Result:=TFieldDefs(Collection).Dataset.GetFieldClass(FDataType)
  140. else
  141. Result:=Nil;
  142. end;
  143. { ---------------------------------------------------------------------
  144. TFieldDefs
  145. ---------------------------------------------------------------------}
  146. {
  147. destructor TFieldDefs.Destroy;
  148. begin
  149. FItems.Free;
  150. // This will destroy all fielddefs since we own them...
  151. Inherited Destroy;
  152. end;
  153. }
  154. procedure TFieldDefs.Add(const AName: string; ADataType: TFieldType);
  155. begin
  156. Add(AName,ADatatype,0,False);
  157. end;
  158. procedure TFieldDefs.Add(const AName: string; ADataType: TFieldType; ASize : Word);
  159. begin
  160. Add(AName,ADatatype,ASize,False);
  161. end;
  162. procedure TFieldDefs.Add(const AName: string; ADataType: TFieldType; ASize: Word;
  163. ARequired: Boolean);
  164. begin
  165. If Length(AName)=0 Then
  166. DatabaseError(SNeedFieldName);
  167. // the fielddef will register itself here as a owned component.
  168. // fieldno is 1 based !
  169. BeginUpdate;
  170. try
  171. TFieldDef.Create(Self,AName,ADataType,ASize,Arequired,Count+1);
  172. finally
  173. EndUpdate;
  174. end;
  175. end;
  176. function TFieldDefs.GetItem(Index: Longint): TFieldDef;
  177. begin
  178. Result := TFieldDef(inherited Items[Index]);;
  179. end;
  180. function TFieldDefs.GetDataset: TDataset;
  181. begin
  182. Result := TDataset(GetOwner);
  183. end;
  184. procedure TFieldDefs.SetItem(Index: Longint; const AValue: TFieldDef);
  185. begin
  186. inherited Items[Index] := AValue;
  187. end;
  188. procedure TFieldDefs.SetItemName(AItem: TCollectionItem);
  189. begin
  190. if AItem is TFieldDef then
  191. with AItem as TFieldDef do
  192. if Name = '' then
  193. Name := Dataset.Name + Copy(ClassName, 2, 5) + IntToStr(ID+1)
  194. else inherited SetItemName(AItem);
  195. end;
  196. constructor TFieldDefs.Create(ADataset: TDataset);
  197. begin
  198. Inherited Create(TPersistent(ADataset), TFieldDef);
  199. end;
  200. procedure TFieldDefs.Assign(FieldDefs: TFieldDefs);
  201. Var I : longint;
  202. begin
  203. Clear;
  204. For i:=0 to FieldDefs.Count-1 do
  205. With FieldDefs[i] do
  206. Add(Name,DataType,Size,Required);
  207. end;
  208. {
  209. procedure TFieldDefs.Clear;
  210. Var I : longint;
  211. begin
  212. For I:=FItems.Count-1 downto 0 do
  213. TFieldDef(Fitems[i]).Free;
  214. FItems.Clear;
  215. end;
  216. }
  217. function TFieldDefs.Find(const AName: string): TFieldDef;
  218. Var I : longint;
  219. begin
  220. I:=IndexOf(AName);
  221. If I=-1 Then
  222. DataBaseErrorFmt(SUnknownField,[AName,DataSet.Name]);
  223. Result:=Items[i];
  224. end;
  225. function TFieldDefs.IndexOf(const AName: string): Longint;
  226. Var I : longint;
  227. begin
  228. For I:=0 to Count-1 do
  229. If AnsiCompareText(Items[I].Name,AName)=0 then
  230. begin
  231. Result:=I;
  232. Exit;
  233. end;
  234. Result:=-1;
  235. end;
  236. procedure TFieldDefs.Update;
  237. begin
  238. DataSet.InitFieldDefs;
  239. end;
  240. Function TFieldDefs.AddFieldDef : TFieldDef;
  241. begin
  242. Result:=TFieldDef.Create(Self,'',ftUnknown,0,False,Count+1);
  243. end;
  244. { ---------------------------------------------------------------------
  245. TField
  246. ---------------------------------------------------------------------}
  247. Const
  248. SBoolean = 'Boolean';
  249. SDateTime = 'TDateTime';
  250. SFloat = 'Float';
  251. SInteger = 'Integer';
  252. SLargeInt = 'LargeInt';
  253. SVariant = 'Variant';
  254. SString = 'String';
  255. constructor TField.Create(AOwner: TComponent);
  256. begin
  257. Inherited Create(AOwner);
  258. FVisible:=True;
  259. FValidChars:=[#0..#255];
  260. FProviderFlags := [pfInUpdate,pfInWhere];
  261. end;
  262. destructor TField.Destroy;
  263. begin
  264. IF Assigned(FDataSet) then
  265. begin
  266. FDataSet.Active:=False;
  267. if Assigned(FFields) then
  268. FFields.Remove(Self);
  269. end;
  270. FLookupList.Free;
  271. Inherited Destroy;
  272. end;
  273. function TField.AccessError(const TypeName: string): EDatabaseError;
  274. begin
  275. Result:=EDatabaseError.CreateFmt(SinvalidTypeConversion,[TypeName,FFieldName]);
  276. end;
  277. procedure TField.Assign(Source: TPersistent);
  278. begin
  279. if Source = nil then Clear
  280. else if Source is TField then begin
  281. Value := TField(Source).Value;
  282. end else
  283. inherited Assign(Source);
  284. end;
  285. procedure TField.AssignValue(const AValue: TVarRec);
  286. procedure Error;
  287. begin
  288. DatabaseErrorFmt(SFieldValueError, [DisplayName]);
  289. end;
  290. begin
  291. with AValue do
  292. case VType of
  293. vtInteger:
  294. AsInteger := VInteger;
  295. vtBoolean:
  296. AsBoolean := VBoolean;
  297. vtChar:
  298. AsString := VChar;
  299. vtExtended:
  300. AsFloat := VExtended^;
  301. vtString:
  302. AsString := VString^;
  303. vtPointer:
  304. if VPointer <> nil then Error;
  305. vtPChar:
  306. AsString := VPChar;
  307. vtObject:
  308. if (VObject = nil) or (VObject is TPersistent) then
  309. Assign(TPersistent(VObject))
  310. else
  311. Error;
  312. vtAnsiString:
  313. AsString := string(VAnsiString);
  314. // vtCurrency:
  315. // AsCurrency := VCurrency^;
  316. vtVariant:
  317. if not VarIsClear(VVariant^) then Self.Value := VVariant^;
  318. vtWideString:
  319. AsString := WideString(VWideString);
  320. vtInt64:
  321. Self.Value := VInt64^;
  322. else
  323. Error;
  324. end;
  325. end;
  326. procedure TField.Change;
  327. begin
  328. If Assigned(FOnChange) Then
  329. FOnChange(Self);
  330. end;
  331. procedure TField.CheckInactive;
  332. begin
  333. If Assigned(FDataSet) then
  334. FDataset.CheckInactive;
  335. end;
  336. procedure TField.Clear;
  337. begin
  338. if FieldKind in [fkData, fkInternalCalc] then
  339. SetData(Nil);
  340. end;
  341. procedure TField.DataChanged;
  342. begin
  343. FDataset.DataEvent(deFieldChange,ptrint(Self));
  344. end;
  345. procedure TField.FocusControl;
  346. begin
  347. FDataSet.DataEvent(deFocusControl,ptrint(Self));
  348. end;
  349. procedure TField.FreeBuffers;
  350. begin
  351. // Empty. Provided for backward compatibiliy;
  352. // TDataset manages the buffers.
  353. end;
  354. function TField.GetAsBoolean: Boolean;
  355. begin
  356. AccessError(SBoolean);
  357. end;
  358. function TField.GetAsDateTime: TDateTime;
  359. begin
  360. AccessError(SdateTime);
  361. end;
  362. function TField.GetAsFloat: Double;
  363. begin
  364. AccessError(SDateTime);
  365. end;
  366. function TField.GetAsLongint: Longint;
  367. begin
  368. AccessError(SInteger);
  369. end;
  370. function TField.GetAsVariant: Variant;
  371. begin
  372. AccessError(SVariant);
  373. end;
  374. function TField.GetAsInteger: Integer;
  375. begin
  376. Result:=GetAsLongint;
  377. end;
  378. function TField.GetAsString: string;
  379. begin
  380. AccessError(SString);
  381. end;
  382. function TField.GetOldValue: Variant;
  383. var SaveState : TDatasetState;
  384. begin
  385. SaveState := FDataset.State;
  386. try
  387. FDataset.SetTempState(dsOldValue);
  388. Result := GetAsVariant;
  389. finally
  390. FDataset.RestoreState(SaveState);
  391. end;
  392. end;
  393. function TField.GetNewValue: Variant;
  394. var SaveState : TDatasetState;
  395. begin
  396. SaveState := FDataset.State;
  397. try
  398. FDataset.SetTempState(dsNewValue);
  399. Result := GetAsVariant;
  400. finally
  401. FDataset.RestoreState(SaveState);
  402. end;
  403. end;
  404. procedure TField.SetNewValue(const AValue: Variant);
  405. var SaveState : TDatasetState;
  406. begin
  407. SaveState := FDataset.State;
  408. try
  409. FDataset.SetTempState(dsNewValue);
  410. SetAsVariant(AValue);
  411. finally
  412. FDataset.RestoreState(SaveState);
  413. end;
  414. end;
  415. function TField.GetCurValue: Variant;
  416. var SaveState : TDatasetState;
  417. begin
  418. SaveState := FDataset.State;
  419. try
  420. FDataset.SetTempState(dsCurValue);
  421. Result := GetAsVariant;
  422. finally
  423. FDataset.RestoreState(SaveState);
  424. end;
  425. end;
  426. function TField.GetCanModify: Boolean;
  427. begin
  428. Result:=Not ReadOnly;
  429. If Result then
  430. begin
  431. Result:=Assigned(DataSet);
  432. If Result then
  433. Result:= DataSet.CanModify;
  434. end;
  435. end;
  436. function TField.GetData(Buffer: Pointer): Boolean;
  437. begin
  438. Result:=GetData(Buffer,True);
  439. end;
  440. function TField.GetData(Buffer: Pointer; NativeFormat : Boolean): Boolean;
  441. begin
  442. IF FDataset=Nil then
  443. DatabaseErrorFmt(SNoDataset,[FieldName]);
  444. If FVAlidating then
  445. begin
  446. result:=Not(FValueBuffer=Nil);
  447. If Result then
  448. Move (FValueBuffer^,Buffer^ ,DataSize);
  449. end
  450. else
  451. Result:=FDataset.GetFieldData(Self,Buffer,NativeFormat);
  452. end;
  453. function TField.GetDataSize: Word;
  454. begin
  455. Result:=0;
  456. end;
  457. function TField.GetDefaultWidth: Longint;
  458. begin
  459. Result:=10;
  460. end;
  461. function TField.GetDisplayName : String;
  462. begin
  463. If FDisplayLabel<>'' then
  464. result:=FDisplayLabel
  465. else
  466. Result:=FFieldName;
  467. end;
  468. Function TField.IsDisplayStored : Boolean;
  469. begin
  470. Result:=(DisplayLabel<>FieldName);
  471. end;
  472. function TField.GetLookupList: TLookupList;
  473. begin
  474. if not Assigned(FLookupList) then
  475. FLookupList := TLookupList.Create;
  476. Result := FLookupList;
  477. end;
  478. procedure TField.CalcLookupValue;
  479. begin
  480. if FLookupCache then
  481. Value := LookupList.ValueOfKey(FDataSet.FieldValues[FKeyFields])
  482. else if (FLookupDataSet <> nil) and FLookupDataSet.Active then
  483. Value := FLookupDataSet.Lookup(FLookupKeyFields,
  484. FDataSet.FieldValues[FKeyFields], FLookupResultField);
  485. end;
  486. function TField.getIndex : longint;
  487. begin
  488. If Assigned(FDataset) then
  489. Result:=FDataset.FFieldList.IndexOf(Self)
  490. else
  491. Result:=-1;
  492. end;
  493. function TField.GetAsLargeInt: LargeInt;
  494. begin
  495. AccessError(SLargeInt);
  496. end;
  497. function TField.GetAsCurrency: Currency;
  498. begin
  499. Result := GetAsFloat;
  500. end;
  501. procedure TField.SetAlignment(const AValue: TAlignMent);
  502. begin
  503. if FAlignment <> AValue then
  504. begin
  505. FAlignment := Avalue;
  506. PropertyChanged(false);
  507. end;
  508. end;
  509. procedure TField.SetIndex(AValue: Integer);
  510. begin
  511. if FFields <> nil then FFields.SetFieldIndex(Self, AValue)
  512. end;
  513. procedure TField.SetAsCurrency(AValue: Currency);
  514. begin
  515. SetAsFloat(AValue);
  516. end;
  517. function TField.GetIsNull: Boolean;
  518. begin
  519. Result:=Not(GetData (Nil));
  520. end;
  521. function TField.GetParentComponent: TComponent;
  522. begin
  523. Result := DataSet;
  524. end;
  525. procedure TField.GetText(var AText: string; ADisplayText: Boolean);
  526. begin
  527. AText:=GetAsString;
  528. end;
  529. function TField.HasParent: Boolean;
  530. begin
  531. HasParent:=True;
  532. end;
  533. function TField.IsValidChar(InputChar: Char): Boolean;
  534. begin
  535. // FValidChars must be set in Create.
  536. Result:=InputChar in FValidChars;
  537. end;
  538. procedure TField.RefreshLookupList;
  539. var SaveActive: Boolean;
  540. begin
  541. if (FLookupDataSet <> nil) And (FLookupKeyFields <> '') And
  542. (FlookupResultField <> '') And (FKeyFields <> '') then begin
  543. SaveActive := FLookupDataSet.Active;
  544. with FLookupDataSet do
  545. try
  546. Active := True;
  547. FFields.CheckFieldNames(FLookupKeyFields);
  548. FieldByName(FLookupResultField);
  549. LookupList.Clear;
  550. DisableControls;
  551. try
  552. First;
  553. while not Eof do begin
  554. FLookupList.Add(FieldValues[FLookupKeyFields],
  555. FieldValues[FLookupResultField]);
  556. Next;
  557. end;
  558. finally
  559. EnableControls;
  560. end;
  561. finally
  562. Active := SaveActive;
  563. end;
  564. end;
  565. end;
  566. procedure TField.Notification(AComponent: TComponent; Operation: TOperation);
  567. begin
  568. Inherited Notification(AComponent,Operation);
  569. if (Operation = opRemove) and (AComponent = FLookupDataSet) then
  570. FLookupDataSet := nil;
  571. end;
  572. procedure TField.PropertyChanged(LayoutAffected: Boolean);
  573. begin
  574. If (FDataset<>Nil) and (FDataset.Active) then
  575. If LayoutAffected then
  576. FDataset.DataEvent(deLayoutChange,0)
  577. else
  578. FDataset.DataEvent(deDatasetchange,0);
  579. end;
  580. procedure TField.ReadState(Reader: TReader);
  581. begin
  582. inherited ReadState(Reader);
  583. if Reader.Parent is TDataSet then
  584. DataSet := TDataSet(Reader.Parent);
  585. end;
  586. procedure TField.SetAsBoolean(AValue: Boolean);
  587. begin
  588. AccessError(SBoolean);
  589. end;
  590. procedure TField.SetAsDateTime(AValue: TDateTime);
  591. begin
  592. AccessError(SDateTime);
  593. end;
  594. procedure TField.SetAsFloat(AValue: Double);
  595. begin
  596. AccessError(SFloat);
  597. end;
  598. procedure TField.SetAsVariant(AValue: Variant);
  599. begin
  600. if VarIsNull(AValue) then
  601. Clear
  602. else
  603. try
  604. SetVarValue(AValue);
  605. except
  606. on EVariantError do DatabaseErrorFmt(SFieldValueError, [DisplayName]);
  607. end;
  608. end;
  609. procedure TField.SetAsLongint(AValue: Longint);
  610. begin
  611. AccessError(SInteger);
  612. end;
  613. procedure TField.SetAsInteger(AValue: Integer);
  614. begin
  615. SetAsLongint(AValue);
  616. end;
  617. procedure TField.SetAsLargeint(AValue: Largeint);
  618. begin
  619. AccessError(SLargeInt);
  620. end;
  621. procedure TField.SetAsString(const AValue: string);
  622. begin
  623. AccessError(SString);
  624. end;
  625. procedure TField.SetData(Buffer: Pointer);
  626. begin
  627. SetData(Buffer,True);
  628. end;
  629. procedure TField.SetData(Buffer: Pointer; NativeFormat : Boolean);
  630. begin
  631. If Not Assigned(FDataset) then
  632. EDatabaseError.CreateFmt(SNoDataset,[FieldName]);
  633. FDataSet.SetFieldData(Self,Buffer, NativeFormat);
  634. end;
  635. Procedure TField.SetDataset (AValue : TDataset);
  636. begin
  637. {$ifdef dsdebug}
  638. Writeln ('Setting dataset');
  639. {$endif}
  640. If AValue=FDataset then exit;
  641. If Assigned(FDataset) Then
  642. begin
  643. FDataset.CheckInactive;
  644. FDataset.FFieldList.Remove(Self);
  645. end;
  646. If Assigned(AValue) then
  647. begin
  648. AValue.CheckInactive;
  649. AValue.FFieldList.Add(Self);
  650. end;
  651. FDataset:=AValue;
  652. end;
  653. procedure TField.SetDataType(AValue: TFieldType);
  654. begin
  655. FDataType := AValue;
  656. end;
  657. procedure TField.SetFieldType(AValue: TFieldType);
  658. begin
  659. //!! To be implemented
  660. end;
  661. procedure TField.SetParentComponent(AParent: TComponent);
  662. begin
  663. if not (csLoading in ComponentState) then
  664. DataSet := AParent as TDataSet;
  665. end;
  666. procedure TField.SetSize(AValue: Word);
  667. begin
  668. CheckInactive;
  669. CheckTypeSize(AValue);
  670. FSize:=AValue;
  671. end;
  672. procedure TField.SetText(const AValue: string);
  673. begin
  674. AsString:=AValue;
  675. end;
  676. procedure TField.SetVarValue(const AValue: Variant);
  677. begin
  678. AccessError(SVariant);
  679. end;
  680. procedure TField.Validate(Buffer: Pointer);
  681. begin
  682. If assigned(OnValidate) Then
  683. begin
  684. FValueBuffer:=Buffer;
  685. FValidating:=True;
  686. Try
  687. OnValidate(Self);
  688. finally
  689. FValidating:=False;
  690. end;
  691. end;
  692. end;
  693. class function Tfield.IsBlob: Boolean;
  694. begin
  695. Result:=False;
  696. end;
  697. class procedure TField.CheckTypeSize(AValue: Longint);
  698. begin
  699. If (AValue<>0) and Not IsBlob Then
  700. DatabaseErrorFmt(SInvalidFieldSize,[AValue]);
  701. end;
  702. // TField private methods
  703. procedure TField.SetEditText(const AValue: string);
  704. begin
  705. if Assigned(OnSetText) then
  706. OnSetText(Self, AValue)
  707. else
  708. SetText(AValue);
  709. end;
  710. function TField.GetEditText: String;
  711. begin
  712. SetLength(Result, 0);
  713. if Assigned(OnGetText) then
  714. OnGetText(Self, Result, False)
  715. else
  716. GetText(Result, False);
  717. end;
  718. function TField.GetDisplayText: String;
  719. begin
  720. SetLength(Result, 0);
  721. if Assigned(OnGetText) then
  722. OnGetText(Self, Result, True)
  723. else
  724. GetText(Result, True);
  725. end;
  726. procedure TField.SetDisplayLabel(const AValue: string);
  727. begin
  728. if FDisplayLabel<>Avalue then
  729. begin
  730. FDisplayLabel:=Avalue;
  731. PropertyChanged(true);
  732. end;
  733. end;
  734. procedure TField.SetDisplayWidth(const AValue: Longint);
  735. begin
  736. if FDisplayWidth<>AValue then
  737. begin
  738. FDisplayWidth:=AValue;
  739. PropertyChanged(True);
  740. end;
  741. end;
  742. function TField.GetDisplayWidth: integer;
  743. begin
  744. if FDisplayWidth=0 then
  745. result:=GetDefaultWidth
  746. else
  747. result:=FDisplayWidth;
  748. end;
  749. procedure TField.SetReadOnly(const AValue: Boolean);
  750. begin
  751. if (FReadOnly<>Avalue) then
  752. begin
  753. FReadOnly:=AValue;
  754. PropertyChanged(True);
  755. end;
  756. end;
  757. procedure TField.SetVisible(const AValue: Boolean);
  758. begin
  759. if FVisible<>Avalue then
  760. begin
  761. FVisible:=AValue;
  762. PropertyChanged(True);
  763. end;
  764. end;
  765. { ---------------------------------------------------------------------
  766. TStringField
  767. ---------------------------------------------------------------------}
  768. constructor TStringField.Create(AOwner: TComponent);
  769. begin
  770. Inherited Create(AOwner);
  771. SetDataType(ftString);
  772. FFixedChar := False;
  773. FTransliterate := False;
  774. Size:=20;
  775. end;
  776. class procedure TStringField.CheckTypeSize(AValue: Longint);
  777. begin
  778. If (AValue<1) or (AValue>dsMaxStringSize) Then
  779. databaseErrorFmt(SInvalidFieldSize,[AValue])
  780. end;
  781. function TStringField.GetAsBoolean: Boolean;
  782. Var S : String;
  783. begin
  784. S:=GetAsString;
  785. result := (Length(S)>0) and (Upcase(S[1]) in ['T',YesNoChars[True]]);
  786. end;
  787. function TStringField.GetAsDateTime: TDateTime;
  788. begin
  789. Result:=StrToDateTime(GetAsString);
  790. end;
  791. function TStringField.GetAsFloat: Double;
  792. begin
  793. Result:=StrToFloat(GetAsString);
  794. end;
  795. function TStringField.GetAsLongint: Longint;
  796. begin
  797. Result:=StrToInt(GetAsString);
  798. end;
  799. function TStringField.GetAsString: string;
  800. begin
  801. If Not GetValue(Result) then
  802. Result:='';
  803. end;
  804. function TStringField.GetAsVariant: Variant;
  805. Var s : string;
  806. begin
  807. If GetValue(s) then
  808. Result:=s
  809. else
  810. Result:=Null;
  811. end;
  812. function TStringField.GetDataSize: Word;
  813. begin
  814. Result:=Size+1;
  815. end;
  816. function TStringField.GetDefaultWidth: Longint;
  817. begin
  818. result:=Size;
  819. end;
  820. Procedure TStringField.GetText(var AText: string; ADisplayText: Boolean);
  821. begin
  822. AText:=GetAsString;
  823. end;
  824. function TStringField.GetValue(var AValue: string): Boolean;
  825. Var Buf, TBuf : TStringFieldBuffer;
  826. begin
  827. Result:=GetData(@Buf);
  828. If Result then
  829. begin
  830. if transliterate then
  831. begin
  832. DataSet.Translate(Buf,TBuf,False);
  833. AValue:=TBuf;
  834. end
  835. else
  836. AValue:=Buf
  837. end
  838. end;
  839. procedure TStringField.SetAsBoolean(AValue: Boolean);
  840. begin
  841. If AValue Then
  842. SetAsString('T')
  843. else
  844. SetAsString('F');
  845. end;
  846. procedure TStringField.SetAsDateTime(AValue: TDateTime);
  847. begin
  848. SetAsString(DateTimeToStr(AValue));
  849. end;
  850. procedure TStringField.SetAsFloat(AValue: Double);
  851. begin
  852. SetAsString(FloatToStr(AValue));
  853. end;
  854. procedure TStringField.SetAsLongint(AValue: Longint);
  855. begin
  856. SetAsString(intToStr(AValue));
  857. end;
  858. procedure TStringField.SetAsString(const AValue: string);
  859. Const NullByte : char = #0;
  860. var Buf : TStringFieldBuffer;
  861. begin
  862. IF Length(AValue)=0 then
  863. SetData(@NullByte)
  864. else if FTransliterate then
  865. begin
  866. DataSet.Translate(@AValue[1],Buf,True);
  867. SetData(@buf);
  868. end
  869. else
  870. SetData(@AValue[1]);
  871. end;
  872. procedure TStringField.SetVarValue(const AValue: Variant);
  873. begin
  874. SetAsString(AValue);
  875. end;
  876. { ---------------------------------------------------------------------
  877. TNumericField
  878. ---------------------------------------------------------------------}
  879. constructor TNumericField.Create(AOwner: TComponent);
  880. begin
  881. Inherited Create(AOwner);
  882. AlignMent:=taRightJustify;
  883. end;
  884. procedure TNumericField.RangeError(AValue, Min, Max: Double);
  885. begin
  886. DatabaseErrorFMT(SRangeError,[AValue,Min,Max,FieldName]);
  887. end;
  888. procedure TNumericField.SetDisplayFormat(const AValue: string);
  889. begin
  890. If FDisplayFormat<>AValue then
  891. begin
  892. FDisplayFormat:=AValue;
  893. PropertyChanged(True);
  894. end;
  895. end;
  896. procedure TNumericField.SetEditFormat(const AValue: string);
  897. begin
  898. If FEDitFormat<>AValue then
  899. begin
  900. FEDitFormat:=AVAlue;
  901. PropertyChanged(True);
  902. end;
  903. end;
  904. { ---------------------------------------------------------------------
  905. TLongintField
  906. ---------------------------------------------------------------------}
  907. constructor TLongintField.Create(AOwner: TComponent);
  908. begin
  909. Inherited Create(AOwner);
  910. SetDatatype(ftinteger);
  911. FMinRange:=Low(LongInt);
  912. FMaxRange:=High(LongInt);
  913. FValidchars:=['+','-','0'..'9'];
  914. end;
  915. function TLongintField.GetAsFloat: Double;
  916. begin
  917. Result:=GetAsLongint;
  918. end;
  919. function TLongintField.GetAsLongint: Longint;
  920. begin
  921. If Not GetValue(Result) then
  922. Result:=0;
  923. end;
  924. function TLongintField.GetAsVariant: Variant;
  925. Var L : Longint;
  926. begin
  927. If GetValue(L) then
  928. Result:=L
  929. else
  930. Result:=Null;
  931. end;
  932. function TLongintField.GetAsString: string;
  933. Var L : Longint;
  934. begin
  935. If GetValue(L) then
  936. Result:=IntTostr(L)
  937. else
  938. Result:='';
  939. end;
  940. function TLongintField.GetDataSize: Word;
  941. begin
  942. Result:=SizeOf(Longint);
  943. end;
  944. procedure TLongintField.GetText(var AText: string; ADisplayText: Boolean);
  945. var l : longint;
  946. fmt : string;
  947. begin
  948. Atext:='';
  949. If Not GetValue(l) then exit;
  950. If ADisplayText or (FEditFormat='') then
  951. fmt:=FDisplayFormat
  952. else
  953. fmt:=FEditFormat;
  954. If length(fmt)<>0 then
  955. AText:=FormatFloat(fmt,L)
  956. else
  957. Str(L,AText);
  958. end;
  959. function TLongintField.GetValue(var AValue: Longint): Boolean;
  960. Var L : Longint;
  961. P : PLongint;
  962. begin
  963. P:=@L;
  964. Result:=GetData(P);
  965. If Result then
  966. Case Datatype of
  967. ftInteger,ftautoinc : AValue:=Plongint(P)^;
  968. ftword : Avalue:=Pword(P)^;
  969. ftsmallint : AValue:=PSmallint(P)^;
  970. end;
  971. end;
  972. procedure TLongintField.SetAsFloat(AValue: Double);
  973. begin
  974. SetAsLongint(Round(Avalue));
  975. end;
  976. procedure TLongintField.SetAsLongint(AValue: Longint);
  977. begin
  978. If CheckRange(AValue) then
  979. SetData(@AValue)
  980. else
  981. RangeError(Avalue,FMinrange,FMaxRange);
  982. end;
  983. procedure TLongintField.SetVarValue(const AValue: Variant);
  984. begin
  985. SetAsLongint(AValue);
  986. end;
  987. procedure TLongintField.SetAsString(const AValue: string);
  988. Var L,Code : longint;
  989. begin
  990. If length(AValue)=0 then
  991. Clear
  992. else
  993. begin
  994. Val(AVAlue,L,Code);
  995. If Code=0 then
  996. SetAsLongint(L)
  997. else
  998. DatabaseErrorFMT(SNotAnInteger,[Avalue]);
  999. end;
  1000. end;
  1001. Function TLongintField.CheckRange(AValue : longint) : Boolean;
  1002. begin
  1003. result := true;
  1004. if (FMaxValue=0) then
  1005. begin
  1006. if (AValue>FMaxRange) Then result := false;
  1007. end
  1008. else
  1009. if AValue>FMaxValue then result := false;
  1010. if (FMinValue=0) then
  1011. begin
  1012. if (AValue<FMinRange) Then result := false;
  1013. end
  1014. else
  1015. if AValue<FMinValue then result := false;
  1016. end;
  1017. Procedure TLongintField.SetMaxValue (AValue : longint);
  1018. begin
  1019. If (AValue>=FMinRange) and (AValue<=FMaxRange) then
  1020. FMaxValue:=AValue
  1021. else
  1022. RangeError(AValue,FMinRange,FMaxRange);
  1023. end;
  1024. Procedure TLongintField.SetMinValue (AValue : longint);
  1025. begin
  1026. If (AValue>=FMinRange) and (AValue<=FMaxRange) then
  1027. FMinValue:=AValue
  1028. else
  1029. RangeError(AValue,FMinRange,FMaxRange);
  1030. end;
  1031. { ---------------------------------------------------------------------
  1032. TLargeintField
  1033. ---------------------------------------------------------------------}
  1034. constructor TLargeintField.Create(AOwner: TComponent);
  1035. begin
  1036. Inherited Create(AOwner);
  1037. SetDatatype(ftLargeint);
  1038. FMinRange:=Low(Largeint);
  1039. FMaxRange:=High(Largeint);
  1040. FValidchars:=['+','-','0'..'9'];
  1041. end;
  1042. function TLargeintField.GetAsFloat: Double;
  1043. begin
  1044. Result:=GetAsLargeint;
  1045. end;
  1046. function TLargeintField.GetAsLargeint: Largeint;
  1047. begin
  1048. If Not GetValue(Result) then
  1049. Result:=0;
  1050. end;
  1051. function TLargeIntField.GetAsVariant: Variant;
  1052. Var L : Largeint;
  1053. begin
  1054. If GetValue(L) then
  1055. Result:=L
  1056. else
  1057. Result:=Null;
  1058. end;
  1059. function TLargeintField.GetAsLongint: Longint;
  1060. begin
  1061. Result:=GetAsLargeint;
  1062. end;
  1063. function TLargeintField.GetAsString: string;
  1064. Var L : Largeint;
  1065. begin
  1066. If GetValue(L) then
  1067. Result:=IntTostr(L)
  1068. else
  1069. Result:='';
  1070. end;
  1071. function TLargeintField.GetDataSize: Word;
  1072. begin
  1073. Result:=SizeOf(Largeint);
  1074. end;
  1075. procedure TLargeintField.GetText(var AText: string; ADisplayText: Boolean);
  1076. var l : largeint;
  1077. fmt : string;
  1078. begin
  1079. Atext:='';
  1080. If Not GetValue(l) then exit;
  1081. If ADisplayText or (FEditFormat='') then
  1082. fmt:=FDisplayFormat
  1083. else
  1084. fmt:=FEditFormat;
  1085. If length(fmt)<>0 then
  1086. AText:=FormatFloat(fmt,L)
  1087. else
  1088. Str(L,AText);
  1089. end;
  1090. function TLargeintField.GetValue(var AValue: Largeint): Boolean;
  1091. type
  1092. PLargeint = ^Largeint;
  1093. Var P : PLargeint;
  1094. begin
  1095. P:=@AValue;
  1096. Result:=GetData(P);
  1097. end;
  1098. procedure TLargeintField.SetAsFloat(AValue: Double);
  1099. begin
  1100. SetAsLargeint(Round(Avalue));
  1101. end;
  1102. procedure TLargeintField.SetAsLargeint(AValue: Largeint);
  1103. begin
  1104. If CheckRange(AValue) then
  1105. SetData(@AValue)
  1106. else
  1107. RangeError(Avalue,FMinrange,FMaxRange);
  1108. end;
  1109. procedure TLargeintField.SetAsLongint(AValue: Longint);
  1110. begin
  1111. SetAsLargeint(Avalue);
  1112. end;
  1113. procedure TLargeintField.SetAsString(const AValue: string);
  1114. Var L : largeint;
  1115. code : longint;
  1116. begin
  1117. If length(AValue)=0 then
  1118. Clear
  1119. else
  1120. begin
  1121. Val(AVAlue,L,Code);
  1122. If Code=0 then
  1123. SetAsLargeint(L)
  1124. else
  1125. DatabaseErrorFMT(SNotAnInteger,[Avalue]);
  1126. end;
  1127. end;
  1128. procedure TLargeintField.SetVarValue(const AValue: Variant);
  1129. begin
  1130. SetAsLargeint(AValue);
  1131. end;
  1132. Function TLargeintField.CheckRange(AValue : largeint) : Boolean;
  1133. begin
  1134. result := true;
  1135. if (FMaxValue=0) then
  1136. begin
  1137. if (AValue>FMaxRange) Then result := false;
  1138. end
  1139. else
  1140. if AValue>FMaxValue then result := false;
  1141. if (FMinValue=0) then
  1142. begin
  1143. if (AValue<FMinRange) Then result := false;
  1144. end
  1145. else
  1146. if AValue<FMinValue then result := false;
  1147. end;
  1148. Procedure TLargeintField.SetMaxValue (AValue : largeint);
  1149. begin
  1150. If (AValue>=FMinRange) and (AValue<=FMaxRange) then
  1151. FMaxValue:=AValue
  1152. else
  1153. RangeError(AValue,FMinRange,FMaxRange);
  1154. end;
  1155. Procedure TLargeintField.SetMinValue (AValue : largeint);
  1156. begin
  1157. If (AValue>=FMinRange) and (AValue<=FMaxRange) then
  1158. FMinValue:=AValue
  1159. else
  1160. RangeError(AValue,FMinRange,FMaxRange);
  1161. end;
  1162. { TSmallintField }
  1163. function TSmallintField.GetDataSize: Word;
  1164. begin
  1165. Result:=SizeOf(SmallInt);
  1166. end;
  1167. constructor TSmallintField.Create(AOwner: TComponent);
  1168. begin
  1169. inherited Create(AOwner);
  1170. SetDataType(ftSmallInt);
  1171. FMinRange:=-32768;
  1172. FMaxRange:=32767;
  1173. end;
  1174. { TWordField }
  1175. function TWordField.GetDataSize: Word;
  1176. begin
  1177. Result:=SizeOf(Word);
  1178. end;
  1179. constructor TWordField.Create(AOwner: TComponent);
  1180. begin
  1181. inherited Create(AOwner);
  1182. SetDataType(ftWord);
  1183. FMinRange:=0;
  1184. FMaxRange:=65535;
  1185. FValidchars:=['+','0'..'9'];
  1186. end;
  1187. { TAutoIncField }
  1188. constructor TAutoIncField.Create(AOwner: TComponent);
  1189. begin
  1190. Inherited Create(AOWner);
  1191. SetDataType(ftAutoInc);
  1192. FReadOnly:=True;
  1193. end;
  1194. Procedure TAutoIncField.SetAsLongint(AValue : Longint);
  1195. begin
  1196. DataBaseError(SCantSetAutoIncfields);
  1197. end;
  1198. { TFloatField }
  1199. function TFloatField.GetAsFloat: Double;
  1200. begin
  1201. If Not GetData(@Result) Then
  1202. Result:=0.0;
  1203. end;
  1204. function TFloatField.GetAsVariant: Variant;
  1205. Var f : Double;
  1206. begin
  1207. If GetData(@f) then
  1208. Result := f
  1209. else
  1210. Result:=Null;
  1211. end;
  1212. function TFloatField.GetAsLongint: Longint;
  1213. begin
  1214. Result:=Round(GetAsFloat);
  1215. end;
  1216. function TFloatField.GetAsString: string;
  1217. Var R : Double;
  1218. begin
  1219. If GetData(@R) then
  1220. Result:=FloatToStr(R)
  1221. else
  1222. Result:='';
  1223. end;
  1224. function TFloatField.GetDataSize: Word;
  1225. begin
  1226. Result:=SizeOf(Double);
  1227. end;
  1228. procedure TFloatField.GetText(var TheText: string; ADisplayText: Boolean);
  1229. Var
  1230. fmt : string;
  1231. E : Double;
  1232. begin
  1233. TheText:='';
  1234. If Not GetData(@E) then exit;
  1235. If ADisplayText or (Length(FEditFormat) = 0) Then
  1236. Fmt:=FDisplayFormat
  1237. else
  1238. Fmt:=FEditFormat;
  1239. If fmt<>'' then
  1240. TheText:=FormatFloat(fmt,E)
  1241. else
  1242. TheText:=FloatToStrF(E,ffgeneral,FPrecision,0);
  1243. end;
  1244. procedure TFloatField.SetAsFloat(AValue: Double);
  1245. begin
  1246. If CheckRange(AValue) then
  1247. SetData(@Avalue)
  1248. else
  1249. RangeError(AValue,FMinValue,FMaxValue);
  1250. end;
  1251. procedure TFloatField.SetAsLongint(AValue: Longint);
  1252. begin
  1253. SetAsFloat(Avalue);
  1254. end;
  1255. procedure TFloatField.SetAsString(const AValue: string);
  1256. Var R : Double;
  1257. begin
  1258. try
  1259. R := StrToFloat(AValue);
  1260. SetAsFloat(R);
  1261. except
  1262. DatabaseErrorFmt(SNotAFloat, [AValue]);
  1263. end;
  1264. end;
  1265. procedure TFloatField.SetVarValue(const AValue: Variant);
  1266. begin
  1267. SetAsFloat(Avalue);
  1268. end;
  1269. constructor TFloatField.Create(AOwner: TComponent);
  1270. begin
  1271. Inherited Create(AOwner);
  1272. SetDatatype(ftfloat);
  1273. FPrecision:=15;
  1274. FValidChars := [DecimalSeparator, '+', '-', '0'..'9', 'E', 'e'];
  1275. end;
  1276. Function TFloatField.CheckRange(AValue : Double) : Boolean;
  1277. begin
  1278. If (FMinValue<>0) or (FmaxValue<>0) then
  1279. Result:=(AValue>=FMinValue) and (AVAlue<=FMAxValue)
  1280. else
  1281. Result:=True;
  1282. end;
  1283. { TCurrencyField }
  1284. Constructor TCurrencyField.Create(AOwner: TComponent);
  1285. begin
  1286. inherited Create(AOwner);
  1287. SetDataType(ftCurrency);
  1288. end;
  1289. procedure TCurrencyField.GetText(var TheText: string; ADisplayText: Boolean);
  1290. Var
  1291. fmt : string;
  1292. ff: TFloatFormat;
  1293. E : Double;
  1294. begin
  1295. TheText:='';
  1296. If Not GetData(@E) then exit;
  1297. If ADisplayText or (Length(FEditFormat) = 0) Then
  1298. Fmt:=FDisplayFormat
  1299. else
  1300. Fmt:=FEditFormat;
  1301. if ADisplayText then
  1302. ff := ffCurrency
  1303. else
  1304. ff := ffFixed;
  1305. If fmt<>'' then
  1306. TheText:=FormatFloat(fmt, E)
  1307. else
  1308. TheText:=FloatToStrF(E, ff, FPrecision, CurrencyDecimals);
  1309. end;
  1310. { TBooleanField }
  1311. function TBooleanField.GetAsBoolean: Boolean;
  1312. var b : wordbool;
  1313. begin
  1314. If GetData(@b) then
  1315. result := b
  1316. else
  1317. Result:=False;
  1318. end;
  1319. function TBooleanField.GetAsVariant: Variant;
  1320. Var b : wordbool;
  1321. begin
  1322. If GetData(@b) then
  1323. Result := b
  1324. else
  1325. Result:=Null;
  1326. end;
  1327. function TBooleanField.GetAsString: string;
  1328. Var B : wordbool;
  1329. begin
  1330. If Getdata(@B) then
  1331. Result:=FDisplays[False,B]
  1332. else
  1333. result:='';
  1334. end;
  1335. function TBooleanField.GetDataSize: Word;
  1336. begin
  1337. Result:=SizeOf(wordBool);
  1338. end;
  1339. function TBooleanField.GetDefaultWidth: Longint;
  1340. begin
  1341. Result:=Length(FDisplays[false,false]);
  1342. If Result<Length(FDisplays[false,True]) then
  1343. Result:=Length(FDisplays[false,True]);
  1344. end;
  1345. procedure TBooleanField.SetAsBoolean(AValue: Boolean);
  1346. var b : wordbool;
  1347. begin
  1348. b := AValue;
  1349. SetData(@b);
  1350. end;
  1351. procedure TBooleanField.SetAsString(const AValue: string);
  1352. Var Temp : string;
  1353. begin
  1354. Temp:=UpperCase(AValue);
  1355. if Temp='' then
  1356. Clear
  1357. else if pos(Temp, FDisplays[True,True])=1 then
  1358. SetAsBoolean(True)
  1359. else if pos(Temp, FDisplays[True,False])=1 then
  1360. SetAsBoolean(False)
  1361. else
  1362. DatabaseErrorFmt(SNotABoolean,[AValue]);
  1363. end;
  1364. procedure TBooleanField.SetVarValue(const AValue: Variant);
  1365. begin
  1366. SetAsBoolean(AValue);
  1367. end;
  1368. constructor TBooleanField.Create(AOwner: TComponent);
  1369. begin
  1370. Inherited Create(AOwner);
  1371. SetDataType(ftBoolean);
  1372. DisplayValues:='True;False';
  1373. end;
  1374. Procedure TBooleanField.SetDisplayValues(AValue : String);
  1375. Var I : longint;
  1376. begin
  1377. If FDisplayValues<>AValue then
  1378. begin
  1379. I:=Pos(';',AValue);
  1380. If (I<2) or (I=Length(AValue)) then
  1381. DatabaseErrorFmt(SInvalidDisplayValues,[AValue]);
  1382. FdisplayValues:=AValue;
  1383. // Store display values and their uppercase equivalents;
  1384. FDisplays[False,True]:=Copy(AValue,1,I-1);
  1385. FDisplays[True,True]:=UpperCase(FDisplays[False,True]);
  1386. FDisplays[False,False]:=Copy(AValue,I+1,Length(AValue)-i);
  1387. FDisplays[True,False]:=UpperCase(FDisplays[False,False]);
  1388. PropertyChanged(True);
  1389. end;
  1390. end;
  1391. { TDateTimeField }
  1392. procedure TDateTimeField.SetDisplayFormat(const AValue: string);
  1393. begin
  1394. if FDisplayFormat<>AValue then begin
  1395. FDisplayFormat:=AValue;
  1396. PropertyChanged(True);
  1397. end;
  1398. end;
  1399. function TDateTimeField.GetAsDateTime: TDateTime;
  1400. begin
  1401. If Not GetData(@Result,False) then
  1402. Result:=0;
  1403. end;
  1404. procedure TDateTimeField.SetVarValue(const AValue: Variant);
  1405. begin
  1406. SetAsDateTime(AValue);
  1407. end;
  1408. function TDateTimeField.GetAsVariant: Variant;
  1409. Var d : tDateTime;
  1410. begin
  1411. If Getdata(@d,False) then
  1412. Result := d
  1413. else
  1414. Result:=Null;
  1415. end;
  1416. function TDateTimeField.GetAsFloat: Double;
  1417. begin
  1418. Result:=GetAsdateTime;
  1419. end;
  1420. function TDateTimeField.GetAsString: string;
  1421. begin
  1422. GetText(Result,False);
  1423. end;
  1424. function TDateTimeField.GetDataSize: Word;
  1425. begin
  1426. Result:=SizeOf(TDateTime);
  1427. end;
  1428. procedure TDateTimeField.GetText(var TheText: string; ADisplayText: Boolean);
  1429. Var R : TDateTime;
  1430. F : String;
  1431. begin
  1432. If Not Getdata(@R,False) then
  1433. TheText:=''
  1434. else
  1435. begin
  1436. If (ADisplayText) and (Length(FDisplayFormat)<>0) then
  1437. F:=FDisplayFormat
  1438. else
  1439. Case DataType of
  1440. ftTime : F:=ShortTimeFormat;
  1441. ftDate : F:=ShortDateFormat;
  1442. else
  1443. F:='c'
  1444. end;
  1445. TheText:=FormatDateTime(F,R);
  1446. end;
  1447. end;
  1448. procedure TDateTimeField.SetAsDateTime(AValue: TDateTime);
  1449. begin
  1450. SetData(@Avalue,False);
  1451. end;
  1452. procedure TDateTimeField.SetAsFloat(AValue: Double);
  1453. begin
  1454. SetAsDateTime(AValue);
  1455. end;
  1456. procedure TDateTimeField.SetAsString(const AValue: string);
  1457. Var R : TDateTime;
  1458. begin
  1459. R:=StrToDateTime(AVAlue);
  1460. SetData(@R,False);
  1461. end;
  1462. constructor TDateTimeField.Create(AOwner: TComponent);
  1463. begin
  1464. Inherited Create(AOwner);
  1465. SetDataType(ftDateTime);
  1466. end;
  1467. { TDateField }
  1468. constructor TDateField.Create(AOwner: TComponent);
  1469. begin
  1470. Inherited Create(AOwner);
  1471. SetDataType(ftDate);
  1472. end;
  1473. { TTimeField }
  1474. constructor TTimeField.Create(AOwner: TComponent);
  1475. begin
  1476. Inherited Create(AOwner);
  1477. SetDataType(ftTime);
  1478. end;
  1479. procedure TTimeField.SetAsString(const AValue: string);
  1480. Var R : TDateTime;
  1481. begin
  1482. R:=StrToTime(AVAlue);
  1483. SetData(@R);
  1484. end;
  1485. { TBinaryField }
  1486. class procedure TBinaryField.CheckTypeSize(AValue: Longint);
  1487. begin
  1488. // Just check for really invalid stuff; actual size is
  1489. // dependent on the record...
  1490. If AValue<1 then
  1491. DatabaseErrorfmt(SInvalidFieldSize,[Avalue]);
  1492. end;
  1493. function TBinaryField.GetAsString: string;
  1494. begin
  1495. Setlength(Result,DataSize);
  1496. GetData(Pointer(Result));
  1497. end;
  1498. procedure TBinaryField.GetText(var TheText: string; ADisplayText: Boolean);
  1499. begin
  1500. TheText:=GetAsString;
  1501. end;
  1502. procedure TBinaryField.SetAsString(const AValue: string);
  1503. Var Buf : PChar;
  1504. Allocated : Boolean;
  1505. begin
  1506. Allocated:=False;
  1507. If Length(AVAlue)=DataSize then
  1508. Buf:=PChar(Avalue)
  1509. else
  1510. begin
  1511. GetMem(Buf,DataSize);
  1512. Move(Pchar(Avalue)[0],Buf^,DataSize);
  1513. Allocated:=True;
  1514. end;
  1515. SetData(Buf);
  1516. If Allocated then
  1517. FreeMem(Buf,DataSize);
  1518. end;
  1519. procedure TBinaryField.SetText(const AValue: string);
  1520. begin
  1521. SetAsString(Avalue);
  1522. end;
  1523. procedure TBinaryField.SetVarValue(const AValue: Variant);
  1524. begin
  1525. SetAsString(Avalue);
  1526. end;
  1527. constructor TBinaryField.Create(AOwner: TComponent);
  1528. begin
  1529. Inherited Create(AOwner);
  1530. end;
  1531. { TBytesField }
  1532. function TBytesField.GetDataSize: Word;
  1533. begin
  1534. Result:=Size;
  1535. end;
  1536. constructor TBytesField.Create(AOwner: TComponent);
  1537. begin
  1538. Inherited Create(AOwner);
  1539. SetDataType(ftBytes);
  1540. Size:=16;
  1541. end;
  1542. { TVarBytesField }
  1543. function TVarBytesField.GetDataSize: Word;
  1544. begin
  1545. Result:=Size+2;
  1546. end;
  1547. constructor TVarBytesField.Create(AOwner: TComponent);
  1548. begin
  1549. INherited Create(AOwner);
  1550. SetDataType(ftvarbytes);
  1551. Size:=16;
  1552. end;
  1553. { TBCDField }
  1554. class procedure TBCDField.CheckTypeSize(AValue: Longint);
  1555. begin
  1556. If not (AValue in [1..4]) then
  1557. DatabaseErrorfmt(SInvalidFieldSize,[Avalue]);
  1558. end;
  1559. function TBCDField.GetAsCurrency: Currency;
  1560. begin
  1561. if not GetData(@Result) then
  1562. result := 0;
  1563. end;
  1564. function TBCDField.GetAsVariant: Variant;
  1565. Var c : system.Currency;
  1566. begin
  1567. If GetData(@c) then
  1568. Result := c
  1569. else
  1570. Result:=Null;
  1571. end;
  1572. function TBCDField.GetAsFloat: Double;
  1573. begin
  1574. result := GetAsCurrency;
  1575. end;
  1576. function TBCDField.GetAsLongint: Longint;
  1577. begin
  1578. result := round(GetAsCurrency);
  1579. end;
  1580. function TBCDField.GetAsString: string;
  1581. var c : system.currency;
  1582. begin
  1583. If GetData(@C) then
  1584. Result:=CurrToStr(C)
  1585. else
  1586. Result:='';
  1587. end;
  1588. function TBCDField.GetValue(var AValue: Currency): Boolean;
  1589. begin
  1590. Result := GetData(@AValue);
  1591. end;
  1592. function TBCDField.GetDataSize: Word;
  1593. begin
  1594. result := sizeof(system.currency);
  1595. end;
  1596. function TBCDField.GetDefaultWidth: Longint;
  1597. begin
  1598. if precision > 0 then result := precision
  1599. else result := 10;
  1600. end;
  1601. procedure TBCDField.GetText(var TheText: string; ADisplayText: Boolean);
  1602. var
  1603. c : system.currency;
  1604. fmt: String;
  1605. begin
  1606. if GetData(@C) then begin
  1607. if aDisplayText or (FEditFormat='') then
  1608. fmt := FDisplayFormat
  1609. else
  1610. fmt := FEditFormat;
  1611. if fmt<>'' then
  1612. TheText := FormatFloat(fmt,C)
  1613. else if fCurrency then begin
  1614. if aDisplayText then
  1615. TheText := FloatToStrF(C, ffCurrency, FPrecision, 2{digits?})
  1616. else
  1617. TheText := FloatToStrF(C, ffFixed, FPrecision, 2{digits?});
  1618. end else
  1619. TheText := FloatToStrF(C, ffGeneral, FPrecision, 0{digits?});
  1620. end else
  1621. TheText := '';
  1622. end;
  1623. procedure TBCDField.SetAsCurrency(AValue: Currency);
  1624. begin
  1625. If CheckRange(AValue) then
  1626. setdata(@AValue)
  1627. else
  1628. RangeError(AValue,FMinValue,FMaxvalue);
  1629. end;
  1630. procedure TBCDField.SetVarValue(const AValue: Variant);
  1631. begin
  1632. SetAsCurrency(AValue);
  1633. end;
  1634. Function TBCDField.CheckRange(AValue : Currency) : Boolean;
  1635. begin
  1636. If (FMinValue<>0) or (FmaxValue<>0) then
  1637. Result:=(AValue>=FMinValue) and (AVAlue<=FMaxValue)
  1638. else
  1639. Result:=True;
  1640. end;
  1641. procedure TBCDField.SetAsFloat(AValue: Double);
  1642. begin
  1643. SetAsCurrency(AValue);
  1644. end;
  1645. procedure TBCDField.SetAsLongint(AValue: Longint);
  1646. begin
  1647. SetAsCurrency(AValue);
  1648. end;
  1649. procedure TBCDField.SetAsString(const AValue: string);
  1650. begin
  1651. SetAsCurrency(strtocurr(AValue));
  1652. end;
  1653. constructor TBCDField.Create(AOwner: TComponent);
  1654. begin
  1655. Inherited Create(AOwner);
  1656. FMaxvalue := 0;
  1657. FMinvalue := 0;
  1658. SetDataType(ftBCD);
  1659. FPrecision := 15;
  1660. Size:=4;
  1661. end;
  1662. { TBlobField }
  1663. procedure TBlobField.AssignTo(Dest: TPersistent);
  1664. begin
  1665. //!! To be implemented
  1666. end;
  1667. Function TBlobField.GetBlobStream(Mode : TBlobStreamMode) : TStream;
  1668. begin
  1669. Result:=FDataset.CreateBlobStream(Self,Mode);
  1670. end;
  1671. procedure TBlobField.FreeBuffers;
  1672. begin
  1673. end;
  1674. function TBlobField.GetAsString: string;
  1675. var
  1676. Stream: TStream;
  1677. begin
  1678. Stream := GetBlobStream(bmRead);
  1679. if Stream <> nil then
  1680. With Stream do
  1681. try
  1682. SetLength(Result,Size);
  1683. ReadBuffer(Pointer(Result)^,Size);
  1684. finally
  1685. Free
  1686. end
  1687. else
  1688. Result := '(blob)';
  1689. end;
  1690. function TBlobField.GetBlobSize: Longint;
  1691. var
  1692. Stream: TStream;
  1693. begin
  1694. Stream := GetBlobStream(bmread);
  1695. if Stream <> nil then
  1696. With Stream do
  1697. try
  1698. Result:=Size;
  1699. finally
  1700. Free;
  1701. end
  1702. else
  1703. result := 0;
  1704. end;
  1705. function TBlobField.GetIsNull: Boolean;
  1706. begin
  1707. If Not Modified then
  1708. result:= inherited GetIsnull
  1709. else
  1710. With GetBlobStream(bmread) do
  1711. try
  1712. Result:=(Size=0);
  1713. Finally
  1714. Free;
  1715. end;
  1716. end;
  1717. procedure TBlobField.GetText(var TheText: string; ADisplayText: Boolean);
  1718. begin
  1719. TheText:=GetAsString;
  1720. end;
  1721. procedure TBlobField.SetAsString(const AValue: string);
  1722. begin
  1723. With GetBlobStream(bmwrite) do
  1724. try
  1725. WriteBuffer(Pointer(Avalue)^,Length(Avalue));
  1726. finally
  1727. Free;
  1728. end;
  1729. end;
  1730. procedure TBlobField.SetText(const AValue: string);
  1731. begin
  1732. SetAsString(AValue);
  1733. end;
  1734. procedure TBlobField.SetVarValue(const AValue: Variant);
  1735. begin
  1736. SetAsString(AValue);
  1737. end;
  1738. constructor TBlobField.Create(AOwner: TComponent);
  1739. begin
  1740. Inherited Create(AOWner);
  1741. SetDataType(ftBlob);
  1742. end;
  1743. procedure TBlobField.Assign(Source: TPersistent);
  1744. begin
  1745. //!! To be implemented
  1746. end;
  1747. procedure TBlobField.Clear;
  1748. begin
  1749. GetBlobStream(bmWrite).free;
  1750. end;
  1751. class function TBlobField.IsBlob: Boolean;
  1752. begin
  1753. Result:=True;
  1754. end;
  1755. procedure TBlobField.LoadFromFile(const FileName: string);
  1756. Var S : TFileStream;
  1757. begin
  1758. S:=TFileStream.Create(FileName,fmOpenRead);
  1759. try
  1760. LoadFromStream(S);
  1761. finally
  1762. S.Free;
  1763. end;
  1764. end;
  1765. procedure TBlobField.LoadFromStream(Stream: TStream);
  1766. begin
  1767. With GetBlobStream(bmWrite) do
  1768. Try
  1769. CopyFrom(Stream,0);
  1770. finally
  1771. Free;
  1772. end;
  1773. end;
  1774. procedure TBlobField.SaveToFile(const FileName: string);
  1775. Var S : TFileStream;
  1776. begin
  1777. S:=TFileStream.Create(FileName,fmCreate);
  1778. try
  1779. SaveToStream(S);
  1780. finally
  1781. S.Free;
  1782. end;
  1783. end;
  1784. procedure TBlobField.SaveToStream(Stream: TStream);
  1785. Var S : TStream;
  1786. begin
  1787. S:=GetBlobStream(bmRead);
  1788. Try
  1789. Stream.CopyFrom(S,0);
  1790. finally
  1791. S.Free;
  1792. end;
  1793. end;
  1794. procedure TBlobField.SetFieldType(AValue: TFieldType);
  1795. begin
  1796. If AValue in [Low(TBlobType)..High(TBlobType)] then
  1797. SetDatatype(Avalue);
  1798. end;
  1799. { TMemoField }
  1800. constructor TMemoField.Create(AOwner: TComponent);
  1801. begin
  1802. Inherited Create(AOwner);
  1803. SetDataType(ftMemo);
  1804. end;
  1805. { TGraphicField }
  1806. constructor TGraphicField.Create(AOwner: TComponent);
  1807. begin
  1808. Inherited Create(AOwner);
  1809. SetDataType(ftGraphic);
  1810. end;
  1811. { TFields }
  1812. Constructor TFields.Create(ADataset : TDataset);
  1813. begin
  1814. FDataSet:=ADataset;
  1815. FFieldList:=TList.Create;
  1816. FValidFieldKinds:=[fkData..fkInternalcalc];
  1817. end;
  1818. Destructor TFields.Destroy;
  1819. begin
  1820. if FFieldList <> nil then Clear;
  1821. FFieldList.Free;
  1822. inherited Destroy;
  1823. end;
  1824. Procedure Tfields.Changed;
  1825. begin
  1826. if (FDataSet <> nil) and not (csDestroying in FDataSet.ComponentState) and FDataset.Active then
  1827. FDataSet.DataEvent(deFieldListChange, 0);
  1828. If Assigned(FOnChange) then
  1829. FOnChange(Self);
  1830. end;
  1831. Procedure TFields.CheckfieldKind(Fieldkind : TFieldKind; Field : TField);
  1832. begin
  1833. If Not (FieldKind in ValidFieldKinds) Then
  1834. DatabaseErrorFmt(SInvalidFieldKind,[Field.FieldName]);
  1835. end;
  1836. Function Tfields.GetCount : Longint;
  1837. begin
  1838. Result:=FFieldList.Count;
  1839. end;
  1840. Function TFields.GetField (Index : longint) : TField;
  1841. begin
  1842. Result:=Tfield(FFieldList[Index]);
  1843. end;
  1844. procedure Tfields.SetField(Index: Integer; Value: TField);
  1845. begin
  1846. Fields[Index].Assign(Value);
  1847. end;
  1848. Procedure TFields.SetFieldIndex (Field : TField;Value : Integer);
  1849. Var Old : Longint;
  1850. begin
  1851. Old := FFieldList.indexOf(Field);
  1852. If Old=-1 then
  1853. Exit;
  1854. // Check value
  1855. If Value<0 Then Value:=0;
  1856. If Value>=Count then Value:=Count-1;
  1857. If Value<>Old then
  1858. begin
  1859. FFieldList.Delete(Old);
  1860. FFieldList.Insert(Value,Field);
  1861. Field.PropertyChanged(True);
  1862. Changed;
  1863. end;
  1864. end;
  1865. Procedure TFields.Add(Field : TField);
  1866. begin
  1867. CheckFieldName(Field.FieldName);
  1868. FFieldList.Add(Field);
  1869. Field.FFields:=Self;
  1870. Changed;
  1871. end;
  1872. Procedure TFields.CheckFieldName (Const Value : String);
  1873. begin
  1874. If FindField(Value)<>Nil then
  1875. DataBaseErrorFmt(SDuplicateFieldName,[Value],FDataset);
  1876. end;
  1877. Procedure TFields.CheckFieldNames (Const Value : String);
  1878. Var I : longint;
  1879. S,T : String;
  1880. begin
  1881. T:=Value;
  1882. Repeat
  1883. I:=Pos(';',T);
  1884. If I=0 Then I:=Length(T)+1;
  1885. S:=Copy(T,1,I-1);
  1886. Delete(T,1,I);
  1887. // Will raise an error if no such field...
  1888. FieldByName(S);
  1889. Until (T='');
  1890. end;
  1891. Procedure TFields.Clear;
  1892. begin
  1893. with FFieldList do
  1894. while Count > 0 do begin
  1895. TField(Last).FDataSet := Nil;
  1896. TField(Last).Free;
  1897. FFieldList.Delete(Count - 1);
  1898. end;
  1899. Changed;
  1900. end;
  1901. Function TFields.FindField (Const Value : String) : TField;
  1902. Var S : String;
  1903. I : longint;
  1904. begin
  1905. Result:=Nil;
  1906. S:=UpperCase(Value);
  1907. For I:=0 To FFieldList.Count-1 do
  1908. If S=UpperCase(TField(FFieldList[i]).FieldName) Then
  1909. Begin
  1910. {$ifdef dsdebug}
  1911. Writeln ('Found field ',Value);
  1912. {$endif}
  1913. Result:=TField(FFieldList[I]);
  1914. Exit;
  1915. end;
  1916. end;
  1917. Function TFields.FieldByName (Const Value : String) : TField;
  1918. begin
  1919. Result:=FindField(Value);
  1920. If result=Nil then
  1921. DatabaseErrorFmt(SFieldNotFound,[Value],FDataset);
  1922. end;
  1923. Function TFields.FieldByNumber(FieldNo : Integer) : TField;
  1924. Var i : Longint;
  1925. begin
  1926. Result:=Nil;
  1927. For I:=0 to FFieldList.Count-1 do
  1928. If FieldNo=TField(FFieldList[I]).FieldNo then
  1929. begin
  1930. Result:=TField(FFieldList[i]);
  1931. Exit;
  1932. end;
  1933. end;
  1934. Procedure TFields.GetFieldNames (Values : TStrings);
  1935. Var i : longint;
  1936. begin
  1937. Values.Clear;
  1938. For I:=0 to FFieldList.Count-1 do
  1939. Values.Add(Tfield(FFieldList[I]).FieldName);
  1940. end;
  1941. Function TFields.IndexOf(Field : TField) : Longint;
  1942. begin
  1943. Result:=FFieldList.IndexOf(Field);
  1944. end;
  1945. procedure TFields.Remove(Value : TField);
  1946. begin
  1947. FFieldList.Remove(Value);
  1948. Value.FFields := nil;
  1949. Changed;
  1950. end;