|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TheCodingMachine\TDBM\SchemaVersionControl; |
| 4 | + |
| 5 | +use Doctrine\DBAL\Schema\Column; |
| 6 | +use Doctrine\DBAL\Schema\Schema; |
| 7 | +use Doctrine\DBAL\Schema\Table; |
| 8 | + |
| 9 | +/** |
| 10 | + * Database schema builder. |
| 11 | + * |
| 12 | + * Given a deep associative array supposed to describe a database schema, SchemaBuilder::build will construct a |
| 13 | + * corresponding Schema object. |
| 14 | + */ |
| 15 | +class SchemaBuilder |
| 16 | +{ |
| 17 | + /** @var array */ |
| 18 | + protected $schemaDesc; |
| 19 | + |
| 20 | + /** |
| 21 | + * Build an array descriptor into a Schema object |
| 22 | + * @param array $schemaDesc |
| 23 | + * @return Schema |
| 24 | + */ |
| 25 | + public function build(array $schemaDesc): Schema |
| 26 | + { |
| 27 | + $this->schemaDesc = $schemaDesc; |
| 28 | + $schema = new Schema(); |
| 29 | + foreach ($schemaDesc['tables'] as $name => $tableDesc) { |
| 30 | + $table = $schema->createTable($name); |
| 31 | + $this->buildTable($tableDesc, $table); |
| 32 | + } |
| 33 | + return $schema; |
| 34 | + } |
| 35 | + |
| 36 | + protected function buildTable(array $tableDesc, Table $table) |
| 37 | + { |
| 38 | + $pk_columns = []; |
| 39 | + |
| 40 | + if (isset($tableDesc['comment'])) { |
| 41 | + $table->addOption("comment", $tableDesc['comment']); |
| 42 | + } |
| 43 | + |
| 44 | + foreach ($tableDesc['columns'] as $columnName => $columnDesc) { |
| 45 | + if (!is_array($columnDesc)) { |
| 46 | + $columnDesc = ['type' => $columnDesc]; |
| 47 | + } |
| 48 | + if (isset($columnDesc['primary_key']) |
| 49 | + && $columnDesc['primary_key']) { |
| 50 | + $pk_columns[] = $columnName; |
| 51 | + } |
| 52 | + $column = $table->addColumn($columnName, $columnDesc['type']); |
| 53 | + $this->buildColumn($columnDesc, $column); |
| 54 | + } |
| 55 | + if (isset($tableDesc['indexes'])) { |
| 56 | + foreach ($tableDesc['indexes'] as $indexName => $indexDesc) { |
| 57 | + $this->buildIndex($indexDesc, $table, $indexName); |
| 58 | + } |
| 59 | + } |
| 60 | + if (!empty($pk_columns)) { |
| 61 | + $table->setPrimaryKey($pk_columns); |
| 62 | + } |
| 63 | + if (isset($tableDesc['foreign_keys'])) { |
| 64 | + foreach ($tableDesc['foreign_keys'] as $constraintName => $constraintDesc) { |
| 65 | + $this->buildForeignKeyConstraint($constraintDesc, $table, $constraintName); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + protected function buildColumn(array $columnDesc, Column $column) |
| 71 | + { |
| 72 | + if (isset($columnDesc['fixed'])) { |
| 73 | + $column->setFixed($columnDesc['fixed']); |
| 74 | + } |
| 75 | + if (isset($columnDesc['length'])) { |
| 76 | + $column->setLength($columnDesc['length']); |
| 77 | + } |
| 78 | + if (isset($columnDesc['precision'])) { |
| 79 | + $column->setPrecision($columnDesc['precision']); |
| 80 | + } |
| 81 | + if (isset($columnDesc['scale'])) { |
| 82 | + $column->setScale($columnDesc['scale']); |
| 83 | + } |
| 84 | + $column->setNotnull(isset($columnDesc['not_null']) && $columnDesc['not_null']); |
| 85 | + if (isset($columnDesc['default'])) { |
| 86 | + $column->setDefault($columnDesc['default']); |
| 87 | + } |
| 88 | + if (isset($columnDesc['auto_increment'])) { |
| 89 | + $column->setAutoincrement($columnDesc['auto_increment']); |
| 90 | + } |
| 91 | + if (isset($columnDesc['comment'])) { |
| 92 | + $column->setComment($columnDesc['comment']); |
| 93 | + } |
| 94 | + if (isset($columnDesc['custom'])) { |
| 95 | + $column->setCustomSchemaOptions($columnDesc['custom']); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + protected function buildForeignKeyConstraint(array $constraintDesc, Table $table, string $name) |
| 100 | + { |
| 101 | + if (isset($constraintDesc['column'])) { |
| 102 | + $localColumns = [$constraintDesc['column']]; |
| 103 | + } else { |
| 104 | + $localColumns = $constraintDesc['columns']; |
| 105 | + } |
| 106 | + $references = $constraintDesc['references']; |
| 107 | + if (is_array($references)) { |
| 108 | + $foreignTable = $references['table']; |
| 109 | + if (isset($references['column'])) { |
| 110 | + $foreignColumns = [$references['column']]; |
| 111 | + } else { |
| 112 | + $foreignColumns = $references['columns']; |
| 113 | + } |
| 114 | + } else { |
| 115 | + $foreignTable = $references; |
| 116 | + $foreignColumns = $this->getPrimaryKeyColumns($foreignTable); |
| 117 | + if (!is_array($foreignColumns)) { |
| 118 | + $foreignColumns = [$foreignColumns]; |
| 119 | + } |
| 120 | + } |
| 121 | + $options = array_diff_key($constraintDesc, ['column' => 0,'columns' => 0,'references' => 0]); |
| 122 | + $table->addForeignKeyConstraint($foreignTable, $localColumns, $foreignColumns, $options, $name); |
| 123 | + } |
| 124 | + |
| 125 | + protected function getPrimaryKeyColumns(string $tableName) |
| 126 | + { |
| 127 | + $pkColumns = []; |
| 128 | + $tableDesc = $this->schemaDesc['tables'][$tableName]; |
| 129 | + foreach ($tableDesc['columns'] as $columnName => $columnDesc) { |
| 130 | + if (isset($columnDesc['primary_key']) |
| 131 | + && $columnDesc['primary_key']) { |
| 132 | + $pkColumns[] = $columnName; |
| 133 | + } |
| 134 | + } |
| 135 | + return $pkColumns; |
| 136 | + } |
| 137 | + |
| 138 | + protected function buildIndex($indexDesc, Table $table, $name) |
| 139 | + { |
| 140 | + if (!is_array($indexDesc)) { |
| 141 | + $indexDesc = ['column' => $indexDesc]; |
| 142 | + } elseif (array_keys($indexDesc) === range(0, count($indexDesc) - 1)) { |
| 143 | + $indexDesc = ['columns' => $indexDesc]; |
| 144 | + } |
| 145 | + |
| 146 | + if (isset($indexDesc['column'])) { |
| 147 | + $columns = [$indexDesc['column']]; |
| 148 | + } else { |
| 149 | + $columns = $indexDesc['columns']; |
| 150 | + } |
| 151 | + |
| 152 | + if (is_int($name)) { |
| 153 | + $name = implode('_', $columns); |
| 154 | + } |
| 155 | + |
| 156 | + $options = array_diff_key($indexDesc, ['column' => 0,'columns' => 0]); |
| 157 | + if (isset($indexDesc['unique']) && $indexDesc['unique'] === true) { |
| 158 | + $table->addUniqueIndex($columns, $name, [], $options); |
| 159 | + } else { |
| 160 | + $table->addIndex($columns, $name, [], $options); |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments