logsqldemo.pas 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.Transaction.Params.Add('version1');
  42. FConn.Transaction.Params.Add('read_committed');
  43. FConn.Transaction.Params.Add('isc_tpb_lock_read=PARMTAB');
  44. FConn.OnLog:=@DoSQLLog;
  45. FConn.LogEvents:=LogAllEventsExtra;
  46. FConn.Connected:=True;
  47. end;
  48. procedure TGenSQLApplication.RunQuery(SQL : String; ParamValues : TStrings);
  49. Var
  50. S,PT,V : String;
  51. I : Integer;
  52. P : TParam;
  53. Q : TSQLQuery;
  54. F : TField;
  55. begin
  56. Q:=TSQLQuery.Create(Self);
  57. try
  58. Q.Database:=FConn;
  59. Q.Transaction:=FConn.Transaction;
  60. Q.SQL.Text:=SQL;
  61. For P in Q.Params do
  62. begin
  63. S:=ParamValues.Values[P.Name];
  64. PT:=ExtractWord(1,S,[':']);
  65. V:=ExtractWord(2,S,[':']);
  66. Case lowercase(PT) of
  67. 's' : P.AsString:=V;
  68. 'i' : P.AsInteger:=StrToInt(V);
  69. 'i64' : P.AsLargeInt:=StrToInt64(V);
  70. 'dt' : P.AsDateTime:=StrToDateTime(V);
  71. 'd' : P.AsDateTime:=StrToDate(V);
  72. 't' : P.AsDateTime:=StrToTime(V);
  73. 'f' : P.AsFloat:=StrToFloat(V);
  74. 'c' : P.AsCurrency:=StrToCurr(V);
  75. else
  76. Raise Exception.CreateFmt('unknown parameter type for %s : %s (value: %s)',[P.Name,PT,V]);
  77. end
  78. end;
  79. Q.Open;
  80. I:=0;
  81. While not Q.EOF do
  82. begin
  83. Inc(I);
  84. Writeln('Record ',I,':');
  85. For F in Q.Fields do
  86. if F.IsNull then
  87. writeln(F.FieldName,'=<Null>')
  88. else
  89. writeln(F.FieldName,'=',F.AsString);
  90. Q.Next;
  91. end;
  92. finally
  93. Q.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. P : TStrings;
  103. begin
  104. // quick check parameters
  105. ErrorMsg:=CheckOptions('hc:d:s:u:p:P:', 'help connection-type: database: sql: user: password: param:');
  106. if ErrorMsg<>'' then
  107. WriteHelp(ErrorMsg);
  108. if HasOption('h', 'help') then
  109. WriteHelp('');
  110. S:=GetOptionValue('c','connection-type');
  111. T:=GetOptionValue('d','database');
  112. if (S='') or (t='') then
  113. Writehelp('Need database and connectiontype');
  114. ConnectToDatabase(S,T,GetOptionValue('u','user'),GetOptionValue('p','password'));
  115. S:=GetOptionValue('s','sql');
  116. P:=TStringList.Create;
  117. try
  118. P.AddStrings(GetOptionValues('P','param'));
  119. RunQuery(S,P);
  120. finally
  121. P.Free;
  122. end;
  123. // stop program loop
  124. Terminate;
  125. end;
  126. constructor TGenSQLApplication.Create(TheOwner: TComponent);
  127. begin
  128. inherited Create(TheOwner);
  129. StopOnException:=True;
  130. end;
  131. destructor TGenSQLApplication.Destroy;
  132. begin
  133. FreeAndNil(FConn);
  134. inherited Destroy;
  135. end;
  136. procedure TGenSQLApplication.WriteHelp(const AMsg: string);
  137. Var
  138. S : String;
  139. L : TStrings;
  140. begin
  141. if AMsg<>'' then
  142. Writeln('Error : ',AMsg);
  143. Writeln('Usage: ', ExeName, ' [options]');
  144. Writeln('Where options is one or more of:');
  145. Writeln('-h --help this help message');
  146. Writeln('-c --connection-type=ctype Set connection type (required)' );
  147. Writeln('Where ctype is one of : ');
  148. L:=TStringList.Create;
  149. try
  150. GetConnectionList(L);
  151. for S in L do
  152. Writeln(' ',lowercase(S));
  153. finally
  154. L.Free;
  155. end;
  156. Writeln('-d --database=db database connection name (required)');
  157. Writeln('-s --sql=sql SQL to execute (required), can contain parameters');
  158. Writeln('-u --user=username User name to connect to database');
  159. Writeln('-p --password=password Password of user to connect to database with');
  160. Writeln('-P --param=name=value Parameter values encoded as ptype:value');
  161. Writeln('Where ptype is one of : ');
  162. Writeln(' s : string');
  163. Writeln(' dt : datetime');
  164. Writeln(' d : date');
  165. Writeln(' t : time');
  166. Writeln(' i : integer');
  167. Writeln(' i64 : int64');
  168. Writeln(' f : float');
  169. Writeln(' c : currency');
  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.