PageRenderTime 46ms CodeModel.GetById 16ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 0ms

/components/datadict/fpcodegenerator.pp

http://github.com/graemeg/lazarus
Pascal | 196 lines | 166 code | 27 blank | 3 comment | 3 complexity | 0c39a6aa55293dcca8a462d02ae6c35f MD5 | raw file
  1unit fpcodegenerator;
  2
  3{$mode objfpc}{$H+}
  4
  5interface
  6
  7uses
  8  Classes, SysUtils, LazUTF8, db, fpddCodegen, fpDataDict, Controls;
  9  
 10Type
 11  { TFPCodeGenerator }
 12
 13  TFPCodeGenerator = Class(TComponent)
 14  Private
 15    FDataset : TDataset;
 16    FFieldDefs : TDDFieldDefs;
 17    FFileName: String;
 18    FGenerator : TDDCustomCodeGenerator;
 19    FShowResult: Boolean;
 20    FSQL: TStrings;
 21    FTableNameHint: String;
 22    function SelectGenerator: TCodeGeneratorItem;
 23    procedure SetDataset(const AValue: TDataset);
 24    procedure SetFieldDefs(const AValue: TDDFieldDefs);
 25    procedure SetSQL(const AValue: TStrings);
 26    function SetupGenerator : Boolean;
 27    procedure ShowCode(L: TStrings);
 28  public
 29    Constructor Create(AOWner : TComponent); override;
 30    Destructor Destroy; override;
 31    Function Execute : Boolean;
 32    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
 33  Published
 34    Property Dataset : TDataset Read FDataset Write SetDataset;
 35    Property DDFieldDefs : TDDFieldDefs Read FFieldDefs Write SetFieldDefs;
 36    Property SQL : TStrings Read FSQL Write SetSQL;
 37    Property ShowResult : Boolean Read FShowResult Write FShowResult default true;
 38    Property FileName : String Read FFileName Write FFileName;
 39    Property TableNameHint : String Read FTableNameHint Write FTableNameHint;
 40  end;
 41
 42implementation
 43
 44uses typinfo, forms, frmSelectCodeGenerator, frmgeneratedcode, frmBaseConfigCodeGenerator;
 45
 46{ TFPCodeGenerator }
 47
 48procedure TFPCodeGenerator.SetDataset(const AValue: TDataset);
 49begin
 50  If (AValue<>FDataset) then
 51    begin
 52    If Assigned(FDataset) then
 53      FDataset.RemoveFreeNotification(Self);
 54    FDataset:=AValue;
 55    If Assigned(FDataset) then
 56      FDataset.FreeNotification(Self);
 57    end;
 58end;
 59
 60procedure TFPCodeGenerator.SetFieldDefs(const AValue: TDDFieldDefs);
 61begin
 62  FFieldDefs.Assign(AVAlue);
 63end;
 64
 65procedure TFPCodeGenerator.SetSQL(const AValue: TStrings);
 66begin
 67  if FSQL=AValue then exit;
 68  FSQL.Assign(AValue);
 69end;
 70
 71constructor TFPCodeGenerator.Create(AOWner: TComponent);
 72begin
 73  inherited Create(AOWner);
 74  FShowResult:=True;
 75  FFieldDefs:=TDDFieldDefs.Create('dummy');
 76  FSQL:=TStringList.Create;
 77  If (AOwner is TDataset) then
 78    Dataset:=AOwner as TDataset;
 79end;
 80
 81destructor TFPCodeGenerator.Destroy;
 82begin
 83  FreeAndNil(FFieldDefs);
 84  FreeAndNil(FSQL);
 85  inherited Destroy;
 86end;
 87
 88function TFPCodeGenerator.SelectGenerator : TCodeGeneratorItem;
 89
 90begin
 91  Result:=Nil;
 92  With TSelectCodeGeneratorForm.Create(Application) do
 93    try
 94      HaveSQL:=SQL.Count<>0;
 95      HaveFields:=Self.Dataset<>Nil;
 96      If (ShowModal=mrOK) then
 97        Result:=SelectedGenerator;
 98    finally
 99      Free;
100    end;
101end;
102
103Function TFPCodeGenerator.SetupGenerator : boolean;
104
105Var
106  FP : TFieldPropDefs;
107  F : TBaseConfigGeneratorForm;
108
109begin
110  If FGenerator.NeedsFieldDefs then
111    begin
112    FP:=FGenerator.Fields;
113    if Assigned(Dataset) then
114      FP.FromDataSet(Dataset)
115    else
116      FP.FromDDFieldDefs(FFieldDefs);
117    end;
118  If FGenerator.NeedsSQL then
119    FGenerator.SQL:=Self.SQL;
120  If (TableNameHint<>'') and IsPublishedProp(FGenerator.CodeOptions,'TableName') then
121    SetStrProp(FGenerator.CodeOptions,'TableName',TableNameHint);
122  F:=TBaseConfigGeneratorForm.Create(Application);
123  try
124    F.ShowExtra:=True;
125    F.FileName:=Self.FileName;
126    F.ShowResult:=Self.ShowResult;
127    F.Generator:=Self.FGenerator;
128    Result:=(F.ShowModal=mrOK);
129    If result then
130      begin
131      Self.FileName   := F.FileName;
132      Self.ShowResult := F.ShowResult;
133      end;
134  finally
135    F.Free
136  end;
137end;
138
139Procedure TFPCodeGenerator.ShowCode(L : TStrings);
140
141begin
142  With TCodeForm.Create(Self) do
143    try
144      Code:=L;
145      ShowModal;
146    Finally
147      Free;
148    end;
149end;
150
151
152function TFPCodeGenerator.Execute: Boolean;
153
154Var
155  G : TCodeGeneratorItem;
156  L : TStrings;
157
158begin
159  G:=SelectGenerator;
160  Result:=(G<>Nil);
161  If Result then
162    begin
163    FGenerator:=G.GeneratorClass.Create(Self);
164    Try
165      if SetupGenerator then
166        begin
167        L:=TStringList.Create;
168        try
169          FGenerator.GenerateCode(L);
170          If (FFileName<>'') then
171            L.SaveToFile(UTF8ToSys(FFileName));
172          If ShowResult then
173            ShowCode(L);
174        finally
175          L.Free;
176        end;
177        end;
178    Finally
179      FreeAndNil(FGenerator);
180    end;
181    end;
182end;
183
184procedure TFPCodeGenerator.Notification(AComponent: TComponent;
185  Operation: TOperation);
186begin
187  inherited Notification(AComponent, Operation);
188  If (Operation=opRemove) then
189    begin
190    If (AComponent=FDataset) then
191      FDataset:=Nil;
192    end;
193end;
194
195end.
196