dimtrovich /
php-db-dumper
| 1 | <?php |
||||
| 2 | |||||
| 3 | /** |
||||
| 4 | * This file is part of dimtrovich/db-dumper". |
||||
| 5 | * |
||||
| 6 | * (c) 2024 Dimitri Sitchet Tomkeu <[email protected]> |
||||
| 7 | * |
||||
| 8 | * For the full copyright and license information, please view |
||||
| 9 | * the LICENSE file that was distributed with this source code. |
||||
| 10 | */ |
||||
| 11 | |||||
| 12 | namespace Dimtrovich\DbDumper\Adapters; |
||||
| 13 | |||||
| 14 | use Dimtrovich\DbDumper\Exceptions\Exception; |
||||
| 15 | use Dimtrovich\DbDumper\Option; |
||||
| 16 | use PDO; |
||||
| 17 | |||||
| 18 | abstract class Factory |
||||
| 19 | { |
||||
| 20 | /** |
||||
| 21 | * Database connection PDO instance |
||||
| 22 | */ |
||||
| 23 | protected PDO $pdo; |
||||
| 24 | |||||
| 25 | /** |
||||
| 26 | * Option instance |
||||
| 27 | */ |
||||
| 28 | protected Option $option; |
||||
| 29 | |||||
| 30 | public function __construct(PDO $pdo, Option $option) |
||||
| 31 | { |
||||
| 32 | $this->pdo = $pdo; |
||||
| 33 | $this->option = $option; |
||||
| 34 | } |
||||
| 35 | |||||
| 36 | /** |
||||
| 37 | * Create an instance of compressor |
||||
| 38 | * |
||||
| 39 | * @internal |
||||
| 40 | * |
||||
| 41 | * @return static |
||||
| 42 | */ |
||||
| 43 | public static function create(PDO $pdo, Option $option) |
||||
| 44 | { |
||||
| 45 | $type = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME); |
||||
| 46 | |||||
| 47 | $class = __NAMESPACE__ . '\\' . ucfirst(strtolower($type)) . 'Adapter'; |
||||
| 48 | |||||
| 49 | if (! class_exists($class) || $class === self::class) { |
||||
| 50 | throw Exception::invalidAdapter($type); |
||||
| 51 | } |
||||
| 52 | |||||
| 53 | return new $class($pdo, $option); |
||||
| 54 | } |
||||
| 55 | |||||
| 56 | /** |
||||
| 57 | * Get information about a current database |
||||
| 58 | */ |
||||
| 59 | public function getDatabaseHeader(): string |
||||
| 60 | { |
||||
| 61 | $args = func_get_args(); |
||||
| 62 | |||||
| 63 | if (! isset($args[0])) { |
||||
| 64 | return ''; |
||||
| 65 | } |
||||
| 66 | |||||
| 67 | return '--' . PHP_EOL . |
||||
| 68 | "-- Current Database: `{$args[0]}`" . PHP_EOL . |
||||
| 69 | '--' . PHP_EOL . PHP_EOL; |
||||
| 70 | } |
||||
| 71 | |||||
| 72 | /** |
||||
| 73 | * Add sql to create and use database |
||||
| 74 | */ |
||||
| 75 | public function databases(string $databaseName): string |
||||
|
0 ignored issues
–
show
|
|||||
| 76 | { |
||||
| 77 | return ''; |
||||
| 78 | } |
||||
| 79 | |||||
| 80 | /** |
||||
| 81 | * Get table creation code from database |
||||
| 82 | */ |
||||
| 83 | abstract public function showCreateTable(string $tableName): string; |
||||
| 84 | |||||
| 85 | /** |
||||
| 86 | * Modify table creation code to add something according options |
||||
| 87 | */ |
||||
| 88 | public function createTable(array $row): string |
||||
|
0 ignored issues
–
show
The parameter
$row is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 89 | { |
||||
| 90 | return ''; |
||||
| 91 | } |
||||
| 92 | |||||
| 93 | /** |
||||
| 94 | * Get view creation code from database |
||||
| 95 | */ |
||||
| 96 | abstract public function showCreateView(string $viewName): string; |
||||
| 97 | |||||
| 98 | /** |
||||
| 99 | * Modify view creation code to add something according options |
||||
| 100 | */ |
||||
| 101 | public function createView(array $row): string |
||||
|
0 ignored issues
–
show
The parameter
$row is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 102 | { |
||||
| 103 | return ''; |
||||
| 104 | } |
||||
| 105 | |||||
| 106 | /** |
||||
| 107 | * Get trigger creation code from database |
||||
| 108 | */ |
||||
| 109 | public function showCreateTrigger(string $triggerName): string |
||||
|
0 ignored issues
–
show
The parameter
$triggerName is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 110 | { |
||||
| 111 | return ''; |
||||
| 112 | } |
||||
| 113 | |||||
| 114 | /** |
||||
| 115 | * Modify trigger creation code, delimiters, etc |
||||
| 116 | */ |
||||
| 117 | public function createTrigger(array $row): string |
||||
|
0 ignored issues
–
show
The parameter
$row is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 118 | { |
||||
| 119 | return ''; |
||||
| 120 | } |
||||
| 121 | |||||
| 122 | /** |
||||
| 123 | * Get procedure creation code from database |
||||
| 124 | */ |
||||
| 125 | public function showCreateProcedure(string $procedureName): string |
||||
|
0 ignored issues
–
show
The parameter
$procedureName is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 126 | { |
||||
| 127 | return ''; |
||||
| 128 | } |
||||
| 129 | |||||
| 130 | /** |
||||
| 131 | * Modify procedure creation code, add delimiters, etc |
||||
| 132 | */ |
||||
| 133 | public function createProcedure(array $row): string |
||||
|
0 ignored issues
–
show
The parameter
$row is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 134 | { |
||||
| 135 | return ''; |
||||
| 136 | } |
||||
| 137 | |||||
| 138 | /** |
||||
| 139 | * Get function creation code from database |
||||
| 140 | */ |
||||
| 141 | public function showCreateFunction(string $functionName): string |
||||
|
0 ignored issues
–
show
The parameter
$functionName is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 142 | { |
||||
| 143 | return ''; |
||||
| 144 | } |
||||
| 145 | |||||
| 146 | /** |
||||
| 147 | * Modify function creation code, add delimiters, etc |
||||
| 148 | */ |
||||
| 149 | public function createFunction(array $row): string |
||||
|
0 ignored issues
–
show
The parameter
$row is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 150 | { |
||||
| 151 | return ''; |
||||
| 152 | } |
||||
| 153 | |||||
| 154 | /** |
||||
| 155 | * Get event creation code from database |
||||
| 156 | */ |
||||
| 157 | public function showCreateEvent(string $eventName): string |
||||
|
0 ignored issues
–
show
The parameter
$eventName is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 158 | { |
||||
| 159 | return ''; |
||||
| 160 | } |
||||
| 161 | |||||
| 162 | /** |
||||
| 163 | * Modify event creation code to add something according options |
||||
| 164 | */ |
||||
| 165 | public function createEvent(array $row): string |
||||
|
0 ignored issues
–
show
The parameter
$row is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 166 | { |
||||
| 167 | return ''; |
||||
| 168 | } |
||||
| 169 | |||||
| 170 | /** |
||||
| 171 | * Get code to list tables of database |
||||
| 172 | */ |
||||
| 173 | abstract public function showTables(string $database): string; |
||||
| 174 | |||||
| 175 | /** |
||||
| 176 | * Get code to list views of database |
||||
| 177 | */ |
||||
| 178 | abstract public function showViews(string $database): string; |
||||
| 179 | |||||
| 180 | /** |
||||
| 181 | * Get code to list triggers of database |
||||
| 182 | */ |
||||
| 183 | abstract public function showTriggers(string $database): string; |
||||
| 184 | |||||
| 185 | /** |
||||
| 186 | * Get code to list columns of table |
||||
| 187 | */ |
||||
| 188 | abstract public function showColumns(string $table): string; |
||||
| 189 | |||||
| 190 | /** |
||||
| 191 | * Get code to list procedures of database |
||||
| 192 | */ |
||||
| 193 | public function showProcedures(string $database): string |
||||
|
0 ignored issues
–
show
The parameter
$database is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 194 | { |
||||
| 195 | return ''; |
||||
| 196 | } |
||||
| 197 | |||||
| 198 | /** |
||||
| 199 | * Get code to list functions of database |
||||
| 200 | */ |
||||
| 201 | public function showFunctions(string $database): string |
||||
|
0 ignored issues
–
show
The parameter
$database is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 202 | { |
||||
| 203 | return ''; |
||||
| 204 | } |
||||
| 205 | |||||
| 206 | /** |
||||
| 207 | * Get code to list events of database |
||||
| 208 | */ |
||||
| 209 | public function showEvents(string $database): string |
||||
|
0 ignored issues
–
show
The parameter
$database is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 210 | { |
||||
| 211 | return ''; |
||||
| 212 | } |
||||
| 213 | |||||
| 214 | /** |
||||
| 215 | * Get code to setup database transaction |
||||
| 216 | */ |
||||
| 217 | public function setupTransaction(): string |
||||
| 218 | { |
||||
| 219 | return ''; |
||||
| 220 | } |
||||
| 221 | |||||
| 222 | /** |
||||
| 223 | * Get code to start database transaction |
||||
| 224 | */ |
||||
| 225 | abstract public function startTransaction(): string; |
||||
| 226 | |||||
| 227 | /** |
||||
| 228 | * Get code to commit transaction |
||||
| 229 | */ |
||||
| 230 | abstract public function commitTransaction(): string; |
||||
| 231 | |||||
| 232 | /** |
||||
| 233 | * Perform lock table |
||||
| 234 | * |
||||
| 235 | * @return false|int |
||||
| 236 | */ |
||||
| 237 | public function lockTable(string $table) |
||||
|
0 ignored issues
–
show
The parameter
$table is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 238 | { |
||||
| 239 | return false; |
||||
| 240 | } |
||||
| 241 | |||||
| 242 | /** |
||||
| 243 | * Perform unlock table |
||||
| 244 | * |
||||
| 245 | * @return false|int |
||||
| 246 | */ |
||||
| 247 | public function unlockTable(string $table) |
||||
|
0 ignored issues
–
show
The parameter
$table is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 248 | { |
||||
| 249 | return false; |
||||
| 250 | } |
||||
| 251 | |||||
| 252 | /** |
||||
| 253 | * Get code to start lock table operation |
||||
| 254 | */ |
||||
| 255 | public function startAddLockTable(string $table): string |
||||
|
0 ignored issues
–
show
The parameter
$table is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 256 | { |
||||
| 257 | return PHP_EOL; |
||||
| 258 | } |
||||
| 259 | |||||
| 260 | /** |
||||
| 261 | * Get code to finish lock table operation |
||||
| 262 | */ |
||||
| 263 | public function endAddLockTable(string $table): string |
||||
|
0 ignored issues
–
show
The parameter
$table is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 264 | { |
||||
| 265 | return PHP_EOL; |
||||
| 266 | } |
||||
| 267 | |||||
| 268 | /** |
||||
| 269 | * Get code to start disabled keys operation |
||||
| 270 | */ |
||||
| 271 | public function startAddDisableKeys(string $table): string |
||||
|
0 ignored issues
–
show
The parameter
$table is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 272 | { |
||||
| 273 | return PHP_EOL; |
||||
| 274 | } |
||||
| 275 | |||||
| 276 | /** |
||||
| 277 | * Get code to finish disabled keys operation |
||||
| 278 | */ |
||||
| 279 | public function endAddDisableKeys(string $table): string |
||||
|
0 ignored issues
–
show
The parameter
$table is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 280 | { |
||||
| 281 | return PHP_EOL; |
||||
| 282 | } |
||||
| 283 | |||||
| 284 | /** |
||||
| 285 | * Get code to start disabled foreign keys operation |
||||
| 286 | */ |
||||
| 287 | public function startDisableForeignKeysCheck(): string |
||||
| 288 | { |
||||
| 289 | return ''; |
||||
| 290 | } |
||||
| 291 | |||||
| 292 | /** |
||||
| 293 | * Get code to finish disabled foreign keys operation |
||||
| 294 | */ |
||||
| 295 | public function endDisableForeignKeysCheck(): string |
||||
| 296 | { |
||||
| 297 | return ''; |
||||
| 298 | } |
||||
| 299 | |||||
| 300 | /** |
||||
| 301 | * Get code to start disabled autocommit operation |
||||
| 302 | */ |
||||
| 303 | public function startDisableAutocommit(): string |
||||
| 304 | { |
||||
| 305 | return PHP_EOL; |
||||
| 306 | } |
||||
| 307 | |||||
| 308 | /** |
||||
| 309 | * Get code to finish disabled autocommit operation |
||||
| 310 | */ |
||||
| 311 | public function endDisableAutocommit(): string |
||||
| 312 | { |
||||
| 313 | return PHP_EOL; |
||||
| 314 | } |
||||
| 315 | |||||
| 316 | /** |
||||
| 317 | * Get code to drop database |
||||
| 318 | */ |
||||
| 319 | public function addDropDatabase(string $database): string |
||||
|
0 ignored issues
–
show
The parameter
$database is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 320 | { |
||||
| 321 | return PHP_EOL; |
||||
| 322 | } |
||||
| 323 | |||||
| 324 | /** |
||||
| 325 | * Get code to drop trigger |
||||
| 326 | */ |
||||
| 327 | public function addDropTrigger(string $trigger): string |
||||
|
0 ignored issues
–
show
The parameter
$trigger is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 328 | { |
||||
| 329 | return PHP_EOL; |
||||
| 330 | } |
||||
| 331 | |||||
| 332 | /** |
||||
| 333 | * Get code to drop table |
||||
| 334 | */ |
||||
| 335 | public function dropTable(): string |
||||
| 336 | { |
||||
| 337 | $this->checkParameters(func_num_args(), $expected_num_args = 1, __METHOD__); |
||||
| 338 | $table = func_get_arg(0); |
||||
| 339 | |||||
| 340 | return "DROP TABLE IF EXISTS `{$table}`;" . PHP_EOL; |
||||
| 341 | } |
||||
| 342 | |||||
| 343 | /** |
||||
| 344 | * Get code to drop view |
||||
| 345 | */ |
||||
| 346 | public function dropView(string $view): string |
||||
|
0 ignored issues
–
show
The parameter
$view is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 347 | { |
||||
| 348 | return PHP_EOL; |
||||
| 349 | } |
||||
| 350 | |||||
| 351 | /** |
||||
| 352 | * Decode column metadata and fill info structure. |
||||
| 353 | * type, is_numeric and is_blob will always be available. |
||||
| 354 | * |
||||
| 355 | * @param array $colType Array returned from "SHOW COLUMNS FROM tableName" |
||||
| 356 | */ |
||||
| 357 | public function parseColumnType(array $colType): array |
||||
| 358 | { |
||||
| 359 | return $this->_parseColumnType($colType, []); |
||||
| 360 | } |
||||
| 361 | |||||
| 362 | /** |
||||
| 363 | * Parse the column type and extract detailed information. |
||||
| 364 | * |
||||
| 365 | * This function takes the column type information and an optional mapping of types, |
||||
| 366 | * and returns an array with detailed information about the column's properties. |
||||
| 367 | * |
||||
| 368 | * @param array $colType An array containing the column type information. |
||||
| 369 | * Expected to have either a 'type' or 'Type' key. |
||||
| 370 | * @param array $mapTypes An optional array containing mappings for numerical and blob types. |
||||
| 371 | * Expected to have 'numerical' and 'blob' keys as arrays. |
||||
| 372 | * |
||||
| 373 | * @return array An array containing parsed column information including: |
||||
| 374 | * - type: The base type of the column |
||||
| 375 | * - length: The length or precision of the column (if applicable) |
||||
| 376 | * - attributes: Any additional attributes of the column |
||||
| 377 | * - type_sql: The full SQL type definition |
||||
| 378 | * - is_numeric: Boolean indicating if the type is numeric |
||||
| 379 | * - is_blob: Boolean indicating if the type is a blob |
||||
| 380 | * - is_virtual: Boolean indicating if the column is virtual (always false in this implementation) |
||||
| 381 | */ |
||||
| 382 | protected function _parseColumnType(array $colType, array $mapTypes = []): array |
||||
| 383 | { |
||||
| 384 | if ('' === $type = $colType['type'] ?? ($colType['Type'] ?? '')) { |
||||
| 385 | return []; |
||||
| 386 | } |
||||
| 387 | |||||
| 388 | $colInfo = []; |
||||
| 389 | $colParts = explode(' ', $type); |
||||
| 390 | |||||
| 391 | if ($fparen = strpos($colParts[0], '(')) { |
||||
| 392 | $colInfo['type'] = substr($colParts[0], 0, $fparen); |
||||
| 393 | $colInfo['length'] = str_replace(')', '', substr($colParts[0], $fparen + 1)); |
||||
| 394 | $colInfo['attributes'] = $colParts[1] ?? null; |
||||
| 395 | } else { |
||||
| 396 | $colInfo['type'] = $colParts[0]; |
||||
| 397 | } |
||||
| 398 | |||||
| 399 | $colInfo['type_sql'] = $type; |
||||
| 400 | $colInfo['is_numeric'] = in_array($colInfo['type'], $mapTypes['numerical'], true); |
||||
| 401 | $colInfo['is_blob'] = in_array($colInfo['type'], $mapTypes['blob'], true); |
||||
| 402 | $colInfo['is_virtual'] = false; |
||||
| 403 | |||||
| 404 | return $colInfo; |
||||
| 405 | } |
||||
| 406 | |||||
| 407 | /** |
||||
| 408 | * Get code backup database parameters |
||||
| 409 | */ |
||||
| 410 | public function backupParameters(): string |
||||
| 411 | { |
||||
| 412 | return PHP_EOL; |
||||
| 413 | } |
||||
| 414 | |||||
| 415 | /** |
||||
| 416 | * Get code restore database parameters |
||||
| 417 | */ |
||||
| 418 | public function restoreParameters(): string |
||||
| 419 | { |
||||
| 420 | return PHP_EOL; |
||||
| 421 | } |
||||
| 422 | |||||
| 423 | /** |
||||
| 424 | * Check number of parameters passed to function, useful when inheriting. |
||||
| 425 | * Raise exception if unexpected. |
||||
| 426 | */ |
||||
| 427 | protected function checkParameters(int $num_args, int $expected_num_args, string $method_name) |
||||
| 428 | { |
||||
| 429 | if ($num_args !== $expected_num_args) { |
||||
| 430 | throw new Exception("Unexpected parameter passed to {$method_name}"); |
||||
| 431 | } |
||||
| 432 | } |
||||
| 433 | } |
||||
| 434 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.