Issue #2550: Use sys.sysprocesses instead of dbo.sysprocesses so this does not fail on old servers. Querying sys.sysprocesses may return an empty result for spid 1, so there is a default of -1 for FServerStarted now, which then reports "(unknown)" in the status bar.

This commit is contained in:
Ansgar Becker
2012-01-07 13:17:41 +00:00
parent 6325d42c4b
commit ff6983820c
2 changed files with 37 additions and 26 deletions

View File

@ -990,7 +990,7 @@ end;
procedure TAdoDBConnection.SetActive(Value: Boolean);
var
tmpdb, Error, NetLib, DataSource: String;
tmpdb, tmp, Error, NetLib, DataSource: String;
rx: TRegExpr;
i: Integer;
begin
@ -1040,7 +1040,11 @@ begin
// CurCharset := CharacterSet;
// Log(lcDebug, 'Characterset: '+CurCharset);
FIsUnicode := True;
FServerStarted := FConnectionStarted - StrToIntDef(GetVar('SELECT DATEDIFF(SECOND, '+QuoteIdent('login_time')+', CURRENT_TIMESTAMP) FROM '+QuoteIdent('dbo')+'.'+QuoteIdent('sysprocesses')+' WHERE '+QuoteIdent('spid')+'=1'), 1);
tmp := GetVar('SELECT DATEDIFF(SECOND, '+QuoteIdent('login_time')+', CURRENT_TIMESTAMP) FROM '+QuoteIdent('sys')+'.'+QuoteIdent('sysprocesses')+' WHERE '+QuoteIdent('spid')+'=1');
if tmp <> '' then
FServerStarted := FConnectionStarted - MakeInt(tmp)
else
FServerStarted := -1;
// Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (Intel X86)
// Apr 2 2010 15:53:02
// Copyright (c) Microsoft Corporation
@ -2184,8 +2188,11 @@ end;
function TDBConnection.GetServerUptime: Integer;
begin
// Return server uptime in seconds
Result := Integer(GetTickCount div 1000) - FServerStarted;
// Return server uptime in seconds. Return -1 if unknown.
if FServerStarted > 0 then
Result := Integer(GetTickCount div 1000) - FServerStarted
else
Result := FServerStarted;
end;

View File

@ -132,7 +132,7 @@ type
function getFirstWord( text: String ): String;
function FormatByteNumber( Bytes: Int64; Decimals: Byte = 1 ): String; Overload;
function FormatByteNumber( Bytes: String; Decimals: Byte = 1 ): String; Overload;
function FormatTimeNumber(Seconds: Cardinal; DisplaySeconds: Boolean): String;
function FormatTimeNumber(Seconds: Int64; DisplaySeconds: Boolean): String;
function GetTempDir: String;
procedure SetWindowSizeGrip(hWnd: HWND; Enable: boolean);
procedure SaveUnicodeFile(Filename: String; Text: String);
@ -1062,10 +1062,13 @@ end;
@param Cardinal Number of seconds
@result String 12:34:56
}
function FormatTimeNumber(Seconds: Cardinal; DisplaySeconds: Boolean): String;
function FormatTimeNumber(Seconds: Int64; DisplaySeconds: Boolean): String;
var
d, h, m, s : Integer;
begin
if Seconds < 0 then begin
Result := '(unknown)';
end else begin
s := Seconds;
d := s div (60*60*24);
s := s mod (60*60*24);
@ -1085,6 +1088,7 @@ begin
Result := Format('%.2d:%.2d h', [h, m])
end;
end;
end;
function GetTempDir: String;