/** * Construct file model. */ public function __construct() { parent::__construct('File'); $this->addField('path', tr('Path'), DataType::string()); $this->addField('name', tr('Name'), DataType::string()); $this->addField('type', tr('Type'), DataType::enum(array('directory', 'file'))); $this->addField('size', tr('Size'), DataType::integer(DataType::UNSIGNED)); $this->addField('modified', tr('Modified'), DataType::dateTime()); $this->addField('created', tr('Created'), DataType::dateTime()); }
/** * Add created and updated timestamps to schema. * @param string $created Created field name. * @param string $updated Updated field name. */ public function addTimestamps($created = 'created', $updated = 'updated') { if (!$this->_readOnly) { $this->{$created} = DataType::dateTime(); $this->{$updated} = DataType::dateTime(); } }
/** * 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 = $row['is_nullable'] != 'NO'; $default = null; if (isset($row['column_default'])) { $default = $row['column_default']; } $type = $row['data_type']; if (strpos($type, 'int') !== false) { $intFlags = 0; if (preg_match('/^nextval\\(/', $default) === 1) { $intFlags = DataType::AUTO_INCREMENT; $default = null; } else { if (isset($default)) { $default = intval($default); } } if (strpos($type, 'bigint') !== false) { return DataType::integer($intFlags | DataType::BIG, $null, $default); } if (strpos($type, 'smallint') !== false) { return DataType::integer($intFlags | DataType::SMALL, $null, $default); } return DataType::integer($intFlags, $null, $default); } if (strpos($type, 'double') !== false) { return DataType::float($null, isset($default) ? floatval($default) : null); } if (strpos($type, 'bool') !== false) { return DataType::boolean($null, isset($default) ? boolval($default) : null); } if (preg_match("/^'(.*)'::[a-z ]+\$/", $default, $matches) === 1) { $default = $matches[1]; } else { $default = null; } if (strpos($type, 'character') !== false) { $length = $row['character_maximum_length']; return DataType::string($length, $null, $default); } if (strpos($type, 'date') !== false) { return DataType::date($null, isset($default) ? strtotime($default . ' UTC') : null); } if (strpos($type, 'timestamp') !== false) { return DataType::dateTime($null, isset($default) ? strtotime($default . ' UTC') : null); } if (strpos($type, 'text') !== false) { return DataType::text($null, $default); } throw new TypeException(tr('Unsupported PostgreSQL type "%1" for column: %2', $row['data_type'], $row['column_name'])); }
/** * 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::AUTO_INCREMENT; } 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(tr('Unsupported MySQL type for column: %1', $row['Field'])); }