fields.inc 39 KB

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