/** * Convert output of SHOW COLUMN to DataType. * * @param array $row * Row result. * @throws TypeException If type unsupported. * @return DataType The type. */ private function toDataType($row) { $null = (isset($row['Null']) and $row['Null'] != 'NO'); $default = null; if (isset($row['Default'])) { $default = $row['Default']; } if (preg_match('/enum\\((.+)\\)/i', $row['Type'], $matches) === 1) { preg_match_all('/\'([^\']+)\'/', $matches[1], $matches); $values = $matches[1]; return DataType::enum($values, $null, $default); } preg_match('/ *([^ (]+) *(\\(([0-9]+)\\))? *(unsigned)? *?/i', $row['Type'], $matches); $actualType = strtolower($matches[1]); $length = isset($matches[3]) ? intval($matches[3]) : 0; $intFlags = 0; if (isset($matches[4])) { $intFlags |= DataType::UNSIGNED; } if (strpos($row['Extra'], 'auto_increment') !== false) { $intFlags |= DataType::SERIAL; } switch ($actualType) { case 'bigint': $intFlags |= DataType::BIG; return DataType::integer($intFlags, $null, isset($default) ? intval($default) : null); case 'smallint': $intFlags |= DataType::SMALL; return DataType::integer($intFlags, $null, isset($default) ? intval($default) : null); case 'tinyint': $intFlags |= DataType::TINY; return DataType::integer($intFlags, $null, isset($default) ? intval($default) : null); case 'int': return DataType::integer($intFlags, $null, isset($default) ? intval($default) : null); case 'double': return DataType::float($null, isset($default) ? floatval($default) : null); case 'varchar': return DataType::string($length, $null, $default); case 'blob': return DataType::binary($null, $default); case 'date': return DataType::date($null, isset($default) ? strtotime($default . ' UTC') : null); case 'datetime': return DataType::dateTime($null, isset($default) ? strtotime($default . ' UTC') : null); case 'text': return DataType::text($null, $default); } throw new TypeException('Unsupported MySQL type for column: ' . $row['Field']); }
/** * Convert output of PRAGMA to DataType. * * @param array $row * Row result. * @throws TypeException If type unsupported. * @return DataType The type. */ private function toDataType($row) { if (preg_match('/ *([^ (]+) *(\\(([0-9]+)\\))? */i', $row['type'], $matches) !== 1) { throw new TypeException('Cannot read type "' . $row['type'] . '" for column: ' . $row['name']); } $actualType = strtolower($matches[1]); $length = isset($matches[3]) ? $matches[3] : 0; $null = (isset($row['notnull']) and $row['notnull'] != '1'); $default = null; if (isset($row['dflt_value'])) { $default = stripslashes(preg_replace('/^\'|\'$/', '', $row['dflt_value'])); } switch ($actualType) { case 'integer': return DataType::integer(DataType::BIG, $null, isset($default) ? intval($default) : null); case 'real': return DataType::float($null, isset($default) ? floatval($default) : null); case 'text': return DataType::text($null, $default); case 'blob': return DataType::binary($null, $default); } throw new TypeException('Unsupported SQLite type for column: ' . $row['name']); }