Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
50.00% |
34 / 68 |
|
14.29% |
2 / 14 |
CRAP | |
0.00% |
0 / 1 |
| Grammar | |
50.00% |
34 / 68 |
|
14.29% |
2 / 14 |
178.50 | |
0.00% |
0 / 1 |
| compileComponents | |
63.41% |
26 / 41 |
|
0.00% |
0 / 1 |
26.02 | |||
| compileSelectTables | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| compileSelectFields | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| compileCreateFields | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| compilePostQueries | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| compileAlterTable | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| compileAlterColumn | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| compileAlterAdd | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| addColumn | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addConstraint | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| compileCreateTable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| compileCreateTableSettings | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| compileDropDatabase | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| compileDropTable | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\DataStorage\Database\Schema\Grammar |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\DataStorage\Database\Schema\Grammar; |
| 16 | |
| 17 | use phpOMS\DataStorage\Database\BuilderAbstract; |
| 18 | use phpOMS\DataStorage\Database\GrammarAbstract; |
| 19 | use phpOMS\DataStorage\Database\Schema\Builder as SchemaBuilder; |
| 20 | use phpOMS\DataStorage\Database\Schema\QueryType; |
| 21 | |
| 22 | /** |
| 23 | * Database query grammar. |
| 24 | * |
| 25 | * @package phpOMS\DataStorage\Database\Schema\Grammar |
| 26 | * @license OMS License 2.0 |
| 27 | * @link https://jingga.app |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | class Grammar extends GrammarAbstract |
| 31 | { |
| 32 | /** |
| 33 | * {@inheritdoc} |
| 34 | */ |
| 35 | protected function compileComponents(BuilderAbstract $query) : array |
| 36 | { |
| 37 | /** @var SchemaBuilder $query */ |
| 38 | |
| 39 | $sql = []; |
| 40 | switch ($query->getType()) { |
| 41 | case QueryType::DROP_DATABASE: |
| 42 | if (empty($query->dropDatabase)) { |
| 43 | break; |
| 44 | } |
| 45 | |
| 46 | $sql[] = $this->compileDropDatabase($query, $query->dropDatabase); |
| 47 | |
| 48 | break; |
| 49 | case QueryType::TABLES: |
| 50 | if (empty($query->selectTables)) { |
| 51 | break; |
| 52 | } |
| 53 | |
| 54 | $sql[] = $this->compileSelectTables($query, $query->selectTables); |
| 55 | |
| 56 | break; |
| 57 | case QueryType::FIELDS: |
| 58 | if (empty($query->selectFields)) { |
| 59 | break; |
| 60 | } |
| 61 | |
| 62 | $sql[] = $this->compileSelectFields($query, $query->selectFields); |
| 63 | |
| 64 | break; |
| 65 | case QueryType::CREATE_TABLE: |
| 66 | if (empty($query->createTable)) { |
| 67 | break; |
| 68 | } |
| 69 | |
| 70 | $sql[] = $this->compileCreateTable($query, $query->createTable); |
| 71 | $sql[] = $this->compileCreateFields($query, $query->createFields); |
| 72 | |
| 73 | if (empty($query->createTableSettings)) { |
| 74 | break; |
| 75 | } |
| 76 | |
| 77 | $sql[] = $this->compileCreateTableSettings($query, $query->createTableSettings); |
| 78 | |
| 79 | break; |
| 80 | case QueryType::DROP_TABLE: |
| 81 | if (empty($query->dropTable)) { |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | $sql[] = $this->compileDropTable($query, $query->dropTable); |
| 86 | |
| 87 | break; |
| 88 | case QueryType::ALTER: |
| 89 | $sql[] = $this->compileAlterTable($query, $query->alterTable); |
| 90 | $sql[] = $this->compileAlterColumn($query, $query->alterColumn); |
| 91 | $sql[] = $this->compileAlterAdd($query, $query->alterAdd); |
| 92 | // $sql[] = $this->compileAlterRename($query, $query->alterRename); |
| 93 | // $sql[] = $this->compileAlterRemove($query, $query->alterRemove); |
| 94 | |
| 95 | break; |
| 96 | case QueryType::RAW: |
| 97 | $sql[] = $query->raw; |
| 98 | |
| 99 | break; |
| 100 | default: |
| 101 | return []; |
| 102 | } |
| 103 | |
| 104 | return $sql; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Compile the select tables. |
| 109 | * |
| 110 | * @param SchemaBuilder $query Query |
| 111 | * @param array $tables Tables |
| 112 | * |
| 113 | * @return string |
| 114 | * |
| 115 | * @since 1.0.0 |
| 116 | */ |
| 117 | protected function compileSelectTables(SchemaBuilder $query, array $tables) : string |
| 118 | { |
| 119 | return ''; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Compile the select fields from table. |
| 124 | * |
| 125 | * @param SchemaBuilder $query Query |
| 126 | * @param array $table Table |
| 127 | * |
| 128 | * @return string |
| 129 | * |
| 130 | * @since 1.0.0 |
| 131 | */ |
| 132 | protected function compileSelectFields(SchemaBuilder $query, string $table) : string |
| 133 | { |
| 134 | return ''; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Compile the create fields. |
| 139 | * |
| 140 | * @param SchemaBuilder $query Query |
| 141 | * @param array $fields Fields |
| 142 | * |
| 143 | * @return string |
| 144 | * |
| 145 | * @since 1.0.0 |
| 146 | */ |
| 147 | protected function compileCreateFields(SchemaBuilder $query, array $fields) : string |
| 148 | { |
| 149 | return ''; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Compile the select tables. |
| 154 | * |
| 155 | * @param SchemaBuilder $query Query |
| 156 | * |
| 157 | * @return string |
| 158 | * |
| 159 | * @since 1.0.0 |
| 160 | */ |
| 161 | public function compilePostQueries(BuilderAbstract $query) : array |
| 162 | { |
| 163 | return []; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Compile alter table query. |
| 168 | * |
| 169 | * @param BuilderAbstract $query Query |
| 170 | * @param string $table Table to alter |
| 171 | * |
| 172 | * @return string |
| 173 | * |
| 174 | * @since 1.0.0 |
| 175 | */ |
| 176 | protected function compileAlterTable(BuilderAbstract $query, string $table) : string |
| 177 | { |
| 178 | return 'ALTER TABLE ' . $this->expressionizeTableColumn([$table]); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Compile alter column query. |
| 183 | * |
| 184 | * @param BuilderAbstract $query Query |
| 185 | * @param string $column Column to alter |
| 186 | * |
| 187 | * @return string |
| 188 | * |
| 189 | * @since 1.0.0 |
| 190 | */ |
| 191 | protected function compileAlterColumn(BuilderAbstract $query, string $column) : string |
| 192 | { |
| 193 | return ''; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Compile alter add query. |
| 198 | * |
| 199 | * @param BuilderAbstract $query Query |
| 200 | * @param array $add Add data |
| 201 | * |
| 202 | * @return string |
| 203 | * |
| 204 | * @since 1.0.0 |
| 205 | */ |
| 206 | protected function compileAlterAdd(BuilderAbstract $query, array $add) : string |
| 207 | { |
| 208 | switch ($add['type']) { |
| 209 | case 'COLUMN': |
| 210 | return $this->addColumn($add); |
| 211 | case 'CONSTRAINT': |
| 212 | return $this->addConstraint($add); |
| 213 | default: |
| 214 | return ''; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Add a new column. |
| 220 | * |
| 221 | * @param array $add Add data |
| 222 | * |
| 223 | * @return string |
| 224 | * |
| 225 | * @since 1.0.0 |
| 226 | */ |
| 227 | private function addColumn(array $add) : string |
| 228 | { |
| 229 | return 'ADD ' . $this->expressionizeTableColumn([$add['name']]) . ' ' . $add['datatype']; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Add a new constraint/foreign key. |
| 234 | * |
| 235 | * @param array $add Add data |
| 236 | * |
| 237 | * @return string |
| 238 | * |
| 239 | * @since 1.0.0 |
| 240 | */ |
| 241 | private function addConstraint(array $add) : string |
| 242 | { |
| 243 | return 'ADD' . (isset($add['constraint']) ? ' CONSTRAINT ' . $add['constraint'] : '') |
| 244 | . ' FOREIGN KEY (' . $this->expressionizeTableColumn([$add['key']]) . ') REFERENCES ' |
| 245 | . $this->expressionizeTableColumn([$add['foreignTable']]) |
| 246 | . ' (' . $this->expressionizeTableColumn([$add['foreignKey']]) . ')'; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Compile create table query. |
| 251 | * |
| 252 | * @param BuilderAbstract $query Query |
| 253 | * @param string $table Tables to drop |
| 254 | * |
| 255 | * @return string |
| 256 | * |
| 257 | * @since 1.0.0 |
| 258 | */ |
| 259 | protected function compileCreateTable(BuilderAbstract $query, string $table) : string |
| 260 | { |
| 261 | return 'CREATE TABLE IF NOT EXISTS ' . $this->expressionizeTableColumn([$table]); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Compile create table settings query. |
| 266 | * |
| 267 | * @param BuilderAbstract $query Query |
| 268 | * @param bool $settings Has settings |
| 269 | * |
| 270 | * @return string |
| 271 | * |
| 272 | * @since 1.0.0 |
| 273 | */ |
| 274 | protected function compileCreateTableSettings(BuilderAbstract $query, bool $settings) : string |
| 275 | { |
| 276 | return ''; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Compile drop query. |
| 281 | * |
| 282 | * @param BuilderAbstract $query Query |
| 283 | * @param string $database Tables to drop |
| 284 | * |
| 285 | * @return string |
| 286 | * |
| 287 | * @since 1.0.0 |
| 288 | */ |
| 289 | protected function compileDropDatabase(BuilderAbstract $query, string $database) : string |
| 290 | { |
| 291 | $expression = $this->expressionizeTableColumn([$database]); |
| 292 | |
| 293 | if ($expression === '') { |
| 294 | $expression = '*'; |
| 295 | } |
| 296 | |
| 297 | return 'DROP DATABASE ' . $expression; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Compile drop query. |
| 302 | * |
| 303 | * @param BuilderAbstract $query Query |
| 304 | * @param array $tables Tables to drop |
| 305 | * |
| 306 | * @return string |
| 307 | * |
| 308 | * @since 1.0.0 |
| 309 | */ |
| 310 | protected function compileDropTable(BuilderAbstract $query, array $tables) : string |
| 311 | { |
| 312 | $expression = $this->expressionizeTableColumn($tables); |
| 313 | |
| 314 | if ($expression === '') { |
| 315 | $expression = '*'; |
| 316 | } |
| 317 | |
| 318 | return 'DROP TABLE ' . $expression; |
| 319 | } |
| 320 | } |