Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
33 / 33 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
Create | |
100.00% |
33 / 33 |
|
100.00% |
7 / 7 |
18 | |
100.00% |
1 / 1 |
ifNotExists | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setEngine | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getEngine | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setCharset | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getCharset | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
render | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
12 | |||
__toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Db\Sql\Schema; |
15 | |
16 | /** |
17 | * Schema CREATE table class |
18 | * |
19 | * @category Pop |
20 | * @package Pop\Db |
21 | * @author Nick Sagona, III <dev@nolainteractive.com> |
22 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 6.5.0 |
25 | */ |
26 | class Create extends AbstractStructure |
27 | { |
28 | |
29 | /** |
30 | * IF NOT EXISTS flag |
31 | * @var bool |
32 | */ |
33 | protected bool $ifNotExists = false; |
34 | |
35 | /** |
36 | * Table engine (MySQL only) |
37 | * @var string |
38 | */ |
39 | protected string $engine = 'InnoDB'; |
40 | |
41 | /** |
42 | * Table charset (MySQL only) |
43 | * @var string |
44 | */ |
45 | protected string $charset = 'utf8'; |
46 | |
47 | /** |
48 | * Set the IF NOT EXISTS flag |
49 | * |
50 | * @return Create |
51 | */ |
52 | public function ifNotExists(): Create |
53 | { |
54 | $this->ifNotExists = true; |
55 | return $this; |
56 | } |
57 | |
58 | /** |
59 | * Set the table engine (MySQL only) |
60 | * |
61 | * @param string $engine |
62 | * @return Create |
63 | */ |
64 | public function setEngine(string $engine): Create |
65 | { |
66 | $this->engine = $engine; |
67 | return $this; |
68 | } |
69 | |
70 | /** |
71 | * Get the table engine (MySQL only) |
72 | * |
73 | * @return string |
74 | */ |
75 | public function getEngine(): string |
76 | { |
77 | return $this->engine; |
78 | } |
79 | |
80 | /** |
81 | * Set the table charset (MySQL only) |
82 | * |
83 | * @param string $charset |
84 | * @return Create |
85 | */ |
86 | public function setCharset(string $charset): Create |
87 | { |
88 | $this->charset = $charset; |
89 | return $this; |
90 | } |
91 | |
92 | /** |
93 | * Get the table charset (MySQL only) |
94 | * |
95 | * @return string |
96 | */ |
97 | public function getCharset(): string |
98 | { |
99 | return $this->charset; |
100 | } |
101 | |
102 | /** |
103 | * Render the table schema |
104 | * |
105 | * @return string |
106 | */ |
107 | public function render(): string |
108 | { |
109 | $schema = ''; |
110 | |
111 | // Create PGSQL sequence |
112 | if (($this->hasIncrement()) && ($this->isPgsql())) { |
113 | $schema .= Formatter\Table::createPgsqlSequences($this->getIncrement(), $this->table, $this->columns); |
114 | } |
115 | |
116 | /* |
117 | * START CREATE TABLE |
118 | */ |
119 | $schema .= 'CREATE TABLE ' . ((($this->ifNotExists) && ($this->dbType != self::SQLSRV)) ? 'IF NOT EXISTS ' : null) . |
120 | $this->quoteId($this->table) . ' (' . PHP_EOL; |
121 | |
122 | // Iterate over the columns |
123 | $i = 0; |
124 | $increment = null; |
125 | foreach ($this->columns as $name => $column) { |
126 | if ($column['increment'] !== false) { |
127 | $increment = $column['increment']; |
128 | } |
129 | $schema .= (($i != 0) ? ',' . PHP_EOL : null) . ' ' . $this->getColumnSchema($name, $column); |
130 | $i++; |
131 | } |
132 | |
133 | // Format primary key schema |
134 | if ($this->hasPrimary()) { |
135 | $schema .= Formatter\Table::formatPrimarySchema($this->dbType, $this->getPrimary(true)); |
136 | } |
137 | |
138 | $schema .= Formatter\Table::formatEndOfTable($this->dbType, $this->engine, $this->charset, $increment); |
139 | |
140 | /* |
141 | * END CREATE TABLE |
142 | */ |
143 | |
144 | // Assign PGSQL or SQLITE sequences |
145 | if ($this->hasIncrement()) { |
146 | $schema .= Formatter\Table::createSequences( |
147 | $this->dbType, $this->getIncrement(), $this->quoteId($this->table), $this->columns |
148 | ); |
149 | } |
150 | |
151 | // Add indices |
152 | if (count($this->indices) > 0) { |
153 | $schema .= Formatter\Table::createIndices($this->indices, $this->table, $this); |
154 | } |
155 | |
156 | // Add constraints |
157 | if (count($this->constraints) > 0) { |
158 | $schema .= Formatter\Table::createConstraints($this->constraints, $this->table, $this); |
159 | } |
160 | |
161 | return $schema . PHP_EOL; |
162 | } |
163 | |
164 | /** |
165 | * Render the table schema to string |
166 | * |
167 | * @return string |
168 | */ |
169 | public function __toString(): string |
170 | { |
171 | return $this->render(); |
172 | } |
173 | |
174 | } |