delphimvcframework/lib/loggerpro/LoggerPro.DBAppender.ADO.pas
Daniele Teti f78fa3744b Merge commit '7aef2d3b8847c96e32e5f6c94b7cca69bcffe1ec'
# Conflicts:
#	lib/loggerpro/LoggerPro.ConsoleAppender.pas
2024-04-03 23:52:06 +02:00

86 lines
1.8 KiB
ObjectPascal

unit LoggerPro.DBAppender.ADO;
// DB log appender - using dbGo aka ADO
interface
uses
System.Classes,
LoggerPro, System.SysUtils, Data.DB,
LoggerPro.DBAppender,
Data.Win.ADODB;
type
ESQLException = class(Exception)
private
FErrorCode: Integer;
public
constructor Create(AMessage: string; AErrorCode: Integer); overload;
constructor Create(StoredProc: TADOStoredProc); overload;
end;
/// <summary>LoggerPro that persists to DB via ADO stored procedure</summary>
TLoggerProDBAppenderADO = class(TLoggerProDBAppender<TADOStoredProc>)
protected
procedure RefreshParams(DataObj: TADOStoredProc); override;
procedure ExecuteDataObject(DataObj: TADOStoredProc); override;
public
procedure Setup; override;
procedure TearDown; override;
end;
implementation
uses
System.IOUtils, Winapi.ActiveX;
{ ESQLException }
constructor ESQLException.Create(AMessage: string; AErrorCode: Integer);
begin
inherited Create(AMessage);
FErrorCode := AErrorCode;
end;
constructor ESQLException.Create(StoredProc: TADOStoredProc);
var
LastErrorIdx: Integer;
begin
if StoredProc.Connection <> nil then
begin
LastErrorIdx := StoredProc.Connection.Errors.Count - 1;
Create(StoredProc.Connection.Errors.Item[LastErrorIdx].Description, StoredProc.Connection.Errors.Item[LastErrorIdx]
.NativeError);
end;
end;
{ TLoggerProDBAppenderADO }
procedure TLoggerProDBAppenderADO.ExecuteDataObject(DataObj: TADOStoredProc);
begin
DataObj.ExecProc;
if DataObj.Connection.Errors.Count > 0 then
raise ESQLException.Create(DataObj);
end;
procedure TLoggerProDBAppenderADO.RefreshParams(DataObj: TADOStoredProc);
begin
DataObj.Parameters.Refresh;
end;
procedure TLoggerProDBAppenderADO.Setup;
begin
CoInitialize(nil);
inherited;
end;
procedure TLoggerProDBAppenderADO.TearDown;
begin
inherited;
CoUninitialize;
end;
end.