|
20 | 20 | */ |
21 | 21 | class MySQLEngine implements Engine |
22 | 22 | { |
23 | | - public bool $convertBoolean = true; |
24 | | - |
25 | | - |
26 | 23 | public function __construct( |
27 | 24 | private readonly Connection $connection, |
28 | 25 | ) { |
@@ -176,23 +173,21 @@ public function getForeignKeys(string $table): array |
176 | 173 | } |
177 | 174 |
|
178 | 175 |
|
179 | | - public function getColumnTypes(\PDOStatement $statement): array |
| 176 | + public function resolveColumnConverter(array $meta, TypeConverter $converter): ?\Closure |
180 | 177 | { |
181 | | - $types = []; |
182 | | - $count = $statement->columnCount(); |
183 | | - for ($col = 0; $col < $count; $col++) { |
184 | | - $meta = $statement->getColumnMeta($col); |
185 | | - if (isset($meta['native_type'])) { |
186 | | - $types[$meta['name']] = match (true) { |
187 | | - $meta['native_type'] === 'NEWDECIMAL' && $meta['precision'] === 0 => Nette\Database\IStructure::FIELD_INTEGER, |
188 | | - $meta['native_type'] === 'TINY' && $meta['len'] === 1 && $this->convertBoolean => Nette\Database\IStructure::FIELD_BOOL, |
189 | | - $meta['native_type'] === 'TIME' => Nette\Database\IStructure::FIELD_TIME_INTERVAL, |
190 | | - default => TypeConverter::detectType($meta['native_type']), |
191 | | - }; |
192 | | - } |
193 | | - } |
194 | | - |
195 | | - return $types; |
| 178 | + return match ($meta['nativeType']) { |
| 179 | + 'NEWDECIMAL' => $meta['precision'] === 0 |
| 180 | + ? $converter->toInt(...) |
| 181 | + : $converter->toFloat(...), // precision in PDOStatement::getColumnMeta() means scale |
| 182 | + 'TINY' => $meta['length'] === 1 && $converter->convertBoolean |
| 183 | + ? $converter->toBool(...) |
| 184 | + : $converter->toInt(...), |
| 185 | + 'TIME' => $converter->toInterval(...), |
| 186 | + 'DATE', 'DATETIME', 'TIMESTAMP' => fn($value): ?\DateTimeInterface => str_starts_with($value, '0000-00') |
| 187 | + ? null |
| 188 | + : $converter->toDateTime($value), |
| 189 | + default => $converter->resolve($meta['nativeType']), |
| 190 | + }; |
196 | 191 | } |
197 | 192 |
|
198 | 193 |
|
|
0 commit comments