Skip to content

Commit db76b2f

Browse files
committed
Outsource RowCount into TDBObject which calls TDBConnection.GetRowCount with a server and version specific query. Introduce SELECT COUNT(*) for MSSQL 2000.
See * http://www.heidisql.com/forum.php?t=18158 * http://www.heidisql.com/forum.php?t=15438
1 parent 34f70ea commit db76b2f

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

source/dbconnection.pas

+53
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ TDBObject = class(TPersistent)
4848
function QuotedName(AlwaysQuote: Boolean=True): String;
4949
function QuotedDbAndTableName(AlwaysQuote: Boolean=True): String;
5050
function QuotedColumn(AlwaysQuote: Boolean=True): String;
51+
function RowCount: Int64;
5152
property ObjType: String read GetObjType;
5253
property ImageIndex: Integer read GetImageIndex;
5354
property OverlayImageIndex: Integer read GetOverlayImageIndex;
@@ -346,6 +347,7 @@ TDBConnection = class(TComponent)
346347
function GetCurrentUserHostCombination: String;
347348
function DecodeAPIString(a: AnsiString): String;
348349
function ExtractIdentifier(var SQL: String): String;
350+
function GetRowCount(Obj: TDBObject): Int64; virtual; abstract;
349351
procedure ClearCache(IncludeDBObjects: Boolean);
350352
procedure FetchDbObjects(db: String; var Cache: TDBObjectList); virtual; abstract;
351353
procedure SetObjectNamesInSelectedDB;
@@ -457,6 +459,7 @@ TMySQLConnection = class(TDBConnection)
457459
function GetCollationTable: TDBQuery; override;
458460
function GetCharsetTable: TDBQuery; override;
459461
function GetCreateViewCode(Database, Name: String): String;
462+
function GetRowCount(Obj: TDBObject): Int64; override;
460463
procedure FetchDbObjects(db: String; var Cache: TDBObjectList); override;
461464
procedure SetLockedByThread(Value: TThread); override;
462465
public
@@ -486,6 +489,7 @@ TAdoDBConnection = class(TDBConnection)
486489
function GetCollationTable: TDBQuery; override;
487490
function GetCharsetTable: TDBQuery; override;
488491
function GetInformationSchemaObjects: TStringList; override;
492+
function GetRowCount(Obj: TDBObject): Int64; override;
489493
procedure FetchDbObjects(db: String; var Cache: TDBObjectList); override;
490494
public
491495
constructor Create(AOwner: TComponent); override;
@@ -524,6 +528,7 @@ TPgConnection = class(TDBConnection)
524528
function Ping(Reconnect: Boolean): Boolean; override;
525529
function GetLastResults: TDBQueryList; override;
526530
function MaxAllowedPacket: Int64; override;
531+
function GetRowCount(Obj: TDBObject): Int64; override;
527532
property LastRawResults: TPGRawResults read FLastRawResults;
528533
end;
529534

@@ -3728,6 +3733,49 @@ function TPGConnection.MaxAllowedPacket: Int64;
37283733
end;
37293734

37303735

