123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776 |
- {
- $Id$
- 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);
- Type PByte = ^Byte;
- 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);
- FName:=Aname;
- FDatatype:=ADatatype;
- FSize:=ASize;
- FRequired:=ARequired;
- FPrecision:=-1;
- // Correct sizes.
- If FDataType=ftFloat then
- begin
- If Not (FSize in [4,8,10]) then FSize:=10
- end
- else If FDataType in [ftWord,ftsmallint,ftinteger] Then
- If Not (FSize in [1,2,4]) then FSize:=4;
- FFieldNo:=AFieldNo;
- AOwner.FItems.Add(Self);
- end;
- Destructor TFieldDef.Destroy;
- Var I : longint;
- begin
- Inherited destroy;
- end;
- Function TFieldDef.CreateField(AOwner: TComponent): TField;
- Var TheField : TFieldClass;
- begin
- Writeln ('Creating field');
- TheField:=GetFieldClass;
- if TheField=Nil then
- DatabaseErrorFmt(SUnknownFieldType,[FName]);
- Result:=Thefield.Create(AOwner);
- Try
- Result.Size:=FSize;
- Result.Required:=FRequired;
- Result.FieldName:=FName;
- Result.SetFieldType(DataType);
- Writeln ('Trying to set dataset');
- Result.Dataset:=TFieldDefs(Owner).FDataset;
- If Result is TFloatField then
- TFloatField(Result).Precision:=FPrecision;
- except
- Result.Free;
- Raise;
- end;
- end;
- Function TFieldDef.GetFieldClass : TFieldClass;
- begin
- //!! Should be owner as tdataset but that doesn't work ??
- If Assigned(Owner) then
- Result:=TFieldDefs(Owner).FDataSet.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; ASize: Word;
- ARequired: Boolean);
- begin
- Writeln ('Adding fielddef');
- If Length(Name)=0 Then
- DatabaseError(SNeedFieldName);
- // the fielddef will register itself here as a owned component.
- // fieldno is 1 based !
- FItems.Add(TFieldDef.Create(Self,AName,ADataType,ASize,Arequired,FItems.Count+1));
- end;
- function TFieldDefs.GetCount: Longint;
- begin
- Result:=FItems.Count;
- end;
- function TFieldDefs.GetItem(Index: Longint): TFieldDef;
- begin
- Result:=TFieldDef(FItems[Index]);
- end;
- constructor TFieldDefs.Create(ADataSet: TDataSet);
- begin
- Inherited Create(ADataSet);
- FItems:=TList.Create;
- FDataset:=ADataset;
- end;
- procedure TFieldDefs.Assign(FieldDefs: TFieldDefs);
- Var I : longint;
- begin
- Clear;
- For i:=1 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;
- end;
- function TFieldDefs.Find(const AName: string): TFieldDef;
- Var I : longint;
- begin
- I:=IndexOf(AName);
- If I=-1 Then
- DataBaseErrorFmt(SUnknownField,[AName,FDataSet.Name]);
- Result:=TFieldDef(Fitems[i]);
- end;
- function TFieldDefs.IndexOf(const AName: string): Longint;
- Var I : longint;
- begin
- For I:=0 to Fitems.Count-1 do
- If AnsiCompareText(TFieldDef(FItems[I]).Name,AName)=0 then
- begin
- Result:=I;
- Exit;
- end;
- Result:=-1;
- end;
- procedure TFieldDefs.Update;
- begin
- FDataSet.UpdateFieldDefs;
- end;
- { ---------------------------------------------------------------------
- TField
- ---------------------------------------------------------------------}
- Const
- SBoolean = 'Boolean';
- SDateTime = 'TDateTime';
- SFloat = 'Float';
- SInteger = 'Integer';
- SString = 'String';
- constructor TField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOwner);
- FVisible:=True;
- FValidChars:=[#0..#155];
- end;
- destructor TField.Destroy;
- begin
- IF Assigned(FDataSet) then
- begin
- FDataSet.Active:=False;
- FDataSet.RemoveField(Self);
- end;
- Inherited Destroy;
- end;
- function TField.AccessError(const TypeName: string): EDatabaseError;
- begin
- Result:=EDatabaseError.CreateFmt(SinvalidTypeConversion,[TypeName,FFieldName]);
- end;
- procedure TField.Assign(Source: TPersistent);
- begin
- //!! To be implemented
- 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
- SetData(Nil);
- end;
- procedure TField.DataChanged;
- begin
- FDataset.DataEvent(deFieldChange,longint(Self));
- end;
- procedure TField.FocusControl;
- begin
- FDataSet.DataEvent(deFocusControl,longint(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: Extended;
- begin
- AccessError(SDateTime);
- end;
- function TField.GetAsLongint: Longint;
- begin
- AccessError(SInteger);
- end;
- function TField.GetAsString: string;
- begin
- AccessError(SString);
- end;
- function TField.GetCanModify: Boolean;
- begin
- Result:=Not ReadOnly;
- If Result then
- begin
- Result:=Assigned(DataSet);
- If Result then
- Result:=Not(DataSet.CanModify);
- end;
- end;
- function TField.GetData(Buffer: Pointer): 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);
- 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.getIndex : longint;
- begin
- If Assigned(FDataset) then
- Result:=FDataset.FFieldList.IndexOf(Self)
- else
- Result:=-1;
- end;
- function TField.GetIsNull: Boolean;
- begin
- Result:=Not(GetData (Nil));
- end;
- function TField.GetParentComponent: TComponent;
- begin
- //!! To be implemented
- 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.Notification(AComponent: TComponent; Operation: TOperation);
- begin
- Inherited Notification(AComponent,Operation);
- 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
- //!! To be implemented
- end;
- procedure TField.SetAsBoolean(AValue: Boolean);
- begin
- AccessError(SBoolean);
- end;
- procedure TField.SetAsDateTime(AValue: TDateTime);
- begin
- AccessError(SDateTime);
- end;
- procedure TField.SetAsFloat(AValue: Extended);
- begin
- AccessError(SFloat);
- end;
- procedure TField.SetAsLongint(AValue: Longint);
- begin
- AccessError(SInteger);
- end;
- procedure TField.SetAsString(const AValue: string);
- begin
- AccessError(SString);
- end;
- procedure TField.SetData(Buffer: Pointer);
- begin
- If Not Assigned(FDataset) then
- EDatabaseError.CreateFmt(SNoDataset,[FieldName]);
- FDataSet.SetFieldData(Self,Buffer);
- end;
- Procedure TField.SetDataset (Value : TDataset);
- begin
- Writeln ('Setting dataset');
- If Value=FDataset then exit;
- If Assigned(FDataset) Then FDataset.CheckInactive;
- If Assigned(Value) then
- begin
- Value.CheckInactive;
- // ?? Identifier idents no member ??
- Value.FFieldList.CheckFieldName(FFieldName);
- end;
- If Assigned(FDataset) then
- FDataset.FFieldList.Remove(Self);
- If Assigned(Value) then
- begin
- Writeln('Adding field to list..');
- Value.FFieldList.Add(Self);
- end;
- FDataset:=Value;
- 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
- //!! To be implemented
- 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.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;
- { ---------------------------------------------------------------------
- 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: Extended;
- 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.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: Extended);
- 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;
- { ---------------------------------------------------------------------
- TNumericField
- ---------------------------------------------------------------------}
- constructor TNumericField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOwner);
- AlignMent:=taRightJustify;
- end;
- procedure TNumericField.RangeError(AValue, Min, Max: Extended);
- 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: Extended;
- begin
- Result:=GetAsLongint;
- end;
- function TLongintField.GetAsLongint: Longint;
- begin
- If Not GetValue(Result) then
- Result:=0;
- 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 GetData(@l) then exit;
- If ADisplayText or (FEditFormat='') then
- fmt:=FDisplayFormat
- else
- fmt:=FEditFormat;
- { // no formatFloat yet
- If length(fmt)<>0 then
- AText:=FormatFloat(fmt,L)
- else
- }
- Str(L,AText);
- end;
- function TLongintField.GetValue(var AValue: Longint): Boolean;
- Type
- PSmallint = ^SmallInt;
- PLongint = ^Longint;
- PWord = ^Word;
- 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: Extended);
- 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.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
- if FMaxValue=0 Then
- Result:=(AValue<=FMaxRange) and (AValue>=FMinRange)
- else
- Result:=(AValue<=FMaxValue) and (AValue>=FMinValue);
- 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;
- { 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);
- end;
- Procedure TAutoIncField.SetAsLongint(AValue : Longint);
- begin
- DataBaseError(SCantSetAutoIncfields);
- end;
- { TFloatField }
- function TFloatField.GetAsFloat: Extended;
- begin
- If Not GetData(@Result) Then
- Result:=0.0;
- end;
- function TFloatField.GetAsLongint: Longint;
- begin
- Result:=Round(GetAsFloat);
- end;
- function TFloatField.GetAsString: string;
- Var R : Extended;
- begin
- If GetData(@R) then
- Result:=FloatToStr(R)
- else
- Result:='';
- end;
- function TFloatField.GetDataSize: Word;
- begin
- Result:=SizeOf(Extended);
- end;
- procedure TFloatField.GetText(var TheText: string; ADisplayText: Boolean);
- Var
- fmt : string;
- E : Extended;
- begin
- text:='';
- If Not GetData(@E) then exit;
- If ADisplayText or (Length(FEditFormat) = 0) Then
- Fmt:=FDisplayFormat
- else
- Fmt:=FEditFormat;
- { // No formatfloat yet
- If fmt<>'' then
- TheText:=FormatFloat(fmt,E)
- else
- }
- Text:=FloatToStrF(E,ffgeneral,FPrecision,0);
- end;
- procedure TFloatField.SetAsFloat(AValue: Extended);
- 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 : Extended;
- Code : longint;
- begin
- Val(AVAlue,R,Code);
- If Code<>0 then
- DatabaseErrorFmt(SNotAFloat,[AVAlue])
- Else
- SetAsFloat(R);
- end;
- constructor TFloatField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOwner);
- SetDatatype(ftfloat);
- end;
- Function TFloatField.CheckRange(AValue : Extended) : Boolean;
- begin
- If (FMinValue<>0) or (FmaxValue<>0) then
- Result:=(AValue>=FMinValue) and (AVAlue<=FMAxValue)
- else
- Result:=True;
- end;
- { TBooleanField }
- function TBooleanField.GetAsBoolean: Boolean;
- begin
- If not GetData(@Result) then
- Result:=False;
- end;
- function TBooleanField.GetAsString: string;
- Var B : boolean;
- begin
- If Getdata(@B) then
- Result:=FDisplays[False,B]
- else
- result:='';
- end;
- function TBooleanField.GetDataSize: Word;
- begin
- Result:=SizeOf(Boolean);
- 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);
- begin
- SetData(@AValue);
- end;
- procedure TBooleanField.SetAsString(const AValue: string);
- Var Temp : string;
- begin
- Temp:=UpperCase(AValue);
- If Temp=FDisplays[True,True] Then
- SetAsBoolean(True)
- else If Temp=FDisplays[True,False] then
- SetAsBoolean(False)
- else
- DatabaseErrorFmt(SNotABoolean,[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 }
- function TDateTimeField.GetAsDateTime: TDateTime;
- begin
- If Not GetData(@Result) then
- Result:=0;
- end;
- function TDateTimeField.GetAsFloat: Extended;
- 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) 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);
- end;
- procedure TDateTimeField.SetAsFloat(AValue: Extended);
- begin
- SetAsDateTime(AValue);
- end;
- procedure TDateTimeField.SetAsString(const AValue: string);
- Var R : TDateTime;
- begin
- R:=StrToDateTime(AVAlue);
- SetData(@R);
- end;
- constructor TDateTimeField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOwner);
- SetDataType(ftDateTime);
- end;
- { TDateField }
- function TDateField.GetDataSize: Word;
- begin
- Result:=SizeOf(TDateTime);
- end;
- constructor TDateField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOwner);
- SetDataType(ftDate);
- end;
- { TTimeField }
- function TTimeField.GetDataSize: Word;
- begin
- Result:=SizeOf(TDateTime);
- end;
- constructor TTimeField.Create(AOwner: TComponent);
- begin
- Inherited Create(AOwner);
- SetDataType(ftTime);
- 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;
- 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
- //!! To be implemented
- end;
- function TBCDField.GetAsFloat: Extended;
- begin
- //!! To be implemented
- end;
- function TBCDField.GetAsLongint: Longint;
- begin
- //!! To be implemented
- end;
- function TBCDField.GetAsString: string;
- begin
- //!! To be implemented
- end;
- function TBCDField.GetDataSize: Word;
- begin
- //!! To be implemented
- end;
- function TBCDField.GetDefaultWidth: Longint;
- begin
- //!! To be implemented
- end;
- procedure TBCDField.GetText(var TheText: string; ADisplayText: Boolean);
- begin
- //!! To be implemented
- end;
- procedure TBCDField.SetAsFloat(AValue: Extended);
- begin
- //!! To be implemented
- end;
- procedure TBCDField.SetAsLongint(AValue: Longint);
- begin
- //!! To be implemented
- end;
- procedure TBCDField.SetAsString(const AValue: string);
- begin
- //!! To be implemented
- end;
- constructor TBCDField.Create(AOwner: TComponent);
- begin
- DatabaseError('BCD fields not supported yet. Sorry !');
- 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;
- begin
- With GetBlobStream(bmRead) do
- try
- SetLength(Result,Size);
- ReadBuffer(Pointer(Result)^,Size);
- finally
- Free
- end;
- end;
- function TBlobField.GetBlobSize: Longint;
- begin
- With GetBlobStream(bmread) do
- try
- Result:=Size;
- finally
- Free;
- end;
- 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;
- 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
- FFieldList.Free;
- end;
- Procedure Tfields.Changed;
- begin
- 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.SetFieldIndex (Field : TField;Value : Integer);
- Var Old : Longint;
- begin
- Old := FFieldList.indexOf(Field);
- If Old=-1 then
- Exit;
- // Check value
- If Value<FFieldList.Count 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);
- Var I : longint;
- S : String;
- begin
- If FindField(Value)<>Nil then
- begin
- S:=UpperCase(Value);
- For I:=0 To FFieldList.Count-1 do
- If S=UpperCase(TField(FFieldList[i]).FieldName) Then
- DataBaseErrorFmt(SDuplicateFieldName,[Value],FDataset);
- end;
- 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);
- 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
- 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;
- Var i : longint;
- begin
- Result:=-1;
- For I:=0 To FFieldList.Count-1 do
- If Pointer(Field)=FFieldList[i] Then
- Exit(I);
- end;
- procedure TFields.Remove(Value : TField);
- Var I : longint;
- begin
- I:=IndexOf(Value);
- If I<>0 then
- FFieldList.Delete(I);
- end;
- {
- $Log$
- Revision 1.4 2000-12-24 12:45:19 peter
- * merges from 1.0.x branch
- Revision 1.3 2000/09/02 09:36:36 sg
- * Changed all occurences of TAbstractReader to TReader, as FCL streaming
- is source compatible to VCL streaming now (for quite a while, BTW)
- Revision 1.2 2000/07/13 11:32:56 michael
- + removed logs
- }
|