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

aplus-framework / database / 14372198759

27 Jan 2025 09:09PM UTC coverage: 99.02%. Remained the same
14372198759

push

github

natanfelles
Merge branch 'development'

2426 of 2450 relevant lines covered (99.02%)

10.85 hits per line

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

100.0
/src/Definition/CreateTable.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\Definition\Table\TableDefinition;
13
use Framework\Database\Definition\Table\TableStatement;
14
use LogicException;
15

16
/**
17
 * Class CreateTable.
18
 *
19
 * @see https://mariadb.com/kb/en/create-table/
20
 *
21
 * @package database
22
 */
23
class CreateTable extends TableStatement
24
{
25
    /**
26
     * Adds a OR REPLACE part.
27
     *
28
     * WARNING: This feature is MariaDB only. It is not compatible with MySQL.
29
     *
30
     * @return static
31
     */
32
    public function orReplace() : static
33
    {
34
        $this->sql['or_replace'] = true;
3✔
35
        return $this;
3✔
36
    }
37

38
    protected function renderOrReplace() : ?string
39
    {
40
        if (!isset($this->sql['or_replace'])) {
12✔
41
            return null;
9✔
42
        }
43
        return ' OR REPLACE';
3✔
44
    }
45

46
    /**
47
     * @return static
48
     */
49
    public function temporary() : static
50
    {
51
        $this->sql['temporary'] = true;
1✔
52
        return $this;
1✔
53
    }
54

55
    protected function renderTemporary() : ?string
56
    {
57
        if (!isset($this->sql['temporary'])) {
12✔
58
            return null;
11✔
59
        }
60
        return ' TEMPORARY';
1✔
61
    }
62

63
    /**
64
     * @return static
65
     */
66
    public function ifNotExists() : static
67
    {
68
        $this->sql['if_not_exists'] = true;
3✔
69
        return $this;
3✔
70
    }
71

72
    protected function renderIfNotExists() : ?string
73
    {
74
        if (!isset($this->sql['if_not_exists'])) {
12✔
75
            return null;
9✔
76
        }
77
        if (isset($this->sql['or_replace'])) {
3✔
78
            throw new LogicException(
2✔
79
                'Clauses OR REPLACE and IF NOT EXISTS can not be used together'
2✔
80
            );
2✔
81
        }
82
        return ' IF NOT EXISTS';
1✔
83
    }
84

85
    /**
86
     * @param string $tableName
87
     *
88
     * @return static
89
     */
90
    public function table(string $tableName) : static
91
    {
92
        $this->sql['table'] = $tableName;
12✔
93
        return $this;
12✔
94
    }
95

96
    protected function renderTable() : string
97
    {
98
        if (isset($this->sql['table'])) {
10✔
99
            return ' ' . $this->database->protectIdentifier($this->sql['table']);
9✔
100
        }
101
        throw new LogicException('TABLE name must be set');
1✔
102
    }
103

104
    /**
105
     * @param callable $definition
106
     *
107
     * @return static
108
     */
109
    public function definition(callable $definition) : static
110
    {
111
        $this->sql['definition'] = $definition;
10✔
112
        return $this;
10✔
113
    }
114

115
    protected function renderDefinition() : string
116
    {
117
        if (!isset($this->sql['definition'])) {
9✔
118
            throw new LogicException('Table definition must be set');
1✔
119
        }
120
        $definition = new TableDefinition($this->database);
8✔
121
        $this->sql['definition']($definition);
8✔
122
        return $definition->sql();
8✔
123
    }
124

125
    public function sql() : string
126
    {
127
        $sql = 'CREATE' . $this->renderOrReplace() . $this->renderTemporary();
12✔
128
        $sql .= ' TABLE' . $this->renderIfNotExists();
12✔
129
        $sql .= $this->renderTable() . ' (' . \PHP_EOL;
10✔
130
        $sql .= $this->renderDefinition() . \PHP_EOL;
9✔
131
        $sql .= ')' . $this->renderOptions();
8✔
132
        return $sql;
8✔
133
    }
134

135
    /**
136
     * Runs the CREATE TABLE statement.
137
     *
138
     * @return int|string The number of affected rows
139
     */
140
    public function run() : int | string
141
    {
142
        return $this->database->exec($this->sql());
1✔
143
    }
144
}
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