Browse Source

* Added import routines for domains/seqences, and populate code

git-svn-id: trunk@11608 -
michael 17 years ago
parent
commit
6a5954201d
2 changed files with 232 additions and 10 deletions
  1. 208 10
      packages/fcl-db/src/codegen/fpddpopcode.pp
  2. 24 0
      packages/fcl-db/src/datadict/fpdatadict.pp

+ 208 - 10
packages/fcl-db/src/codegen/fpddpopcode.pp

@@ -8,7 +8,8 @@ uses
   Classes, SysUtils, typinfo, fpdatadict, db;
   Classes, SysUtils, typinfo, fpdatadict, db;
 
 
 Type
 Type
-  TDDCodeGenOption = (dcoFields,dcoIndexes,dcoProcedurePerTable,dcoUseWith,dcoClassDecl);
+  TDDCodeGenOption = (dcoFields,dcoIndexes,dcoProcedurePerTable,dcoUseWith,
+                      dcoClassDecl,dcoGenerators,dcoDomains,dcoMergeDomains);
   TDDCodeGenOptions = Set of TDDCodeGenoption;
   TDDCodeGenOptions = Set of TDDCodeGenoption;
   
   
   { TFPDDPopulateCodeGenerator }
   { TFPDDPopulateCodeGenerator }
@@ -45,11 +46,24 @@ Type
     Function DoTable (Const ATable : TDDtableDef) : Boolean; virtual;
     Function DoTable (Const ATable : TDDtableDef) : Boolean; virtual;
     procedure CreateTableCode(T: TDDTableDef; Lines: TStrings);
     procedure CreateTableCode(T: TDDTableDef; Lines: TStrings);
     procedure AddTableVars(Lines: TStrings);
     procedure AddTableVars(Lines: TStrings);
+    procedure AddDomainVars(Lines: TStrings);
+    procedure AddSequenceVars(Lines: TStrings);
     procedure DoTableHeader(ATable: TDDTableDef; Lines: TStrings);
     procedure DoTableHeader(ATable: TDDTableDef; Lines: TStrings);
     procedure DoTableFooter(ATable: TDDTableDef; Lines: TStrings);
     procedure DoTableFooter(ATable: TDDTableDef; Lines: TStrings);
     // Field code
     // Field code
     Function DoField (Const ATable : TDDtableDef; Const AField : TDDFieldDef) : Boolean; virtual;
     Function DoField (Const ATable : TDDtableDef; Const AField : TDDFieldDef) : Boolean; virtual;
     procedure CreateFieldCode(ATable: TDDTableDef; AField: TDDFieldDef;  Lines: TStrings);
     procedure CreateFieldCode(ATable: TDDTableDef; AField: TDDFieldDef;  Lines: TStrings);
+    // Index code
+    Function DoIndex (Const ATable : TDDtableDef; Const AIndex : TDDIndexDef) : Boolean; virtual;
+    procedure CreateIndexCode(ATable: TDDTableDef; AIndex: TDDIndexDef;  Lines: TStrings);
+    // Sequence code
+    Procedure WriteSequences(Const ASequences : TDDSequenceDefs; Lines :TStrings);
+    Function DoSequence (Const ASequence : TDDSequenceDef) : Boolean; virtual;
+    procedure CreateSequenceCode(ASequence: TDDSequenceDef;  Lines: TStrings);
+    // Domain code
+    Procedure WriteDomains(Const ADomains : TDDDomainDefs; Lines :TStrings);
+    Function DoDomain (Const ADomain : TDDDomainDef) : Boolean; virtual;
+    procedure CreateDomainCode(ADomain: TDDDomainDef;  Lines: TStrings);
   Public
   Public
     Constructor Create(AOwner : TComponent); override;
     Constructor Create(AOwner : TComponent); override;
     Destructor Destroy; override;
     Destructor Destroy; override;
@@ -179,6 +193,24 @@ begin
   AddLine('T : TDDTableDef;',lines);
   AddLine('T : TDDTableDef;',lines);
   If dcoFields in Options then
   If dcoFields in Options then
     AddLine('F : TDDFieldDef;',lines);
     AddLine('F : TDDFieldDef;',lines);
+  If dcoIndexes in Options then
+    AddLine('ID : TDDIndexDef;',lines);
+  Undent;
+end;
+
+procedure TFPDDPopulateCodeGenerator.AddDomainVars(Lines: TStrings);
+begin
+  AddLine('Var',Lines);
+  Indent;
+  AddLine('D : TDDDomainDef;',lines);
+  Undent;
+end;
+
+procedure TFPDDPopulateCodeGenerator.AddSequenceVars(Lines: TStrings);
+begin
+  AddLine('Var',Lines);
+  Indent;
+  AddLine('D : TDDSequenceDef;',lines);
   Undent;
   Undent;
 end;
 end;
 
 
