• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aplus-framework / database / 4905248682

pending completion
4905248682

push

github

Natan Felles
Merge branch 'jobs' into development

2406 of 2411 relevant lines covered (99.79%)

11.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Definition/DropTable.php
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
 */
10
namespace Framework\Database\Definition;
11

12
use Framework\Database\Statement;
13
use InvalidArgumentException;
14
use LogicException;
15

16
/**
17
 * Class DropTable.
18
 *
19
 * @see https://mariadb.com/kb/en/drop-table/
20
 *
21
 * @package database
22
 */
23
class DropTable extends Statement
24
{
25
    /**
26
     * @return static
27
     */
28
    public function temporary() : static
29
    {
30
        $this->sql['temporary'] = true;
1✔
31
        return $this;
1✔
32
    }
33

34
    protected function renderTemporary() : ?string
35
    {
36
        if ( ! isset($this->sql['temporary'])) {
8✔
37
            return null;
7✔
38
        }
39
        return ' TEMPORARY';
1✔
40
    }
41

42
    /**
43
     * @return static
44
     */
45
    public function ifExists() : static
46
    {
47
        $this->sql['if_exists'] = true;
1✔
48
        return $this;
1✔
49
    }
50

51
    protected function renderIfExists() : ?string
52
    {
53
        if ( ! isset($this->sql['if_exists'])) {
8✔
54
            return null;
7✔
55
        }
56
        return ' IF EXISTS';
1✔
57
    }
58

59
    /**
60
     * @param string $comment
61
     *
62
     * @return static
63
     */
64
    public function commentToSave(string $comment) : static
65
    {
66
        $this->sql['comment'] = $comment;
1✔
67
        return $this;
1✔
68
    }
69

70
    protected function renderCommentToSave() : ?string
71
    {
72
        if ( ! isset($this->sql['comment'])) {
8✔
73
            return null;
7✔
74
        }
75
        $comment = \strtr($this->sql['comment'], ['*/' => '* /']);
1✔
76
        return " /* {$comment} */";
1✔
77
    }
78

79
    /**
80
     * @param string $table
81
     * @param string ...$tables
82
     *
83
     * @return static
84
     */
85
    public function table(string $table, string ...$tables) : static
86
    {
87
        $this->sql['tables'] = $tables ? \array_merge([$table], $tables) : [$table];
8✔
88
        return $this;
8✔
89
    }
90

91
    protected function renderTables() : string
92
    {
93
        if ( ! isset($this->sql['tables'])) {
8✔
94
            throw new LogicException('Table names can not be empty');
1✔
95
        }
96
        $tables = $this->sql['tables'];
7✔
97
        foreach ($tables as &$table) {
7✔
98
            $table = $this->database->protectIdentifier($table);
7✔
99
        }
100
        unset($table);
7✔
101
        $tables = \implode(', ', $tables);
7✔
102
        return " {$tables}";
7✔
103
    }
104

105
    /**
106
     * @param int $seconds
107
     *
108
     * @return static
109
     */
110
    public function wait(int $seconds) : static
111
    {
112
        $this->sql['wait'] = $seconds;
2✔
113
        return $this;
2✔
114
    }
115

116
    public function renderWait() : ?string
117
    {
118
        if ( ! isset($this->sql['wait'])) {
7✔
119
            return null;
5✔
120
        }
121
        if ($this->sql['wait'] < 0) {
2✔
122
            throw new InvalidArgumentException(
1✔
123
                "Invalid WAIT value: {$this->sql['wait']}"
1✔
124
            );
1✔
125
        }
126
        return " WAIT {$this->sql['wait']}";
1✔
127
    }
128

129
    public function sql() : string
130
    {
131
        $sql = 'DROP' . $this->renderTemporary();
8✔
132
        $sql .= ' TABLE' . $this->renderIfExists();
8✔
133
        $sql .= $this->renderCommentToSave();
8✔
134
        $sql .= $this->renderTables() . \PHP_EOL;
8✔
135
        $part = $this->renderWait();
7✔
136
        if ($part) {
6✔
137
            $sql .= $part . \PHP_EOL;
1✔
138
        }
139
        return $sql;
6✔
140
    }
141

142
    /**
143
     * Runs the DROP TABLE statement.
144
     *
145
     * @return int|string The number of affected rows
146
     */
147
    public function run() : int|string
148
    {
149
        return $this->database->exec($this->sql());
1✔
150
    }
151
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc