Skip to content

Commit

Permalink
Introduced concept of TableMetadata. Still a few tests to finish fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermeblanco authored and lcobucci committed Dec 31, 2017
1 parent e5b216c commit 8a4ca55
Show file tree
Hide file tree
Showing 44 changed files with 1,014 additions and 524 deletions.
97 changes: 5 additions & 92 deletions lib/Doctrine/ORM/Mapping/Builder/ClassMetadataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\DiscriminatorColumnMetadata;
use Doctrine\ORM\Mapping\FieldMetadata;
use Doctrine\ORM\Mapping\TableMetadata;
use Doctrine\ORM\Mapping\VersionFieldMetadata;

/**
Expand Down Expand Up @@ -132,23 +133,15 @@ public function setReadOnly()
}

/**
* Sets the table name.
* Sets the table metadata.
*
* @param string $name
* @param string|null $schema
* @param array $options
* @param TableMetadata $tableMetadata
*
* @return ClassMetadataBuilder
*/
public function setTable($name, $schema = null, array $options = [])
public function withTable(TableMetadata $tableMetadata)
{
$this->cm->setPrimaryTable(
[
'name' => $name,
'schema' => $schema,
'options' => $options,
]
);
$this->cm->setPrimaryTable($tableMetadata);

return $this;
}
Expand All @@ -169,86 +162,6 @@ public function setCache($usage, $region = null)
);
}

/**
* Adds Index.
*
* @param array $columns
* @param string|null $name
* @param bool $unique
* @param array $options
* @param array $flags
*
* @return ClassMetadataBuilder
*/
public function addIndex(array $columns, $name, $unique = false, array $options = [], array $flags = [])
{
if (!isset($this->cm->table['indexes'])) {
$this->cm->table['indexes'] = [];
}

$index = [
'columns' => $columns,
'unique' => $unique,
];

if ( ! empty($options)) {
$index['options'] = $options;
}

if ( ! empty($flags)) {
$index['flags'] = $flags;
}

if (!$name) {
$this->cm->table['indexes'][] = $index;

return $this;
}

$this->cm->table['indexes'][$name] = $index;

return $this;
}

/**
* Adds Unique Constraint.
*
* @param array $columns
* @param string|null $name
* @param array $options
* @param array $flags
*
* @return ClassMetadataBuilder
*/
public function addUniqueConstraint(array $columns, $name, array $options = [], array $flags = [])
{
if ( ! isset($this->cm->table['uniqueConstraints'])) {
$this->cm->table['uniqueConstraints'] = [];
}

$index = ['columns' => $columns];

if ( ! empty($options)) {
$index['options'] = $options;
}

if ( ! empty($flags)) {
$index['flags'] = $flags;
}

if (!$name) {
$this->cm->table['uniqueConstraints'][] = $index;

return $this;
}

$this->cm->table['uniqueConstraints'][$name] = $index;

return $this;


}

/**
* Adds named query.
*
Expand Down
173 changes: 173 additions & 0 deletions lib/Doctrine/ORM/Mapping/Builder/TableMetadataBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php

declare(strict_types = 1);

/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\ORM\Mapping\Builder;

use Doctrine\ORM\Mapping\DefaultNamingStrategy;
use Doctrine\ORM\Mapping\FieldMetadata;
use Doctrine\ORM\Mapping\NamingStrategy;
use Doctrine\ORM\Mapping\TableMetadata;

class TableMetadataBuilder implements Builder
{
/** @var string */
protected $schema;

/** @var string */
protected $name;

/** @var array */
protected $options = [];

/** @var array */
protected $indexes = [];

/** @var array */
protected $uniqueConstraints = [];

/**
* @param string $name
*
* @return self
*/
public function withName(string $name)
{
$this->name = $name;

return $this;
}

/**
* @param string $schema
*
* @return self
*/
public function withSchema(string $schema)
{
$this->schema = $schema;

return $this;
}

/**
* @param array $options
*
* @return self
*/
public function withOptions(array $options)
{
$this->options = $options;

return $this;
}

/**
* @param string $name
* @param mixed $value
*
* @return self
*/
public function withOption(string $name, $value)
{
$this->options[$name] = $value;

return $this;
}

/**
* @param string|null $name
* @param array $columns
* @param bool $unique
* @param array $options
* @param array $flags
*
* @return self
*/
public function withIndex($name, array $columns, bool $unique = false, array $options = [], array $flags = [])
{
$this->indexes[] = [
'name' => $name,
'columns' => $columns,
'unique' => $unique,
'options' => $options,
'flags' => $flags,
];

return $this;
}

/**
* @param string|null $name
* @param array $columns
* @param array $options
* @param array $flags
*
* @return self
*/
public function withUniqueConstraint($name, array $columns, array $options = [], array $flags = [])
{
$this->uniqueConstraints[] = [
'name' => $name,
'columns' => $columns,
'options' => $options,
'flags' => $flags,
];

return $this;
}

/**
* @return TableMetadata
*/
public function build()
{
$tableMetadata = $this->createMetadataObject();

if ($this->name !== null) {
$tableMetadata->setName($this->name);
}

if ($this->schema !== null) {
$tableMetadata->setSchema($this->schema);
}

$tableMetadata->setOptions($this->options);

foreach ($this->indexes as $index) {
$tableMetadata->addIndex($index);
}

foreach ($this->uniqueConstraints as $constraint) {
$tableMetadata->addUniqueConstraint($constraint);
}

return $tableMetadata;
}

/**
* @return TableMetadata
*/
protected function createMetadataObject()
{
return new TableMetadata();
}
}
Loading

0 comments on commit 8a4ca55

Please sign in to comment.