@@ -234,6 +266,10 @@ end;
 
 
 procedure TFPDDPopulateCodeGenerator.CreateFieldCode(ATable : TDDTableDef; AField : TDDFieldDef; Lines: TStrings);
 procedure TFPDDPopulateCodeGenerator.CreateFieldCode(ATable : TDDTableDef; AField : TDDFieldDef; Lines: TStrings);
 
 
+Var
+  I : Integer;
+  S : String;
+
 begin
 begin
   AddLine(Format('F:=T.Fields.AddField(''%s'');',[AField.FieldName]),Lines);
   AddLine(Format('F:=T.Fields.AddField(''%s'');',[AField.FieldName]),Lines);
   If (dcoUseWith in Options) then
   If (dcoUseWith in Options) then
@@ -251,17 +287,24 @@ begin
   AddStringProperty('F','DBDefault',AField.DBDefault,Lines);
   AddStringProperty('F','DBDefault',AField.DBDefault,Lines);
   AddStringProperty('F','DefaultExpression',AField.DefaultExpression,Lines);
   AddStringProperty('F','DefaultExpression',AField.DefaultExpression,Lines);
   AddStringProperty('F','DisplayLabel',AField.DisplayLabel,Lines);
   AddStringProperty('F','DisplayLabel',AField.DisplayLabel,Lines);
+  AddStringProperty('F','DomainName',AField.DomainName,Lines);
   If (AField.DisplayWidth<>0) then
   If (AField.DisplayWidth<>0) then
-    AddProperty('F','DisplayWidth',IntToStr(AField.DisplayWidth),Lines);
+    AddProperty('F','DisplayWidth1',IntToStr(AField.DisplayWidth),Lines);
   AddStringProperty('F','Constraint',AField.Constraint,Lines);
   AddStringProperty('F','Constraint',AField.Constraint,Lines);
   AddProperty('F','ReadOnly',AField.ReadOnly,Lines);
   AddProperty('F','ReadOnly',AField.ReadOnly,Lines);
-  AddProperty('F','Required',AField.Required,Lines);
+  If (dcoMergeDomains in Options) then
+    AddProperty('F','Required',AField.FieldIsRequired,Lines)
+  else
+    AddProperty('F','Required',AField.Required,Lines);
   AddProperty('F','Visible',AField.Visible,Lines);
   AddProperty('F','Visible',AField.Visible,Lines);
   If (AField.Size<>0) then
   If (AField.Size<>0) then
     AddProperty('F','Size',IntToStr(AField.Size),Lines);
     AddProperty('F','Size',IntToStr(AField.Size),Lines);
   If (AField.Precision<>0) then
   If (AField.Precision<>0) then
     AddProperty('F','Precision',IntToStr(AField.Precision),Lines);
     AddProperty('F','Precision',IntToStr(AField.Precision),Lines);
   AddStringProperty('F','Hint',AField.Hint,Lines);
   AddStringProperty('F','Hint',AField.Hint,Lines);
+  I:=Integer(AField.ProviderFlags);
+  S:=SetToString(PTypeInfo(TypeInfo(TProviderFlags)),I,True);
+  AddProperty('F','ProviderFlags',S,Lines);
   If (dcoUseWith in Options) then
   If (dcoUseWith in Options) then
      begin
      begin
      AddLine('end;',Lines);
      AddLine('end;',Lines);
@@ -269,6 +312,149 @@ begin
      end;
      end;
 end;
 end;
 
 
+function TFPDDPopulateCodeGenerator.DoIndex(const ATable: TDDtableDef;
+  const AIndex: TDDIndexDef): Boolean;
+begin
+  Result:=Assigned(ATable) and Assigned(AIndex);
+end;
+
+procedure TFPDDPopulateCodeGenerator.CreateIndexCode(ATable: TDDTableDef;
+  AIndex: TDDIndexDef; Lines: TStrings);
+
+Var
+  S : string;
+  I : Integer;
+
+begin
+  AddLine(Format('ID:=T.Indexes.AddIndex(''%s'');',[AIndex.IndexName]),Lines);
+  If (dcoUseWith in Options) then
+     begin
+     AddLine('With ID do',Lines);
+     Indent;
+     AddLine('begin',Lines);
+     end;
+  AddStringProperty('ID','Expression',AIndex.Expression,Lines);
+  AddStringProperty('ID','Fields',AIndex.Fields,Lines);
+  AddStringProperty('ID','CaseInsFields',AIndex.CaseInsFields,Lines);
+  AddStringProperty('ID','DescFields',AIndex.DescFields,Lines);
+  AddStringProperty('ID','Source',AIndex.Source,Lines);
+  I:=Integer(AIndex.Options);
+  S:=SetToString(PTypeInfo(TypeInfo(TIndexOptions)),I,True);
+  AddProperty('ID','Options',S,Lines);
+  If (dcoUseWith in Options) then
+     begin
+     AddLine('end;',Lines);
+     Undent;
+     end;
+
+end;
+
+procedure TFPDDPopulateCodeGenerator.WriteSequences(
+  const ASequences: TDDSequenceDefs; Lines: TStrings);
+
+Var
+  I : Integer;
+  S : TDDSequenceDef;
+
+begin
+  If (dcoProcedurePerTable in Options) then
+    begin
+    AddProcedure('PopulateSequences',Lines);
+    AddSequenceVars(Lines);
+    AddLine('',Lines);
+    AddLine('begin',Lines);
+    Indent;
+    end;
+  For I:=0 to ASequences.Count-1 do
+    begin
+    S:=ASequences[i];
+    If DoSequence(S) then
+      CreateSequenceCode(S,Lines);
+    end;
+  If (dcoProcedurePerTable in Options) then
+    EndProcedure(Lines);
+end;
+
+function TFPDDPopulateCodeGenerator.DoSequence(const ASequence: TDDSequenceDef): Boolean;
+begin
+  Result:=Assigned(ASequence);
+end;
+
+procedure TFPDDPopulateCodeGenerator.CreateSequenceCode(ASequence: TDDSequenceDef; Lines: TStrings);
+begin
+  AddLine(Format('S:=%s.Sequences.AddSequence(''%s'');',[FDDV,ASequence.SequenceName]),Lines);
+  If (dcoUseWith in Options) then
+     begin
+     AddLine('With S do',Lines);
+     Indent;
+     AddLine('begin',Lines);
+     end;
+  If (ASequence.StartValue<>0) then
+    AddProperty('S','StartValue',IntToStr(ASequence.StartValue),Lines);
+  If (ASequence.Increment<>0) then
+    AddProperty('S','Increment',IntToStr(ASequence.Increment),Lines);
+  If (dcoUseWith in Options) then
+     begin
+     AddLine('end;',Lines);
+     Indent;
+     end;
+end;
+
+procedure TFPDDPopulateCodeGenerator.WriteDomains(const ADomains: TDDDomainDefs; Lines :TStrings);
+
+Var
+  I : Integer;
+  D : TDDDomainDef;
+
+begin
+  If (dcoProcedurePerTable in Options) then
+    begin
+    AddProcedure('PopulateDomains',Lines);
+    AddDomainVars(Lines);
+    AddLine('',Lines);
+    AddLine('begin',Lines);
+    Indent;
+    end;
+  For I:=0 to FDD.Domains.Count-1 do
+    begin
+    D:=FDD.Domains[i];
+    If DoDomain(D) then
+      CreateDomainCode(D,Lines);
+    end;
+  If (dcoProcedurePerTable in Options) then
+    EndProcedure(Lines);
+end;
+
+function TFPDDPopulateCodeGenerator.DoDomain(const ADomain: TDDDomainDef
+  ): Boolean;
+begin
+  Result:=Assigned(ADomain);
+end;
+
+procedure TFPDDPopulateCodeGenerator.CreateDomainCode(ADomain: TDDDomainDef;
+  Lines: TStrings);
+begin
+  AddLine(Format('D:=%s.Domains.AddDomain(''%s'');',[FDDV,ADomain.DomainName]),Lines);
+  If (dcoUseWith in Options) then
+     begin
+     AddLine('With D do',Lines);
+     Indent;
+     AddLine('begin',Lines);
+     end;
+  if (ADomain.FieldType<>ftUnknown) then
+    AddProperty('D','FieldType',GetEnumName(TypeInfo(TFieldType),Ord(ADomain.FieldType)),Lines);
+  AddProperty('D','Required',ADomain.Required,Lines);
+  If (ADomain.Size<>0) then
+    AddProperty('D','Size',IntToStr(ADomain.Size),Lines);
+  If (ADomain.Precision<>0) then
+    AddProperty('D','Precision',IntToStr(ADomain.Precision),Lines);
+  If (dcoUseWith in Options) then
+     begin
+     AddLine('end;',Lines);
+     Indent;
+     end;
+end;
+
 procedure TFPDDPopulateCodeGenerator.CreateHeader(Lines: TStrings);
 procedure TFPDDPopulateCodeGenerator.CreateHeader(Lines: TStrings);
 
 
 begin
 begin
