logsqldemo.pas 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. program logsqldemo;
  2. {$mode objfpc}{$H+}
  3. uses
  4. {$IFDEF UNIX}{$IFDEF UseCThreads}
  5. cthreads,
  6. {$ENDIF}{$ENDIF}
  7. typinfo, Classes, SysUtils, CustApp, db, sqldb,
  8. ibconnection, sqlite3conn, oracleconnection, mysql40conn,mysql41conn, mssqlconn,
  9. mysql50conn, mysql55conn, mysql56conn, odbcconn, pqconnection, strutils;
  10. type
  11. { TGenSQLApplication }
  12. TGenSQLApplication = class(TCustomApplication)
  13. procedure DoSQLLog(Sender: TSQLConnection; EventType: TDBEventType;
  14. const Msg: String);
  15. private
  16. procedure ConnectToDatabase(const AType, ADatabaseName,AUserName,APassword: String);
  17. procedure RunQuery(SQL: String; ParamValues: TStrings);
  18. protected
  19. FConn : TSQLConnector;
  20. procedure DoRun; override;
  21. public
  22. constructor Create(TheOwner: TComponent); override;
  23. destructor Destroy; override;
  24. procedure WriteHelp(Const AMsg : string); virtual;
  25. end;
  26. { TGenSQLApplication }
  27. procedure TGenSQLApplication.DoSQLLog(Sender: TSQLConnection;
  28. EventType: TDBEventType; const Msg: String);
  29. begin
  30. Writeln(stderr,'[',EventType,'] : ',Msg);
  31. end;
  32. procedure TGenSQLApplication.ConnectToDatabase(const AType, ADatabaseName,
  33. AUserName, APassword: String);
  34. begin
  35. FConn:=TSQLConnector.Create(Self);
  36. FConn.ConnectorType:=AType;
  37. FConn.DatabaseName:=ADatabaseName;
  38. FConn.UserName:=AUserName;
  39. FConn.Password:=APassword;
  40. FConn.Transaction:=TSQLTransaction.Create(Self);
  41. FConn.OnLog:=@DoSQLLog;
  42. FConn.LogEvents:=LogAllEventsExtra;
  43. FConn.Connected:=True;
  44. end;
  45. procedure TGenSQLApplication.RunQuery(SQL : String; ParamValues : TStrings);
  46. Var
  47. S,PT,V : String;
  48. I : Integer;
  49. P : TParam;
  50. Q : TSQLQuery;
  51. F : TField;
  52. begin
  53. Q:=TSQLQuery.Create(Self);
  54. try
  55. Q.Database:=FConn;
  56. Q.Transaction:=FConn.Transaction;
  57. Q.SQL.Text:=SQL;
  58. For P in Q.Params do
  59. begin
  60. S:=ParamValues.Values[P.Name];
  61. PT:=ExtractWord(1,S,[':']);
  62. V:=ExtractWord(2,S,[':']);
  63. Case lowercase(PT) of
  64. 's' : P.AsString:=V;
  65. 'i' : P.AsInteger:=StrToInt(V);
  66. 'i64' : P.AsLargeInt:=StrToInt64(V);
  67. 'dt' : P.AsDateTime:=StrToDateTime(V);
  68. 'd' : P.AsDateTime:=StrToDate(V);
  69. 't' : P.AsDateTime:=StrToTime(V);
  70. 'f' : P.AsFloat:=StrToFloat(V);
  71. 'c' : P.AsCurrency:=StrToCurr(V);
  72. else
  73. Raise Exception.CreateFmt('unknown parameter type for %s : %s (value: %s)',[P.Name,PT,V]);
  74. end
  75. end;
  76. Q.Open;
  77. I:=0;
  78. While not Q.EOF do
  79. begin
  80. Inc(I);
  81. Writeln('Record ',I,':');
  82. For F in Q.Fields do
  83. if F.IsNull then
  84. writeln(F.FieldName,'=<Null>')
  85. else
  86. writeln(F.FieldName,'=',F.AsString);
  87. Q.Next;
  88. end;
  89. finally
  90. Q.Free;
  91. end;
  92. end;
  93. procedure TGenSQLApplication.DoRun;
  94. var
  95. ErrorMsg: String;
  96. S,T,KF : String;
  97. I : Integer;
  98. ST : TStatementType;
  99. P : TStrings;
  100. begin
  101. // quick check parameters
  102. ErrorMsg:=CheckOptions('hc:d:s:u:p:P:', 'help connection-type: database: sql: user: password: param:');
  103. if ErrorMsg<>'' then
  104. WriteHelp(ErrorMsg);
  105. if HasOption('h', 'help') then
  106. WriteHelp('');
  107. S:=GetOptionValue('c','connection-type');
  108. T:=GetOptionValue('d','database');
  109. if (S='') or (t='') then
  110. Writehelp('Need database and connectiontype');
  111. ConnectToDatabase(S,T,GetOptionValue('u','user'),GetOptionValue('p','password'));
  112. S:=GetOptionValue('s','sql');
  113. P:=TStringList.Create;
  114. try
  115. P.AddStrings(GetOptionValues('P','param'));
  116. RunQuery(S,P);
  117. finally
  118. P.Free;
  119. end;
  120. // stop program loop
  121. Terminate;
  122. end;
  123. constructor TGenSQLApplication.Create(TheOwner: TComponent);
  124. begin
  125. inherited Create(TheOwner);
  126. StopOnException:=True;
  127. end;
  128. destructor TGenSQLApplication.Destroy;
  129. begin
  130. FreeAndNil(FConn);
  131. inherited Destroy;
  132. end;
  133. procedure TGenSQLApplication.WriteHelp(const AMsg: string);
  134. Var
  135. S : String;
  136. L : TStrings;
  137. begin
  138. if AMsg<>'' then
  139. Writeln('Error : ',AMsg);
  140. Writeln('Usage: ', ExeName, ' [options]');
  141. Writeln('Where options is one or more of:');
  142. Writeln('-h --help this help message');
  143. Writeln('-c --connection-type=ctype Set connection type (required)' );
  144. Writeln('Where ctype is one of : ');
  145. L:=TStringList.Create;
  146. try
  147. GetConnectionList(L);
  148. for S in L do
  149. Writeln(' ',lowercase(S));
  150. finally
  151. L.Free;
  152. end;
  153. Writeln('-d --database=db database connection name (required)');
  154. Writeln('-s --sql=sql SQL to execute (required), can contain parameters');
  155. Writeln('-u --user=username User name to connect to database');
  156. Writeln('-p --password=password Password of user to connect to database with');
  157. Writeln('-P --param=name=value Parameter values encoded as ptype:value');
  158. Writeln('Where ptype is one of : ');
  159. Writeln(' s : string');
  160. Writeln(' dt : datetime');
  161. Writeln(' d : date');
  162. Writeln(' t : time');
  163. Writeln(' i : integer');
  164. Writeln(' i64 : int64');
  165. Writeln(' f : float');
  166. Writeln(' c : currency');
  167. Halt(Ord(AMsg<>''));
  168. end;
  169. var
  170. Application: TGenSQLApplication;
  171. begin
  172. Application:=TGenSQLApplication.Create(nil);
  173. Application.Title:='Generate SQL Demo';
  174. Application.Run;
  175. Application.Free;
  176. end.