fields.inc 39 KB

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