Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
12 / 12
CRAP
100.00% covered (success)
100.00%
1 / 1
CreateTable
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
12 / 12
18
100.00% covered (success)
100.00%
1 / 1
 orReplace
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 renderOrReplace
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 temporary
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 renderTemporary
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 ifNotExists
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 renderIfNotExists
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 table
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 renderTable
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 definition
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 renderDefinition
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 sql
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 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\Definition;
11
12use Framework\Database\Definition\Table\TableDefinition;
13use Framework\Database\Definition\Table\TableStatement;
14use LogicException;
15
16/**
17 * Class CreateTable.
18 *
19 * @see https://mariadb.com/kb/en/create-table/
20 *
21 * @package database
22 */
23class 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;
35        return $this;
36    }
37
38    protected function renderOrReplace() : ?string
39    {
40        if ( ! isset($this->sql['or_replace'])) {
41            return null;
42        }
43        return ' OR REPLACE';
44    }
45
46    /**
47     * @return static
48     */
49    public function temporary() : static
50    {
51        $this->sql['temporary'] = true;
52        return $this;
53    }
54
55    protected function renderTemporary() : ?string
56    {
57        if ( ! isset($this->sql['temporary'])) {
58            return null;
59        }
60        return ' TEMPORARY';
61    }
62
63    /**
64     * @return static
65     */
66    public function ifNotExists() : static
67    {
68        $this->sql['if_not_exists'] = true;
69        return $this;
70    }
71
72    protected function renderIfNotExists() : ?string
73    {
74        if ( ! isset($this->sql['if_not_exists'])) {
75            return null;
76        }
77        if (isset($this->sql['or_replace'])) {
78            throw new LogicException(
79                'Clauses OR REPLACE and IF NOT EXISTS can not be used together'
80            );
81        }
82        return ' IF NOT EXISTS';
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;
93        return $this;
94    }
95
96    protected function renderTable() : string
97    {
98        if (isset($this->sql['table'])) {
99            return ' ' . $this->database->protectIdentifier($this->sql['table']);
100        }
101        throw new LogicException('TABLE name must be set');
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;
112        return $this;
113    }
114
115    protected function renderDefinition() : string
116    {
117        if ( ! isset($this->sql['definition'])) {
118            throw new LogicException('Table definition must be set');
119        }
120        $definition = new TableDefinition($this->database);
121        $this->sql['definition']($definition);
122        return $definition->sql();
123    }
124
125    public function sql() : string
126    {
127        $sql = 'CREATE' . $this->renderOrReplace() . $this->renderTemporary();
128        $sql .= ' TABLE' . $this->renderIfNotExists();
129        $sql .= $this->renderTable() . ' (' . \PHP_EOL;
130        $sql .= $this->renderDefinition() . \PHP_EOL;
131        $sql .= ')' . $this->renderOptions();
132        return $sql;
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());
143    }
144}