Browse Source

* Implemented TIndexDef.Assign
* Implemented TDataset.GetIndexDefs (bug 8220)
* Implemented TIndexDefs.GetExpression and SetExpression
* Clear indexdefinitions before update
* added tests for the above and r6481

git-svn-id: trunk@6485 -

joost 18 years ago
parent
commit
4c4c69c89a

+ 34 - 0
fcl/db/dataset.inc

@@ -660,6 +660,40 @@ begin
   //!! To be implemented
   //!! To be implemented
 end;
 end;
 
 
+function TDataSet.GetIndexDefs(IndexDefs: TIndexDefs; IndexTypes: TIndexOptions
+  ): TIndexDefs;
+  
+var i,f : integer;
+    IndexFields : TStrings;
+    
+begin
+  IndexDefs.Update;
+  Result := TIndexDefs.Create(Self);
+  Result.Assign(IndexDefs);
+  i := 0;
+  IndexFields := TStringList.Create;
+  while i < result.Count do
+    begin
+    if (not ((IndexTypes = []) and (result[i].Options = []))) and
+       ((IndexTypes * result[i].Options) = []) then
+      begin
+      result.Delete(i);
+      dec(i);
+      end
+    else
+      begin
+      ExtractStrings([';'],[' '],pchar(result[i].Fields),Indexfields);
+      for f := 0 to IndexFields.Count-1 do if FindField(Indexfields[f]) = nil then
+        begin
+        result.Delete(i);
+        dec(i);
+        break;
+        end;
+      end;
+    inc(i);
+    end;
+  IndexFields.Free;
+end;
 
 
 Function TDataset.GetNextRecord: Boolean;
 Function TDataset.GetNextRecord: Boolean;
 
 

+ 34 - 17
fcl/db/db.pp

@@ -797,13 +797,16 @@ type
     FOptions : TIndexOptions;
     FOptions : TIndexOptions;
     FSource : String;
     FSource : String;
   protected
   protected
+    procedure Assign(Source: TPersistent); override;
+    function GetExpression: string;
     procedure SetCaseInsFields(const AValue: string); virtual;
     procedure SetCaseInsFields(const AValue: string); virtual;
     procedure SetDescFields(const AValue: string);
     procedure SetDescFields(const AValue: string);
+    procedure SetExpression(const AValue: string);
   public
   public
     constructor Create(Owner: TIndexDefs; const AName, TheFields: string;
     constructor Create(Owner: TIndexDefs; const AName, TheFields: string;
       TheOptions: TIndexOptions); overload;
       TheOptions: TIndexOptions); overload;
     destructor Destroy; override;
     destructor Destroy; override;
-    property Expression: string read FExpression;
+    property Expression: string read GetExpression write SetExpression;
     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;
@@ -825,15 +828,12 @@ type
     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;
-    procedure Assign(IndexDefs: TIndexDefs); overload;
-//    procedure Clear;
     function Find(const IndexName: string): TIndexDef;
     function Find(const IndexName: string): TIndexDef;
     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;
     function IndexOf(const Name: string): Longint;
     procedure Update; overload;
     procedure Update; overload;
-//    property Count: Longint read FCount;
     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;
     property Updated: Boolean read FUpdated write FUpdated;
   end;
   end;
@@ -1065,6 +1065,7 @@ type
     Function  GetfieldCount : Integer;
     Function  GetfieldCount : Integer;
     function  GetFieldValues(fieldname : string) : Variant; virtual;
     function  GetFieldValues(fieldname : string) : Variant; virtual;
     function  GetIsIndexField(Field: TField): Boolean; virtual;
     function  GetIsIndexField(Field: TField): Boolean; virtual;
+    function  GetIndexDefs(IndexDefs : TIndexDefs; IndexTypes : TIndexOptions) : TIndexDefs;
     function  GetNextRecords: Longint; virtual;
     function  GetNextRecords: Longint; virtual;
     function  GetNextRecord: Boolean; virtual;
     function  GetNextRecord: Boolean; virtual;
     function  GetPriorRecords: Longint; virtual;
     function  GetPriorRecords: Longint; virtual;
@@ -1885,6 +1886,35 @@ begin
   FDescFields:=AValue;
   FDescFields:=AValue;
 end;
 end;
 
 
+procedure TIndexDef.Assign(Source: TPersistent);
+var idef : TIndexDef;
+begin
+  idef := nil;
+  if Source is TIndexDef then idef := Source as TIndexDef;
+  if Assigned(idef) then
+     begin
+     FName := idef.Name;
+     FFields := idef.Fields;
+     FOptions := idef.Options;
+     FCaseinsFields := idef.CaseInsFields;
+     FDescFields := idef.DescFields;
+     FSource := idef.Source;
+     FExpression := idef.Expression;
+     end
+  else
+    inherited Assign(Source);
+end;
+
+function TIndexDef.GetExpression: string;
+begin
+  Result := FExpression;
+end;
+
+procedure TIndexDef.SetExpression(const AValue: string);
+begin
+  FExpression := AValue;
+end;
+
 procedure TIndexDef.SetCaseInsFields(const AValue: string);
 procedure TIndexDef.SetCaseInsFields(const AValue: string);
 begin
 begin
   if FCaseinsFields=AValue then exit;
   if FCaseinsFields=AValue then exit;
@@ -1950,19 +1980,6 @@ begin
   TIndexDef.Create(Self,Name,Fields,Options);
   TIndexDef.Create(Self,Name,Fields,Options);
 end;
 end;
 
 
-
-procedure TIndexDefs.Assign(IndexDefs: TIndexDefs);
-
-begin
-  //!! To be implemented
-end;
-
-{procedure TIndexDefs.Clear;
-
-begin
-  //!! To be implemented
-end;}
-
 function TIndexDefs.Find(const IndexName: string): TIndexDef;
 function TIndexDefs.Find(const IndexName: string): TIndexDef;
 var i: integer;
 var i: integer;
 begin
 begin

+ 1 - 1
fcl/db/sqldb/interbase/ibconnection.pp

@@ -986,7 +986,7 @@ begin
               'ind.rdb$index_name;');
               'ind.rdb$index_name;');
     open;
     open;
     end;
     end;
-
+  IndexDefs.Clear;
   while not qry.eof do with IndexDefs.AddIndexDef do
   while not qry.eof do with IndexDefs.AddIndexDef do
     begin
     begin
     Name := trim(qry.fields[0].asstring);
     Name := trim(qry.fields[0].asstring);

+ 1 - 1
fcl/db/sqldb/mysql/mysqlconn.inc

@@ -776,7 +776,7 @@ begin
     sql.add('show index from ' +  TableName);
     sql.add('show index from ' +  TableName);
     open;
     open;
     end;
     end;
-
+  IndexDefs.Clear;
   while not qry.eof do with IndexDefs.AddIndexDef do
   while not qry.eof do with IndexDefs.AddIndexDef do
     begin
     begin
     Name := trim(qry.fieldbyname('Key_name').asstring);
     Name := trim(qry.fieldbyname('Key_name').asstring);

+ 1 - 1
fcl/db/sqldb/postgres/pqconnection.pp

@@ -774,7 +774,7 @@ begin
               'ic.relname;');
               'ic.relname;');
     open;
     open;
     end;
     end;
-
+  IndexDefs.Clear;
   while not qry.eof do with IndexDefs.AddIndexDef do
   while not qry.eof do with IndexDefs.AddIndexDef do
     begin
     begin
     Name := trim(qry.fields[0].asstring);
     Name := trim(qry.fields[0].asstring);