1 | <?php declare(strict_types=1); |
||
23 | abstract class AbstractUtil { |
||
24 | |||
25 | /** |
||
26 | * Reference to the current connection object |
||
27 | * @var DriverInterface |
||
28 | */ |
||
29 | private $connection; |
||
30 | |||
31 | /** |
||
32 | * Save a reference to the connection object for later use |
||
33 | * |
||
34 | * @param DriverInterface $connection |
||
35 | */ |
||
36 | public function __construct(DriverInterface $connection) |
||
37 | { |
||
38 | $this->connection = $connection; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Get the driver object for the current connection |
||
43 | * |
||
44 | * @return DriverInterface |
||
45 | */ |
||
46 | public function getDriver() |
||
47 | { |
||
48 | return $this->connection; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Convenience public function to generate sql for creating a db table |
||
53 | * |
||
54 | * @param string $name |
||
55 | * @param array $fields |
||
56 | * @param array $constraints |
||
57 | * @param bool $ifNotExists |
||
58 | * @return string |
||
59 | */ |
||
60 | public function createTable($name, $fields, array $constraints=[], $ifNotExists=TRUE) |
||
61 | { |
||
62 | $existsStr = $ifNotExists ? ' IF NOT EXISTS ' : ' '; |
||
63 | |||
64 | // Reorganize into an array indexed with column information |
||
65 | // Eg $columnArray[$colname] = [ |
||
66 | // 'type' => ..., |
||
67 | // 'constraint' => ..., |
||
68 | // 'index' => ..., |
||
69 | // ] |
||
70 | $columnArray = \arrayZipper([ |
||
71 | 'type' => $fields, |
||
72 | 'constraint' => $constraints |
||
73 | ]); |
||
74 | |||
75 | // Join column definitions together |
||
76 | $columns = []; |
||
77 | foreach($columnArray as $n => $props) |
||
78 | { |
||
79 | $str = $this->getDriver()->quoteIdent($n); |
||
80 | $str .= isset($props['type']) ? " {$props['type']}" : ""; |
||
81 | $str .= isset($props['constraint']) ? " {$props['constraint']}" : ""; |
||
82 | |||
83 | $columns[] = $str; |
||
84 | } |
||
85 | |||
86 | // Generate the sql for the creation of the table |
||
87 | $sql = 'CREATE TABLE'.$existsStr.$this->getDriver()->quoteTable($name).' ('; |
||
88 | $sql .= implode(', ', $columns); |
||
89 | $sql .= ')'; |
||
90 | |||
91 | return $sql; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Drop the selected table |
||
96 | * |
||
97 | * @param string $name |
||
98 | * @return string |
||
99 | */ |
||
100 | public function deleteTable($name): string |
||
101 | { |
||
102 | return 'DROP TABLE IF EXISTS '.$this->getDriver()->quoteTable($name); |
||
103 | } |
||
104 | |||
105 | // -------------------------------------------------------------------------- |
||
106 | // ! Abstract Methods |
||
107 | // -------------------------------------------------------------------------- |
||
108 | |||
109 | /** |
||
110 | * Return an SQL file with the database table structure |
||
111 | * |
||
112 | * @abstract |
||
113 | * @return string |
||
114 | */ |
||
115 | abstract public function backupStructure(); |
||
116 | |||
117 | /** |
||
118 | * Return an SQL file with the database data as insert statements |
||
119 | * |
||
120 | * @abstract |
||
121 | * @return string |
||
122 | */ |
||
123 | abstract public function backupData(); |
||
124 | |||
125 | } |