Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
55 / 55 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
Table | |
100.00% |
55 / 55 |
|
100.00% |
6 / 6 |
23 | |
100.00% |
1 / 1 |
createPgsqlSequences | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
formatPrimarySchema | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
formatEndOfTable | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 | |||
createSequences | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
6 | |||
createIndices | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
createConstraints | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 |
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\Formatter; |
15 | |
16 | use Pop\Db\Sql; |
17 | use Pop\Db\Sql\Schema\AbstractStructure; |
18 | |
19 | /** |
20 | * Schema table formatter class |
21 | * |
22 | * @category Pop |
23 | * @package Pop\Db |
24 | * @author Nick Sagona, III <dev@nolainteractive.com> |
25 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
26 | * @license http://www.popphp.org/license New BSD License |
27 | * @version 6.5.0 |
28 | */ |
29 | class Table extends AbstractFormatter |
30 | { |
31 | |
32 | /** |
33 | * Create PostgreSQL sequences |
34 | * |
35 | * @param array $increment |
36 | * @param string $table |
37 | * @param array $columns |
38 | * @return string |
39 | */ |
40 | public static function createPgsqlSequences(array $increment, string $table, array $columns): string |
41 | { |
42 | $sequences = ''; |
43 | |
44 | foreach ($increment as $name) { |
45 | $sequences .= 'CREATE SEQUENCE ' . $table . '_' . $name . '_seq START ' . |
46 | (int)$columns[$name]['increment'] . ';' . PHP_EOL; |
47 | } |
48 | |
49 | return $sequences . PHP_EOL; |
50 | } |
51 | |
52 | /** |
53 | * Format primary key schema |
54 | * |
55 | * @param string $dbType |
56 | * @param array $primary |
57 | * @return string |
58 | */ |
59 | public static function formatPrimarySchema(string $dbType, array $primary): string |
60 | { |
61 | $primarySchema = ''; |
62 | |
63 | if ($dbType != Sql::SQLSRV) { |
64 | $primarySchema .= ($dbType == Sql::SQLITE) ? |
65 | ',' . PHP_EOL . ' UNIQUE (' . implode(', ', $primary) . ')' : |
66 | ',' . PHP_EOL . ' PRIMARY KEY (' . implode(', ', $primary) . ')'; |
67 | } |
68 | |
69 | return $primarySchema; |
70 | } |
71 | |
72 | /** |
73 | * Format end of table |
74 | * |
75 | * @param string $dbType |
76 | * @param ?string $engine |
77 | * @param ?string $charset |
78 | * @param ?int $increment |
79 | * @return string |
80 | */ |
81 | public static function formatEndOfTable(string $dbType, ?string $engine = null, ?string $charset = null, ?int $increment = null): string |
82 | { |
83 | $sql = PHP_EOL . ')'; |
84 | |
85 | if ($dbType == Sql::MYSQL) { |
86 | if ($engine !== null) { |
87 | $sql .= ' ENGINE=' . $engine; |
88 | } |
89 | if ($charset !== null) { |
90 | $sql .= ' DEFAULT CHARSET=' . $charset; |
91 | } |
92 | if ($increment !== null) { |
93 | $sql .= ' AUTO_INCREMENT=' . (int)$increment; |
94 | } |
95 | $sql .= ';' . PHP_EOL . PHP_EOL; |
96 | } else { |
97 | $sql .= ';' . PHP_EOL . PHP_EOL; |
98 | } |
99 | |
100 | return $sql; |
101 | } |
102 | |
103 | /** |
104 | * Create sequences |
105 | * |
106 | * @param string $dbType |
107 | * @param array $increment |
108 | * @param string $table |
109 | * @param array $columns |
110 | * @return string |
111 | */ |
112 | public static function createSequences(string $dbType, array $increment, string $table, array $columns): string |
113 | { |
114 | $sequences = ''; |
115 | |
116 | if ($dbType == Sql::PGSQL) { |
117 | foreach ($increment as $name) { |
118 | $sequences .= 'ALTER SEQUENCE ' . self::unquoteId($table) . '_' . $name . '_seq OWNED BY ' . |
119 | $table . '."' . $name . '";' . PHP_EOL; |
120 | } |
121 | $sequences .= PHP_EOL; |
122 | } else if ($dbType == Sql::SQLITE) { |
123 | foreach ($increment as $name) { |
124 | $start = (int)$columns[$name]['increment']; |
125 | if (substr((string)$start, -1) == '1') { |
126 | $start -= 1; |
127 | } |
128 | $sequences .= 'INSERT INTO "sqlite_sequence" ("name", "seq") ' . |
129 | 'VALUES (' . $table . ', ' . $start . ');' . PHP_EOL; |
130 | } |
131 | $sequences .= PHP_EOL; |
132 | } |
133 | |
134 | return $sequences; |
135 | } |
136 | |
137 | /** |
138 | * Create indices |
139 | * |
140 | * @param array $indices |
141 | * @param string $table |
142 | * @param AbstractStructure $schema |
143 | * @return string |
144 | */ |
145 | public static function createIndices(array $indices, string $table, AbstractStructure $schema): string |
146 | { |
147 | $indexSchema = ''; |
148 | |
149 | foreach ($indices as $name => $index) { |
150 | foreach ($index['column'] as $i => $column) { |
151 | $index['column'][$i] = $schema->quoteId($column); |
152 | } |
153 | |
154 | if ($index['type'] != 'primary') { |
155 | $indexSchema .= 'CREATE ' . (($index['type'] == 'unique') ? 'UNIQUE ' : null) . 'INDEX ' . |
156 | $schema->quoteId($name) . ' ON ' . $schema->quoteId($table) . |
157 | ' (' . implode(', ', $index['column']) . ');' . PHP_EOL; |
158 | } |
159 | } |
160 | |
161 | return $indexSchema; |
162 | } |
163 | |
164 | /** |
165 | * Create constraints |
166 | * |
167 | * @param array $constraints |
168 | * @param string $table |
169 | * @param AbstractStructure $schema |
170 | * @return string |
171 | */ |
172 | public static function createConstraints(array $constraints, string $table, AbstractStructure $schema): string |
173 | { |
174 | $constraintSchema = PHP_EOL; |
175 | |
176 | foreach ($constraints as $name => $constraint) { |
177 | $constraintSchema .= 'ALTER TABLE ' . $schema->quoteId($table) . |
178 | ' ADD CONSTRAINT ' . $schema->quoteId($name) . |
179 | ' FOREIGN KEY (' . $schema->quoteId($constraint['column']) . ')' . |
180 | ' REFERENCES ' . $schema->quoteId($constraint['references']) . |
181 | ' (' . $schema->quoteId($constraint['on']) . ')' . ' ON DELETE ' . |
182 | $constraint['delete'] . ' ON UPDATE CASCADE;' . PHP_EOL; |
183 | } |
184 | |
185 | return $constraintSchema; |
186 | } |
187 | |
188 | } |