Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
12 / 12
CRAP
100.00% covered (success)
100.00%
1 / 1
CreateSchema
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
12 / 12
20
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
 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
 schema
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 renderSchema
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 charset
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 renderCharset
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 collate
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 renderCollate
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 sql
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 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\Statement;
13use LogicException;
14
15/**
16 * Class CreateSchema.
17 *
18 * @see https://mariadb.com/kb/en/create-database/
19 *
20 * @package database
21 */
22class CreateSchema extends Statement
23{
24    /**
25     * @return static
26     */
27    public function orReplace() : static
28    {
29        $this->sql['or_replace'] = true;
30        return $this;
31    }
32
33    protected function renderOrReplace() : ?string
34    {
35        if ( ! isset($this->sql['or_replace'])) {
36            return null;
37        }
38        return ' OR REPLACE';
39    }
40
41    /**
42     * @return static
43     */
44    public function ifNotExists() : static
45    {
46        $this->sql['if_not_exists'] = true;
47        return $this;
48    }
49
50    protected function renderIfNotExists() : ?string
51    {
52        if ( ! isset($this->sql['if_not_exists'])) {
53            return null;
54        }
55        if (isset($this->sql['or_replace'])) {
56            throw new LogicException(
57                'Clauses OR REPLACE and IF NOT EXISTS can not be used together'
58            );
59        }
60        return ' IF NOT EXISTS';
61    }
62
63    /**
64     * @param string $schemaName
65     *
66     * @return static
67     */
68    public function schema(string $schemaName) : static
69    {
70        $this->sql['schema'] = $schemaName;
71        return $this;
72    }
73
74    protected function renderSchema() : string
75    {
76        if (isset($this->sql['schema'])) {
77            return ' ' . $this->database->protectIdentifier($this->sql['schema']);
78        }
79        throw new LogicException('SCHEMA name must be set');
80    }
81
82    /**
83     * @param string $charset
84     *
85     * @return static
86     */
87    public function charset(string $charset) : static
88    {
89        $this->sql['charset'] = $charset;
90        return $this;
91    }
92
93    protected function renderCharset() : ?string
94    {
95        if ( ! isset($this->sql['charset'])) {
96            return null;
97        }
98        $charset = $this->database->quote($this->sql['charset']);
99        return " CHARACTER SET = {$charset}";
100    }
101
102    /**
103     * @param string $collation
104     *
105     * @return static
106     */
107    public function collate(string $collation) : static
108    {
109        $this->sql['collation'] = $collation;
110        return $this;
111    }
112
113    protected function renderCollate() : ?string
114    {
115        if ( ! isset($this->sql['collation'])) {
116            return null;
117        }
118        $collation = $this->database->quote($this->sql['collation']);
119        return " COLLATE = {$collation}";
120    }
121
122    public function sql() : string
123    {
124        $sql = 'CREATE' . $this->renderOrReplace();
125        $sql .= ' SCHEMA' . $this->renderIfNotExists();
126        $sql .= $this->renderSchema() . \PHP_EOL;
127        $part = $this->renderCharset();
128        if ($part) {
129            $sql .= $part . \PHP_EOL;
130        }
131        $part = $this->renderCollate();
132        if ($part) {
133            $sql .= $part . \PHP_EOL;
134        }
135        return $sql;
136    }
137
138    /**
139     * Runs the CREATE SCHEMA statement.
140     *
141     * @return int|string The number of affected rows
142     */
143    public function run() : int|string
144    {
145        return $this->database->exec($this->sql());
146    }
147}