Skip to content

Commit c414dde

Browse files
committed
feat(db): add truncateTable method
Signed-off-by: Varun Patil <varunpatil@ucla.edu>
1 parent 0717412 commit c414dde

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

lib/private/DB/Connection.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,19 @@ public function dropTable($table) {
698698
}
699699
}
700700

701+
/**
702+
* Truncate a table data if it exists
703+
*
704+
* @param string $table table name without the prefix
705+
* @param bool $cascade whether to truncate cascading
706+
*
707+
* @throws Exception
708+
*/
709+
public function truncateTable(string $table, bool $cascade) {
710+
$this->executeStatement($this->getDatabasePlatform()
711+
->getTruncateTableSQL($this->tablePrefix . trim($table), $cascade));
712+
}
713+
701714
/**
702715
* Check if a table exists
703716
*

lib/private/DB/ConnectionAdapter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ public function dropTable(string $table): void {
189189
}
190190
}
191191

192+
public function truncateTable(string $table, bool $cascade): void {
193+
try {
194+
$this->inner->truncateTable($table, $cascade);
195+
} catch (Exception $e) {
196+
throw DbalException::wrap($e);
197+
}
198+
}
199+
192200
public function tableExists(string $table): bool {
193201
try {
194202
return $this->inner->tableExists($table);

lib/public/IDBConnection.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,21 @@ public function getDatabasePlatform();
295295
*/
296296
public function dropTable(string $table): void;
297297

298+
/**
299+
* Truncate a table data if it exists
300+
*
301+
* Cascade is not supported on many platforms but would optionally cascade the truncate by
302+
* following the foreign keys.
303+
*
304+
* @param string $table table name without the prefix
305+
* @param bool $cascade whether to truncate cascading
306+
* @throws Exception
307+
* @since 32.0.0
308+
*
309+
* @psalm-taint-sink sql $table
310+
*/
311+
public function truncateTable(string $table, bool $cascade): void;
312+
298313
/**
299314
* Check if a table exists
300315
*

0 commit comments

Comments
 (0)