/** * 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, ','); }
public function testMultiPrimaryKey() { $primaryKey = new PrimaryKey(array('foo', 'bar')); $primaryKey->setTable(new Table('test')); $this->assertEquals('CONSTRAINT test_pkey PRIMARY KEY (foo,bar)', (string) $primaryKey); }