|
7 | 7 |
|
8 | 8 | use Doctrine\DBAL\Types\Type; |
9 | 9 | use Doctrine\DBAL\Schema\Column; |
| 10 | +use Doctrine\DBAL\Schema\Index; |
| 11 | +use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
10 | 12 |
|
11 | 13 | /** |
12 | 14 | * @author Igor Mukhin <igor.mukhin@gmail.com> |
13 | 15 | */ |
14 | 16 | abstract class BaseLoader |
15 | 17 | { |
16 | | - protected $typesMap = array( |
17 | | - 'bigint'=>'bigint', |
18 | | - 'tinyint'=>'boolean', |
19 | | - 'integer'=>'integer', |
20 | | - 'int'=>'integer', |
21 | | - 'longtext'=>'text', |
22 | | - 'text'=>'text', |
23 | | - 'varchar'=>'string', |
24 | | - 'char'=>'string', |
25 | | - 'decimal'=>'decimal', |
26 | | - 'float'=>'float', |
27 | | - 'datetime'=>'datetime', |
28 | | - 'date'=>'date' |
| 18 | + protected $extraOptions = array( |
| 19 | + 'default'=>'typeaware', |
| 20 | + 'notnull'=>'boolean', |
| 21 | + 'length'=>'integer', |
| 22 | + 'precision'=>'integer', |
| 23 | + 'scale'=>'integer', |
| 24 | + 'fixed'=>'boolean', |
| 25 | + 'unsigned'=>'boolean', |
| 26 | + 'autoincrement'=>'boolean', |
| 27 | + 'comment'=>'string' |
29 | 28 | ); |
30 | 29 |
|
31 | 30 | /** |
32 | | - * @param string $type |
33 | | - * @return boolean |
| 31 | + * @param array $columns |
| 32 | + * @return Column[] |
34 | 33 | */ |
35 | | - public function isTypeSupported($type) |
| 34 | + public function loadColumns($columns) |
36 | 35 | { |
37 | | - return isset($this->typesMap[$type]); |
| 36 | + return array_map(function($columnNode){ |
| 37 | + return $this->loadColumn($columnNode); |
| 38 | + }, $columns); |
38 | 39 | } |
39 | 40 |
|
40 | 41 | /** |
41 | | - * Apply extra options for special types. |
42 | | - * |
43 | | - * @param Column $column |
44 | | - * @param string $extra |
| 42 | + * @param array $column |
| 43 | + * @return Column |
45 | 44 | */ |
46 | | - public function applyType(Column $column, $type, $extra) |
| 45 | + public function loadColumn($columnNode) |
47 | 46 | { |
48 | | - switch(strtolower($type)) { |
49 | | - case "varchar": |
50 | | - case "char": |
51 | | - $column |
52 | | - ->setLength($extra) |
53 | | - ; |
54 | | - break; |
55 | | - |
56 | | - case "decimal": |
57 | | - case "float": |
58 | | - list($precision, $scale) = explode(',', $extra); |
59 | | - $column |
60 | | - ->setPrecision($precision) |
61 | | - ->setScale($scale) |
62 | | - ; |
63 | | - break; |
| 47 | + $type = trim($columnNode['type']); |
| 48 | + |
| 49 | + if (!Type::hasType($type)) { |
| 50 | + throw new UnsupportedFieldTypeException($type); |
64 | 51 | } |
| 52 | + |
| 53 | + $options = $this->getColumnExtraOptions($type, $columnNode); |
| 54 | + return new Column((string)$columnNode['name'], Type::getType($type), $options); |
65 | 55 | } |
66 | 56 |
|
67 | 57 | /** |
68 | 58 | * @param string $type |
69 | | - * @return Type |
70 | | - * |
71 | | - * @throws UnsupportedFieldTypeException |
| 59 | + * @param string $columnNode |
| 60 | + * @return array |
72 | 61 | */ |
73 | | - public function getType($type) |
| 62 | + public function getColumnExtraOptions($type, $columnNode) |
74 | 63 | { |
75 | | - $type = strtolower($type); |
76 | | - if (!$this->isTypeSupported($type)) { |
77 | | - throw new UnsupportedFieldTypeException($type); |
| 64 | + $options = array(); |
| 65 | + foreach ($this->extraOptions as $optionName=>$optionType) { |
| 66 | + if (isset($columnNode[$optionName])) { |
| 67 | + $options[$optionName] = $this->convertColumnOptionValue((string)$columnNode[$optionName], $type, $optionType); |
| 68 | + } |
| 69 | + } |
| 70 | + return $options; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param string $optionValue |
| 75 | + * @param string $type |
| 76 | + * @param string $optionType |
| 77 | + * @return mixed |
| 78 | + */ |
| 79 | + protected function convertColumnOptionValue($optionValue, $type, $optionType) |
| 80 | + { |
| 81 | + switch ($optionType) { |
| 82 | + case 'boolean': |
| 83 | + return $optionValue === 'true' ? true : false; |
| 84 | + |
| 85 | + case 'integer': |
| 86 | + return (int)$optionValue; |
| 87 | + |
| 88 | + case 'string': |
| 89 | + return (string)$optionValue; |
| 90 | + |
| 91 | + case 'typeaware': |
| 92 | + if ('null' == $optionValue) { |
| 93 | + return NULL; |
| 94 | + } |
| 95 | + |
| 96 | + switch ($type) { |
| 97 | + case 'tinyint': |
| 98 | + case 'integer': |
| 99 | + case 'bigint': |
| 100 | + case 'float': |
| 101 | + case 'decimal': |
| 102 | + return (int)$optionValue; |
| 103 | + |
| 104 | + case 'boolean': |
| 105 | + return $optionValue === 'true' ? true : false; |
| 106 | + |
| 107 | + default: |
| 108 | + return (string)$optionValue; |
| 109 | + } |
| 110 | + |
| 111 | + default: |
| 112 | + throw new RuntimeException(sprintf("Uknown optionType '%s'", $optionType)); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @param array $indexes |
| 118 | + * @return Index[] |
| 119 | + */ |
| 120 | + public function loadIndexes($indexes) |
| 121 | + { |
| 122 | + return array_map(function($indexNode){ |
| 123 | + return $this->loadIndex($indexNode); |
| 124 | + }, $indexes); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @param array $indexNode |
| 129 | + * @return Index |
| 130 | + */ |
| 131 | + public function loadIndex($indexNode) |
| 132 | + { |
| 133 | + $indexName = (string) $indexNode['name']; |
| 134 | + $columns = explode(',', (string) $indexNode['columns']); |
| 135 | + $isUnique = isset($indexNode['unique']) && 'true' === (string) $indexNode['unique'] ? true : false; |
| 136 | + $isPrimary = isset($indexNode['primary']) && 'true' === (string) $indexNode['primary'] ? true : false; |
| 137 | + |
| 138 | + return new Index($indexName, $columns, $isUnique, $isPrimary); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @param array $constraints |
| 143 | + * @return ForeignKeyConstraint[] |
| 144 | + */ |
| 145 | + public function loadContstraints($constraints) |
| 146 | + { |
| 147 | + return array_map(function($constraintNode){ |
| 148 | + return $this->loadContstraint($constraintNode); |
| 149 | + }, $constraints); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @param array $constraintNode |
| 154 | + * @return ForeignKeyConstraint |
| 155 | + */ |
| 156 | + public function loadContstraint($constraintNode) |
| 157 | + { |
| 158 | + $name = null; |
| 159 | + if (isset($constraintNode['name'])) { |
| 160 | + $name = (string) $constraintNode['name']; |
78 | 161 | } |
79 | 162 |
|
80 | | - $actialType = $this->typesMap[$type]; |
| 163 | + $columns = explode(',', (string) $constraintNode['columns']); |
| 164 | + $foreignTable = (string) $constraintNode['foreign-table']; |
| 165 | + $foreignColumns = explode(',', (string) $constraintNode['foreign-columns']); |
81 | 166 |
|
82 | | - return Type::getType($actialType); |
| 167 | + return new ForeignKeyConstraint($columns, $foreignTable, $foreignColumns, $name); |
83 | 168 | } |
84 | 169 | } |
0 commit comments