fpcgsqlconst.pp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. {$IFNDEF FPC_DOTTEDUNITS}
  13. unit fpcgsqlconst;
  14. {$ENDIF FPC_DOTTEDUNITS}
  15. {$mode objfpc}{$H+}
  16. interface
  17. {$IFDEF FPC_DOTTEDUNITS}
  18. uses
  19. System.Classes, System.SysUtils, Data.CodeGen.Base;
  20. {$ELSE FPC_DOTTEDUNITS}
  21. uses
  22. Classes, SysUtils, fpddCodeGen;
  23. {$ENDIF FPC_DOTTEDUNITS}
  24. Type
  25. { TDDSQLConstOptions }
  26. TMode = (mConst,mTStrings);
  27. TDDSQLConstOptions = Class(TCodeGeneratorOptions)
  28. private
  29. FIDent: String;
  30. FMode: TMode;
  31. procedure SetIdent(const AValue: String);
  32. Public
  33. Constructor Create; override;
  34. Procedure Assign(ASource : TPersistent); override;
  35. Published
  36. Property Identifier : String Read FIDent Write SetIdent;
  37. Property Mode : TMode Read FMode Write FMode;
  38. end;
  39. { TDDSQLConstGenerator }
  40. TDDSQLConstGenerator = Class(TDDCustomCodeGenerator)
  41. Private
  42. FSQL : TStrings;
  43. Protected
  44. Function CreateOptions : TCodeGeneratorOptions; override;
  45. Procedure DoGenerateInterface(Strings: TStrings); override;
  46. Procedure DoGenerateImplementation(Strings: TStrings); override;
  47. function GetSQL: TStrings; override;
  48. procedure SetSQL(const AValue: TStrings); override;
  49. Function SQLOptions : TDDSQLConstOptions;
  50. Public
  51. Constructor Create(AOwner : TComponent); override;
  52. Destructor Destroy; override;
  53. Class Function NeedsSQL : Boolean; override;
  54. Class Function NeedsFieldDefs : Boolean; override;
  55. end;
  56. Const
  57. SSQLConst = 'SQLConst';
  58. Resourcestring
  59. SSQLConstDescr = 'Generate Pascal constant/Stringlist from SQL';
  60. implementation
  61. { TDDSQLConstOptions }
  62. procedure TDDSQLConstOptions.SetIdent(const AValue: String);
  63. begin
  64. if FIDent=AValue then exit;
  65. If Not IsValidIdent(AValue) then
  66. Raise ECodeGenerator.CreateFmt(SErrInvalidIdentifier,[AValue]);
  67. FIDent:=AValue;
  68. end;
  69. constructor TDDSQLConstOptions.Create;
  70. begin
  71. Inherited;
  72. FIdent:='SQL'; // Do not localize
  73. end;
  74. procedure TDDSQLConstOptions.Assign(ASource: TPersistent);
  75. Var
  76. CO: TDDSQLConstOptions;
  77. begin
  78. If ASource is TDDSQLConstOptions then
  79. begin
  80. CO:=ASource as TDDSQLConstOptions;
  81. FIDent:=CO.FIdent;
  82. FMode:=CO.FMode;
  83. end;
  84. inherited Assign(ASource);
  85. end;
  86. { TDDSQLConstGenerator }
  87. function TDDSQLConstGenerator.CreateOptions: TCodeGeneratorOptions;
  88. begin
  89. Result:=TDDSQLConstOptions.Create;
  90. end;
  91. procedure TDDSQLConstGenerator.DoGenerateInterface(Strings: TStrings);
  92. Var
  93. S : String;
  94. I,L : Integer;
  95. begin
  96. If (SQLOptions.Mode=mConst) then
  97. begin
  98. Addln(Strings,'Const');
  99. L:=Length(SQLOPtions.Identifier);
  100. IncIndent;
  101. try
  102. For I:=0 to FSQL.Count-1 do
  103. begin
  104. If (I=0) then
  105. S:=SQLOPtions.Identifier+' = '
  106. else
  107. S:=StringOfChar(' ',L)+' +';
  108. S:=S+CreateString(FSQL[i]);
  109. If (I=FSQL.Count-1) then
  110. S:=S+';'
  111. else
  112. S:=S+'+sLineBreak';
  113. Addln(Strings,S);
  114. end;
  115. finally
  116. DecIndent;
  117. end;
  118. end;
  119. end;
  120. procedure TDDSQLConstGenerator.DoGenerateImplementation(Strings: TStrings);
  121. Var
  122. S : String;
  123. I,L : Integer;
  124. begin
  125. If (SQLOptions.Mode=mTStrings) then
  126. begin
  127. Addln(Strings,'With %s do',[SQLOPtions.Identifier]);
  128. IncIndent;
  129. try
  130. Addln(Strings,'begin');
  131. For I:=0 to FSQL.Count-1 do
  132. Addln(Strings,'Add(%s);',[CreateString(FSQL[i])]);
  133. Addln(Strings,'end;');
  134. finally
  135. DecIndent;
  136. end;
  137. end;
  138. end;
  139. function TDDSQLConstGenerator.GetSQL: TStrings;
  140. begin
  141. Result:=FSQL;
  142. end;
  143. procedure TDDSQLConstGenerator.SetSQL(const AValue: TStrings);
  144. begin
  145. FSQL.Assign(AValue);
  146. end;
  147. function TDDSQLConstGenerator.SQLOptions: TDDSQLConstOptions;
  148. begin
  149. Result:=CodeOptions as TDDSQLConstOptions;
  150. end;
  151. constructor TDDSQLConstGenerator.Create(AOwner: TComponent);
  152. begin
  153. inherited Create(AOwner);
  154. FSQL:=TSTringList.Create;
  155. end;
  156. destructor TDDSQLConstGenerator.Destroy;
  157. begin
  158. FreeAndNil(FSQL);
  159. inherited Destroy;
  160. end;
  161. class function TDDSQLConstGenerator.NeedsSQL: Boolean;
  162. begin
  163. Result:=True;
  164. end;
  165. class function TDDSQLConstGenerator.NeedsFieldDefs: Boolean;
  166. begin
  167. Result:=False;
  168. end;
  169. Initialization
  170. RegisterCodeGenerator(SSQLConst, SSQLConstDescr, TDDSQLConstGenerator);
  171. Finalization
  172. UnRegisterCodeGenerator(TDDSQLConstGenerator);
  173. end.