3736+
function TMySQLConnection.GetRowCount(Obj: TDBObject): Int64;
3737+
var
3738+
Rows: String;
3739+
begin
3740+
// Get row number from a mysql table
3741+
Rows := GetVar('SHOW TABLE STATUS LIKE '+EscapeString(Obj.Name), 'Rows');
3742+
Result := MakeInt(Rows);
3743+
end;
3744+
3745+
3746+
function TAdoDBConnection.GetRowCount(Obj: TDBObject): Int64;
3747+
var
3748+
Rows: String;
3749+
begin
3750+
// Get row number from a mssql table
3751+
if ServerVersionInt >= 900 then begin
3752+
Rows := GetVar('SELECT SUM('+QuoteIdent('rows')+') FROM '+QuoteIdent('sys')+'.'+QuoteIdent('partitions')+
3753+
' WHERE '+QuoteIdent('index_id')+' IN (0, 1)'+
3754+
' AND '+QuoteIdent('object_id')+' = object_id('+EscapeString(Obj.Database+'.'+Obj.Schema+'.'+Obj.Name)+')'
3755+
);
3756+
end else begin
3757+
Rows := GetVar('SELECT COUNT(*) FROM '+QuoteIdent(Obj.Schema)+'.'+QuoteIdent(Obj.Name));
3758+
end;
3759+
Result := MakeInt(Rows);
3760+
end;
3761+
3762+
3763+
function TPgConnection.GetRowCount(Obj: TDBObject): Int64;
3764+
var
3765+
Rows: String;
3766+
begin
3767+
// Get row number from a postgres table
3768+
Rows := GetVar('SELECT '+QuoteIdent('reltuples')+'::bigint FROM '+QuoteIdent('pg_class')+
3769+
' LEFT JOIN '+QuoteIdent('pg_namespace')+
3770+
' ON ('+QuoteIdent('pg_namespace')+'.'+QuoteIdent('oid')+' = '+QuoteIdent('pg_class')+'.'+QuoteIdent('relnamespace')+')'+
3771+
' WHERE '+QuoteIdent('pg_class')+'.'+QuoteIdent('relkind')+'='+EscapeString('r')+
3772+
' AND '+QuoteIdent('pg_namespace')+'.'+QuoteIdent('nspname')+'='+EscapeString(Obj.Database)+
3773+
' AND '+QuoteIdent('pg_class')+'.'+QuoteIdent('relname')+'='+EscapeString(Obj.Name)
3774+
);
3775+
Result := MakeInt(Rows);
3776+
end;
3777+
3778+
37313779
function TDBConnection.GetSQLSpecifity(Specifity: TSQLSpecifityId): String;
37323780
begin
37333781
// Return some version specific SQL clause or snippet
@@ -6689,6 +6737,11 @@ function TDBObject.QuotedColumn(AlwaysQuote: Boolean=True): String;
66896737
Result := Connection.QuoteIdent(Column, AlwaysQuote);
66906738
end;
66916739

6740+
function TDBObject.RowCount: Int64;
6741+
begin
6742+
Result := Connection.GetRowCount(Self);
6743+
end;
6744+
66926745

66936746
{ *** TTableColumn }
66946747

source/main.pas

+2-10
Original file line numberDiff line numberDiff line change
@@ -4943,16 +4943,8 @@ procedure TMainForm.DisplayRowCountStats(Sender: TBaseVirtualTree);
49434943
if DBObject.NodeType = lntTable then begin
49444944
if (not IsLimited) and (not IsFiltered) then
49454945
RowsTotal := DataGrid.RootNodeCount // No need to fetch via SHOW TABLE STATUS
4946-
else case DBObject.Connection.Parameters.NetTypeGroup of
4947-
ngMySQL:
4948-
RowsTotal := MakeInt(DBObject.Connection.GetVar('SHOW TABLE STATUS LIKE '+esc(DBObject.Name), 'Rows'));
4949-
ngMSSQL:
4950-
RowsTotal := MakeInt(DBObject.Connection.GetVar('SELECT SUM(rows) FROM sys.partitions WHERE index_id IN (0, 1) AND object_id = object_id('+esc(DBObject.Database+'.'+DBObject.Schema+'.'+DBObject.Name)+')'));
4951-
ngPgSQL:
4952-
RowsTotal := MakeInt(DBObject.Connection.GetVar('SELECT reltuples::bigint FROM pg_class AS c LEFT JOIN pg_namespace AS n ON (n.oid = c.relnamespace) WHERE c.relkind='+esc('r')+' AND n.nspname='+esc(DBObject.Database)+' AND c.relname='+esc(DBObject.Name)));
4953-
else
4954-
raise Exception.Create(MsgUnhandledNetType);
4955-
end;
4946+
else
4947+
RowsTotal := DBObject.RowCount;
49564948
if RowsTotal > -1 then begin
49574949
cap := cap + ': ' + FormatNumber(RowsTotal) + ' ' + _('rows total');
49584950
if DBObject.Engine = 'InnoDB' then

0 commit comments

Comments
 (0)