createsql.pas 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. program createsql;
  2. {$mode objfpc}{$H+}
  3. uses
  4. {$IFDEF UNIX}{$IFDEF UseCThreads}
  5. cthreads,
  6. {$ENDIF}{$ENDIF}
  7. typinfo, Classes, SysUtils, CustApp, db, sqldb, fpdatadict,
  8. fpddfb,fpddpq,fpddOracle,fpddsqlite3,fpddmysql40,fpddmysql41,fpddmysql50, fpddodbc,
  9. strutils;
  10. type
  11. { TGenSQLApplication }
  12. TGenSQLApplication = class(TCustomApplication)
  13. private
  14. function CreateSQLEngine(AType: String): TFPDDSQLEngine;
  15. procedure ConnectToDatabase(const AType, ADatabaseName,AUserName,APassword: String);
  16. procedure DoConvertQuery(const S, T, KF: String; ST: TSTatementType);
  17. protected
  18. FConn : TSQLConnector;
  19. FDD : TFPDataDictionary;
  20. FENG : TFPDDSQLEngine;
  21. procedure DoRun; override;
  22. public
  23. constructor Create(TheOwner: TComponent); override;
  24. destructor Destroy; override;
  25. procedure WriteHelp(Const AMsg : string); virtual;
  26. end;
  27. { TGenSQLApplication }
  28. procedure TGenSQLApplication.ConnectToDatabase(Const AType,ADatabaseName,AUSerName,APassword : String);
  29. begin
  30. FConn:=TSQLConnector.Create(Self);
  31. FConn.ConnectorType:=AType;
  32. FConn.DatabaseName:=ADatabaseName;
  33. FConn.UserName:=AUserName;
  34. FConn.Password:=APassword;
  35. FConn.Transaction:=TSQLTransaction.Create(Self);
  36. FConn.Connected:=True;
  37. FDD:=TFPDataDictionary.Create;
  38. FENG:=CreateSQLEngine(AType);
  39. end;
  40. Function TGenSQLApplication.CreateSQLEngine(AType : String): TFPDDSQLEngine;
  41. begin
  42. Case lowercase(AType) of
  43. 'firebird' : Result:=TFPDDFBSQLEngine.Create;
  44. else
  45. Result:=TFPDDSQLEngine.Create;
  46. end;
  47. end;
  48. procedure TGenSQLApplication.DoConvertQuery(Const S,T,KF : String; ST : TSTatementType);
  49. Var
  50. Q : TSQLQuery;
  51. TD : TDDTableDef;
  52. Fields,KeyFields : TFPDDFieldList;
  53. I : Integer;
  54. F : TDDFieldDef;
  55. FN,SQL : String;
  56. begin
  57. TD:=FDD.Tables.AddTable(T);
  58. Q:=TSQLQuery.Create(Self);
  59. try
  60. Q.Database:=FConn;
  61. Q.Transaction:=FConn.Transaction;
  62. Q.SQL.Text:=S;
  63. Q.Open;
  64. TD.ImportFromDataset(Q);
  65. finally
  66. Q.Free;
  67. end;
  68. if (KF<>'') then
  69. begin
  70. KeyFields:=TFPDDFieldList.Create(False);
  71. For I:=1 to WordCount(KF,[',']) do
  72. begin
  73. FN:=ExtractWord(I,KF,[',']);
  74. F:=TD.Fields.FieldByName(FN);
  75. if (F=nil) then
  76. Writeln('Warning: Field ',FN,' does not exist.')
  77. else
  78. KeyFields.Add(F);
  79. end;
  80. end;
  81. Fields:=TFPDDFieldList.CreateFromTableDef(TD);
  82. try
  83. FEng.TableDef:=TD;
  84. Case ST of
  85. stDDL : SQL:=FEng.CreateCreateSQL(KeyFields);
  86. stSelect : SQL:=FEng.CreateSelectSQL(Fields,KeyFields);
  87. stInsert : SQL:=FEng.CreateInsertSQL(Fields);
  88. stUpdate : SQL:=FEng.CreateUpdateSQL(Fields,KeyFields);
  89. stDelete : SQL:=FEng.CreateDeleteSQL(KeyFields);
  90. end;
  91. Writeln(SQL);
  92. finally
  93. KeyFields.Free;
  94. end;
  95. end;
  96. procedure TGenSQLApplication.DoRun;
  97. var
  98. ErrorMsg: String;
  99. S,T,KF : String;
  100. I : Integer;
  101. ST : TStatementType;
  102. begin
  103. // quick check parameters
  104. ErrorMsg:=CheckOptions('hc:d:s:t:y:k:u:p:', 'help connection-type: database: sql: table: type: keyfields: user: password:');
  105. if ErrorMsg<>'' then
  106. WriteHelp(ErrorMsg);
  107. if HasOption('h', 'help') then
  108. WriteHelp('');
  109. S:=GetOptionValue('c','connection-type');
  110. T:=GetOptionValue('d','database');
  111. if (S='') or (t='') then
  112. Writehelp('Need database and connectiontype');
  113. ConnectToDatabase(S,T,GetOptionValue('u','user'),GetOptionValue('p','password'));
  114. S:=GetOptionValue('s','sql');
  115. T:=GetOptionValue('t','table');
  116. if (t='') then
  117. Writehelp('Need table name');
  118. i:=GetEnumValue(TypeInfo(TStatementType),'st'+GetOptionValue('y','type'));
  119. if I=-1 then
  120. Writehelp(Format('Unknown statement type : %s',[GetOptionValue('y','type')]));
  121. ST:=TStatementType(i);
  122. KF:=GetOptionValue('k','keyfields');
  123. if (KF='') and (st in [stselect, stupdate, stdelete]) then
  124. Writehelp('Need key fields for delete, select and update');
  125. if (S='') then
  126. S:='SELECT * FROM '+T+' WHERE 0=1';
  127. DoConvertQuery(S,T,KF,ST);
  128. // stop program loop
  129. Terminate;
  130. end;
  131. constructor TGenSQLApplication.Create(TheOwner: TComponent);
  132. begin
  133. inherited Create(TheOwner);
  134. StopOnException:=True;
  135. end;
  136. destructor TGenSQLApplication.Destroy;
  137. begin
  138. FreeAndNil(FConn);
  139. FreeAndNil(FDD);
  140. FreeAndNil(FENG);
  141. inherited Destroy;
  142. end;
  143. procedure TGenSQLApplication.WriteHelp(Const AMsg : string);
  144. Var
  145. S : String;
  146. L : TStrings;
  147. begin
  148. if AMsg<>'' then
  149. Writeln('Error : ',AMsg);
  150. Writeln('Usage: ', ExeName, ' [options]');
  151. Writeln('Where options is one or more of:');
  152. Writeln('-h --help this help message');
  153. Writeln('-c --connection-type=ctype Set connection type (required)' );
  154. Writeln('-d --database=db database connection name (required)');
  155. Writeln('-s --sql=sql SQL to execute (optional)');
  156. Writeln('-t --table=tablename tablename to use for statement (required)');
  157. Writeln('-y --type=stype Statement type (required) one of ddl, select, insert, update, delete)');
  158. Writeln('-k --keyfields=fields Comma-separated list of key fields (required for delete, update, optional for select,ddl)');
  159. Writeln('-u --user=username User name to connect to database');
  160. Writeln('-p --password=password Password of user to connect to database with');
  161. Writeln('Where ctype is one of : ');
  162. L:=TStringList.Create;
  163. try
  164. GetConnectionList(L);
  165. for S in L do
  166. Writeln(' ',lowercase(S));
  167. finally
  168. L.Free;
  169. end;
  170. Halt(Ord(AMsg<>''));
  171. end;
  172. var
  173. Application: TGenSQLApplication;
  174. begin
  175. Application:=TGenSQLApplication.Create(nil);
  176. Application.Title:='Generate SQL Demo';
  177. Application.Run;
  178. Application.Free;
  179. end.