Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
18 / 18 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
AbstractUtil | |
100.00% |
18 / 18 |
|
100.00% |
4 / 4 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDriver | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createTable | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
5 | |||
deleteTable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
backupStructure | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
backupData | n/a |
0 / 0 |
n/a |
0 / 0 |
0 |
1 | <?php declare(strict_types=1); |
2 | /** |
3 | * Query |
4 | * |
5 | * SQL Query Builder / Database Abstraction Layer |
6 | * |
7 | * PHP version 8.1 |
8 | * |
9 | * @package Query |
10 | * @author Timothy J. Warren <tim@timshome.page> |
11 | * @copyright 2012 - 2023 Timothy J. Warren |
12 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
13 | * @link https://git.timshomepage.net/aviat/Query |
14 | * @version 4.0.0 |
15 | */ |
16 | |
17 | namespace Query\Drivers; |
18 | |
19 | use function arrayZipper; |
20 | |
21 | /** |
22 | * Abstract class defining database / table creation methods |
23 | */ |
24 | abstract class AbstractUtil |
25 | { |
26 | /** |
27 | * Save a reference to the connection object for later use |
28 | */ |
29 | public function __construct(private readonly DriverInterface $connection) |
30 | { |
31 | } |
32 | |
33 | /** |
34 | * Get the driver object for the current connection |
35 | */ |
36 | public function getDriver(): DriverInterface |
37 | { |
38 | return $this->connection; |
39 | } |
40 | |
41 | /** |
42 | * Convenience public function to generate sql for creating a db table |
43 | */ |
44 | public function createTable(string $name, array $fields, array $constraints=[], bool $ifNotExists=TRUE): string |
45 | { |
46 | $existsStr = $ifNotExists ? ' IF NOT EXISTS ' : ' '; |
47 | |
48 | // Reorganize into an array indexed with column information |
49 | // Eg $columnArray[$colname] = [ |
50 | // 'type' => ..., |
51 | // 'constraint' => ..., |
52 | // 'index' => ..., |
53 | // ] |
54 | $columnArray = arrayZipper([ |
55 | 'type' => $fields, |
56 | 'constraint' => $constraints, |
57 | ]); |
58 | |
59 | // Join column definitions together |
60 | $columns = []; |
61 | |
62 | foreach ($columnArray as $n => $props) |
63 | { |
64 | $str = $this->getDriver()->quoteIdent($n); |
65 | $str .= isset($props['type']) ? " {$props['type']}" : ''; |
66 | $str .= isset($props['constraint']) ? " {$props['constraint']}" : ''; |
67 | |
68 | $columns[] = $str; |
69 | } |
70 | |
71 | // Generate the sql for the creation of the table |
72 | $sql = 'CREATE TABLE' . $existsStr . $this->getDriver()->quoteTable($name) . ' ('; |
73 | $sql .= implode(', ', $columns); |
74 | $sql .= ')'; |
75 | |
76 | return $sql; |
77 | } |
78 | |
79 | /** |
80 | * Drop the selected table |
81 | */ |
82 | public function deleteTable(string $name): string |
83 | { |
84 | return 'DROP TABLE IF EXISTS ' . $this->getDriver()->quoteTable($name); |
85 | } |
86 | |
87 | // -------------------------------------------------------------------------- |
88 | // ! Abstract Methods |
89 | // -------------------------------------------------------------------------- |
90 | /** |
91 | * Return an SQL file with the database table structure |
92 | */ |
93 | abstract public function backupStructure(): string; |
94 | |
95 | /** |
96 | * Return an SQL file with the database data as insert statements |
97 | */ |
98 | abstract public function backupData(): string; |
99 | } |