@@ -315,7 +501,8 @@ procedure TFPDDPopulateCodeGenerator.CreateTableCode(T : TDDTableDef; Lines: TSt
 Var
 Var
   I : Integer;
   I : Integer;
   F : TDDFieldDef;
   F : TDDFieldDef;
-  
+  Id : TDDindexDef;
+
 begin
 begin
   DoTableHeader(T,Lines);
   DoTableHeader(T,Lines);
   try
   try
@@ -326,6 +513,13 @@ begin
         If DoField(T,F) then
         If DoField(T,F) then
           CreateFieldcode(T,F,Lines);
           CreateFieldcode(T,F,Lines);
         end;
         end;
+    If dcoIndexes in Options then
+      For I:=0 to T.Indexes.Count-1 Do
+        begin
+        ID:=T.Indexes[I];
+        If DoIndex(T,ID) then
+          CreateIndexCode(T,ID,Lines);
+        end;
   Finally
   Finally
     DoTableFooter(T,Lines);
     DoTableFooter(T,Lines);
   end;
   end;
@@ -369,12 +563,16 @@ begin
   try
   try
     CreateHeader(Lines);
     CreateHeader(Lines);
     Try
     Try
-    For I:=0 to FDD.Tables.Count-1 do
-      begin
-      T:=FDD.Tables[i];
-      If DoTable(T) then
-        CreateTableCode(T,Lines);
-      end;
+      If (FDD.Domains.Count>0) then
+        WriteDomains(FDD.Domains,Lines);
+      If (FDD.Sequences.Count>0) then
+        WriteSequences(FDD.Sequences,Lines);
+      For I:=0 to FDD.Tables.Count-1 do
+        begin
+        T:=FDD.Tables[i];
+        If DoTable(T) then
+          CreateTableCode(T,Lines);
+        end;
     Finally
     Finally
       CreateFooter(Lines);
       CreateFooter(Lines);
     end;
     end;

+ 24 - 0
packages/fcl-db/src/datadict/fpdatadict.pp

@@ -71,6 +71,8 @@ Type
     Constructor Create(ACollection : TCollection); override;
     Constructor Create(ACollection : TCollection); override;
     Function FieldDefs : TDDFieldDefs;
     Function FieldDefs : TDDFieldDefs;
     Function DataDictionary : TFPDataDictionary;
     Function DataDictionary : TFPDataDictionary;
+    // Will return True if the field or the domain it is based on is required
+    Function FieldIsRequired : Boolean;
     Procedure ResolveDomain(ErrorOnFail : Boolean);
     Procedure ResolveDomain(ErrorOnFail : Boolean);
     Procedure ImportFromField(F: TField; Existing : Boolean = True);
     Procedure ImportFromField(F: TField; Existing : Boolean = True);
     Procedure ApplyToField(F : TField);
     Procedure ApplyToField(F : TField);
@@ -552,6 +554,8 @@ Type
     Procedure Disconnect ; virtual; abstract;
     Procedure Disconnect ; virtual; abstract;
     Function GetTableList(List : TStrings) : Integer; virtual; abstract;
     Function GetTableList(List : TStrings) : Integer; virtual; abstract;
     Function ImportFields(Table : TDDTableDef) : Integer; virtual; abstract;
     Function ImportFields(Table : TDDTableDef) : Integer; virtual; abstract;
+    Function ImportDomains(Domains : TDDDomainDefs) : Integer; virtual;
+    Function ImportSequences(Sequences : TDDSequenceDefs) : Integer; virtual;
     // Override depending on capabilities
     // Override depending on capabilities
     Procedure CreateTable(Table : TDDTableDef); virtual;
     Procedure CreateTable(Table : TDDTableDef); virtual;
     // Should not open the dataset.
     // Should not open the dataset.
@@ -1003,6 +1007,16 @@ begin
     Result:=Nil;
     Result:=Nil;
 end;
 end;
 
 
+function TDDFieldDef.FieldIsRequired: Boolean;
+begin
+  Result:=Required;
+  If (Not Result) and (DomainName<>'') then
+    begin
+    ResolveDomain(True);
+    Result:=Domain.Required;
+    end;
+end;
+
 procedure TDDFieldDef.ResolveDomain(ErrorOnFail : Boolean);
 procedure TDDFieldDef.ResolveDomain(ErrorOnFail : Boolean);
 
 
 Var
 Var
@@ -1782,6 +1796,16 @@ begin
   Result:=[];
   Result:=[];
 end;
 end;
 
 
+function TFPDDEngine.ImportDomains(Domains: TDDDomainDefs): Integer;
+begin
+  Domains.Clear;
+end;
+
+function TFPDDEngine.ImportSequences(Sequences: TDDSequenceDefs): Integer;
+begin
+  Sequences.Clear;
+end;
+
 procedure TFPDDEngine.CreateTable(Table: TDDTableDef);
 procedure TFPDDEngine.CreateTable(Table: TDDTableDef);
 begin
 begin
   Raise EDataDict.CreateFmt(SErrCreateTableNotSupported,[DBType]);
   Raise EDataDict.CreateFmt(SErrCreateTableNotSupported,[DBType]);