* Implemented TNamedItem and TDefCollection, bug #8212

git-svn-id: trunk@6596 -
This commit is contained in:
joost 2007-02-21 22:51:19 +00:00
parent 90ea73a90b
commit bfcfcdad8a
3 changed files with 120 additions and 117 deletions

View File

@ -132,9 +132,39 @@ type
TFieldAttribute = (faHiddenCol, faReadonly, faRequired, faLink, faUnNamed, faFixed); TFieldAttribute = (faHiddenCol, faReadonly, faRequired, faLink, faUnNamed, faFixed);
TFieldAttributes = set of TFieldAttribute; 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 }
TFieldDef = class(TCollectionItem) TFieldDef = class(TNamedItem)
Private Private
FDataType : TFieldType; FDataType : TFieldType;
FFieldNo : Longint; FFieldNo : Longint;
@ -142,8 +172,6 @@ type
FPrecision : Longint; FPrecision : Longint;
FRequired : Boolean; FRequired : Boolean;
FSize : Word; FSize : Word;
FName : String;
FDisplayName : String;
FAttributes : TFieldAttributes; FAttributes : TFieldAttributes;
Function GetFieldClass : TFieldClass; Function GetFieldClass : TFieldClass;
procedure SetAttributes(AValue: TFieldAttributes); procedure SetAttributes(AValue: TFieldAttributes);
@ -151,9 +179,6 @@ type
procedure SetPrecision(const AValue: Longint); procedure SetPrecision(const AValue: Longint);
procedure SetSize(const AValue: Word); procedure SetSize(const AValue: Word);
procedure SetRequired(const AValue: Boolean); procedure SetRequired(const AValue: Boolean);
protected
function GetDisplayName: string; override;
procedure SetDisplayName(const AValue: string); override;
public public
constructor Create(AOwner: TFieldDefs; const AName: string; constructor Create(AOwner: TFieldDefs; const AName: string;
ADataType: TFieldType; ASize: Word; ARequired: Boolean; AFieldNo: Longint); overload; ADataType: TFieldType; ASize: Word; ARequired: Boolean; AFieldNo: Longint); overload;
@ -166,8 +191,6 @@ type
property Required: Boolean read FRequired write SetRequired; property Required: Boolean read FRequired write SetRequired;
Published Published
property Attributes: TFieldAttributes read FAttributes write SetAttributes default []; 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 DataType: TFieldType read FDataType write SetDataType;
property Precision: Longint read FPrecision write SetPrecision; property Precision: Longint read FPrecision write SetPrecision;
property Size: Word read FSize write SetSize; property Size: Word read FSize write SetSize;
@ -175,15 +198,11 @@ type
{ TFieldDefs } { TFieldDefs }
TFieldDefs = class(TOwnedCollection) TFieldDefs = class(TDefCollection)
private private
FUpdated: Boolean;
FHiddenFields : Boolean; FHiddenFields : Boolean;
function GetItem(Index: Longint): TFieldDef; function GetItem(Index: Longint): TFieldDef;
function GetDataset: TDataset;
procedure SetItem(Index: Longint; const AValue: TFieldDef); procedure SetItem(Index: Longint; const AValue: TFieldDef);
protected
procedure SetItemName(AItem: TCollectionItem); override;
public public
constructor Create(ADataSet: TDataSet); constructor Create(ADataSet: TDataSet);
// destructor Destroy; override; // destructor Destroy; override;
@ -194,13 +213,9 @@ type
procedure Assign(FieldDefs: TFieldDefs); overload; procedure Assign(FieldDefs: TFieldDefs); overload;
// procedure Clear; // procedure Clear;
// procedure Delete(Index: Longint); // procedure Delete(Index: Longint);
function Find(const AName: string): TFieldDef;
function IndexOf(const AName: string): Longint;
procedure Update; overload; procedure Update; overload;
Property HiddenFields : Boolean Read FHiddenFields Write FHiddenFields; Property HiddenFields : Boolean Read FHiddenFields Write FHiddenFields;
property Items[Index: Longint]: TFieldDef read GetItem write SetItem; default; property Items[Index: Longint]: TFieldDef read GetItem write SetItem; default;
property Dataset: TDataset read GetDataset;
property Updated: Boolean read FUpdated write FUpdated;
end; end;
{ TField } { TField }
@ -789,13 +804,12 @@ type
ixExpression, ixNonMaintained); ixExpression, ixNonMaintained);
TIndexOptions = set of TIndexOption; TIndexOptions = set of TIndexOption;
TIndexDef = class(TCollectionItem) TIndexDef = class(TNamedItem)
Private Private
FCaseinsFields: string; FCaseinsFields: string;
FDescFields: string; FDescFields: string;
FExpression : String; FExpression : String;
FFields : String; FFields : String;
FName : String;
FOptions : TIndexOptions; FOptions : TIndexOptions;
FSource : String; FSource : String;
protected protected
@ -812,21 +826,18 @@ type
property Fields: string read FFields write FFields; property Fields: string read FFields write FFields;
property CaseInsFields: string read FCaseinsFields write SetCaseInsFields; property CaseInsFields: string read FCaseinsFields write SetCaseInsFields;
property DescFields: string read FDescFields write SetDescFields; property DescFields: string read FDescFields write SetDescFields;
property Name: string read FName write FName;
property Options: TIndexOptions read FOptions write FOptions; property Options: TIndexOptions read FOptions write FOptions;
property Source: string read FSource write FSource; property Source: string read FSource write FSource;
end; end;
{ TIndexDefs } { TIndexDefs }
TIndexDefs = class(TOwnedCollection) TIndexDefs = class(TDefCollection)
Private Private
FUpDated : Boolean;
FDataset : Tdataset;
Function GetItem(Index: Integer): TIndexDef; Function GetItem(Index: Integer): TIndexDef;
Procedure SetItem(Index: Integer; Value: TIndexDef); Procedure SetItem(Index: Integer; Value: TIndexDef);
public public
constructor Create(DataSet: TDataSet); overload; constructor Create(ADataSet: TDataSet); overload;
destructor Destroy; override; destructor Destroy; override;
procedure Add(const Name, Fields: string; Options: TIndexOptions); procedure Add(const Name, Fields: string; Options: TIndexOptions);
Function AddIndexDef: TIndexDef; Function AddIndexDef: TIndexDef;
@ -834,10 +845,8 @@ type
function FindIndexForFields(const Fields: string): TIndexDef; function FindIndexForFields(const Fields: string): TIndexDef;
function GetIndexForFields(const Fields: string; function GetIndexForFields(const Fields: string;
CaseInsensitive: Boolean): TIndexDef; CaseInsensitive: Boolean): TIndexDef;
function IndexOf(const Name: string): Longint;
procedure Update; overload; procedure Update; overload;
Property Items[Index: Integer] : TIndexDef read GetItem write SetItem; default; Property Items[Index: Integer] : TIndexDef read GetItem write SetItem; default;
property Updated: Boolean read FUpdated write FUpdated;
end; end;
{ TCheckConstraint } { TCheckConstraint }
@ -1886,6 +1895,71 @@ begin
Inherited; Inherited;
end; 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 } { TIndexDef }
procedure TIndexDef.SetDescFields(const AValue: string); procedure TIndexDef.SetDescFields(const AValue: string);
@ -1935,8 +2009,8 @@ constructor TIndexDef.Create(Owner: TIndexDefs; const AName, TheFields: string;
TheOptions: TIndexOptions); TheOptions: TIndexOptions);
begin begin
inherited create(Owner);
FName := aname; FName := aname;
inherited create(Owner);
FFields := TheFields; FFields := TheFields;
FOptions := TheOptions; FOptions := TheOptions;
end; end;
@ -1962,11 +2036,10 @@ begin
Inherited SetItem(Index,Value); Inherited SetItem(Index,Value);
end; end;
constructor TIndexDefs.Create(DataSet: TDataSet); constructor TIndexDefs.Create(ADataSet: TDataSet);
begin begin
FDataset := Dataset; inherited create(ADataset, Owner, TIndexDef);
inherited create(Dataset, TIndexDef);
end; end;
@ -1990,14 +2063,8 @@ begin
end; end;
function TIndexDefs.Find(const IndexName: string): TIndexDef; function TIndexDefs.Find(const IndexName: string): TIndexDef;
var i: integer;
begin begin
Result := Nil; Result := (inherited Find(IndexName)) as TIndexDef;
for i := 0 to Count - 1 do
if AnsiSameText(Items[i].Name, IndexName) then begin
Result := Items[i];
Break;
end;
if (Result=Nil) Then if (Result=Nil) Then
DatabaseErrorFmt(SIndexNotFound, [IndexName], FDataSet); DatabaseErrorFmt(SIndexNotFound, [IndexName], FDataSet);
end; end;
@ -2039,26 +2106,14 @@ begin
Result := Last; Result := Last;
end; 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; procedure TIndexDefs.Update;
begin begin
if assigned(Fdataset) then if (not updated) and assigned(Dataset) then
Fdataset.UpdateIndexDefs; begin
Dataset.UpdateIndexDefs;
updated := True;
end;
end; end;
{ TCheckConstraint } { TCheckConstraint }

