📄 sdpgsql.pas
字号:
[DllImport(DefSqlApiDLL, CallingConvention = CallingConvention.cdecl, CharSet = CharSet.Ansi, SetLastError = True, EntryPoint = 'PQcmdStatus')]
function PQcmdStatus(res: PPGresult): TSDCharPtr; external;
[DllImport(DefSqlApiDLL, CallingConvention = CallingConvention.cdecl, CharSet = CharSet.Ansi, SetLastError = True, EntryPoint = 'PQoidStatus')]
function PQoidStatus(const res: PPGresult): TSDCharPtr; external;
[DllImport(DefSqlApiDLL, CallingConvention = CallingConvention.cdecl, CharSet = CharSet.Ansi, SetLastError = True, EntryPoint = 'PQoidValue')]
function PQoidValue(const res: PPGresult): Oid; external;
[DllImport(DefSqlApiDLL, CallingConvention = CallingConvention.cdecl, CharSet = CharSet.Ansi, SetLastError = True, EntryPoint = 'PQcmdTuples')]
function PQcmdTuples(res: PPGresult): TSDCharPtr; external;
[DllImport(DefSqlApiDLL, CallingConvention = CallingConvention.cdecl, CharSet = CharSet.Ansi, SetLastError = True, EntryPoint = 'PQgetvalue')]
function PQgetvalue(const res: PPGresult; tup_num, field_num: Integer): TSDCharPtr; external;
[DllImport(DefSqlApiDLL, CallingConvention = CallingConvention.cdecl, CharSet = CharSet.Ansi, SetLastError = True, EntryPoint = 'PQgetlength')]
function PQgetlength(const res: PPGresult; tup_num, field_num: Integer): Integer; external;
[DllImport(DefSqlApiDLL, CallingConvention = CallingConvention.cdecl, CharSet = CharSet.Ansi, SetLastError = True, EntryPoint = 'PQgetisnull')]
function PQgetisnull(const res: PPGresult; tup_num, field_num: Integer): Integer; external;
[DllImport(DefSqlApiDLL, CallingConvention = CallingConvention.cdecl, CharSet = CharSet.Ansi, SetLastError = True, EntryPoint = 'PQclear')]
procedure PQclear(res: PPGresult); external;
[DllImport(DefSqlApiDLL, CallingConvention = CallingConvention.cdecl, CharSet = CharSet.Ansi, SetLastError = True, EntryPoint = 'PQmakeEmptyPGresult')]
function PQmakeEmptyPGresult(conn: PPGconn; status: TExecStatusType): PPGresult; external;
[DllImport(DefSqlApiDLL, CallingConvention = CallingConvention.cdecl, CharSet = CharSet.Ansi, SetLastError = True, EntryPoint = 'PQprint')]
procedure PQprint(fout: TVoid; const res: PPGresult; const ps: PPQprintOpt); external;
[DllImport(DefSqlApiDLL, CallingConvention = CallingConvention.cdecl, CharSet = CharSet.Ansi, SetLastError = True, EntryPoint = 'PQmblen')]
function PQmblen(const s: TSDCharPtr; encoding: Integer): Integer; external;
[DllImport(DefSqlApiDLL, CallingConvention = CallingConvention.cdecl, CharSet = CharSet.Ansi, SetLastError = True, EntryPoint = 'PQenv2encoding')]
function PQenv2encoding: Integer; external;
{$ENDIF}
implementation
resourcestring
SErrLibLoading = 'Error loading library ''%s''';
SErrLibUnloading = 'Error unloading library ''%s''';
SErrFuncNotFound = 'Function ''%s'' not found in PostgreSQL library';
const
SDummySelect = 'select 1 from pg_class where 0=1';
var
hSqlLibModule: THandle;
SqlLibRefCount: Integer;
SqlLibLock: TCriticalSection;
dwLoadedFileVer: LongInt; // version of client DLL used
function InitSqlDatabase(ADbParams: TStrings): TISqlDatabase;
var
s: string;
begin
if hSqlLibModule = 0 then begin
s := Trim( ADbParams.Values[GetSqlLibParamName( Ord(istPostgreSQL) )] );
if s <> '' then
SqlApiDLL := s;
end;
Result := TIPgDatabase.Create( ADbParams );
end;
(*******************************************************************************
Load/Unload Sql-library
********************************************************************************)
procedure SetProcAddresses;
begin
{$IFNDEF SD_CLR}
// checked function availability in v.6-7.1
@PQconnectStart := GetProcAddress(hSqlLibModule, 'PQconnectStart'); // ASSERT( @PQconnectStart <>nil, Format(SErrFuncNotFound, ['PQconnectStart']) );
@PQconnectPoll := GetProcAddress(hSqlLibModule, 'PQconnectPoll'); // ASSERT( @PQconnectPoll <>nil, Format(SErrFuncNotFound, ['PQconnectPoll']) );
@PQconnectdb := GetProcAddress(hSqlLibModule, 'PQconnectdb'); ASSERT( @PQconnectdb <>nil, Format(SErrFuncNotFound, ['PQconnectdb']) );
@PQsetdbLogin := GetProcAddress(hSqlLibModule, 'PQsetdbLogin'); ASSERT( @PQsetdbLogin <>nil, Format(SErrFuncNotFound, ['PQsetdbLogin']) );
@PQfinish := GetProcAddress(hSqlLibModule, 'PQfinish'); ASSERT( @PQfinish <>nil, Format(SErrFuncNotFound, ['PQfinish']) );
@PQconndefaults := GetProcAddress(hSqlLibModule, 'PQconndefaults'); ASSERT( @PQconndefaults <>nil, Format(SErrFuncNotFound, ['PQconndefaults']) );
@PQconninfoFree := GetProcAddress(hSqlLibModule, 'PQconninfoFree'); ASSERT( @PQconninfoFree <>nil, Format(SErrFuncNotFound, ['PQconninfoFree']) );
@PQresetStart := GetProcAddress(hSqlLibModule, 'PQresetStart'); // ASSERT( @PQresetStart <>nil, Format(SErrFuncNotFound, ['PQresetStart']) );
@PQresetPoll := GetProcAddress(hSqlLibModule, 'PQresetPoll'); // ASSERT( @PQresetPoll <>nil, Format(SErrFuncNotFound, ['PQresetPoll']) );
@PQreset := GetProcAddress(hSqlLibModule, 'PQreset'); ASSERT( @PQreset <>nil, Format(SErrFuncNotFound, ['PQreset']) );
@PQrequestCancel := GetProcAddress(hSqlLibModule, 'PQrequestCancel'); ASSERT( @PQrequestCancel <>nil, Format(SErrFuncNotFound, ['PQrequestCancel']) );
@PQdb := GetProcAddress(hSqlLibModule, 'PQdb'); ASSERT( @PQdb <>nil, Format(SErrFuncNotFound, ['PQdb']) );
@PQuser := GetProcAddress(hSqlLibModule, 'PQuser'); ASSERT( @PQuser <>nil, Format(SErrFuncNotFound, ['PQuser']) );
@PQpass := GetProcAddress(hSqlLibModule, 'PQpass'); ASSERT( @PQpass <>nil, Format(SErrFuncNotFound, ['PQpass']) );
@PQhost := GetProcAddress(hSqlLibModule, 'PQhost'); ASSERT( @PQhost <>nil, Format(SErrFuncNotFound, ['PQhost']) );
@PQport := GetProcAddress(hSqlLibModule, 'PQport'); ASSERT( @PQport <>nil, Format(SErrFuncNotFound, ['PQport']) );
@PQtty := GetProcAddress(hSqlLibModule, 'PQtty'); ASSERT( @PQtty <>nil, Format(SErrFuncNotFound, ['PQtty']) );
@PQoptions := GetProcAddress(hSqlLibModule, 'PQoptions'); ASSERT( @PQoptions <>nil, Format(SErrFuncNotFound, ['PQoptions']) );
@PQstatus := GetProcAddress(hSqlLibModule, 'PQstatus'); ASSERT( @PQstatus <>nil, Format(SErrFuncNotFound, ['PQstatus']) );
@PQerrorMessage := GetProcAddress(hSqlLibModule, 'PQerrorMessage'); ASSERT( @PQerrorMessage <>nil, Format(SErrFuncNotFound, ['PQerrorMessage']) );
@PQsocket := GetProcAddress(hSqlLibModule, 'PQsocket'); ASSERT( @PQsocket <>nil, Format(SErrFuncNotFound, ['PQsocket']) );
@PQbackendPID := GetProcAddress(hSqlLibModule, 'PQbackendPID'); ASSERT( @PQbackendPID <>nil, Format(SErrFuncNotFound, ['PQbackendPID']) );
@PQclientEncoding := GetProcAddress(hSqlLibModule, 'PQclientEncoding'); ASSERT( @PQclientEncoding <>nil, Format(SErrFuncNotFound, ['PQclientEncoding']) );
@PQsetClientEncoding := GetProcAddress(hSqlLibModule, 'PQsetClientEncoding'); // ASSERT( @PQsetClientEncoding <>nil, Format(SErrFuncNotFound, ['PQsetClientEncoding']) );
@PQtrace := GetProcAddress(hSqlLibModule, 'PQtrace'); ASSERT( @PQtrace <>nil, Format(SErrFuncNotFound, ['PQtrace']) );
@PQuntrace := GetProcAddress(hSqlLibModule, 'PQuntrace'); ASSERT( @PQuntrace <>nil, Format(SErrFuncNotFound, ['PQuntrace']) );
@PQsetNoticeProcessor := GetProcAddress(hSqlLibModule, 'PQsetNoticeProcessor'); ASSERT( @PQsetNoticeProcessor <>nil, Format(SErrFuncNotFound, ['PQsetNoticeProcessor']) );
@PQexec := GetProcAddress(hSqlLibModule, 'PQexec'); ASSERT( @PQexec <>nil, Format(SErrFuncNotFound, ['PQexec']) );
@PQnotifies := GetProcAddress(hSqlLibModule, 'PQnotifies'); ASSERT( @PQnotifies <>nil, Format(SErrFuncNotFound, ['PQnotifies']) );
@PQsendQuery := GetProcAddress(hSqlLibModule, 'PQsendQuery'); ASSERT( @PQsendQuery <>nil, Format(SErrFuncNotFound, ['PQsendQuery']) );
@PQgetResult := GetProcAddress(hSqlLibModule, 'PQgetResult'); ASSERT( @PQgetResult <>nil, Format(SErrFuncNotFound, ['PQgetResult']) );
@PQisBusy := GetProcAddress(hSqlLibModule, 'PQisBusy'); ASSERT( @PQisBusy <>nil, Format(SErrFuncNotFound, ['PQisBusy']) );
@PQconsumeInput := GetProcAddress(hSqlLibModule, 'PQconsumeInput'); ASSERT( @PQconsumeInput <>nil, Format(SErrFuncNotFound, ['PQconsumeInput']) );
@PQgetline := GetProcAddress(hSqlLibModule, 'PQgetline'); ASSERT( @PQgetline <>nil, Format(SErrFuncNotFound, ['PQgetline']) );
@PQputline := GetProcAddress(hSqlLibModule, 'PQputline'); ASSERT( @PQputline <>nil, Format(SErrFuncNotFound, ['PQputline']) );
@PQgetlineAsync := GetProcAddress(hSqlLibModule, 'PQgetlineAsync'); ASSERT( @PQgetlineAsync <>nil, Format(SErrFuncNotFound, ['PQgetlineAsync']) );
@PQputnbytes := GetProcAddress(hSqlLibModule, 'PQputnbytes'); ASSERT( @PQputnbytes <>nil, Format(SErrFuncNotFound, ['PQputnbytes']) );
@PQendcopy := GetProcAddress(hSqlLibModule, 'PQendcopy'); ASSERT( @PQendcopy <>nil, Format(SErrFuncNotFound, ['PQendcopy']) );
@PQsetnonblocking := GetProcAddress(hSqlLibModule, 'PQsetnonblocking'); // ASSERT( @PQsetnonblocking <>nil, Format(SErrFuncNotFound, ['PQsetnonblocking']) );
@PQisnonblocking := GetProcAddress(hSqlLibModule, 'PQisnonblocking'); // ASSERT( @PQisnonblocking <>nil, Format(SErrFuncNotFound, ['PQisnonblocking']) );
@PQflush := GetProcAddress(hSqlLibModule, 'PQflush'); // ASSERT( @PQflush <>nil, Format(SErrFuncNotFound, ['PQflush']) );
@PQresultStatus := GetProcAddress(hSqlLibModule, 'PQresultStatus'); ASSERT( @PQresultStatus <>nil, Format(SErrFuncNotFound, ['PQresultStatus']) );
@PQresStatus := GetProcAddress(hSqlLibModule, 'PQresStatus'); ASSERT( @PQresStatus <>nil, Format(SErrFuncNotFound, ['PQresStatus']) );
@PQresultErrorMessage := GetProcAddress(hSqlLibModule, 'PQresultErrorMessage'); ASSERT( @PQresultErrorMessage <>nil, Format(SErrFuncNotFound, ['PQresultErrorMessage']) );
@PQntuples := GetProcAddress(hSqlLibModule, 'PQntuples'); ASSERT( @PQntuples <>nil, Format(SErrFuncNotFound, ['PQntuples']) );
@PQnfields := GetProcAddress(hSqlLibModule, 'PQnfields'); ASSERT( @PQnfields <>nil, Format(SErrFuncNotFound, ['PQnfields']) );
@PQbinaryTuples := GetProcAddress(hSqlLibModule, 'PQbinaryTuples'); ASSERT( @PQbinaryTuples <>nil, Format(SErrFuncNotFound, ['PQbinaryTuples']) );
@PQfname := GetProcAddress(hSqlLibModule, 'PQfname'); ASSERT( @PQfname <>nil, Format(SErrFuncNotFound, ['PQfname']) );
@PQfnumber := GetProcAddress(hSqlLibModule, 'PQfnumber'); ASSERT( @PQfnumber <>nil, Format(SErrFuncNotFound, ['PQfnumber']) );
@PQftype := GetProcAddress(hSqlLibModule, 'PQftype'); ASSERT( @PQftype <>nil, Format(SErrFuncNotFound, ['PQftype']) );
@PQfsize := GetProcAddress(hSqlLibModule, 'PQfsize'); ASSERT( @PQfsize <>nil, Format(SErrFuncNotFound, ['PQfsize']) );
@PQfmod := GetProcAddress(hSqlLibModule, 'PQfmod'); ASSERT( @PQfmod <>nil, Format(SErrFuncNotFound, ['PQfmod']) );
@PQcmdStatus := GetProcAddress(hSqlLibModule, 'PQcmdStatus'); ASSERT( @PQcmdStatus <>nil, Format(SErrFuncNotFound, ['PQcmdStatus']) );
@PQoidStatus := GetProcAddress(hSqlLibModule, 'PQoidStatus'); ASSERT( @PQoidStatus <>nil, Format(SErrFuncNotFound, ['PQoidStatus']) );
@PQoidValue := GetProcAddress(hSqlLibModule, 'PQoidValue'); ASSERT( @PQoidValue <>nil, Format(SErrFuncNotFound, ['PQoidValue']) );
@PQcmdTuples := GetProcAddress(hSqlLibModule, 'PQcmdTuples'); ASSERT( @PQcmdTuples <>nil, Format(SErrFuncNotFound, ['PQcmdTuples']) );
@PQgetvalue := GetProcAddress(hSqlLibModule, 'PQgetvalue'); ASSERT( @PQgetvalue <>nil, Format(SErrFuncNotFound, ['PQgetvalue']) );
@PQgetlength := GetProcAddress(hSqlLibModule, 'PQgetlength'); ASSERT( @PQgetlength <>nil, Format(SErrFuncNotFound, ['PQgetlength']) );
@PQgetisnull := GetProcAddress(hSqlLibModule, 'PQgetisnull'); ASSERT( @PQgetisnull <>nil, Format(SErrFuncNotFound, ['PQgetisnull']) );
@PQclear := GetProcAddress(hSqlLibModule, 'PQclear'); ASSERT( @PQclear <>nil, Format(SErrFuncNotFound, ['PQclear']) );
@PQmakeEmptyPGresult := GetProcAddress(hSqlLibModule, 'PQmakeEmptyPGresult'); ASSERT( @PQmakeEmptyPGresult <>nil, Format(SErrFuncNotFound, ['PQmakeEmptyPGresult']) );
@PQprint := GetProcAddress(hSqlLibModule, 'PQprint'); ASSERT( @PQprint <>nil, Format(SErrFuncNotFound, ['PQprint']) );
@PQmblen := GetProcAddress(hSqlLibModule, 'PQmblen'); ASSERT( @PQmblen <>nil, Format(SErrFuncNotFound, ['PQmblen']) );
@PQenv2encoding := GetProcAddress(hSqlLibModule, 'PQenv2encoding'); ASSERT( @PQenv2encoding <>nil, Format(SErrFuncNotFound, ['PQenv2encoding']) );
@lo_open := GetProcAddress(hSqlLibModule, 'lo_open'); ASSERT( @lo_open <>nil, Format(SErrFuncNotFound, ['lo_open']) );
@lo_close := GetProcAddress(hSqlLibModule, 'lo_close'); ASSERT( @lo_close <>nil, Format(SErrFuncNotFound, ['lo_close']) );
@lo_read := GetProcAddress(hSqlLibModule, 'lo_read'); ASSERT( @lo_read <>nil, Format(SErrFuncNotFound, ['lo_read']) );
@lo_write := GetProcAddress(hSqlLibModule, 'lo_write'); ASSERT( @lo_write <>nil, Format(SErrFuncNotFound, ['lo_write']) );
@lo_lseek := GetProcAddress(hSqlLibModule, 'lo_lseek'); ASSERT( @lo_lseek <>nil, Format(SErrFuncNotFound, ['lo_lseek']) );
@lo_creat := GetProcAddress(hSqlLibModule, 'lo_creat'); ASSERT( @lo_creat <>nil, Format(SErrFuncNotFound, ['lo_creat']) );
@lo_tell := GetProcAddress(hSqlLibModule, 'lo_tell'); ASSERT( @lo_tell <>nil, Format(SErrFuncNotFound, ['lo_tell']) );
@lo_unlink := GetProcAddress(hSqlLibModule, 'lo_unlink'); ASSERT( @lo_unlink <>nil, Format(SErrFuncNotFound, ['lo_unlink']) );
@lo_import := GetProcAddress(hSqlLibModule, 'lo_import'); ASSERT( @lo_import <>nil, Format(SErrFuncNotFound, ['lo_import']) );
@lo_export := GetProcAddress(hSqlLibModule, 'lo_export'); ASSERT( @lo_export <>nil, Format(SErrFuncNotFound, ['lo_export']) );
@PQgetssl := GetProcAddress(hSqlLibModule, 'PQgetssl');
{$ENDIF}
end;
procedure ResetProcAddresses;
begin
{$IFNDEF SD_CLR}
@PQconnectStart := nil;
@PQconnectPoll := nil;
@PQconnectdb := nil;
@PQsetdbLogin := nil;
@PQfinish := nil;
@PQconndefaults := nil;
@PQconninfoFree := nil;
@PQresetStart := nil;
@PQresetPoll := nil;
@PQreset := nil;
@PQrequestCancel := nil;
@PQdb := nil;
@PQuser := nil;
@PQpass := nil;
@PQhost := nil;
@PQport := nil;
@PQtty := nil;
@PQoptions := nil;
@PQstatus := nil;
@PQerrorMessage := nil;
@PQsocket := nil;
@PQbackendPID := nil;
@PQclientEncoding := nil;
@PQsetClientEncoding := nil;
@PQgetssl := nil;
@PQtrace := nil;
@PQuntrace := nil;
@PQsetNoticeProcessor := nil;
@PQexec := nil;
@PQnotifies := nil;
@PQsendQuery := nil;
@PQgetResult := nil;
@PQisBusy := nil;
@PQconsumeInput := nil;
@PQgetline := nil;
@PQputline := nil;
@PQgetlineAsync := nil;
@PQputnbytes := nil;
@PQendcopy := nil;
@PQsetnonblocking := nil;
@PQisnonblocking := nil;
@PQflush := nil;
@PQresultStatus := nil;
@PQresStatus := nil;
@PQresultErrorMessage := nil;
@PQntuples := nil;
@PQnfields := nil;
@PQbinaryTuples := nil;
@PQfname := nil;
@PQfnumber := nil;
@PQftype := nil;
@PQfsize := nil;
@PQfmod := nil;
@PQcmdStatus := nil;
@PQoidStatus := nil;
@PQoidValue := nil;
@PQcmdTuples := nil;
@PQgetvalue := nil;
@PQgetlength := nil;
@PQgetisnull := nil;
@PQclear := nil;
@PQmakeEmptyPGresult := nil;
@PQprint := nil;
@PQmblen := nil;
@PQenv2encoding := nil;
@lo_open := nil;
@lo_close := nil;
@lo_read := nil;
@lo_write := nil;
@lo_lseek := nil;
@lo_creat := nil;
@lo_tell := nil;
@lo_unlink := nil;
@lo_import := nil;
@lo_export := nil;
{$ENDIF}
end;
procedure LoadSqlLib;
var
sFileName:string;
begin
SqlLibLock.Acquire;
try
if (SqlLibRefCount = 0) then begin
sFileName := SqlApiDLL;
hSqlLibModule := HelperLoadLibrary( sFileName );
if (hSqlLibModule = 0) then
raise ESDSqlLibError.CreateFmt(SErrLibLoading, [ sFileName ]);
Inc(SqlLibRefCount);
SetProcAddresses;
dwLoadedFileVer := GetFileVersion(sFileName);
end else
Inc(SqlLibRefCount);
finally
SqlLibLock.Release;
end;
end;
procedure FreeSqlLib;
begin
if SqlLibRefCount = 0 then
Exit;
SqlLibLock.Acquire;
try
if (SqlLibRefCount = 1) then begin
if FreeLibrary(hSqlLibModule) then
hSqlLibModule := 0
else
raise ESDSqlLibError.CreateFmt(SErrLibUnloading, [ SqlApiDLL ]);
Dec(SqlLibRefCount);
ResetProcAddresses;
dwLoadedFileVer := 0;
end else
Dec(SqlLibRefCount);
finally
SqlLibLock.Release;
end;
end;
{ TIPgDatabase }
constructor TIPgDatabase.Create(ADbParams: TStrings);
begin
inherited Create(ADbParams);
end;
destructor TIPgDatabase.Destroy;
begin
inherited;
end;
function TIPgDatabase.CreateSqlCommand: TISqlCommand;
begin
Result := TIPgCommand.Create( Self );
end;
procedure TIPgDatabase.Check(pgconn: PPGconn);
var
E: ESDPgSQLError;
szErrMsg: TSDCharPtr;
sMsg: string;
begin
ResetIdleTimeOut;
if PQstatus( pgconn ) = CONNECTION_OK then
Exit;
ResetBusyState;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -