diff --git a/lib/private/DB/Connection.php b/lib/private/DB/Connection.php index 3fddfdcb09496..eecf83ace9562 100644 --- a/lib/private/DB/Connection.php +++ b/lib/private/DB/Connection.php @@ -698,6 +698,19 @@ public function dropTable($table) { } } + /** + * Truncate a table data if it exists + * + * @param string $table table name without the prefix + * @param bool $cascade whether to truncate cascading + * + * @throws Exception + */ + public function truncateTable(string $table, bool $cascade) { + $this->executeStatement($this->getDatabasePlatform() + ->getTruncateTableSQL($this->tablePrefix . trim($table), $cascade)); + } + /** * Check if a table exists * diff --git a/lib/private/DB/ConnectionAdapter.php b/lib/private/DB/ConnectionAdapter.php index 2baeda9cfb7c2..ba3bf90c2e8f5 100644 --- a/lib/private/DB/ConnectionAdapter.php +++ b/lib/private/DB/ConnectionAdapter.php @@ -189,6 +189,14 @@ public function dropTable(string $table): void { } } + public function truncateTable(string $table, bool $cascade): void { + try { + $this->inner->truncateTable($table, $cascade); + } catch (Exception $e) { + throw DbalException::wrap($e); + } + } + public function tableExists(string $table): bool { try { return $this->inner->tableExists($table); diff --git a/lib/public/IDBConnection.php b/lib/public/IDBConnection.php index 36369732b6449..e0fe603ec57e7 100644 --- a/lib/public/IDBConnection.php +++ b/lib/public/IDBConnection.php @@ -295,6 +295,21 @@ public function getDatabasePlatform(); */ public function dropTable(string $table): void; + /** + * Truncate a table data if it exists + * + * Cascade is not supported on many platforms but would optionally cascade the truncate by + * following the foreign keys. + * + * @param string $table table name without the prefix + * @param bool $cascade whether to truncate cascading + * @throws Exception + * @since 32.0.0 + * + * @psalm-taint-sink sql $table + */ + public function truncateTable(string $table, bool $cascade): void; + /** * Check if a table exists *