示例#1
0
 /**
  * Build a Doctrine column object for TYPE/TYPE columns.
  *
  * @param array $tableColumn
  * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  * @return \Doctrine\DBAL\Schema\Column
  * @throws \Doctrine\DBAL\DBALException
  * @todo: The $tableColumn source currently only support MySQL definition style.
  */
 protected function getEnumerationTableColumnDefinition(array $tableColumn, AbstractPlatform $platform) : Column
 {
     $options = ['length' => $tableColumn['length'] ?: null, 'unsigned' => false, 'fixed' => false, 'default' => $tableColumn['default'] ?: null, 'notnull' => (bool) ($tableColumn['null'] !== 'YES'), 'scale' => null, 'precision' => null, 'autoincrement' => false, 'comment' => $tableColumn['comment'] ?: null];
     $dbType = $this->getDatabaseType($tableColumn['type']);
     $doctrineType = $platform->getDoctrineTypeMapping($dbType);
     $column = new Column($tableColumn['field'], Type::getType($doctrineType), $options);
     $column->setPlatformOption('unquotedValues', $this->getUnquotedEnumerationValues($tableColumn['type']));
     return $column;
 }
 /**
  * {@inheritdoc}
  */
 protected function _getPortableTableColumnDefinition($tableColumn)
 {
     $dbType = strtolower($tableColumn['DATA_TYPE']);
     $type = $this->_platform->getDoctrineTypeMapping($dbType);
     $type = $this->extractDoctrineTypeFromComment($tableColumn['COLUMN_COMMENT'], $type);
     $tableColumn['COLUMN_COMMENT'] = $this->removeDoctrineTypeFromComment($tableColumn['COLUMN_COMMENT'], $type);
     $options = array('notnull' => !(bool) $tableColumn['IS_NULLABLE'], 'length' => (int) $tableColumn['CHARACTER_MAXIMUM_LENGTH'], 'default' => isset($tableColumn['COLUMN_DEFAULT']) ? $tableColumn['COLUMN_DEFAULT'] : null, 'autoincrement' => (bool) $tableColumn['IS_AUTO_INCREMENT'], 'scale' => (int) $tableColumn['NUMERIC_SCALE'], 'precision' => (int) $tableColumn['NUMERIC_PRECISION'], 'comment' => isset($tableColumn['COLUMN_COMMENT']) && '' !== $tableColumn['COLUMN_COMMENT'] ? $tableColumn['COLUMN_COMMENT'] : null);
     $column = new Column($tableColumn['COLUMN_NAME'], Type::getType($type), $options);
     if (!empty($tableColumn['COLLATION_NAME'])) {
         $column->setPlatformOption('collation', $tableColumn['COLLATION_NAME']);
     }
     return $column;
 }
 /**
  * {@inheritdoc}
  */
 protected function _getPortableTableColumnDefinition($tableColumn)
 {
     $dbType = strtok($tableColumn['type'], '(), ');
     $fixed = null;
     $length = (int) $tableColumn['length'];
     $default = $tableColumn['default'];
     if (!isset($tableColumn['name'])) {
         $tableColumn['name'] = '';
     }
     while ($default != ($default2 = preg_replace("/^\\((.*)\\)\$/", '$1', $default))) {
         $default = trim($default2, "'");
         if ($default == 'getdate()') {
             $default = $this->_platform->getCurrentTimestampSQL();
         }
     }
     switch ($dbType) {
         case 'nchar':
         case 'nvarchar':
         case 'ntext':
             // Unicode data requires 2 bytes per character
             $length = $length / 2;
             break;
         case 'varchar':
             // TEXT type is returned as VARCHAR(MAX) with a length of -1
             if ($length == -1) {
                 $dbType = 'text';
             }
             break;
     }
     if ('char' === $dbType || 'nchar' === $dbType || 'binary' === $dbType) {
         $fixed = true;
     }
     $type = $this->_platform->getDoctrineTypeMapping($dbType);
     $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
     $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
     $options = array('length' => $length == 0 || !in_array($type, array('text', 'string')) ? null : $length, 'unsigned' => false, 'fixed' => (bool) $fixed, 'default' => $default !== 'NULL' ? $default : null, 'notnull' => (bool) $tableColumn['notnull'], 'scale' => $tableColumn['scale'], 'precision' => $tableColumn['precision'], 'autoincrement' => (bool) $tableColumn['autoincrement'], 'comment' => $tableColumn['comment'] !== '' ? $tableColumn['comment'] : null);
     $column = new Column($tableColumn['name'], Type::getType($type), $options);
     if (isset($tableColumn['collation']) && $tableColumn['collation'] !== 'NULL') {
         $column->setPlatformOption('collation', $tableColumn['collation']);
     }
     return $column;
 }
 /**
  * {@inheritdoc}
  */
 protected function _getPortableTableColumnDefinition($tableColumn)
 {
     $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
     if (strtolower($tableColumn['type']) === 'varchar' || strtolower($tableColumn['type']) === 'bpchar') {
         // get length from varchar definition
         $length = preg_replace('~.*\\(([0-9]*)\\).*~', '$1', $tableColumn['complete_type']);
         $tableColumn['length'] = $length;
     }
     $matches = array();
     $autoincrement = false;
     if (preg_match("/^nextval\\('(.*)'(::.*)?\\)\$/", $tableColumn['default'], $matches)) {
         $tableColumn['sequence'] = $matches[1];
         $tableColumn['default'] = null;
         $autoincrement = true;
     }
     if (preg_match("/^'(.*)'::.*\$/", $tableColumn['default'], $matches)) {
         $tableColumn['default'] = $matches[1];
     }
     if (stripos($tableColumn['default'], 'NULL') === 0) {
         $tableColumn['default'] = null;
     }
     $length = isset($tableColumn['length']) ? $tableColumn['length'] : null;
     if ($length == '-1' && isset($tableColumn['atttypmod'])) {
         $length = $tableColumn['atttypmod'] - 4;
     }
     if ((int) $length <= 0) {
         $length = null;
     }
     $fixed = null;
     if (!isset($tableColumn['name'])) {
         $tableColumn['name'] = '';
     }
     $precision = null;
     $scale = null;
     $dbType = strtolower($tableColumn['type']);
     if (strlen($tableColumn['domain_type']) && !$this->_platform->hasDoctrineTypeMappingFor($tableColumn['type'])) {
         $dbType = strtolower($tableColumn['domain_type']);
         $tableColumn['complete_type'] = $tableColumn['domain_complete_type'];
     }
     $type = $this->_platform->getDoctrineTypeMapping($dbType);
     $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
     $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
     switch ($dbType) {
         case 'smallint':
         case 'int2':
             $length = null;
             break;
         case 'int':
         case 'int4':
         case 'integer':
             $length = null;
             break;
         case 'bigint':
         case 'int8':
             $length = null;
             break;
         case 'bool':
         case 'boolean':
             if ($tableColumn['default'] === 'true') {
                 $tableColumn['default'] = true;
             }
             if ($tableColumn['default'] === 'false') {
                 $tableColumn['default'] = false;
             }
             $length = null;
             break;
         case 'text':
             $fixed = false;
             break;
         case 'varchar':
         case 'interval':
         case '_varchar':
             $fixed = false;
             break;
         case 'char':
         case 'bpchar':
             $fixed = true;
             break;
         case 'float':
         case 'float4':
         case 'float8':
         case 'double':
         case 'double precision':
         case 'real':
         case 'decimal':
         case 'money':
         case 'numeric':
             if (preg_match('([A-Za-z]+\\(([0-9]+)\\,([0-9]+)\\))', $tableColumn['complete_type'], $match)) {
                 $precision = $match[1];
                 $scale = $match[2];
                 $length = null;
             }
             break;
         case 'year':
             $length = null;
             break;
     }
     if ($tableColumn['default'] && preg_match("('([^']+)'::)", $tableColumn['default'], $match)) {
         $tableColumn['default'] = $match[1];
     }
     $options = array('length' => $length, 'notnull' => (bool) $tableColumn['isnotnull'], 'default' => $tableColumn['default'], 'primary' => (bool) ($tableColumn['pri'] == 't'), 'precision' => $precision, 'scale' => $scale, 'fixed' => $fixed, 'unsigned' => false, 'autoincrement' => $autoincrement, 'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== '' ? $tableColumn['comment'] : null);
     $column = new Column($tableColumn['field'], Type::getType($type), $options);
     if (isset($tableColumn['collation']) && !empty($tableColumn['collation'])) {
         $column->setPlatformOption('collation', $tableColumn['collation']);
     }
     return $column;
 }
 /**
  * {@inheritdoc}
  */
 protected function _getPortableTableColumnDefinition($tableColumn)
 {
     $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
     $dbType = strtolower($tableColumn['type']);
     $dbType = strtok($dbType, '(), ');
     if (isset($tableColumn['length'])) {
         $length = $tableColumn['length'];
     } else {
         $length = strtok('(), ');
     }
     $fixed = null;
     if (!isset($tableColumn['name'])) {
         $tableColumn['name'] = '';
     }
     $scale = null;
     $precision = null;
     $type = $this->_platform->getDoctrineTypeMapping($dbType);
     // In cases where not connected to a database DESCRIBE $table does not return 'Comment'
     if (isset($tableColumn['comment'])) {
         $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
         $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
     }
     switch ($dbType) {
         case 'char':
         case 'binary':
             $fixed = true;
             break;
         case 'float':
         case 'double':
         case 'real':
         case 'numeric':
         case 'decimal':
             if (preg_match('([A-Za-z]+\\(([0-9]+)\\,([0-9]+)\\))', $tableColumn['type'], $match)) {
                 $precision = $match[1];
                 $scale = $match[2];
                 $length = null;
             }
             break;
         case 'tinytext':
             $length = MySqlPlatform::LENGTH_LIMIT_TINYTEXT;
             break;
         case 'text':
             $length = MySqlPlatform::LENGTH_LIMIT_TEXT;
             break;
         case 'mediumtext':
             $length = MySqlPlatform::LENGTH_LIMIT_MEDIUMTEXT;
             break;
         case 'tinyblob':
             $length = MySqlPlatform::LENGTH_LIMIT_TINYBLOB;
             break;
         case 'blob':
             $length = MySqlPlatform::LENGTH_LIMIT_BLOB;
             break;
         case 'mediumblob':
             $length = MySqlPlatform::LENGTH_LIMIT_MEDIUMBLOB;
             break;
         case 'tinyint':
         case 'smallint':
         case 'mediumint':
         case 'int':
         case 'integer':
         case 'bigint':
         case 'year':
             $length = null;
             break;
     }
     $length = (int) $length == 0 ? null : (int) $length;
     $options = array('length' => $length, 'unsigned' => (bool) (strpos($tableColumn['type'], 'unsigned') !== false), 'fixed' => (bool) $fixed, 'default' => isset($tableColumn['default']) ? $tableColumn['default'] : null, 'notnull' => (bool) ($tableColumn['null'] != 'YES'), 'scale' => null, 'precision' => null, 'autoincrement' => (bool) (strpos($tableColumn['extra'], 'auto_increment') !== false), 'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== '' ? $tableColumn['comment'] : null);
     if ($scale !== null && $precision !== null) {
         $options['scale'] = $scale;
         $options['precision'] = $precision;
     }
     $column = new Column($tableColumn['field'], Type::getType($type), $options);
     if (isset($tableColumn['collation'])) {
         $column->setPlatformOption('collation', $tableColumn['collation']);
     }
     return $column;
 }
示例#6
0
 protected static function parseFieldOpts(Schema $schema, Column $field, SimpleXMLElement $xOptParent, AbstractPlatform $platform)
 {
     $opts = static::getOptArray($xOptParent, $platform);
     foreach ($opts as $name => $value) {
         $field->setPlatformOption($name, $value);
     }
 }