Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
50 / 50
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
Delete
100.00% covered (success)
100.00%
50 / 50
100.00% covered (success)
100.00%
6 / 6
19
100.00% covered (success)
100.00%
1 / 1
 renderOptions
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
4
 table
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 renderTable
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 limit
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 sql
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
8
 run
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php declare(strict_types=1);
2/*
3 * This file is part of Aplus Framework Database Library.
4 *
5 * (c) Natan Felles <natanfelles@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10namespace Framework\Database\Manipulation;
11
12use Closure;
13use InvalidArgumentException;
14
15/**
16 * Class Delete.
17 *
18 * @see https://mariadb.com/kb/en/delete/
19 *
20 * @package database
21 */
22class Delete extends Statement
23{
24    use Traits\Join;
25    use Traits\OrderBy;
26    use Traits\Where;
27
28    /**
29     * @var string
30     */
31    public const OPT_LOW_PRIORITY = 'LOW_PRIORITY';
32    /**
33     * @var string
34     */
35    public const OPT_QUICK = 'QUICK';
36    /**
37     * @var string
38     */
39    public const OPT_IGNORE = 'IGNORE';
40
41    protected function renderOptions() : ?string
42    {
43        if ( ! $this->hasOptions()) {
44            return null;
45        }
46        $options = $this->sql['options'];
47        foreach ($options as &$option) {
48            $input = $option;
49            $option = \strtoupper($option);
50            if ( ! \in_array($option, [
51                static::OPT_LOW_PRIORITY,
52                static::OPT_QUICK,
53                static::OPT_IGNORE,
54            ], true)) {
55                throw new InvalidArgumentException("Invalid option: {$input}");
56            }
57        }
58        unset($option);
59        $options = \implode(' ', $options);
60        return " {$options}";
61    }
62
63    /**
64     * Sets the table references.
65     *
66     * @param array<string,Closure|string>|Closure|string $reference The table
67     * name as string, a subquery as Closure or an array for aliased table where
68     * the key is the alias name and the value is the table name or a subquery
69     * @param array<string,Closure|string>|Closure|string ...$references Extra
70     * references. Same values as $reference
71     *
72     * @return static
73     */
74    public function table(
75        array | Closure | string $reference,
76        array | Closure | string ...$references
77    ) : static {
78        $this->sql['table'] = [];
79        foreach ([$reference, ...$references] as $reference) {
80            $this->sql['table'][] = $reference;
81        }
82        return $this;
83    }
84
85    /**
86     * Renders the table references.
87     *
88     * @return string|null The table references or null if none was set
89     */
90    protected function renderTable() : ?string
91    {
92        if ( ! isset($this->sql['table'])) {
93            return null;
94        }
95        $tables = [];
96        foreach ($this->sql['table'] as $table) {
97            $tables[] = $this->renderAliasedIdentifier($table);
98        }
99        return ' ' . \implode(', ', $tables);
100    }
101
102    /**
103     * Sets the LIMIT clause.
104     *
105     * @param int $limit
106     *
107     * @see https://mariadb.com/kb/en/limit/
108     *
109     * @return static
110     */
111    public function limit(int $limit) : static
112    {
113        return $this->setLimit($limit);
114    }
115
116    /**
117     * Renders de DELETE statement.
118     *
119     * @return string
120     */
121    public function sql() : string
122    {
123        $sql = 'DELETE' . \PHP_EOL;
124        $part = $this->renderOptions();
125        if ($part) {
126            $sql .= $part . \PHP_EOL;
127        }
128        $part = $this->renderTable();
129        if ($part) {
130            $sql .= $part . \PHP_EOL;
131        }
132        $part = $this->renderFrom();
133        if ($part) {
134            $sql .= $part . \PHP_EOL;
135        }
136        $part = $this->renderJoin();
137        if ($part) {
138            $sql .= $part . \PHP_EOL;
139        }
140        $part = $this->renderWhere();
141        if ($part) {
142            $sql .= $part . \PHP_EOL;
143        }
144        $part = $this->renderOrderBy();
145        if ($part) {
146            $sql .= $part . \PHP_EOL;
147        }
148        $part = $this->renderLimit();
149        if ($part) {
150            $sql .= $part . \PHP_EOL;
151        }
152        return $sql;
153    }
154
155    /**
156     * Runs the DELETE statement.
157     *
158     * @return int|string The number of affected rows
159     */
160    public function run() : int|string
161    {
162        return $this->database->exec($this->sql());
163    }
164}