Skip to content

Commit 5ebc6e7

Browse files
authored
Merge pull request #965 from nextcloud/bug/556/update-pgsql-db-size
refactor: Use modern way to obtain pgsql size
2 parents 001a0b5 + 99cd1b7 commit 5ebc6e7

3 files changed

Lines changed: 24 additions & 30 deletions

File tree

lib/DatabaseStatistics.php

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,30 +97,11 @@ protected function databaseSize(): string {
9797
}
9898
break;
9999
case 'pgsql':
100-
$sql = "SELECT proname
101-
FROM pg_proc
102-
WHERE proname = 'pg_database_size'";
103-
$result = $this->connection->executeQuery($sql);
104-
$row = $result->fetch();
100+
$database = $this->config->getSystemValueString('dbname');
101+
$sql = 'SELECT pg_database_size(:dbname) as size';
102+
$result = $this->connection->executeQuery($sql, ['dbname' => $database]);
103+
$database_size = $result->fetchOne();
105104
$result->closeCursor();
106-
if ($row['proname'] === 'pg_database_size') {
107-
$database = $this->config->getSystemValue('dbname');
108-
if (strpos($database, '.') !== false) {
109-
[$database, ] = explode('.', $database);
110-
}
111-
$sql = "SELECT oid
112-
FROM pg_database
113-
WHERE datname = '$database'";
114-
$result = $this->connection->executeQuery($sql);
115-
$row = $result->fetch();
116-
$result->closeCursor();
117-
$oid = $row['oid'];
118-
$sql = 'SELECT pg_database_size(' . $oid . ') as size';
119-
$result = $this->connection->executeQuery($sql);
120-
$row = $result->fetch();
121-
$result->closeCursor();
122-
$database_size = $row['size'];
123-
}
124105
break;
125106
case 'oci':
126107
$sql = 'SELECT SUM(bytes) as dbsize

lib/ShareStatistics.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@ protected function countEntries(string $tableName): int {
5959
$query->select($query->func()->count('*', 'num_entries'))
6060
->from($tableName);
6161
$result = $query->executeQuery();
62-
$row = $result->fetch();
62+
$row = $result->fetchOne();
6363
$result->closeCursor();
6464

65-
return (int)$row['num_entries'];
65+
if ($row === false) {
66+
return 0;
67+
}
68+
69+
return (int)$row;
6670
}
6771

6872
/**
@@ -72,7 +76,7 @@ protected function countEntries(string $tableName): int {
7276
*/
7377
protected function countShares(int $type, bool $noPassword = false): int {
7478
$query = $this->connection->getQueryBuilder();
75-
$query->selectAlias($query->createFunction('COUNT(*)'), 'num_entries')
79+
$query->select($query->func()->count('*', 'num_entries'))
7680
->from('share')
7781
->where($query->expr()->eq('share_type', $query->createNamedParameter($type)));
7882

@@ -81,9 +85,13 @@ protected function countShares(int $type, bool $noPassword = false): int {
8185
}
8286

8387
$result = $query->executeQuery();
84-
$row = $result->fetch();
88+
$row = $result->fetchOne();
8589
$result->closeCursor();
8690

87-
return (int)$row['num_entries'];
91+
if ($row === false) {
92+
return 0;
93+
}
94+
95+
return (int)$row;
8896
}
8997
}

lib/StorageStatistics.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,14 @@ protected function countStorages(string $type): int {
9595
$query->andWhere($query->expr()->notLike('id', $query->createNamedParameter('local::%')));
9696
}
9797
$result = $query->executeQuery();
98-
$row = $result->fetch();
98+
$row = $result->fetchOne();
9999
$result->closeCursor();
100-
return (int)$row['num_entries'];
100+
101+
if ($row === false) {
102+
return 0;
103+
}
104+
105+
return (int)$row;
101106
}
102107

103108
public function updateAppDataStorageStats(): void {

0 commit comments

Comments
 (0)