1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539 |
- {
- This file is part of the Free Pascal run time library.
- Copyright (c) 1999-2000 by Michael Van Canneyt, member of the
- Free Pascal development team
- TFields and related components implementations.
- See the file COPYING.FPC, included in this distribution,
- for details about the copyright.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- **********************************************************************}
- Procedure DumpMem (P : Pointer;Size : Longint);
- Var i : longint;
- begin
- Write ('Memory dump : ');
- For I:=0 to Size-1 do
- Write (Pbyte(P)[i],' ');
- Writeln;
- end;
- { ---------------------------------------------------------------------
- TFieldDef
- ---------------------------------------------------------------------}
- Constructor TFieldDef.Create(AOwner: TFieldDefs; const AName: string;
- ADataType: TFieldType; ASize: Word; ARequired: Boolean; AFieldNo: Longint);
- begin
- Inherited Create(AOwner);
- {$ifdef dsdebug }
- Writeln('TFieldDef.Create : ',Aname,'(',AFieldNo,')');
- {$endif}
- FName:=Aname;
- FDisplayName := '';
- FDatatype:=ADatatype;
- FSize:=ASize;
- FRequired:=ARequired;
- FPrecision:=-1;
- FFieldNo:=AFieldNo;
- end;
- Destructor TFieldDef.Destroy;
- begin
- Inherited destroy;
- end;
- procedure TFieldDef.Assign(APersistent: TPersistent);
- var fd: TFieldDef;
- begin
- fd := nil;
- if APersistent is TFieldDef then
- fd := APersistent as TFieldDef;
- if Assigned(fd) then begin
- Collection.BeginUpdate;
- try
- Name := fd.Name;
- DataType := fd.DataType;
- Size := fd.Size;
- Precision := fd.Precision;
- FRequired := fd.Required;
- finally
- Collection.EndUpdate;
- end;
- end else
- inherited Assign(APersistent);
- end;
- Function TFieldDef.CreateField(AOwner: TComponent): TField;
- Var TheField : TFieldClass;
- begin
- {$ifdef dsdebug}
- Writeln ('Creating field '+FNAME);
- {$endif dsdebug}
- TheField:=GetFieldClass;
- if TheField=Nil then
- DatabaseErrorFmt(SUnknownFieldType,[FName]);
- Result:=Thefield.Create(AOwner);
- Try
- Result.Size:=FSize;
- Result.Required:=FRequired;
- Result.FFieldName:=FName;
- Result.FDisplayLabel:=FDisplayName;
- Result.FFieldNo:=Self.FieldNo;
- Result.SetFieldType(DataType);
- Result.FReadOnly:= (faReadOnly in Attributes);
- {$ifdef dsdebug}
- Writeln ('TFieldDef.CReateField : Trying to set dataset');
- {$endif dsdebug}
- {$ifdef dsdebug}
- Writeln ('TFieldDef.CReateField : Result Fieldno : ',Result.FieldNo,' Self : ',FieldNo);
- {$endif dsdebug}
- Result.Dataset:=TFieldDefs(Collection).Dataset;
- If Result is TFloatField then
- TFloatField(Result).Precision:=FPrecision;
- except
- Result.Free;
- Raise;
- end;
- end;
- procedure TFieldDef.SetAttributes(AValue: TFieldAttributes);
- begin
- FAttributes := AValue;
- Changed(False);
- end;
- procedure TFieldDef.SetDataType(AValue: TFieldType);
- begin
- FDataType := AValue;
- Changed(False);
- end;
- procedure TFieldDef.SetPrecision(const AValue: Longint);
- begin
- FPrecision := AValue;
- Changed(False);
- end;
- procedure TFieldDef.SetSize(const AValue: Word);
- begin
- FSize := AValue;
- Changed(False);
- end;
- function TFieldDef.GetDisplayName: string;
- begin
- Result := FDisplayName;
- if Result = '' then
- Result := Fname;
- end;
- procedure TFieldDef.SetDisplayName(const AValue: string);
- begin
- if (AValue <> '') and (AnsiCompareText(AValue, DisplayName) <> 0) and
- (Collection is TOwnedCollection) and
- (TFieldDefs(Collection).IndexOf(AValue) >= 0) then
- DatabaseErrorFmt(SDuplicateName, [AValue, Collection.ClassName]);
- FName := AValue;
- end;
- Function TFieldDef.GetFieldClass : TFieldClass;
- begin
- //!! Should be owner as tdataset but that doesn't work ??
- If Assigned(Collection) And
- (Collection is TFieldDefs) And
- Assigned(TFieldDefs(Collection).Dataset) then
- Result:=TFieldDefs(Collection).Dataset.GetFieldClass(FDataType)
- else
- Result:=Nil;
- end;
- { ---------------------------------------------------------------------
- TFieldDefs
- ---------------------------------------------------------------------}
- {
- destructor TFieldDefs.Destroy;
- begin
- FItems.Free;
- // This will destroy all fielddefs since we own them...
- Inherited Destroy;
- end;
- }
- procedure TFieldDefs.Add(const AName: string; ADataType: TFieldType);
- begin
- Add(AName,ADatatype,0,False);
- end;
- procedure TFieldDefs.Add(const AName: string; ADataType: TFieldType; ASize : Word);
- begin
- Add(AName,ADatatype,ASize,False);
- end;
- procedure TFieldDefs.Add(const AName: string; ADataType: TFieldType; ASize: Word;
- ARequired: Boolean);
- begin
- If Length(AName)=0 Then
- DatabaseError(SNeedFieldName);
- // the fielddef will register itself here as a owned component.
- // fieldno is 1 based !
- BeginUpdate;
- try
- TFieldDef.Create(Self,AName,ADataType,ASize,Arequired,Count+1);
- finally
- EndUpdate;
- end;
- end;
- function TFieldDefs.GetItem(Index: Longint): TFieldDef;
- begin
- Result := TFieldDef(inherited Items[Index]);;
- end;
- function TFieldDefs.GetDataset: TDataset;
- begin
- Result := TDataset(GetOwner);
- end;
- procedure TFieldDefs.SetItem(Index: Longint; const AValue: TFieldDef);
- begin
- inherited Items[Index] := AValue;
- end;
- procedure TFieldDefs.SetItemName(AItem: TCollectionItem);
- begin
- if AItem is TFieldDef then
- with AItem as TFieldDef do
- if Name = '' then
- Name := Dataset.Name + Copy(ClassName, 2, 5) + IntToStr(ID+1)
- else inherited SetItemName(AItem);
- end;
- constructor TFieldDefs.Create(ADataset: TDataset);
- begin
- Inherited Create(TPersistent(ADataset), TFieldDef);
- end;
- procedure TFieldDefs.Assign(FieldDefs: TFieldDefs);
- Var I : longint;
- begin
- Clear;
- For i:=0 to FieldDefs.Count-1 do
- With FieldDefs[i] do
- Add(Name,DataType,Size,Required);
- end;
- {
- procedure TFieldDefs.Clear;
- Var I : longint;
- begin
- For I:=FItems.Count-1 downto 0 do
- TFieldDef(Fitems[i]).Free;
- FItems.Clear;
- end;
- }
- function TFieldDefs.Find(const AName: string): TFieldDef;
- Var I : longint;
- begin
- I:=IndexOf(AName);
- If I=-1 Then
- DataBaseErrorFmt(SUnknownField,[AName,DataSet.Name]);
- Result:=Items[i];
- end;
- function TFieldDefs.IndexOf(const AName: string): Longint;
- Var I : longint;
- begin
- For I:=0 to Count-1 do
- If AnsiCompareText(Items[I].Name,AName)=0 then
- begin
- Result:=I;
- Exit;
- end;
- Result:=-1;
- end;
- procedure TFieldDefs.Update;
- begin
- DataSet.InitFieldDefs;
- end;
- Function TFieldDefs.AddFieldDef : TFieldDef;
- begin
- Result:=TFieldDef.Create(Self,'',ftUnknown,0,False,Count+1);
- end;
- { ---------------------------------------------------------------------
- TField
- ---------------------------------------------------------------------}
- Const
- SBoolean = 'Boolean';
- SDateTime = 'TDateTime';
- SFloat = 'Float';
- SInteger = 'Integer';
- SVariant = 'Variant';
- SString = 'String';
- constructor TField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOwner);
- FVisible:=True;
- FValidChars:=[#0..#255];
- FProviderFlags := [pfInUpdate,pfInWhere];
- end;
- destructor TField.Destroy;
- begin
- IF Assigned(FDataSet) then
- begin
- FDataSet.Active:=False;
- if Assigned(FFields) then
- FFields.Remove(Self);
- end;
- FLookupList.Free;
- Inherited Destroy;
- end;
- function TField.AccessError(const TypeName: string): EDatabaseError;
- begin
- Result:=EDatabaseError.CreateFmt(SinvalidTypeConversion,[TypeName,FFieldName]);
- end;
- procedure TField.Assign(Source: TPersistent);
- begin
- if Source = nil then Clear
- else if Source is TField then begin
- Value := TField(Source).Value;
- end else
- inherited Assign(Source);
- end;
- procedure TField.AssignValue(const AValue: TVarRec);
- procedure Error;
- begin
- DatabaseErrorFmt(SFieldValueError, [DisplayName]);
- end;
- begin
- with AValue do
- case VType of
- vtInteger:
- AsInteger := VInteger;
- vtBoolean:
- AsBoolean := VBoolean;
- vtChar:
- AsString := VChar;
- vtExtended:
- AsFloat := VExtended^;
- vtString:
- AsString := VString^;
- vtPointer:
- if VPointer <> nil then Error;
- vtPChar:
- AsString := VPChar;
- vtObject:
- if (VObject = nil) or (VObject is TPersistent) then
- Assign(TPersistent(VObject))
- else
- Error;
- vtAnsiString:
- AsString := string(VAnsiString);
- // vtCurrency:
- // AsCurrency := VCurrency^;
- vtVariant:
- if not VarIsClear(VVariant^) then Self.Value := VVariant^;
- vtWideString:
- AsString := WideString(VWideString);
- vtInt64:
- Self.Value := VInt64^;
- else
- Error;
- end;
- end;
- procedure TField.Change;
- begin
- If Assigned(FOnChange) Then
- FOnChange(Self);
- end;
- procedure TField.CheckInactive;
- begin
- If Assigned(FDataSet) then
- FDataset.CheckInactive;
- end;
- procedure TField.Clear;
- begin
- if FieldKind in [fkData, fkInternalCalc] then
- SetData(Nil);
- end;
- procedure TField.DataChanged;
- begin
- FDataset.DataEvent(deFieldChange,ptrint(Self));
- end;
- procedure TField.FocusControl;
- begin
- FDataSet.DataEvent(deFocusControl,ptrint(Self));
- end;
- procedure TField.FreeBuffers;
- begin
- // Empty. Provided for backward compatibiliy;
- // TDataset manages the buffers.
- end;
- function TField.GetAsBoolean: Boolean;
- begin
- AccessError(SBoolean);
- end;
- function TField.GetAsDateTime: TDateTime;
- begin
- AccessError(SdateTime);
- end;
- function TField.GetAsFloat: Double;
- begin
- AccessError(SDateTime);
- end;
- function TField.GetAsLongint: Longint;
- begin
- AccessError(SInteger);
- end;
- function TField.GetAsVariant: Variant;
- begin
- AccessError(SVariant);
- end;
- function TField.GetAsInteger: Integer;
- begin
- Result:=GetAsLongint;
- end;
- function TField.GetAsString: string;
- begin
- AccessError(SString);
- end;
- function TField.GetOldValue: Variant;
- var SaveState : TDatasetState;
- begin
- SaveState := FDataset.State;
- try
- FDataset.SetTempState(dsOldValue);
- Result := GetAsVariant;
- finally
- FDataset.RestoreState(SaveState);
- end;
- end;
- function TField.GetNewValue: Variant;
- var SaveState : TDatasetState;
- begin
- SaveState := FDataset.State;
- try
- FDataset.SetTempState(dsNewValue);
- Result := GetAsVariant;
- finally
- FDataset.RestoreState(SaveState);
- end;
- end;
- procedure TField.SetNewValue(const AValue: Variant);
- var SaveState : TDatasetState;
- begin
- SaveState := FDataset.State;
- try
- FDataset.SetTempState(dsNewValue);
- SetAsVariant(AValue);
- finally
- FDataset.RestoreState(SaveState);
- end;
- end;
- function TField.GetCurValue: Variant;
- var SaveState : TDatasetState;
- begin
- SaveState := FDataset.State;
- try
- FDataset.SetTempState(dsCurValue);
- Result := GetAsVariant;
- finally
- FDataset.RestoreState(SaveState);
- end;
- end;
- function TField.GetCanModify: Boolean;
- begin
- Result:=Not ReadOnly;
- If Result then
- begin
- Result:=Assigned(DataSet);
- If Result then
- Result:= DataSet.CanModify;
- end;
- end;
- function TField.GetData(Buffer: Pointer): Boolean;
- begin
- Result:=GetData(Buffer,True);
- end;
- function TField.GetData(Buffer: Pointer; NativeFormat : Boolean): Boolean;
- begin
- IF FDataset=Nil then
- DatabaseErrorFmt(SNoDataset,[FieldName]);
- If FVAlidating then
- begin
- result:=Not(FValueBuffer=Nil);
- If Result then
- Move (FValueBuffer^,Buffer^ ,DataSize);
- end
- else
- Result:=FDataset.GetFieldData(Self,Buffer,NativeFormat);
- end;
- function TField.GetDataSize: Word;
- begin
- Result:=0;
- end;
- function TField.GetDefaultWidth: Longint;
- begin
- Result:=10;
- end;
- function TField.GetDisplayName : String;
- begin
- If FDisplayLabel<>'' then
- result:=FDisplayLabel
- else
- Result:=FFieldName;
- end;
- Function TField.IsDisplayStored : Boolean;
- begin
- Result:=(DisplayLabel<>FieldName);
- end;
- function TField.GetLookupList: TLookupList;
- begin
- if not Assigned(FLookupList) then
- FLookupList := TLookupList.Create;
- Result := FLookupList;
- end;
- procedure TField.CalcLookupValue;
- begin
- if FLookupCache then
- Value := LookupList.ValueOfKey(FDataSet.FieldValues[FKeyFields])
- else if (FLookupDataSet <> nil) and FLookupDataSet.Active then
- Value := FLookupDataSet.Lookup(FLookupKeyFields,
- FDataSet.FieldValues[FKeyFields], FLookupResultField);
- end;
- function TField.getIndex : longint;
- begin
- If Assigned(FDataset) then
- Result:=FDataset.FFieldList.IndexOf(Self)
- else
- Result:=-1;
- end;
- function TField.GetAsCurrency: Currency;
- begin
- Result := GetAsFloat;
- end;
- procedure TField.SetAlignment(const AValue: TAlignMent);
- begin
- if FAlignment <> AValue then
- begin
- FAlignment := Avalue;
- PropertyChanged(false);
- end;
- end;
- procedure TField.SetIndex(AValue: Integer);
- begin
- if FFields <> nil then FFields.SetFieldIndex(Self, AValue)
- end;
- procedure TField.SetAsCurrency(AValue: Currency);
- begin
- SetAsFloat(AValue);
- end;
- function TField.GetIsNull: Boolean;
- begin
- Result:=Not(GetData (Nil));
- end;
- function TField.GetParentComponent: TComponent;
- begin
- Result := DataSet;
- end;
- procedure TField.GetText(var AText: string; ADisplayText: Boolean);
- begin
- AText:=GetAsString;
- end;
- function TField.HasParent: Boolean;
- begin
- HasParent:=True;
- end;
- function TField.IsValidChar(InputChar: Char): Boolean;
- begin
- // FValidChars must be set in Create.
- Result:=InputChar in FValidChars;
- end;
- procedure TField.RefreshLookupList;
- var SaveActive: Boolean;
- begin
- if (FLookupDataSet <> nil) And (FLookupKeyFields <> '') And
- (FlookupResultField <> '') And (FKeyFields <> '') then begin
- SaveActive := FLookupDataSet.Active;
- with FLookupDataSet do
- try
- Active := True;
- FFields.CheckFieldNames(FLookupKeyFields);
- FieldByName(FLookupResultField);
- LookupList.Clear;
- DisableControls;
- try
- First;
- while not Eof do begin
- FLookupList.Add(FieldValues[FLookupKeyFields],
- FieldValues[FLookupResultField]);
- Next;
- end;
- finally
- EnableControls;
- end;
- finally
- Active := SaveActive;
- end;
- end;
- end;
- procedure TField.Notification(AComponent: TComponent; Operation: TOperation);
- begin
- Inherited Notification(AComponent,Operation);
- if (Operation = opRemove) and (AComponent = FLookupDataSet) then
- FLookupDataSet := nil;
- end;
- procedure TField.PropertyChanged(LayoutAffected: Boolean);
- begin
- If (FDataset<>Nil) and (FDataset.Active) then
- If LayoutAffected then
- FDataset.DataEvent(deLayoutChange,0)
- else
- FDataset.DataEvent(deDatasetchange,0);
- end;
- procedure TField.ReadState(Reader: TReader);
- begin
- inherited ReadState(Reader);
- if Reader.Parent is TDataSet then
- DataSet := TDataSet(Reader.Parent);
- end;
- procedure TField.SetAsBoolean(AValue: Boolean);
- begin
- AccessError(SBoolean);
- end;
- procedure TField.SetAsDateTime(AValue: TDateTime);
- begin
- AccessError(SDateTime);
- end;
- procedure TField.SetAsFloat(AValue: Double);
- begin
- AccessError(SFloat);
- end;
- procedure TField.SetAsVariant(AValue: Variant);
- begin
- if VarIsNull(AValue) then
- Clear
- else
- try
- SetVarValue(AValue);
- except
- on EVariantError do DatabaseErrorFmt(SFieldValueError, [DisplayName]);
- end;
- end;
- procedure TField.SetAsLongint(AValue: Longint);
- begin
- AccessError(SInteger);
- end;
- procedure TField.SetAsInteger(AValue: Integer);
- begin
- SetAsLongint(AValue);
- end;
- procedure TField.SetAsString(const AValue: string);
- begin
- AccessError(SString);
- end;
- procedure TField.SetData(Buffer: Pointer);
- begin
- SetData(Buffer,True);
- end;
- procedure TField.SetData(Buffer: Pointer; NativeFormat : Boolean);
- begin
- If Not Assigned(FDataset) then
- EDatabaseError.CreateFmt(SNoDataset,[FieldName]);
- FDataSet.SetFieldData(Self,Buffer, NativeFormat);
- end;
- Procedure TField.SetDataset (AValue : TDataset);
- begin
- {$ifdef dsdebug}
- Writeln ('Setting dataset');
- {$endif}
- If AValue=FDataset then exit;
- If Assigned(FDataset) Then
- begin
- FDataset.CheckInactive;
- FDataset.FFieldList.Remove(Self);
- end;
- If Assigned(AValue) then
- begin
- AValue.CheckInactive;
- AValue.FFieldList.Add(Self);
- end;
- FDataset:=AValue;
- end;
- procedure TField.SetDataType(AValue: TFieldType);
- begin
- FDataType := AValue;
- end;
- procedure TField.SetFieldType(AValue: TFieldType);
- begin
- //!! To be implemented
- end;
- procedure TField.SetParentComponent(AParent: TComponent);
- begin
- if not (csLoading in ComponentState) then
- DataSet := AParent as TDataSet;
- end;
- procedure TField.SetSize(AValue: Word);
- begin
- CheckInactive;
- CheckTypeSize(AValue);
- FSize:=AValue;
- end;
- procedure TField.SetText(const AValue: string);
- begin
- AsString:=AValue;
- end;
- procedure TField.SetVarValue(const AValue: Variant);
- begin
- AccessError(SVariant);
- end;
- procedure TField.Validate(Buffer: Pointer);
- begin
- If assigned(OnValidate) Then
- begin
- FValueBuffer:=Buffer;
- FValidating:=True;
- Try
- OnValidate(Self);
- finally
- FValidating:=False;
- end;
- end;
- end;
- class function Tfield.IsBlob: Boolean;
- begin
- Result:=False;
- end;
- class procedure TField.CheckTypeSize(AValue: Longint);
- begin
- If (AValue<>0) and Not IsBlob Then
- DatabaseErrorFmt(SInvalidFieldSize,[AValue]);
- end;
- // TField private methods
- function TField.GetDisplayText: String;
- begin
- SetLength(Result, 0);
- if Assigned(OnGetText) then
- OnGetText(Self, Result, True)
- else
- GetText(Result, True);
- end;
- procedure TField.SetDisplayLabel(const AValue: string);
- begin
- if FDisplayLabel<>Avalue then
- begin
- FDisplayLabel:=Avalue;
- PropertyChanged(true);
- end;
- end;
- procedure TField.SetDisplayWidth(const AValue: Longint);
- begin
- if FDisplayWidth<>AValue then
- begin
- FDisplayWidth:=AValue;
- PropertyChanged(True);
- end;
- end;
- function TField.GetDisplayWidth: integer;
- begin
- if FDisplayWidth=0 then
- result:=GetDefaultWidth
- else
- result:=FDisplayWidth;
- end;
- procedure TField.SetReadOnly(const AValue: Boolean);
- begin
- if (FReadOnly<>Avalue) then
- begin
- FReadOnly:=AValue;
- PropertyChanged(True);
- end;
- end;
- procedure TField.SetVisible(const AValue: Boolean);
- begin
- if FVisible<>Avalue then
- begin
- FVisible:=AValue;
- PropertyChanged(True);
- end;
- end;
- { ---------------------------------------------------------------------
- TStringField
- ---------------------------------------------------------------------}
- constructor TStringField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOwner);
- SetDataType(ftString);
- Size:=20;
- end;
- class procedure TStringField.CheckTypeSize(AValue: Longint);
- begin
- If (AValue<1) or (AValue>dsMaxStringSize) Then
- databaseErrorFmt(SInvalidFieldSize,[AValue])
- end;
- function TStringField.GetAsBoolean: Boolean;
- Var S : String;
- begin
- S:=GetAsString;
- result := (Length(S)>0) and (Upcase(S[1]) in ['T',YesNoChars[True]]);
- end;
- function TStringField.GetAsDateTime: TDateTime;
- begin
- Result:=StrToDateTime(GetAsString);
- end;
- function TStringField.GetAsFloat: Double;
- begin
- Result:=StrToFloat(GetAsString);
- end;
- function TStringField.GetAsLongint: Longint;
- begin
- Result:=StrToInt(GetAsString);
- end;
- function TStringField.GetAsString: string;
- begin
- If Not GetValue(Result) then
- Result:='';
- end;
- function TStringField.GetAsVariant: Variant;
- Var s : string;
- begin
- If GetValue(s) then
- Result:=s
- else
- Result:=Null;
- end;
- function TStringField.GetDataSize: Word;
- begin
- Result:=Size+1;
- end;
- function TStringField.GetDefaultWidth: Longint;
- begin
- result:=Size;
- end;
- Procedure TStringField.GetText(var AText: string; ADisplayText: Boolean);
- begin
- AText:=GetAsString;
- end;
- function TStringField.GetValue(var AValue: string): Boolean;
- Var Buf : TStringFieldBuffer;
- begin
- Result:=GetData(@Buf);
- If Result then
- AValue:=Buf;
- end;
- procedure TStringField.SetAsBoolean(AValue: Boolean);
- begin
- If AValue Then
- SetAsString('T')
- else
- SetAsString('F');
- end;
- procedure TStringField.SetAsDateTime(AValue: TDateTime);
- begin
- SetAsString(DateTimeToStr(AValue));
- end;
- procedure TStringField.SetAsFloat(AValue: Double);
- begin
- SetAsString(FloatToStr(AValue));
- end;
- procedure TStringField.SetAsLongint(AValue: Longint);
- begin
- SetAsString(intToStr(AValue));
- end;
- procedure TStringField.SetAsString(const AValue: string);
- Const NullByte : char = #0;
- begin
- IF Length(AValue)=0 then
- SetData(@NullByte)
- else
- SetData(@AValue[1]);
- end;
- procedure TStringField.SetVarValue(const AValue: Variant);
- begin
- SetAsString(AValue);
- end;
- { ---------------------------------------------------------------------
- TNumericField
- ---------------------------------------------------------------------}
- constructor TNumericField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOwner);
- AlignMent:=taRightJustify;
- end;
- procedure TNumericField.RangeError(AValue, Min, Max: Double);
- begin
- DatabaseErrorFMT(SRangeError,[AValue,Min,Max,FieldName]);
- end;
- procedure TNumericField.SetDisplayFormat(const AValue: string);
- begin
- If FDisplayFormat<>AValue then
- begin
- FDisplayFormat:=AValue;
- PropertyChanged(True);
- end;
- end;
- procedure TNumericField.SetEditFormat(const AValue: string);
- begin
- If FEDitFormat<>AValue then
- begin
- FEDitFormat:=AVAlue;
- PropertyChanged(True);
- end;
- end;
- { ---------------------------------------------------------------------
- TLongintField
- ---------------------------------------------------------------------}
- constructor TLongintField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOwner);
- SetDatatype(ftinteger);
- FMinRange:=Low(LongInt);
- FMaxRange:=High(LongInt);
- FValidchars:=['+','-','0'..'9'];
- end;
- function TLongintField.GetAsFloat: Double;
- begin
- Result:=GetAsLongint;
- end;
- function TLongintField.GetAsLongint: Longint;
- begin
- If Not GetValue(Result) then
- Result:=0;
- end;
- function TLongintField.GetAsVariant: Variant;
- Var L : Longint;
- begin
- If GetValue(L) then
- Result:=L
- else
- Result:=Null;
- end;
- function TLongintField.GetAsString: string;
- Var L : Longint;
- begin
- If GetValue(L) then
- Result:=IntTostr(L)
- else
- Result:='';
- end;
- function TLongintField.GetDataSize: Word;
- begin
- Result:=SizeOf(Longint);
- end;
- procedure TLongintField.GetText(var AText: string; ADisplayText: Boolean);
- var l : longint;
- fmt : string;
- begin
- Atext:='';
- If Not GetValue(l) then exit;
- If ADisplayText or (FEditFormat='') then
- fmt:=FDisplayFormat
- else
- fmt:=FEditFormat;
- If length(fmt)<>0 then
- AText:=FormatFloat(fmt,L)
- else
- Str(L,AText);
- end;
- function TLongintField.GetValue(var AValue: Longint): Boolean;
- Var L : Longint;
- P : PLongint;
- begin
- P:=@L;
- Result:=GetData(P);
- If Result then
- Case Datatype of
- ftInteger,ftautoinc : AValue:=Plongint(P)^;
- ftword : Avalue:=Pword(P)^;
- ftsmallint : AValue:=PSmallint(P)^;
- end;
- end;
- procedure TLongintField.SetAsFloat(AValue: Double);
- begin
- SetAsLongint(Round(Avalue));
- end;
- procedure TLongintField.SetAsLongint(AValue: Longint);
- begin
- If CheckRange(AValue) then
- SetData(@AValue)
- else
- RangeError(Avalue,FMinrange,FMaxRange);
- end;
- procedure TLongintField.SetVarValue(const AValue: Variant);
- begin
- SetAsLongint(AValue);
- end;
- procedure TLongintField.SetAsString(const AValue: string);
- Var L,Code : longint;
- begin
- If length(AValue)=0 then
- Clear
- else
- begin
- Val(AVAlue,L,Code);
- If Code=0 then
- SetAsLongint(L)
- else
- DatabaseErrorFMT(SNotAnInteger,[Avalue]);
- end;
- end;
- Function TLongintField.CheckRange(AValue : longint) : Boolean;
- begin
- result := true;
- if (FMaxValue=0) then
- begin
- if (AValue>FMaxRange) Then result := false;
- end
- else
- if AValue>FMaxValue then result := false;
- if (FMinValue=0) then
- begin
- if (AValue<FMinRange) Then result := false;
- end
- else
- if AValue<FMinValue then result := false;
- end;
- Procedure TLongintField.SetMaxValue (AValue : longint);
- begin
- If (AValue>=FMinRange) and (AValue<=FMaxRange) then
- FMaxValue:=AValue
- else
- RangeError(AValue,FMinRange,FMaxRange);
- end;
- Procedure TLongintField.SetMinValue (AValue : longint);
- begin
- If (AValue>=FMinRange) and (AValue<=FMaxRange) then
- FMinValue:=AValue
- else
- RangeError(AValue,FMinRange,FMaxRange);
- end;
- { ---------------------------------------------------------------------
- TLargeintField
- ---------------------------------------------------------------------}
- constructor TLargeintField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOwner);
- SetDatatype(ftLargeint);
- FMinRange:=Low(Largeint);
- FMaxRange:=High(Largeint);
- FValidchars:=['+','-','0'..'9'];
- end;
- function TLargeintField.GetAsFloat: Double;
- begin
- Result:=GetAsLargeint;
- end;
- function TLargeintField.GetAsLargeint: Largeint;
- begin
- If Not GetValue(Result) then
- Result:=0;
- end;
- function TLargeIntField.GetAsVariant: Variant;
- Var L : Largeint;
- begin
- If GetValue(L) then
- Result:=L
- else
- Result:=Null;
- end;
- function TLargeintField.GetAsLongint: Longint;
- begin
- Result:=GetAsLargeint;
- end;
- function TLargeintField.GetAsString: string;
- Var L : Largeint;
- begin
- If GetValue(L) then
- Result:=IntTostr(L)
- else
- Result:='';
- end;
- function TLargeintField.GetDataSize: Word;
- begin
- Result:=SizeOf(Largeint);
- end;
- procedure TLargeintField.GetText(var AText: string; ADisplayText: Boolean);
- var l : largeint;
- fmt : string;
- begin
- Atext:='';
- If Not GetValue(l) then exit;
- If ADisplayText or (FEditFormat='') then
- fmt:=FDisplayFormat
- else
- fmt:=FEditFormat;
- If length(fmt)<>0 then
- AText:=FormatFloat(fmt,L)
- else
- Str(L,AText);
- end;
- function TLargeintField.GetValue(var AValue: Largeint): Boolean;
- type
- PLargeint = ^Largeint;
- Var P : PLargeint;
- begin
- P:=@AValue;
- Result:=GetData(P);
- end;
- procedure TLargeintField.SetAsFloat(AValue: Double);
- begin
- SetAsLargeint(Round(Avalue));
- end;
- procedure TLargeintField.SetAsLargeint(AValue: Largeint);
- begin
- If CheckRange(AValue) then
- SetData(@AValue)
- else
- RangeError(Avalue,FMinrange,FMaxRange);
- end;
- procedure TLargeintField.SetAsLongint(AValue: Longint);
- begin
- SetAsLargeint(Avalue);
- end;
- procedure TLargeintField.SetAsString(const AValue: string);
- Var L : largeint;
- code : longint;
- begin
- If length(AValue)=0 then
- Clear
- else
- begin
- Val(AVAlue,L,Code);
- If Code=0 then
- SetAsLargeint(L)
- else
- DatabaseErrorFMT(SNotAnInteger,[Avalue]);
- end;
- end;
- procedure TLargeintField.SetVarValue(const AValue: Variant);
- begin
- SetAsLargeint(AValue);
- end;
- Function TLargeintField.CheckRange(AValue : largeint) : Boolean;
- begin
- result := true;
- if (FMaxValue=0) then
- begin
- if (AValue>FMaxRange) Then result := false;
- end
- else
- if AValue>FMaxValue then result := false;
- if (FMinValue=0) then
- begin
- if (AValue<FMinRange) Then result := false;
- end
- else
- if AValue<FMinValue then result := false;
- end;
- Procedure TLargeintField.SetMaxValue (AValue : largeint);
- begin
- If (AValue>=FMinRange) and (AValue<=FMaxRange) then
- FMaxValue:=AValue
- else
- RangeError(AValue,FMinRange,FMaxRange);
- end;
- Procedure TLargeintField.SetMinValue (AValue : largeint);
- begin
- If (AValue>=FMinRange) and (AValue<=FMaxRange) then
- FMinValue:=AValue
- else
- RangeError(AValue,FMinRange,FMaxRange);
- end;
- { TSmallintField }
- function TSmallintField.GetDataSize: Word;
- begin
- Result:=SizeOf(SmallInt);
- end;
- constructor TSmallintField.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- SetDataType(ftSmallInt);
- FMinRange:=-32768;
- FMaxRange:=32767;
- end;
- { TWordField }
- function TWordField.GetDataSize: Word;
- begin
- Result:=SizeOf(Word);
- end;
- constructor TWordField.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- SetDataType(ftWord);
- FMinRange:=0;
- FMaxRange:=65535;
- FValidchars:=['+','0'..'9'];
- end;
- { TAutoIncField }
- constructor TAutoIncField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOWner);
- SetDataType(ftAutoInc);
- FReadOnly:=True;
- end;
- Procedure TAutoIncField.SetAsLongint(AValue : Longint);
- begin
- DataBaseError(SCantSetAutoIncfields);
- end;
- { TFloatField }
- function TFloatField.GetAsFloat: Double;
- begin
- If Not GetData(@Result) Then
- Result:=0.0;
- end;
- function TFloatField.GetAsVariant: Variant;
- Var f : Double;
- begin
- If GetData(@f) then
- Result := f
- else
- Result:=Null;
- end;
- function TFloatField.GetAsLongint: Longint;
- begin
- Result:=Round(GetAsFloat);
- end;
- function TFloatField.GetAsString: string;
- Var R : Double;
- begin
- If GetData(@R) then
- Result:=FloatToStr(R)
- else
- Result:='';
- end;
- function TFloatField.GetDataSize: Word;
- begin
- Result:=SizeOf(Double);
- end;
- procedure TFloatField.GetText(var TheText: string; ADisplayText: Boolean);
- Var
- fmt : string;
- E : Double;
- begin
- TheText:='';
- If Not GetData(@E) then exit;
- If ADisplayText or (Length(FEditFormat) = 0) Then
- Fmt:=FDisplayFormat
- else
- Fmt:=FEditFormat;
- If fmt<>'' then
- TheText:=FormatFloat(fmt,E)
- else
- TheText:=FloatToStrF(E,ffgeneral,FPrecision,0);
- end;
- procedure TFloatField.SetAsFloat(AValue: Double);
- begin
- If CheckRange(AValue) then
- SetData(@Avalue)
- else
- RangeError(AValue,FMinValue,FMaxValue);
- end;
- procedure TFloatField.SetAsLongint(AValue: Longint);
- begin
- SetAsFloat(Avalue);
- end;
- procedure TFloatField.SetAsString(const AValue: string);
- Var R : Double;
- begin
- try
- R := StrToFloat(AValue);
- SetAsFloat(R);
- except
- DatabaseErrorFmt(SNotAFloat, [AValue]);
- end;
- end;
- procedure TFloatField.SetVarValue(const AValue: Variant);
- begin
- SetAsFloat(Avalue);
- end;
- constructor TFloatField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOwner);
- SetDatatype(ftfloat);
- FPrecision:=15;
- FValidChars := [DecimalSeparator, '+', '-', '0'..'9', 'E', 'e'];
- end;
- Function TFloatField.CheckRange(AValue : Double) : Boolean;
- begin
- If (FMinValue<>0) or (FmaxValue<>0) then
- Result:=(AValue>=FMinValue) and (AVAlue<=FMAxValue)
- else
- Result:=True;
- end;
- { TCurrencyField }
- Constructor TCurrencyField.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- SetDataType(ftCurrency);
- end;
- procedure TCurrencyField.GetText(var TheText: string; ADisplayText: Boolean);
- Var
- fmt : string;
- ff: TFloatFormat;
- E : Double;
- begin
- TheText:='';
- If Not GetData(@E) then exit;
- If ADisplayText or (Length(FEditFormat) = 0) Then
- Fmt:=FDisplayFormat
- else
- Fmt:=FEditFormat;
- if ADisplayText then
- ff := ffCurrency
- else
- ff := ffFixed;
- If fmt<>'' then
- TheText:=FormatFloat(fmt, E)
- else
- TheText:=FloatToStrF(E, ff, FPrecision, CurrencyDecimals);
- end;
- { TBooleanField }
- function TBooleanField.GetAsBoolean: Boolean;
- var b : wordbool;
- begin
- If GetData(@b) then
- result := b
- else
- Result:=False;
- end;
- function TBooleanField.GetAsVariant: Variant;
- Var b : wordbool;
- begin
- If GetData(@b) then
- Result := b
- else
- Result:=Null;
- end;
- function TBooleanField.GetAsString: string;
- Var B : wordbool;
- begin
- If Getdata(@B) then
- Result:=FDisplays[False,B]
- else
- result:='';
- end;
- function TBooleanField.GetDataSize: Word;
- begin
- Result:=SizeOf(wordBool);
- end;
- function TBooleanField.GetDefaultWidth: Longint;
- begin
- Result:=Length(FDisplays[false,false]);
- If Result<Length(FDisplays[false,True]) then
- Result:=Length(FDisplays[false,True]);
- end;
- procedure TBooleanField.SetAsBoolean(AValue: Boolean);
- var b : wordbool;
- begin
- b := AValue;
- SetData(@b);
- end;
- procedure TBooleanField.SetAsString(const AValue: string);
- Var Temp : string;
- begin
- Temp:=UpperCase(AValue);
- if Temp='' then
- Clear
- else if pos(Temp, FDisplays[True,True])=1 then
- SetAsBoolean(True)
- else if pos(Temp, FDisplays[True,False])=1 then
- SetAsBoolean(False)
- else
- DatabaseErrorFmt(SNotABoolean,[AValue]);
- end;
- procedure TBooleanField.SetVarValue(const AValue: Variant);
- begin
- SetAsBoolean(AValue);
- end;
- constructor TBooleanField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOwner);
- SetDataType(ftBoolean);
- DisplayValues:='True;False';
- end;
- Procedure TBooleanField.SetDisplayValues(AValue : String);
- Var I : longint;
- begin
- If FDisplayValues<>AValue then
- begin
- I:=Pos(';',AValue);
- If (I<2) or (I=Length(AValue)) then
- DatabaseErrorFmt(SInvalidDisplayValues,[AValue]);
- FdisplayValues:=AValue;
- // Store display values and their uppercase equivalents;
- FDisplays[False,True]:=Copy(AValue,1,I-1);
- FDisplays[True,True]:=UpperCase(FDisplays[False,True]);
- FDisplays[False,False]:=Copy(AValue,I+1,Length(AValue)-i);
- FDisplays[True,False]:=UpperCase(FDisplays[False,False]);
- PropertyChanged(True);
- end;
- end;
- { TDateTimeField }
- procedure TDateTimeField.SetDisplayFormat(const AValue: string);
- begin
- if FDisplayFormat<>AValue then begin
- FDisplayFormat:=AValue;
- PropertyChanged(True);
- end;
- end;
- function TDateTimeField.GetAsDateTime: TDateTime;
- begin
- If Not GetData(@Result,False) then
- Result:=0;
- end;
- procedure TDateTimeField.SetVarValue(const AValue: Variant);
- begin
- SetAsDateTime(AValue);
- end;
- function TDateTimeField.GetAsVariant: Variant;
- Var d : tDateTime;
- begin
- If Getdata(@d,False) then
- Result := d
- else
- Result:=Null;
- end;
- function TDateTimeField.GetAsFloat: Double;
- begin
- Result:=GetAsdateTime;
- end;
- function TDateTimeField.GetAsString: string;
- begin
- GetText(Result,False);
- end;
- function TDateTimeField.GetDataSize: Word;
- begin
- Result:=SizeOf(TDateTime);
- end;
- procedure TDateTimeField.GetText(var TheText: string; ADisplayText: Boolean);
- Var R : TDateTime;
- F : String;
- begin
- If Not Getdata(@R,False) then
- TheText:=''
- else
- begin
- If (ADisplayText) and (Length(FDisplayFormat)<>0) then
- F:=FDisplayFormat
- else
- Case DataType of
- ftTime : F:=ShortTimeFormat;
- ftDate : F:=ShortDateFormat;
- else
- F:='c'
- end;
- TheText:=FormatDateTime(F,R);
- end;
- end;
- procedure TDateTimeField.SetAsDateTime(AValue: TDateTime);
- begin
- SetData(@Avalue,False);
- end;
- procedure TDateTimeField.SetAsFloat(AValue: Double);
- begin
- SetAsDateTime(AValue);
- end;
- procedure TDateTimeField.SetAsString(const AValue: string);
- Var R : TDateTime;
- begin
- R:=StrToDateTime(AVAlue);
- SetData(@R,False);
- end;
- constructor TDateTimeField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOwner);
- SetDataType(ftDateTime);
- end;
- { TDateField }
- constructor TDateField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOwner);
- SetDataType(ftDate);
- end;
- { TTimeField }
- constructor TTimeField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOwner);
- SetDataType(ftTime);
- end;
- procedure TTimeField.SetAsString(const AValue: string);
- Var R : TDateTime;
- begin
- R:=StrToTime(AVAlue);
- SetData(@R);
- end;
- { TBinaryField }
- class procedure TBinaryField.CheckTypeSize(AValue: Longint);
- begin
- // Just check for really invalid stuff; actual size is
- // dependent on the record...
- If AValue<1 then
- DatabaseErrorfmt(SInvalidFieldSize,[Avalue]);
- end;
- function TBinaryField.GetAsString: string;
- begin
- Setlength(Result,DataSize);
- GetData(Pointer(Result));
- end;
- procedure TBinaryField.GetText(var TheText: string; ADisplayText: Boolean);
- begin
- TheText:=GetAsString;
- end;
- procedure TBinaryField.SetAsString(const AValue: string);
- Var Buf : PChar;
- Allocated : Boolean;
- begin
- Allocated:=False;
- If Length(AVAlue)=DataSize then
- Buf:=PChar(Avalue)
- else
- begin
- GetMem(Buf,DataSize);
- Move(Pchar(Avalue)[0],Buf^,DataSize);
- Allocated:=True;
- end;
- SetData(Buf);
- If Allocated then
- FreeMem(Buf,DataSize);
- end;
- procedure TBinaryField.SetText(const AValue: string);
- begin
- SetAsString(Avalue);
- end;
- procedure TBinaryField.SetVarValue(const AValue: Variant);
- begin
- SetAsString(Avalue);
- end;
- constructor TBinaryField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOwner);
- end;
- { TBytesField }
- function TBytesField.GetDataSize: Word;
- begin
- Result:=Size;
- end;
- constructor TBytesField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOwner);
- SetDataType(ftBytes);
- Size:=16;
- end;
- { TVarBytesField }
- function TVarBytesField.GetDataSize: Word;
- begin
- Result:=Size+2;
- end;
- constructor TVarBytesField.Create(AOwner: TComponent);
- begin
- INherited Create(AOwner);
- SetDataType(ftvarbytes);
- Size:=16;
- end;
- { TBCDField }
- class procedure TBCDField.CheckTypeSize(AValue: Longint);
- begin
- If not (AValue in [1..4]) then
- DatabaseErrorfmt(SInvalidFieldSize,[Avalue]);
- end;
- function TBCDField.GetAsCurrency: Currency;
- Var C : system.Currency;
- begin
- if GetData(@C) then
- result := C;
- end;
- function TBCDField.GetAsVariant: Variant;
- Var c : system.Currency;
- begin
- If GetData(@c) then
- Result := c
- else
- Result:=Null;
- end;
- function TBCDField.GetAsFloat: Double;
- begin
- result := GetAsCurrency;
- end;
- function TBCDField.GetAsLongint: Longint;
- begin
- result := round(GetAsCurrency);
- end;
- function TBCDField.GetAsString: string;
- var c : system.currency;
- begin
- If GetData(@C) then
- Result:=CurrToStr(C)
- else
- Result:='';
- end;
- function TBCDField.GetValue(var AValue: Currency): Boolean;
- begin
- Result := GetData(@AValue);
- end;
- function TBCDField.GetDataSize: Word;
- begin
- result := sizeof(currency);
- end;
- function TBCDField.GetDefaultWidth: Longint;
- begin
- if precision > 0 then result := precision
- else result := 10;
- end;
- procedure TBCDField.GetText(var TheText: string; ADisplayText: Boolean);
- var
- c : system.currency;
- fmt: String;
- begin
- if GetData(@C) then begin
- if aDisplayText or (FEditFormat='') then
- fmt := FDisplayFormat
- else
- fmt := FEditFormat;
- if fmt<>'' then
- TheText := FormatFloat(fmt,C)
- else if fCurrency then begin
- if aDisplayText then
- TheText := FloatToStrF(C, ffCurrency, FPrecision, 2{digits?})
- else
- TheText := FloatToStrF(C, ffFixed, FPrecision, 2{digits?});
- end else
- TheText := FloatToStrF(C, ffGeneral, FPrecision, 0{digits?});
- end else
- TheText := '';
- end;
- procedure TBCDField.SetAsCurrency(AValue: Currency);
- begin
- If CheckRange(AValue) then
- setdata(@AValue)
- else
- RangeError(AValue,FMinValue,FMaxvalue);
- end;
- procedure TBCDField.SetVarValue(const AValue: Variant);
- begin
- SetAsCurrency(AValue);
- end;
- Function TBCDField.CheckRange(AValue : Currency) : Boolean;
- begin
- If (FMinValue<>0) or (FmaxValue<>0) then
- Result:=(AValue>=FMinValue) and (AVAlue<=FMaxValue)
- else
- Result:=True;
- end;
- procedure TBCDField.SetAsFloat(AValue: Double);
- begin
- SetAsCurrency(AValue);
- end;
- procedure TBCDField.SetAsLongint(AValue: Longint);
- begin
- SetAsCurrency(AValue);
- end;
- procedure TBCDField.SetAsString(const AValue: string);
- begin
- SetAsCurrency(strtocurr(AValue));
- end;
- constructor TBCDField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOwner);
- FMaxvalue := 0;
- FMinvalue := 0;
- SetDataType(ftBCD);
- FPrecision := 15;
- Size:=4;
- end;
- { TBlobField }
- procedure TBlobField.AssignTo(Dest: TPersistent);
- begin
- //!! To be implemented
- end;
- Function TBlobField.GetBlobStream(Mode : TBlobStreamMode) : TStream;
- begin
- Result:=FDataset.CreateBlobStream(Self,Mode);
- end;
- procedure TBlobField.FreeBuffers;
- begin
- end;
- function TBlobField.GetAsString: string;
- var
- Stream: TStream;
- begin
- Stream := GetBlobStream(bmRead);
- if Stream <> nil then
- With Stream do
- try
- SetLength(Result,Size);
- ReadBuffer(Pointer(Result)^,Size);
- finally
- Free
- end
- else
- Result := '(blob)';
- end;
- function TBlobField.GetBlobSize: Longint;
- var
- Stream: TStream;
- begin
- Stream := GetBlobStream(bmread);
- if Stream <> nil then
- With Stream do
- try
- Result:=Size;
- finally
- Free;
- end
- else
- result := 0;
- end;
- function TBlobField.GetIsNull: Boolean;
- begin
- If Not Modified then
- result:= inherited GetIsnull
- else
- With GetBlobStream(bmread) do
- try
- Result:=(Size=0);
- Finally
- Free;
- end;
- end;
- procedure TBlobField.GetText(var TheText: string; ADisplayText: Boolean);
- begin
- TheText:=GetAsString;
- end;
- procedure TBlobField.SetAsString(const AValue: string);
- begin
- With GetBlobStream(bmwrite) do
- try
- WriteBuffer(Pointer(Avalue)^,Length(Avalue));
- finally
- Free;
- end;
- end;
- procedure TBlobField.SetText(const AValue: string);
- begin
- SetAsString(AValue);
- end;
- procedure TBlobField.SetVarValue(const AValue: Variant);
- begin
- SetAsString(AValue);
- end;
- constructor TBlobField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOWner);
- SetDataType(ftBlob);
- end;
- procedure TBlobField.Assign(Source: TPersistent);
- begin
- //!! To be implemented
- end;
- procedure TBlobField.Clear;
- begin
- GetBlobStream(bmWrite).free;
- end;
- class function TBlobField.IsBlob: Boolean;
- begin
- Result:=True;
- end;
- procedure TBlobField.LoadFromFile(const FileName: string);
- Var S : TFileStream;
- begin
- S:=TFileStream.Create(FileName,fmOpenRead);
- try
- LoadFromStream(S);
- finally
- S.Free;
- end;
- end;
- procedure TBlobField.LoadFromStream(Stream: TStream);
- begin
- With GetBlobStream(bmWrite) do
- Try
- CopyFrom(Stream,0);
- finally
- Free;
- end;
- end;
- procedure TBlobField.SaveToFile(const FileName: string);
- Var S : TFileStream;
- begin
- S:=TFileStream.Create(FileName,fmCreate);
- try
- SaveToStream(S);
- finally
- S.Free;
- end;
- end;
- procedure TBlobField.SaveToStream(Stream: TStream);
- Var S : TStream;
- begin
- S:=GetBlobStream(bmRead);
- Try
- Stream.CopyFrom(S,0);
- finally
- S.Free;
- end;
- end;
- procedure TBlobField.SetFieldType(AValue: TFieldType);
- begin
- If AValue in [Low(TBlobType)..High(TBlobType)] then
- SetDatatype(Avalue);
- end;
- { TMemoField }
- constructor TMemoField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOwner);
- SetDataType(ftMemo);
- end;
- { TGraphicField }
- constructor TGraphicField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOwner);
- SetDataType(ftGraphic);
- end;
- { TFields }
- Constructor TFields.Create(ADataset : TDataset);
- begin
- FDataSet:=ADataset;
- FFieldList:=TList.Create;
- FValidFieldKinds:=[fkData..fkInternalcalc];
- end;
- Destructor TFields.Destroy;
- begin
- if FFieldList <> nil then Clear;
- FFieldList.Free;
- inherited Destroy;
- end;
- Procedure Tfields.Changed;
- begin
- if (FDataSet <> nil) and not (csDestroying in FDataSet.ComponentState) then
- FDataSet.DataEvent(deFieldListChange, 0);
- If Assigned(FOnChange) then
- FOnChange(Self);
- end;
- Procedure TFields.CheckfieldKind(Fieldkind : TFieldKind; Field : TField);
- begin
- If Not (FieldKind in ValidFieldKinds) Then
- DatabaseErrorFmt(SInvalidFieldKind,[Field.FieldName]);
- end;
- Function Tfields.GetCount : Longint;
- begin
- Result:=FFieldList.Count;
- end;
- Function TFields.GetField (Index : longint) : TField;
- begin
- Result:=Tfield(FFieldList[Index]);
- end;
- procedure Tfields.SetField(Index: Integer; Value: TField);
- begin
- Fields[Index].Assign(Value);
- end;
- Procedure TFields.SetFieldIndex (Field : TField;Value : Integer);
- Var Old : Longint;
- begin
- Old := FFieldList.indexOf(Field);
- If Old=-1 then
- Exit;
- // Check value
- If Value<0 Then Value:=0;
- If Value>=Count then Value:=Count-1;
- If Value<>Old then
- begin
- FFieldList.Delete(Old);
- FFieldList.Insert(Value,Field);
- Field.PropertyChanged(True);
- Changed;
- end;
- end;
- Procedure TFields.Add(Field : TField);
- begin
- CheckFieldName(Field.FieldName);
- FFieldList.Add(Field);
- Field.FFields:=Self;
- Changed;
- end;
- Procedure TFields.CheckFieldName (Const Value : String);
- begin
- If FindField(Value)<>Nil then
- DataBaseErrorFmt(SDuplicateFieldName,[Value],FDataset);
- end;
- Procedure TFields.CheckFieldNames (Const Value : String);
- Var I : longint;
- S,T : String;
- begin
- T:=Value;
- Repeat
- I:=Pos(';',T);
- If I=0 Then I:=Length(T)+1;
- S:=Copy(T,1,I-1);
- Delete(T,1,I);
- // Will raise an error if no such field...
- FieldByName(S);
- Until (T='');
- end;
- Procedure TFields.Clear;
- begin
- with FFieldList do
- while Count > 0 do begin
- TField(Last).FDataSet := Nil;
- TField(Last).Free;
- FFieldList.Delete(Count - 1);
- end;
- Changed;
- end;
- Function TFields.FindField (Const Value : String) : TField;
- Var S : String;
- I : longint;
- begin
- Result:=Nil;
- S:=UpperCase(Value);
- For I:=0 To FFieldList.Count-1 do
- If S=UpperCase(TField(FFieldList[i]).FieldName) Then
- Begin
- {$ifdef dsdebug}
- Writeln ('Found field ',Value);
- {$endif}
- Result:=TField(FFieldList[I]);
- Exit;
- end;
- end;
- Function TFields.FieldByName (Const Value : String) : TField;
- begin
- Result:=FindField(Value);
- If result=Nil then
- DatabaseErrorFmt(SFieldNotFound,[Value],FDataset);
- end;
- Function TFields.FieldByNumber(FieldNo : Integer) : TField;
- Var i : Longint;
- begin
- Result:=Nil;
- For I:=0 to FFieldList.Count-1 do
- If FieldNo=TField(FFieldList[I]).FieldNo then
- begin
- Result:=TField(FFieldList[i]);
- Exit;
- end;
- end;
- Procedure TFields.GetFieldNames (Values : TStrings);
- Var i : longint;
- begin
- Values.Clear;
- For I:=0 to FFieldList.Count-1 do
- Values.Add(Tfield(FFieldList[I]).FieldName);
- end;
- Function TFields.IndexOf(Field : TField) : Longint;
- begin
- Result:=FFieldList.IndexOf(Field);
- end;
- procedure TFields.Remove(Value : TField);
- begin
- FFieldList.Remove(Value);
- Value.FFields := nil;
- Changed;
- end;
|