| 12 | | class MySqlSchemaInspector extends SchemaInspector |
| 13 | | { |
| 14 | | |
| 15 | | |
| 16 | | |
| 17 | | |
| 18 | | |
| 19 | | protected function getDefaultSchemaGrammar(): \BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar |
| 20 | | { |
| 21 | | return new \BlueprintAU\Radiant\Database\Schema\Grammars\MySqlSchemaGrammar(); |
| 22 | | } |
| 23 | | |
| 24 | | |
| 25 | | |
| 26 | | |
| 27 | | |
| 28 | | |
| 29 | | |
| 30 | | |
| 31 | | |
| 32 | | |
| 33 | | |
| 34 | | |
| 35 | | public function columnTypeMatches(string $liveType, \BlueprintAU\Radiant\Database\Schema\Enums\ColumnType $declaredType, int|null $declaredLength, int|null $declaredPrecision = null, int|null $declaredScale = null): bool |
| 36 | | { |
| 37 | | return strtolower($liveType) === strtolower($this->schemaGrammar->type($declaredType, $declaredLength, $declaredPrecision, $declaredScale)); |
| 38 | | } |
| 39 | | |
| 40 | | |
| 41 | | |
| 42 | | |
| 43 | | |
| 44 | | |
| 45 | | |
| 46 | | public function referencingTables(string $table): array |
| 47 | | { |
| 48 | | $statement = $this->pdo->prepare( |
| 49 | | 'SELECT DISTINCT table_name FROM information_schema.key_column_usage ' |
| 50 | | . 'WHERE table_schema = DATABASE() AND referenced_table_name = ? ' |
| 51 | | . 'AND referenced_table_name IS NOT NULL', |
| 52 | | ); |
| 53 | | $statement->execute([$table]); |
| 54 | | |
| 55 | | $tables = []; |
| 56 | | foreach ($statement->fetchAll(\PDO::FETCH_COLUMN) as $name) { |
| 57 | | $tables[] = (string) $name; |
| 58 | | } |
| 59 | | |
| 60 | | return $tables; |
| 61 | | } |
| 62 | | |
| 63 | | |
| 64 | | |
| 65 | | |
| 66 | | |
| 67 | | public function tables(): array |
| 68 | | { |
| 69 | | $statement = $this->pdo->prepare( |
| 70 | | 'SELECT table_name FROM information_schema.tables ' |
| 71 | | . 'WHERE table_schema = DATABASE() ORDER BY table_name', |
| 72 | | ); |
| 73 | | $statement->execute(); |
| 74 | | |
| 75 | | |
| 76 | | |
| 77 | | |
| 78 | | $tables = []; |
| 79 | | foreach ($statement->fetchAll(\PDO::FETCH_COLUMN) as $name) { |
| 80 | | $tables[] = (string) $name; |
| 81 | | } |
| 82 | | |
| 83 | | return $tables; |
| 84 | | } |
| 85 | | |
| 86 | | |
| 87 | | |
| 88 | | |
| 89 | | |
| 90 | | |
| 91 | | |
| 92 | | |
| 93 | | public function table(string $name): LiveTable |
| 94 | | { |
| 95 | | if (!$this->hasTable($name)) { |
| 96 | | throw new \RuntimeException("Table [{$name}] does not exist in the MySQL schema."); |
| 97 | | } |
| 98 | | |
| 99 | | return new LiveTable( |
| 100 | | $name, |
| 101 | | $this->columns($name), |
| 102 | | $this->indexes($name), |
| 103 | | $this->foreignKeys($name), |
| 104 | | ); |
| 105 | | } |
| 106 | | |
| 107 | | |
| 108 | | |
| 109 | | |
| 110 | | |
| 111 | | |
| 112 | | |
| 113 | | private function columns(string $name): array |
| 114 | | { |
| 115 | | $statement = $this->pdo->prepare( |
| 116 | | 'SELECT c.column_name, c.column_type, c.is_nullable, c.column_default, ' |
| 117 | | . '(c.column_key = \'PRI\') AS is_primary ' |
| 118 | | . 'FROM information_schema.columns c ' |
| 119 | | . 'WHERE c.table_schema = DATABASE() AND c.table_name = ? ' |
| 120 | | . 'ORDER BY c.ordinal_position', |
| 121 | | ); |
| 122 | | $statement->execute([$name]); |
| 123 | | |
| 124 | | $columns = []; |
| 125 | | |
| 126 | | |
| 127 | | foreach ($statement->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
| 128 | | |
| 129 | | |
| 130 | | |
| 131 | | $row = array_change_key_case($row, CASE_LOWER); |
| 132 | | $default = $row['column_default']; |
| 133 | | |
| 134 | | $columns[] = [ |
| 135 | | 'name' => (string) $row['column_name'], |
| 136 | | 'type' => $this->normalizeColumnType(strtolower((string) $row['column_type'])), |
| 137 | | 'nullable' => strtoupper((string) $row['is_nullable']) === 'YES', |
| 138 | | |
| 139 | | |
| 140 | | 'default' => $this->normalizeColumnDefault($default), |
| 141 | | 'primaryKey' => ((int) $row['is_primary']) === 1, |
| 142 | | ]; |
| 143 | | } |
| 144 | | |
| 145 | | return $columns; |
| 146 | | } |
| 147 | | |
| 148 | | |
| 149 | | |
| 150 | | |
| 151 | | |
| 152 | | |
| 153 | | |
| 154 | | protected function normalizeColumnType(string $type): string |
| 155 | | { |
| 156 | | return $type; |
| 157 | | } |
| 158 | | |
| 159 | | |
| 160 | | |
| 161 | | |
| 162 | | |
| 163 | | |
| 164 | | |
| 165 | | protected function normalizeColumnDefault(mixed $default): mixed |
| 166 | | { |
| 167 | | return $default; |
| 168 | | } |
| 169 | | |
| 170 | | |
| 171 | | |
| 172 | | |
| 173 | | |
| 174 | | |
| 175 | | |
| 176 | | private function indexes(string $name): array |
| 177 | | { |
| 178 | | $statement = $this->pdo->prepare( |
| 179 | | 'SELECT index_name, column_name, non_unique, seq_in_index ' |
| 180 | | . 'FROM information_schema.statistics ' |
| 181 | | . 'WHERE table_schema = DATABASE() AND table_name = ? ' |
| 182 | | . 'ORDER BY index_name, seq_in_index', |
| 183 | | ); |
| 184 | | $statement->execute([$name]); |
| 185 | | |
| 186 | | |
| 187 | | $rows = $statement->fetchAll(\PDO::FETCH_ASSOC); |
| 188 | | |
| 189 | | $groups = []; |
| 190 | | |
| 191 | | foreach ($rows as $row) { |
| 192 | | |
| 193 | | $row = array_change_key_case($row, CASE_LOWER); |
| 194 | | $indexName = (string) $row['index_name']; |
| 195 | | |
| 196 | | |
| 197 | | if ($indexName === 'PRIMARY') { |
| 198 | | continue; |
| 199 | | } |
| 200 | | |
| 201 | | $groups[$indexName]['columns'][(int) $row['seq_in_index']] = (string) $row['column_name']; |
| 202 | | $groups[$indexName]['unique'] = ((int) $row['non_unique']) === 0; |
| 203 | | } |
| 204 | | |
| 205 | | $indexes = []; |
| 206 | | |
| 207 | | foreach ($groups as $indexName => $group) { |
| 208 | | $indexes[] = [ |
| 209 | | 'name' => $indexName, |
| 210 | | 'columns' => array_values($group['columns']), |
| 211 | | 'unique' => $group['unique'], |
| 212 | | 'where' => null, |
| 213 | | 'nullsNotDistinct' => false, |
| 214 | | ]; |
| 215 | | } |
| 216 | | |
| 217 | | return $indexes; |
| 218 | | } |
| 219 | | |
| 220 | | |
| 221 | | |
| 222 | | |
| 223 | | |
| 224 | | |
| 225 | | |
| 226 | | |
| 227 | | private function foreignKeys(string $name): array |
| 228 | | { |
| 229 | | $statement = $this->pdo->prepare( |
| 230 | | 'SELECT kcu.constraint_name, kcu.column_name, kcu.referenced_table_name, ' |
| 231 | | . 'kcu.referenced_column_name, kcu.ordinal_position, ' |
| 232 | | . 'rc.delete_rule, rc.update_rule ' |
| 233 | | . 'FROM information_schema.key_column_usage kcu ' |
| 234 | | . 'JOIN information_schema.referential_constraints rc ' |
| 235 | | . 'ON rc.constraint_name = kcu.constraint_name ' |
| 236 | | . 'AND rc.constraint_schema = kcu.constraint_schema ' |
| 237 | | . 'WHERE kcu.table_schema = DATABASE() AND kcu.table_name = ? ' |
| 238 | | . 'AND kcu.referenced_table_name IS NOT NULL ' |
| 239 | | . 'ORDER BY kcu.constraint_name, kcu.ordinal_position', |
| 240 | | ); |
| 241 | | $statement->execute([$name]); |
| 242 | | |
| 243 | | |
| 244 | | $rows = $statement->fetchAll(\PDO::FETCH_ASSOC); |
| 245 | | |
| 246 | | $groups = []; |
| 247 | | |
| 248 | | foreach ($rows as $row) { |
| 249 | | |
| 250 | | $row = array_change_key_case($row, CASE_LOWER); |
| 251 | | $constraintName = (string) $row['constraint_name']; |
| 252 | | $groups[$constraintName]['columns'][] = (string) $row['column_name']; |
| 253 | | $groups[$constraintName]['referencesTable'] = (string) $row['referenced_table_name']; |
| 254 | | $groups[$constraintName]['referencesColumns'][(int) $row['ordinal_position']] = (string) $row['referenced_column_name']; |
| 255 | | $groups[$constraintName]['onDelete'] = $row['delete_rule']; |
| 256 | | $groups[$constraintName]['onUpdate'] = $row['update_rule']; |
| 257 | | } |
| 258 | | |
| 259 | | $constraints = []; |
| 260 | | |
| 261 | | foreach ($groups as $constraintName => $group) { |
| 262 | | $constraints[] = [ |
| 263 | | 'columns' => $group['columns'], |
| 264 | | 'referencesTable' => $group['referencesTable'], |
| 265 | | 'referencesColumns' => array_values($group['referencesColumns']), |
| 266 | | 'onDelete' => $this->normalizeAction($group['onDelete']), |
| 267 | | 'onUpdate' => $this->normalizeAction($group['onUpdate']), |
| 268 | | |
| 269 | | 'deferrable' => false, |
| 270 | | |
| 271 | | 'name' => $constraintName, |
| 272 | | ]; |
| 273 | | } |
| 274 | | |
| 275 | | return $constraints; |
| 276 | | } |
| 277 | | |
| 278 | | |
| 279 | | |
| 280 | | |
| 281 | | |
| 282 | | |
| 283 | | |
| 284 | | private function normalizeAction(mixed $action): ?string |
| 285 | | { |
| 286 | | $normalized = strtoupper(trim((string) $action)); |
| 287 | | |
| 288 | | return $normalized === 'NO ACTION' ? null : $normalized; |
| 289 | | } |
| 290 | | } |