fpcgsqlconst.pp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2007 by Michael Van Canneyt, member of the
  4. Free Pascal development team
  5. Data Dictionary Code Generator Implementation.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit fpcgsqlconst;
  13. {$mode objfpc}{$H+}
  14. interface
  15. uses
  16. Classes, SysUtils, fpddCodeGen;
  17. Type
  18. { TDDSQLConstOptions }
  19. TMode = (mConst,mTStrings);
  20. TDDSQLConstOptions = Class(TCodeGeneratorOptions)
  21. private
  22. FIDent: String;
  23. FMode: TMode;
  24. procedure SetIdent(const AValue: String);
  25. Public
  26. Constructor Create; override;
  27. Procedure Assign(ASource : TPersistent); override;
  28. Published
  29. Property Identifier : String Read FIDent Write SetIdent;
  30. Property Mode : TMode Read FMode Write FMode;
  31. end;
  32. { TDDSQLConstGenerator }
  33. TDDSQLConstGenerator = Class(TDDCustomCodeGenerator)
  34. Private
  35. FSQL : TStrings;
  36. Protected
  37. Function CreateOptions : TCodeGeneratorOptions; override;
  38. Procedure DoGenerateInterface(Strings: TStrings); override;
  39. Procedure DoGenerateImplementation(Strings: TStrings); override;
  40. function GetSQL: TStrings; override;
  41. procedure SetSQL(const AValue: TStrings); override;
  42. Function SQLOptions : TDDSQLConstOptions;
  43. Public
  44. Constructor Create(AOwner : TComponent); override;
  45. Destructor Destroy; override;
  46. Class Function NeedsSQL : Boolean; override;
  47. Class Function NeedsFieldDefs : Boolean; override;
  48. end;
  49. Const
  50. SSQLConst = 'SQLConst';
  51. Resourcestring
  52. SSQLConstDescr = 'Generate Pascal constant/Stringlist from SQL';
  53. implementation
  54. { TDDSQLConstOptions }
  55. procedure TDDSQLConstOptions.SetIdent(const AValue: String);
  56. begin
  57. if FIDent=AValue then exit;
  58. If Not IsValidIdent(AValue) then
  59. Raise ECodeGenerator.CreateFmt(SErrInvalidIdentifier,[AValue]);
  60. FIDent:=AValue;
  61. end;
  62. constructor TDDSQLConstOptions.Create;
  63. begin
  64. Inherited;
  65. FIdent:='SQL'; // Do not localize
  66. end;
  67. procedure TDDSQLConstOptions.Assign(ASource: TPersistent);
  68. Var
  69. CO: TDDSQLConstOptions;
  70. begin
  71. If ASource is TDDSQLConstOptions then
  72. begin
  73. CO:=ASource as TDDSQLConstOptions;
  74. FIDent:=CO.FIdent;
  75. FMode:=CO.FMode;
  76. end;
  77. inherited Assign(ASource);
  78. end;
  79. { TDDSQLConstGenerator }
  80. function TDDSQLConstGenerator.CreateOptions: TCodeGeneratorOptions;
  81. begin
  82. Result:=TDDSQLConstOptions.Create;
  83. end;
  84. procedure TDDSQLConstGenerator.DoGenerateInterface(Strings: TStrings);
  85. Var
  86. S : String;
  87. I,L : Integer;
  88. begin
  89. If (SQLOptions.Mode=mConst) then
  90. begin
  91. Addln(Strings,'Const');
  92. L:=Length(SQLOPtions.Identifier);
  93. IncIndent;
  94. try
  95. For I:=0 to FSQL.Count-1 do
  96. begin
  97. If (I=0) then
  98. S:=SQLOPtions.Identifier+' = '
  99. else
  100. S:=StringOfChar(' ',L)+' +';
  101. S:=S+CreateString(FSQL[i]);
  102. If (I=FSQL.Count-1) then
  103. S:=S+';'
  104. else
  105. S:=S+'+sLineBreak';
  106. Addln(Strings,S);
  107. end;
  108. finally
  109. DecIndent;
  110. end;
  111. end;
  112. end;
  113. procedure TDDSQLConstGenerator.DoGenerateImplementation(Strings: TStrings);
  114. Var
  115. S : String;
  116. I,L : Integer;
  117. begin
  118. If (SQLOptions.Mode=mTStrings) then
  119. begin
  120. Addln(Strings,'With %s do',[SQLOPtions.Identifier]);
  121. IncIndent;
  122. try
  123. Addln(Strings,'begin');
  124. For I:=0 to FSQL.Count-1 do
  125. Addln(Strings,'Add(%s);',[CreateString(FSQL[i])]);
  126. Addln(Strings,'end;');
  127. finally
  128. DecIndent;
  129. end;
  130. end;
  131. end;
  132. function TDDSQLConstGenerator.GetSQL: TStrings;
  133. begin
  134. Result:=FSQL;
  135. end;
  136. procedure TDDSQLConstGenerator.SetSQL(const AValue: TStrings);
  137. begin
  138. FSQL.Assign(AValue);
  139. end;
  140. function TDDSQLConstGenerator.SQLOptions: TDDSQLConstOptions;
  141. begin
  142. Result:=CodeOptions as TDDSQLConstOptions;
  143. end;
  144. constructor TDDSQLConstGenerator.Create(AOwner: TComponent);
  145. begin
  146. inherited Create(AOwner);
  147. FSQL:=TSTringList.Create;
  148. end;
  149. destructor TDDSQLConstGenerator.Destroy;
  150. begin
  151. FreeAndNil(FSQL);
  152. inherited Destroy;
  153. end;
  154. class function TDDSQLConstGenerator.NeedsSQL: Boolean;
  155. begin
  156. Result:=True;
  157. end;
  158. class function TDDSQLConstGenerator.NeedsFieldDefs: Boolean;
  159. begin
  160. Result:=False;
  161. end;
  162. Initialization
  163. RegisterCodeGenerator(SSQLConst, SSQLConstDescr, TDDSQLConstGenerator);
  164. Finalization
  165. UnRegisterCodeGenerator(TDDSQLConstGenerator);
  166. end.