diff --git a/source/main.pas b/source/main.pas index 5d6c8373..9828a751 100644 --- a/source/main.pas +++ b/source/main.pas @@ -538,7 +538,6 @@ type procedure DiscardNodeData(Sender: TVirtualStringTree; Node: PVirtualNode); procedure viewdata(Sender: TObject); procedure LogSQL(Msg: WideString; Category: TMySQLLogCategory=lcInfo); - procedure CheckUptime; procedure KillProcess(Sender: TObject); procedure ExecSQLClick(Sender: TObject; Selection: Boolean = false; CurrentLine: Boolean=false); @@ -702,7 +701,6 @@ type private ReachedEOT : Boolean; FDelimiter: String; - ServerUptime : Integer; viewingdata : Boolean; CachedTableLists : TWideStringList; EditVariableForm : TfrmEditVariable; @@ -1562,8 +1560,6 @@ begin DBTree.Color := GetRegValue(REGNAME_TREEBACKGROUND, clWindow, SessionName); - CheckUptime; - // Reselect last used database if GetRegValue( REGNAME_RESTORELASTUSEDDB, DEFAULT_RESTORELASTUSEDDB ) then begin lastUsedDB := Utf8Decode(GetRegValue(REGNAME_LASTUSEDDB, '', SessionName)); @@ -1678,8 +1674,6 @@ begin ListCommandstats.Tag := VTREE_NOTLOADED; Application.Title := APPNAME; - - TimerHostUptime.Enabled := False; end; @@ -3880,15 +3874,6 @@ begin end; -procedure TMainForm.CheckUptime; -begin - ServerUptime := MakeInt(Connection.GetVar('SHOW STATUS LIKE ''Uptime''', 1)); - // Avoid division by zero - ServerUptime := Max(ServerUptime, 1); - TimerHostUptime.Enabled := true; -end; - - procedure TMainForm.KillProcess(Sender: TObject); var t : Boolean; ProcessIDs : TWideStringList; @@ -4293,22 +4278,20 @@ end; procedure TMainForm.TimerHostUptimeTimer(Sender: TObject); var - days, hours, minutes, seconds : Integer; - msg: string; + ServerUptime, days, hours, minutes, seconds : Integer; begin // Host-Uptime - days:= ServerUptime div (60*60*24); - seconds := ServerUptime mod (60*60*24); - hours := seconds div (60*60); - seconds := seconds mod (60*60); - minutes := seconds div 60; - seconds := seconds mod 60; - - inc(ServerUptime); - msg := Format('%d days, %.2d:%.2d:%.2d', [days,hours,minutes,seconds]); - if TimerHostUptime.Enabled then msg := Format('Uptime: %s', [msg]) - else msg := ''; - showstatus(msg, 4); + if Assigned(Connection) then begin + ServerUptime := Connection.ServerUptime; + days:= ServerUptime div (60*60*24); + seconds := ServerUptime mod (60*60*24); + hours := seconds div (60*60); + seconds := seconds mod (60*60); + minutes := seconds div 60; + seconds := seconds mod 60; + showstatus(Format('Uptime: %d days, %.2d:%.2d:%.2d', [days,hours,minutes,seconds]), 4); + end else + showstatus('', 4); end; @@ -4366,7 +4349,7 @@ procedure TMainForm.TimerConnectedTimer(Sender: TObject); begin if Assigned(Connection) and Connection.Active then begin // calculate and display connection-time - showstatus('Connected: ' + FormatTimeNumber((GetTickCount-Connection.ConnectionStarted) Div 1000), 2 ); + showstatus('Connected: ' + FormatTimeNumber(Connection.ConnectionUptime), 2); end else begin showstatus('Disconnected.', 2); end; @@ -7702,11 +7685,11 @@ begin if valIsNumber then begin valCount := MakeInt(val); // ... per hour - tmpval := valCount / ( ServerUptime / 60 / 60 ); + tmpval := valCount / ( Connection.ServerUptime / 60 / 60 ); if valIsBytes then avg_perhour := FormatByteNumber( Trunc(tmpval) ) else avg_perhour := FormatNumber( tmpval, 1 ); // ... per second - tmpval := valCount / ServerUptime; + tmpval := valCount / Connection.ServerUptime; if valIsBytes then avg_persec := FormatByteNumber( Trunc(tmpval) ) else avg_persec := FormatNumber( tmpval, 1 ); end; @@ -7802,10 +7785,10 @@ procedure TMainForm.ListCommandStatsBeforePaint(Sender: TBaseVirtualTree; Target // Total Frequency VTRowDataListCommandStats[idx].Captions.Add( FormatNumber( commandCount ) ); // Average per hour - tmpval := commandCount / ( ServerUptime / 60 / 60 ); + tmpval := commandCount / ( Connection.ServerUptime / 60 / 60 ); VTRowDataListCommandStats[idx].Captions.Add( FormatNumber( tmpval, 1 ) ); // Average per second - tmpval := commandCount / ServerUptime; + tmpval := commandCount / Connection.ServerUptime; VTRowDataListCommandStats[idx].Captions.Add( FormatNumber( tmpval, 1 ) ); // Percentage. Take care of division by zero errors and Int64's if commandCount < 1 then diff --git a/source/mysql_connection.pas b/source/mysql_connection.pas index a7bee79c..fdc82596 100644 --- a/source/mysql_connection.pas +++ b/source/mysql_connection.pas @@ -66,7 +66,8 @@ type private FHandle: PMYSQL; FActive: Boolean; - FConnectionStarted: Cardinal; + FConnectionStarted: Integer; + FServerStarted: Integer; FHostname: String; FSocketname: String; FPort: Integer; @@ -99,6 +100,8 @@ type function GetTableEngines: TStringList; function GetCollationTable: TMySQLQuery; function GetCollationList: TStringList; + function GetConnectionUptime: Integer; + function GetServerUptime: Integer; procedure Log(Category: TMySQLLogCategory; Msg: WideString); procedure DetectCapabilities; procedure ClearCache; @@ -116,7 +119,8 @@ type function GetVar(SQL: WideString; Column: WideString): WideString; overload; function Ping: Boolean; property ThreadId: Cardinal read GetThreadId; - property ConnectionStarted: Cardinal read FConnectionStarted; + property ConnectionUptime: Integer read GetConnectionUptime; + property ServerUptime: Integer read GetServerUptime; property CharacterSet: String read GetCharacterSet write SetCharacterSet; property LastError: WideString read GetLastError; property ServerVersionUntouched: String read FServerVersionUntouched; @@ -281,7 +285,8 @@ begin CurCharset := CharacterSet; Log(lcInfo, 'Characterset: '+CurCharset); FIsUnicode := CurCharset = 'utf8'; - FConnectionStarted := GetTickCount; + FConnectionStarted := GetTickCount div 1000; + FServerStarted := FConnectionStarted - StrToIntDef(GetVar('SHOW STATUS LIKE ''Uptime''', 1), 1); FServerVersionUntouched := mysql_get_server_info(FHandle); DetectCapabilities; tmpdb := FDatabase; @@ -709,6 +714,23 @@ begin end; +function TMySQLConnection.GetConnectionUptime: Integer; +begin + // Return seconds since last connect + if not FActive then + Result := 0 + else + Result := Integer(GetTickCount div 1000) - FConnectionStarted; +end; + + +function TMySQLConnection.GetServerUptime: Integer; +begin + // Return server uptime in seconds + Result := Integer(GetTickCount div 1000) - FServerStarted; +end; + + procedure TMySQLConnection.ClearCache; begin // Free cached lists and results. Called when the connection was closed and/or destroyed