/** * {@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; }
public function testGetNative() { $columnTypeMapper = new ColumnTypeMapper(); $this->assertEquals('character varying', $columnTypeMapper->getNative('string')); $this->assertEquals('text', $columnTypeMapper->getNative('text')); $this->assertEquals('integer', $columnTypeMapper->getNative('integer')); $this->assertEquals('bigint', $columnTypeMapper->getNative('biginteger')); $this->assertEquals('smallint', $columnTypeMapper->getNative('smallinteger')); $this->assertEquals('real', $columnTypeMapper->getNative('float')); $this->assertEquals('decimal', $columnTypeMapper->getNative('decimal')); $this->assertEquals('timestamp', $columnTypeMapper->getNative('datetime')); $this->assertEquals('date', $columnTypeMapper->getNative('date')); $this->assertEquals('time', $columnTypeMapper->getNative('time')); $this->assertEquals('bytea', $columnTypeMapper->getNative('binary')); }
/** * 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, ','); }