aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVarun Patil <varunpatil@ucla.edu>2025-03-02 14:52:21 -0800
committerGitHub <noreply@github.com>2025-03-02 14:52:21 -0800
commit0080853543af787103777bd7033b210f1fcdfa02 (patch)
treea912862deb6bd0eeb8ddbb7665980692539ec7d4
parent933c36d6e5869d33cb8b2764384b8d4da65f62f0 (diff)
parentc414ddee54a401e70f14a06e5456c09a3a7a9b93 (diff)
downloadnextcloud-server-0080853543af787103777bd7033b210f1fcdfa02.tar.gz
nextcloud-server-0080853543af787103777bd7033b210f1fcdfa02.zip
Merge pull request #51190 from nextcloud/pulsejet/truncate-1
feat(db): add truncateTable method
-rw-r--r--lib/private/DB/Connection.php13
-rw-r--r--lib/private/DB/ConnectionAdapter.php8
-rw-r--r--lib/public/IDBConnection.php15
3 files changed, 36 insertions, 0 deletions
diff --git a/lib/private/DB/Connection.php b/lib/private/DB/Connection.php
index 3fddfdcb094..eecf83ace95 100644
--- a/lib/private/DB/Connection.php
+++ b/lib/private/DB/Connection.php
@@ -699,6 +699,19 @@ class Connection extends PrimaryReadReplicaConnection {
}
/**
+ * 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
*
* @param string $table table name without the prefix
diff --git a/lib/private/DB/ConnectionAdapter.php b/lib/private/DB/ConnectionAdapter.php
index 2baeda9cfb7..ba3bf90c2e8 100644
--- a/lib/private/DB/ConnectionAdapter.php
+++ b/lib/private/DB/ConnectionAdapter.php
@@ -189,6 +189,14 @@ class ConnectionAdapter implements IDBConnection {
}
}
+ 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 36369732b64..e0fe603ec57 100644
--- a/lib/public/IDBConnection.php
+++ b/lib/public/IDBConnection.php
@@ -296,6 +296,21 @@ interface IDBConnection {
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
*
* @param string $table table name without the prefix