diff --git a/fcl/db/db.pp b/fcl/db/db.pp index cd7ff64174..63860bf983 100644 --- a/fcl/db/db.pp +++ b/fcl/db/db.pp @@ -132,9 +132,39 @@ type TFieldAttribute = (faHiddenCol, faReadonly, faRequired, faLink, faUnNamed, faFixed); TFieldAttributes = set of TFieldAttribute; + { TNamedItem } + + TNamedItem = class(TCollectionItem) + private + FName: string; + protected + function GetDisplayName: string; override; + procedure SetDisplayName(const AValue: string); override; + public + property Name : string read FName write SetDisplayName; + property DisplayName : string read GetDisplayName; + end; + + { TDefCollection } + + TDefCollection = class(TOwnedCollection) + private + FDataset: TDataset; + FUpdated: boolean; + protected + procedure SetItemName(AItem: TCollectionItem); override; + public + constructor create(ADataset: TDataset; AOwner: TPersistent; AClass: TCollectionItemClass); + function Find(const AName: string): TNamedItem; + procedure GetItemNames(List: TStrings); + function IndexOf(const AName: string): Longint; + property Dataset: TDataset read FDataset; + property Updated: boolean read FUpdated write FUpdated; + end; + { TFieldDef } - TFieldDef = class(TCollectionItem) + TFieldDef = class(TNamedItem) Private FDataType : TFieldType; FFieldNo : Longint; @@ -142,8 +172,6 @@ type FPrecision : Longint; FRequired : Boolean; FSize : Word; - FName : String; - FDisplayName : String; FAttributes : TFieldAttributes; Function GetFieldClass : TFieldClass; procedure SetAttributes(AValue: TFieldAttributes); @@ -151,9 +179,6 @@ type procedure SetPrecision(const AValue: Longint); procedure SetSize(const AValue: Word); procedure SetRequired(const AValue: Boolean); - protected - function GetDisplayName: string; override; - procedure SetDisplayName(const AValue: string); override; public constructor Create(AOwner: TFieldDefs; const AName: string; ADataType: TFieldType; ASize: Word; ARequired: Boolean; AFieldNo: Longint); overload; @@ -166,8 +191,6 @@ type property Required: Boolean read FRequired write SetRequired; Published property Attributes: TFieldAttributes read FAttributes write SetAttributes default []; - property Name: string read FName write FName; // Must move to TNamedItem - property DisplayName : string read FDisplayName write FDisplayName; // Must move to TNamedItem property DataType: TFieldType read FDataType write SetDataType; property Precision: Longint read FPrecision write SetPrecision; property Size: Word read FSize write SetSize; @@ -175,15 +198,11 @@ type { TFieldDefs } - TFieldDefs = class(TOwnedCollection) + TFieldDefs = class(TDefCollection) private - FUpdated: Boolean; FHiddenFields : Boolean; function GetItem(Index: Longint): TFieldDef; - function GetDataset: TDataset; procedure SetItem(Index: Longint; const AValue: TFieldDef); - protected - procedure SetItemName(AItem: TCollectionItem); override; public constructor Create(ADataSet: TDataSet); // destructor Destroy; override; @@ -194,13 +213,9 @@ type procedure Assign(FieldDefs: TFieldDefs); overload; // procedure Clear; // procedure Delete(Index: Longint); - function Find(const AName: string): TFieldDef; - function IndexOf(const AName: string): Longint; procedure Update; overload; Property HiddenFields : Boolean Read FHiddenFields Write FHiddenFields; property Items[Index: Longint]: TFieldDef read GetItem write SetItem; default; - property Dataset: TDataset read GetDataset; - property Updated: Boolean read FUpdated write FUpdated; end; { TField } @@ -789,13 +804,12 @@ type ixExpression, ixNonMaintained); TIndexOptions = set of TIndexOption; - TIndexDef = class(TCollectionItem) + TIndexDef = class(TNamedItem) Private FCaseinsFields: string; FDescFields: string; FExpression : String; FFields : String; - FName : String; FOptions : TIndexOptions; FSource : String; protected @@ -812,21 +826,18 @@ type property Fields: string read FFields write FFields; property CaseInsFields: string read FCaseinsFields write SetCaseInsFields; property DescFields: string read FDescFields write SetDescFields; - property Name: string read FName write FName; property Options: TIndexOptions read FOptions write FOptions; property Source: string read FSource write FSource; end; { TIndexDefs } - TIndexDefs = class(TOwnedCollection) + TIndexDefs = class(TDefCollection) Private - FUpDated : Boolean; - FDataset : Tdataset; Function GetItem(Index: Integer): TIndexDef; Procedure SetItem(Index: Integer; Value: TIndexDef); public - constructor Create(DataSet: TDataSet); overload; + constructor Create(ADataSet: TDataSet); overload; destructor Destroy; override; procedure Add(const Name, Fields: string; Options: TIndexOptions); Function AddIndexDef: TIndexDef; @@ -834,10 +845,8 @@ type function FindIndexForFields(const Fields: string): TIndexDef; function GetIndexForFields(const Fields: string; CaseInsensitive: Boolean): TIndexDef; - function IndexOf(const Name: string): Longint; procedure Update; overload; Property Items[Index: Integer] : TIndexDef read GetItem write SetItem; default; - property Updated: Boolean read FUpdated write FUpdated; end; { TCheckConstraint } @@ -1886,6 +1895,71 @@ begin Inherited; end; +{ TNamedItem } + +function TNamedItem.GetDisplayName: string; +begin + Result := FName; +end; + +procedure TNamedItem.SetDisplayName(const AValue: string); +begin + if FName=AValue then exit; + if (AValue <> '') and + (Collection is TOwnedCollection) and + (TFieldDefs(Collection).IndexOf(AValue) >= 0) then + DatabaseErrorFmt(SDuplicateName, [AValue, Collection.ClassName]); + FName:=AValue; + inherited SetDisplayName(AValue); +end; + +{ TDefCollection } + +procedure TDefCollection.SetItemName(AItem: TCollectionItem); +begin + with AItem as TNamedItem do + if Name = '' then + Name := Dataset.Name + Copy(ClassName, 2, 5) + IntToStr(ID+1) + else inherited SetItemName(AItem); +end; + +constructor TDefCollection.create(ADataset: TDataset; AOwner: TPersistent; + AClass: TCollectionItemClass); +begin + inherited Create(AOwner,AClass); + FDataset := ADataset; +end; + +function TDefCollection.Find(const AName: string): TNamedItem; +var i: integer; +begin + Result := Nil; + for i := 0 to Count - 1 do if AnsiSameText(TNamedItem(Items[i]).Name, AName) then + begin + Result := TNamedItem(Items[i]); + Break; + end; +end; + +procedure TDefCollection.GetItemNames(List: TStrings); +var i: LongInt; +begin + for i := 0 to Count - 1 do + List.Add(TNamedItem(Items[i]).Name); +end; + +function TDefCollection.IndexOf(const AName: string): Longint; +var i: LongInt; +begin + Result := -1; + for i := 0 to Count - 1 do + if AnsiSameText(TNamedItem(Items[i]).Name, AName) then + begin + Result := i; + Break; + end; +end; + { TIndexDef } procedure TIndexDef.SetDescFields(const AValue: string); @@ -1935,8 +2009,8 @@ constructor TIndexDef.Create(Owner: TIndexDefs; const AName, TheFields: string; TheOptions: TIndexOptions); begin - inherited create(Owner); FName := aname; + inherited create(Owner); FFields := TheFields; FOptions := TheOptions; end; @@ -1962,11 +2036,10 @@ begin Inherited SetItem(Index,Value); end; -constructor TIndexDefs.Create(DataSet: TDataSet); +constructor TIndexDefs.Create(ADataSet: TDataSet); begin - FDataset := Dataset; - inherited create(Dataset, TIndexDef); + inherited create(ADataset, Owner, TIndexDef); end; @@ -1990,14 +2063,8 @@ begin end; function TIndexDefs.Find(const IndexName: string): TIndexDef; -var i: integer; begin - Result := Nil; - for i := 0 to Count - 1 do - if AnsiSameText(Items[i].Name, IndexName) then begin - Result := Items[i]; - Break; - end; + Result := (inherited Find(IndexName)) as TIndexDef; if (Result=Nil) Then DatabaseErrorFmt(SIndexNotFound, [IndexName], FDataSet); end; @@ -2039,26 +2106,14 @@ begin Result := Last; end; - -function TIndexDefs.IndexOf(const Name: string): Longint; - -var i: LongInt; -begin - Result := -1; - for i := 0 to Count - 1 do - if AnsiSameText(Items[i].Name, Name) then - begin - Result := i; - Break; - end; -end; - - procedure TIndexDefs.Update; begin - if assigned(Fdataset) then - Fdataset.UpdateIndexDefs; + if (not updated) and assigned(Dataset) then + begin + Dataset.UpdateIndexDefs; + updated := True; + end; end; { TCheckConstraint } diff --git a/fcl/db/fields.inc b/fcl/db/fields.inc index 78f633409a..d7759e7b94 100644 --- a/fcl/db/fields.inc +++ b/fcl/db/fields.inc @@ -32,12 +32,11 @@ 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 := ''; + Name:=Aname; + Inherited Create(AOwner); FDatatype:=ADatatype; FSize:=ASize; FRequired:=ARequired; @@ -88,7 +87,7 @@ begin Result.Size:=FSize; Result.Required:=FRequired; Result.FFieldName:=FName; - Result.FDisplayLabel:=FDisplayName; + Result.FDisplayLabel:=DisplayName; Result.FFieldNo:=Self.FieldNo; Result.SetFieldType(DataType); Result.FReadOnly:= (faReadOnly in Attributes); @@ -138,22 +137,6 @@ begin 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 @@ -215,28 +198,14 @@ 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); + Inherited Create(ADataset, Owner, TFieldDef); end; procedure TFieldDefs.Assign(FieldDefs: TFieldDefs); @@ -262,35 +231,14 @@ begin 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; + if not Updated then + begin + DataSet.InitFieldDefs; + Updated := True; + end; end; Function TFieldDefs.AddFieldDef : TFieldDef; diff --git a/fcl/db/sqldb/interbase/ibconnection.pp b/fcl/db/sqldb/interbase/ibconnection.pp index 38265e7831..32b32cd833 100644 --- a/fcl/db/sqldb/interbase/ibconnection.pp +++ b/fcl/db/sqldb/interbase/ibconnection.pp @@ -632,7 +632,7 @@ begin FD := TFieldDef.Create(FieldDefs, SQLDA^.SQLVar[x].AliasName, TransType, TransLen, False, (x + 1)); if TransType = ftBCD then FD.precision := SQLDA^.SQLVar[x].SQLLen; - FD.DisplayName := SQLDA^.SQLVar[x].AliasName; +// FD.DisplayName := SQLDA^.SQLVar[x].AliasName; FieldBinding[FD.FieldNo-1] := x; end; end; @@ -994,7 +994,7 @@ begin If qry.fields[4].asstring = 'PRIMARY KEY' then options := options + [ixPrimary]; If qry.fields[2].asinteger = 1 then options := options + [ixUnique]; qry.next; - while (name = qry.fields[0].asstring) and (not qry.eof) do + while (name = trim(qry.fields[0].asstring)) and (not qry.eof) do begin Fields := Fields + ';' + trim(qry.Fields[3].asstring); qry.next;