Example #1
0
 /**
  * {@inheritdoc}
  */
 public function execute()
 {
     $columnTypeMapper = new ColumnTypeMapper();
     $this->preExecute();
     $columns = $this->connection->query($this->getSql());
     if (empty($columns)) {
         throw new TableNotExistsException($this->tableName);
     }
     $table = new Table($this->tableName);
     if (null === $table->getSchema()) {
         $table->setSchema($columns[0]['table_schema']);
     }
     $columnCreator = new ColumnCreator();
     foreach ($columns as $column) {
         $columnType = $columnTypeMapper->getCommon($column['data_type']);
         $options = array();
         $options['not_null'] = $column['is_nullable'] === 'NO';
         $options['default'] = $column['column_default'];
         if ($columnType === 'string') {
             preg_match("/'(.*)'::character varying/", $column['column_default'], $matches);
             $options['default'] = isset($matches[1]) ? $matches[1] : '';
             $options['limit'] = $column['character_maximum_length'];
         }
         $column = $columnCreator->create($column['column_name'], $columnType, $options);
         $table->addColumn($column);
     }
     $this->loadConstraints($table);
     $this->postExecute();
     return $table;
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function getSql()
 {
     if ($this->column instanceof CustomColumn) {
         $columnType = $this->column->getType();
     } else {
         $columnTypeMapper = new ColumnTypeMapper();
         $columnType = $columnTypeMapper->getNative($this->column->getType());
     }
     $sql = sprintf('ALTER TABLE %s ADD COLUMN %s %s;', $this->column->getTable()->getQualifiedName(), $this->column->getName(), $columnType);
     $columnDescription = $this->column->getDescription();
     if (!empty($tableDescription)) {
         $sql .= sprintf("COMMENT ON COLUMN %s.%s IS '%s';", $this->column->getTable()->getQualifiedName(), $this->column->getName(), $columnDescription);
     }
     return $sql;
 }
Example #3
0
 /**
  * Gets columns query.
  *
  * @return string
  */
 private function getColumnsSql()
 {
     $columnTypeMapper = new ColumnTypeMapper();
     foreach ($this->table->getConstraints() as $constraint) {
         if ($constraint instanceof PrimaryKey) {
             $primaryKey = $constraint;
         }
     }
     if (!isset($primaryKey)) {
         // TODO find better solution
         $primaryKey = new PrimaryKey();
         $primaryKey->setTable($this->table);
         $this->table->addConstraint($primaryKey);
     }
     $sql = '';
     if (!$primaryKey->isMulti() && $primaryKey->isAutoCreateColumn()) {
         $sql = sprintf('%s %s NOT NULL,', $primaryKey->getColumns(), $primaryKey->isAutoIncrement() ? 'serial' : 'integer');
     }
     foreach ($this->table->getColumns() as $column) {
         if ($column instanceof CustomColumn) {
             $columnType = $column->getType();
         } else {
             $columnType = $columnTypeMapper->getNative($column->getType());
         }
         $sql .= sprintf('%s %s%s %s %s,', $column->getName(), $columnType, $this->getTypeConstraints($column), $column->isNotNull() ? 'NOT NULL' : '', null === $column->getDefault() ? '' : 'DEFAULT' . ' ' . $this->addQuotesIfNeeded($column, $column->getDefault()));
     }
     return rtrim($sql, ',');
 }
Example #4
0
 public function testGetCommon()
 {
     $columnTypeMapper = new ColumnTypeMapper();
     $this->assertEquals('string', $columnTypeMapper->getCommon('character varying'));
     $this->assertEquals('text', $columnTypeMapper->getCommon('text'));
     $this->assertEquals('integer', $columnTypeMapper->getCommon('integer'));
     $this->assertEquals('biginteger', $columnTypeMapper->getCommon('bigint'));
     $this->assertEquals('smallinteger', $columnTypeMapper->getCommon('smallint'));
     $this->assertEquals('decimal', $columnTypeMapper->getCommon('decimal'));
     $this->assertEquals('float', $columnTypeMapper->getCommon('real'));
     $this->assertEquals('datetime', $columnTypeMapper->getCommon('timestamp'));
     $this->assertEquals('date', $columnTypeMapper->getCommon('date'));
     $this->assertEquals('time', $columnTypeMapper->getCommon('time'));
     $this->assertEquals('binary', $columnTypeMapper->getCommon('bytea'));
 }