MCPcopy Index your code
hub / github.com/bytebase/dbhub / getTableRowCount

Function getTableRowCount

src/tools/search-objects.ts:95–118  ·  view source on GitHub ↗

* Get row count estimate for a table. * Prefers the connector's native statistics-based method (e.g. pg_class.reltuples) * when available, falling back to COUNT(*) for connectors that don't implement it.

(
  connector: Connector,
  tableName: string,
  schemaName?: string
)

Source from the content-addressed store, hash-verified

93 * when available, falling back to COUNT(*) for connectors that don't implement it.
94 */
95async function getTableRowCount(
96 connector: Connector,
97 tableName: string,
98 schemaName?: string
99): Promise<number | null> {
100 try {
101 if (connector.getTableRowCount) {
102 return await connector.getTableRowCount(tableName, schemaName);
103 }
104
105 // Fallback: COUNT(*) for connectors without a statistics-based implementation
106 const qualifiedTable = quoteQualifiedIdentifier(tableName, schemaName, connector.id);
107 const countQuery = `SELECT COUNT(*) as count FROM ${qualifiedTable}`;
108 const result = await connector.executeSQL(countQuery, { maxRows: 1 });
109
110 if (result.rows && result.rows.length > 0) {
111 return Number(result.rows[0].count || result.rows[0].COUNT || 0);
112 }
113 } catch (error) {
114 // If we can't get row count, return null (not critical)
115 return null;
116 }
117 return null;
118}
119
120/**
121 * Get table comment from the connector if supported.

Callers 1

searchTablesFunction · 0.85

Calls 3

quoteQualifiedIdentifierFunction · 0.85
getTableRowCountMethod · 0.65
executeSQLMethod · 0.65

Tested by

no test coverage detected