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
This commit is contained in:
Ansgar Becker
2015-04-18 08:12:29 +00:00
parent 34f70eaf08
commit db76b2f867
2 changed files with 55 additions and 10 deletions

View File

@@ -48,6 +48,7 @@ type
function QuotedName(AlwaysQuote: Boolean=True): String;
function QuotedDbAndTableName(AlwaysQuote: Boolean=True): String;
function QuotedColumn(AlwaysQuote: Boolean=True): String;
function RowCount: Int64;
property ObjType: String read GetObjType;
property ImageIndex: Integer read GetImageIndex;
property OverlayImageIndex: Integer read GetOverlayImageIndex;
@@ -346,6 +347,7 @@ type
function GetCurrentUserHostCombination: String;
function DecodeAPIString(a: AnsiString): String;
function ExtractIdentifier(var SQL: String): String;
function GetRowCount(Obj: TDBObject): Int64; virtual; abstract;
procedure ClearCache(IncludeDBObjects: Boolean);
procedure FetchDbObjects(db: String; var Cache: TDBObjectList); virtual; abstract;
procedure SetObjectNamesInSelectedDB;
@@ -457,6 +459,7 @@ type
function GetCollationTable: TDBQuery; override;
function GetCharsetTable: TDBQuery; override;
function GetCreateViewCode(Database, Name: String): String;
function GetRowCount(Obj: TDBObject): Int64; override;
procedure FetchDbObjects(db: String; var Cache: TDBObjectList); override;
procedure SetLockedByThread(Value: TThread); override;
public
@@ -486,6 +489,7 @@ type
function GetCollationTable: TDBQuery; override;
function GetCharsetTable: TDBQuery; override;
function GetInformationSchemaObjects: TStringList; override;
function GetRowCount(Obj: TDBObject): Int64; override;
procedure FetchDbObjects(db: String; var Cache: TDBObjectList); override;
public
constructor Create(AOwner: TComponent); override;
@@ -524,6 +528,7 @@ type
function Ping(Reconnect: Boolean): Boolean; override;
function GetLastResults: TDBQueryList; override;
function MaxAllowedPacket: Int64; override;
function GetRowCount(Obj: TDBObject): Int64; override;
property LastRawResults: TPGRawResults read FLastRawResults;
end;
@@ -3728,6 +3733,49 @@ begin
end;
function TMySQLConnection.GetRowCount(Obj: TDBObject): Int64;
var
Rows: String;
begin
// Get row number from a mysql table
Rows := GetVar('SHOW TABLE STATUS LIKE '+EscapeString(Obj.Name), 'Rows');
Result := MakeInt(Rows);
end;
function TAdoDBConnection.GetRowCount(Obj: TDBObject): Int64;
var
Rows: String;
begin
// Get row number from a mssql table
if ServerVersionInt >= 900 then begin
Rows := GetVar('SELECT SUM('+QuoteIdent('rows')+') FROM '+QuoteIdent('sys')+'.'+QuoteIdent('partitions')+
' WHERE '+QuoteIdent('index_id')+' IN (0, 1)'+
' AND '+QuoteIdent('object_id')+' = object_id('+EscapeString(Obj.Database+'.'+Obj.Schema+'.'+Obj.Name)+')'
);
end else begin
Rows := GetVar('SELECT COUNT(*) FROM '+QuoteIdent(Obj.Schema)+'.'+QuoteIdent(Obj.Name));
end;
Result := MakeInt(Rows);
end;
function TPgConnection.GetRowCount(Obj: TDBObject): Int64;
var
Rows: String;
begin
// Get row number from a postgres table
Rows := GetVar('SELECT '+QuoteIdent('reltuples')+'::bigint FROM '+QuoteIdent('pg_class')+
' LEFT JOIN '+QuoteIdent('pg_namespace')+
' ON ('+QuoteIdent('pg_namespace')+'.'+QuoteIdent('oid')+' = '+QuoteIdent('pg_class')+'.'+QuoteIdent('relnamespace')+')'+
' WHERE '+QuoteIdent('pg_class')+'.'+QuoteIdent('relkind')+'='+EscapeString('r')+
' AND '+QuoteIdent('pg_namespace')+'.'+QuoteIdent('nspname')+'='+EscapeString(Obj.Database)+
' AND '+QuoteIdent('pg_class')+'.'+QuoteIdent('relname')+'='+EscapeString(Obj.Name)
);
Result := MakeInt(Rows);
end;
function TDBConnection.GetSQLSpecifity(Specifity: TSQLSpecifityId): String;
begin
// Return some version specific SQL clause or snippet
@@ -6689,6 +6737,11 @@ begin
Result := Connection.QuoteIdent(Column, AlwaysQuote);
end;
function TDBObject.RowCount: Int64;
begin
Result := Connection.GetRowCount(Self);
end;
{ *** TTableColumn }

View File

@@ -4943,16 +4943,8 @@ begin
if DBObject.NodeType = lntTable then begin
if (not IsLimited) and (not IsFiltered) then
RowsTotal := DataGrid.RootNodeCount // No need to fetch via SHOW TABLE STATUS
else case DBObject.Connection.Parameters.NetTypeGroup of
ngMySQL:
RowsTotal := MakeInt(DBObject.Connection.GetVar('SHOW TABLE STATUS LIKE '+esc(DBObject.Name), 'Rows'));
ngMSSQL:
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)+')'));
ngPgSQL:
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)));
else
raise Exception.Create(MsgUnhandledNetType);
end;
else
RowsTotal := DBObject.RowCount;
if RowsTotal > -1 then begin
cap := cap + ': ' + FormatNumber(RowsTotal) + ' ' + _('rows total');
if DBObject.Engine = 'InnoDB' then