Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Truncate
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 cascade
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Db\Sql\Schema;
16
17/**
18 * Schema TRUNCATE table class
19 *
20 * @category   Pop
21 * @package    Pop\Db
22 * @author     Nick Sagona, III <nick@popphp.org>
23 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
24 * @license    https://www.popphp.org/license     New BSD License
25 * @version    7.0.0
26 */
27class Truncate extends AbstractTable
28{
29
30    /**
31     * CASCADE flag
32     * @var bool
33     */
34    protected bool $cascade  = false;
35
36    /**
37     * Set the CASCADE flag
38     *
39     * @return Truncate
40     */
41    public function cascade(): Truncate
42    {
43        $this->cascade = true;
44        return $this;
45    }
46
47    /**
48     * Render the table schema
49     *
50     * SQLite has no TRUNCATE TABLE statement, so DELETE FROM is used instead to achieve
51     * the same effect of removing all rows from the table.
52     *
53     * @return string
54     */
55    public function render(): string
56    {
57        if ($this->isSqlite()) {
58            return 'DELETE FROM ' . $this->quoteId($this->table) . ';' . PHP_EOL;
59        }
60
61        return 'TRUNCATE TABLE ' . $this->quoteId($this->table) .
62            ((($this->isPgsql()) && ($this->cascade)) ? ' CASCADE' : null) . ';' . PHP_EOL;
63    }
64
65    /**
66     * Render the table schema to string
67     *
68     * @return string
69     */
70    public function __toString(): string
71    {
72        return $this->render();
73    }
74
75}