/components/datadict/fpcodegenerator.pp

http://github.com/graemeg/lazarus · Puppet · 196 lines · 169 code · 27 blank · 0 comment · 3 complexity · 0c39a6aa55293dcca8a462d02ae6c35f MD5 · raw file

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