mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-09-10 21:49:14 +02:00
* Added logging demo
git-svn-id: trunk@31160 -
This commit is contained in:
parent
4cc83ba03f
commit
33916f51d0
2
.gitattributes
vendored
2
.gitattributes
vendored
@ -2065,6 +2065,8 @@ packages/fcl-db/examples/fbadmindemo.pp svneol=native#text/plain
|
||||
packages/fcl-db/examples/fbeventstest.pp svneol=native#text/plain
|
||||
packages/fcl-db/examples/loadlibdemo.lpi svneol=native#text/plain
|
||||
packages/fcl-db/examples/loadlibdemo.pp svneol=native#text/plain
|
||||
packages/fcl-db/examples/logsqldemo.lpi svneol=native#text/plain
|
||||
packages/fcl-db/examples/logsqldemo.pas svneol=native#text/plain
|
||||
packages/fcl-db/examples/pqeventstest.pp svneol=native#text/plain
|
||||
packages/fcl-db/examples/sqlite3loadlib.lpr svneol=native#text/plain
|
||||
packages/fcl-db/examples/sqlparser.pp svneol=native#text/plain
|
||||
|
64
packages/fcl-db/examples/logsqldemo.lpi
Normal file
64
packages/fcl-db/examples/logsqldemo.lpi
Normal file
@ -0,0 +1,64 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<CONFIG>
|
||||
<ProjectOptions>
|
||||
<Version Value="9"/>
|
||||
<General>
|
||||
<Flags>
|
||||
<MainUnitHasCreateFormStatements Value="False"/>
|
||||
</Flags>
|
||||
<SessionStorage Value="InProjectDir"/>
|
||||
<MainUnit Value="0"/>
|
||||
<Title Value="Generate SQL Demo"/>
|
||||
<UseAppBundle Value="False"/>
|
||||
<ResourceType Value="res"/>
|
||||
</General>
|
||||
<i18n>
|
||||
<EnableI18N LFM="False"/>
|
||||
</i18n>
|
||||
<VersionInfo>
|
||||
<StringTable ProductVersion=""/>
|
||||
</VersionInfo>
|
||||
<BuildModes Count="1">
|
||||
<Item1 Name="Default" Default="True"/>
|
||||
</BuildModes>
|
||||
<PublishOptions>
|
||||
<Version Value="2"/>
|
||||
</PublishOptions>
|
||||
<RunParams>
|
||||
<local>
|
||||
<FormatVersion Value="1"/>
|
||||
<CommandLineParams Value="-c firebird -d localhost:/home/firebird/timetrack.fb -u WISASOFT -p SysteemD -s 'SELECT * FROM PROJECT WHERE PJ_ID=:ID' -P ID=s:632F3D2F-055A-4DD9-852B-4050BF6A2ED9"/>
|
||||
</local>
|
||||
</RunParams>
|
||||
<Units Count="1">
|
||||
<Unit0>
|
||||
<Filename Value="logsqldemo.pas"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
</Unit0>
|
||||
</Units>
|
||||
</ProjectOptions>
|
||||
<CompilerOptions>
|
||||
<Version Value="11"/>
|
||||
<SearchPaths>
|
||||
<IncludeFiles Value="$(ProjOutDir)"/>
|
||||
</SearchPaths>
|
||||
<Parsing>
|
||||
<SyntaxOptions>
|
||||
<UseAnsiStrings Value="False"/>
|
||||
</SyntaxOptions>
|
||||
</Parsing>
|
||||
</CompilerOptions>
|
||||
<Debugging>
|
||||
<Exceptions Count="3">
|
||||
<Item1>
|
||||
<Name Value="EAbort"/>
|
||||
</Item1>
|
||||
<Item2>
|
||||
<Name Value="ECodetoolError"/>
|
||||
</Item2>
|
||||
<Item3>
|
||||
<Name Value="EFOpenError"/>
|
||||
</Item3>
|
||||
</Exceptions>
|
||||
</Debugging>
|
||||
</CONFIG>
|
200
packages/fcl-db/examples/logsqldemo.pas
Normal file
200
packages/fcl-db/examples/logsqldemo.pas
Normal file
@ -0,0 +1,200 @@
|
||||
program logsqldemo;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
|
||||
uses
|
||||
{$IFDEF UNIX}{$IFDEF UseCThreads}
|
||||
cthreads,
|
||||
{$ENDIF}{$ENDIF}
|
||||
typinfo, Classes, SysUtils, CustApp, db, sqldb,
|
||||
ibconnection, sqlite3conn, oracleconnection, mysql40conn,mysql41conn, mssqlconn,
|
||||
mysql50conn, mysql55conn, mysql56conn, odbcconn, pqconnection, strutils;
|
||||
|
||||
|
||||
type
|
||||
|
||||
{ TGenSQLApplication }
|
||||
|
||||
TGenSQLApplication = class(TCustomApplication)
|
||||
procedure DoSQLLog(Sender: TSQLConnection; EventType: TDBEventType;
|
||||
const Msg: String);
|
||||
private
|
||||
procedure ConnectToDatabase(const AType, ADatabaseName,AUserName,APassword: String);
|
||||
procedure RunQuery(SQL: String; ParamValues: TStrings);
|
||||
protected
|
||||
FConn : TSQLConnector;
|
||||
procedure DoRun; override;
|
||||
public
|
||||
constructor Create(TheOwner: TComponent); override;
|
||||
destructor Destroy; override;
|
||||
procedure WriteHelp(Const AMsg : string); virtual;
|
||||
end;
|
||||
|
||||
{ TGenSQLApplication }
|
||||
|
||||
procedure TGenSQLApplication.DoSQLLog(Sender: TSQLConnection;
|
||||
EventType: TDBEventType; const Msg: String);
|
||||
begin
|
||||
Writeln(stderr,'[',EventType,'] : ',Msg);
|
||||
end;
|
||||
|
||||
procedure TGenSQLApplication.ConnectToDatabase(const AType, ADatabaseName,
|
||||
AUserName, APassword: String);
|
||||
begin
|
||||
FConn:=TSQLConnector.Create(Self);
|
||||
FConn.ConnectorType:=AType;
|
||||
FConn.DatabaseName:=ADatabaseName;
|
||||
FConn.UserName:=AUserName;
|
||||
FConn.Password:=APassword;
|
||||
FConn.Transaction:=TSQLTransaction.Create(Self);
|
||||
FConn.OnLog:=@DoSQLLog;
|
||||
FConn.LogEvents:=LogAllEventsExtra;
|
||||
FConn.Connected:=True;
|
||||
end;
|
||||
|
||||
procedure TGenSQLApplication.RunQuery(SQL : String; ParamValues : TStrings);
|
||||
|
||||
Var
|
||||
S,PT,V : String;
|
||||
I : Integer;
|
||||
P : TParam;
|
||||
Q : TSQLQuery;
|
||||
F : TField;
|
||||
|
||||
begin
|
||||
Q:=TSQLQuery.Create(Self);
|
||||
try
|
||||
Q.Database:=FConn;
|
||||
Q.Transaction:=FConn.Transaction;
|
||||
Q.SQL.Text:=SQL;
|
||||
For P in Q.Params do
|
||||
begin
|
||||
S:=ParamValues.Values[P.Name];
|
||||
PT:=ExtractWord(1,S,[':']);
|
||||
V:=ExtractWord(2,S,[':']);
|
||||
Case lowercase(PT) of
|
||||
'','s' : P.AsString:=V;
|
||||
'i' : P.AsInteger:=StrToInt(V);
|
||||
'i64' : P.AsLargeInt:=StrToInt64(V);
|
||||
'dt' : P.AsDateTime:=StrToDateTime(V);
|
||||
'd' : P.AsDateTime:=StrToDate(V);
|
||||
't' : P.AsDateTime:=StrToTime(V);
|
||||
'f' : P.AsFloat:=StrToFloat(V);
|
||||
'c' : P.AsCurrency:=StrToCurr(V);
|
||||
else
|
||||
Raise Exception.CreateFmt('unknown parameter type for %s : %s (value: %s)',[P.Name,PT,V]);
|
||||
end
|
||||
end;
|
||||
Q.Open;
|
||||
I:=0;
|
||||
While not Q.EOF do
|
||||
begin
|
||||
Inc(I);
|
||||
Writeln('Record ',I,':');
|
||||
For F in Q.Fields do
|
||||
if F.IsNull then
|
||||
writeln(F.FieldName,'=<Null>')
|
||||
else
|
||||
writeln(F.FieldName,'=',F.AsString);
|
||||
Q.Next;
|
||||
end;
|
||||
finally
|
||||
Q.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TGenSQLApplication.DoRun;
|
||||
|
||||
var
|
||||
ErrorMsg: String;
|
||||
S,T,KF : String;
|
||||
I : Integer;
|
||||
ST : TStatementType;
|
||||
P : TStrings;
|
||||
|
||||
begin
|
||||
|
||||
// quick check parameters
|
||||
ErrorMsg:=CheckOptions('hc:d:s:u:p:P:', 'help connection-type: database: sql: user: password: param:');
|
||||
if ErrorMsg<>'' then
|
||||
WriteHelp(ErrorMsg);
|
||||
if HasOption('h', 'help') then
|
||||
WriteHelp('');
|
||||
S:=GetOptionValue('c','connection-type');
|
||||
T:=GetOptionValue('d','database');
|
||||
if (S='') or (t='') then
|
||||
Writehelp('Need database and connectiontype');
|
||||
ConnectToDatabase(S,T,GetOptionValue('u','user'),GetOptionValue('p','password'));
|
||||
S:=GetOptionValue('s','sql');
|
||||
P:=TStringList.Create;
|
||||
try
|
||||
P.AddStrings(GetOptionValues('P','param'));
|
||||
RunQuery(S,P);
|
||||
finally
|
||||
P.Free;
|
||||
end;
|
||||
// stop program loop
|
||||
Terminate;
|
||||
end;
|
||||
|
||||
constructor TGenSQLApplication.Create(TheOwner: TComponent);
|
||||
begin
|
||||
inherited Create(TheOwner);
|
||||
StopOnException:=True;
|
||||
end;
|
||||
|
||||
destructor TGenSQLApplication.Destroy;
|
||||
begin
|
||||
FreeAndNil(FConn);
|
||||
inherited Destroy;
|
||||
end;
|
||||
|
||||
procedure TGenSQLApplication.WriteHelp(const AMsg: string);
|
||||
|
||||
Var
|
||||
S : String;
|
||||
L : TStrings;
|
||||
begin
|
||||
if AMsg<>'' then
|
||||
Writeln('Error : ',AMsg);
|
||||
Writeln('Usage: ', ExeName, ' [options]');
|
||||
Writeln('Where options is one or more of:');
|
||||
Writeln('-h --help this help message');
|
||||
Writeln('-c --connection-type=ctype Set connection type (required)' );
|
||||
Writeln('Where ctype is one of : ');
|
||||
L:=TStringList.Create;
|
||||
try
|
||||
GetConnectionList(L);
|
||||
for S in L do
|
||||
Writeln(' ',lowercase(S));
|
||||
|
||||
finally
|
||||
L.Free;
|
||||
end;
|
||||
Writeln('-d --database=db database connection name (required)');
|
||||
Writeln('-s --sql=sql SQL to execute (required), can contain parameters');
|
||||
Writeln('-u --user=username User name to connect to database');
|
||||
Writeln('-p --password=password Password of user to connect to database with');
|
||||
Writeln('-P --param=name=value Parameter values encoded as ptype:value');
|
||||
Writeln('Where ptype is one of : ');
|
||||
Writeln(' s : string');
|
||||
Writeln(' dt : datetime');
|
||||
Writeln(' d : date');
|
||||
Writeln(' t : time');
|
||||
Writeln(' i : integer');
|
||||
Writeln(' i64 : int64');
|
||||
Writeln(' f : float');
|
||||
Writeln(' c : currency');
|
||||
|
||||
Halt(Ord(AMsg<>''));
|
||||
end;
|
||||
|
||||
var
|
||||
Application: TGenSQLApplication;
|
||||
begin
|
||||
Application:=TGenSQLApplication.Create(nil);
|
||||
Application.Title:='Generate SQL Demo';
|
||||
Application.Run;
|
||||
Application.Free;
|
||||
end.
|
||||
|
Loading…
Reference in New Issue
Block a user