View File

@ -32,12 +32,11 @@ Constructor TFieldDef.Create(AOwner: TFieldDefs; const AName: string;
ADataType: TFieldType; ASize: Word; ARequired: Boolean; AFieldNo: Longint); ADataType: TFieldType; ASize: Word; ARequired: Boolean; AFieldNo: Longint);
begin begin
Inherited Create(AOwner);
{$ifdef dsdebug } {$ifdef dsdebug }
Writeln('TFieldDef.Create : ',Aname,'(',AFieldNo,')'); Writeln('TFieldDef.Create : ',Aname,'(',AFieldNo,')');
{$endif} {$endif}
FName:=Aname; Name:=Aname;
FDisplayName := ''; Inherited Create(AOwner);
FDatatype:=ADatatype; FDatatype:=ADatatype;
FSize:=ASize; FSize:=ASize;
FRequired:=ARequired; FRequired:=ARequired;
@ -88,7 +87,7 @@ begin
Result.Size:=FSize; Result.Size:=FSize;
Result.Required:=FRequired; Result.Required:=FRequired;
Result.FFieldName:=FName; Result.FFieldName:=FName;
Result.FDisplayLabel:=FDisplayName; Result.FDisplayLabel:=DisplayName;
Result.FFieldNo:=Self.FieldNo; Result.FFieldNo:=Self.FieldNo;
Result.SetFieldType(DataType); Result.SetFieldType(DataType);
Result.FReadOnly:= (faReadOnly in Attributes); Result.FReadOnly:= (faReadOnly in Attributes);
@ -138,22 +137,6 @@ begin
Changed(False); Changed(False);
end; 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; Function TFieldDef.GetFieldClass : TFieldClass;
begin begin
@ -215,28 +198,14 @@ begin
Result := TFieldDef(inherited Items[Index]); Result := TFieldDef(inherited Items[Index]);
end; end;
function TFieldDefs.GetDataset: TDataset;
begin
Result := TDataset(GetOwner);
end;
procedure TFieldDefs.SetItem(Index: Longint; const AValue: TFieldDef); procedure TFieldDefs.SetItem(Index: Longint; const AValue: TFieldDef);
begin begin
inherited Items[Index] := AValue; inherited Items[Index] := AValue;
end; 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); constructor TFieldDefs.Create(ADataset: TDataset);
begin begin
Inherited Create(TPersistent(ADataset), TFieldDef); Inherited Create(ADataset, Owner, TFieldDef);
end; end;
procedure TFieldDefs.Assign(FieldDefs: TFieldDefs); procedure TFieldDefs.Assign(FieldDefs: TFieldDefs);
@ -262,35 +231,14 @@ begin
end; 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; procedure TFieldDefs.Update;
begin begin
DataSet.InitFieldDefs; if not Updated then
begin
DataSet.InitFieldDefs;
Updated := True;
end;
end; end;
Function TFieldDefs.AddFieldDef : TFieldDef; Function TFieldDefs.AddFieldDef : TFieldDef;

View File

@ -632,7 +632,7 @@ begin
FD := TFieldDef.Create(FieldDefs, SQLDA^.SQLVar[x].AliasName, TransType, FD := TFieldDef.Create(FieldDefs, SQLDA^.SQLVar[x].AliasName, TransType,
TransLen, False, (x + 1)); TransLen, False, (x + 1));
if TransType = ftBCD then FD.precision := SQLDA^.SQLVar[x].SQLLen; 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; FieldBinding[FD.FieldNo-1] := x;
end; end;
end; end;
@ -994,7 +994,7 @@ begin
If qry.fields[4].asstring = 'PRIMARY KEY' then options := options + [ixPrimary]; If qry.fields[4].asstring = 'PRIMARY KEY' then options := options + [ixPrimary];
If qry.fields[2].asinteger = 1 then options := options + [ixUnique]; If qry.fields[2].asinteger = 1 then options := options + [ixUnique];
qry.next; 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 begin
Fields := Fields + ';' + trim(qry.Fields[3].asstring); Fields := Fields + ';' + trim(qry.Fields[3].asstring);
qry.next; qry.next;