fields.inc 51 KB

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