Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
87.50% covered (success)
87.50%
7 / 8
CRAP
97.78% covered (success)
97.78%
44 / 45
MySQLDeleteCollectionManyAssignmentStoredProcedure
0.00% covered (danger)
0.00%
0 / 1
87.50% covered (success)
87.50%
7 / 8
14
97.78% covered (success)
97.78%
44 / 45
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
8 / 8
 buildElements
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
6 / 6
 buildSignature
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
9 / 9
 buildStatement
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 getWhereCondition
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
12 / 12
 getCorrespondingColumnForPKAttribute
0.00% covered (danger)
0.00%
0 / 1
3.14
75.00% covered (success)
75.00%
3 / 4
 buildTableColumn
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 buildParameterName
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
<?php
declare(strict_types = 1);
namespace Siesta\Driver\MySQL\StoredProcedure;
use Siesta\Database\StoredProcedureNaming;
use Siesta\Model\Attribute;
use Siesta\Model\CollectionMany;
use Siesta\Model\DataModel;
use Siesta\Model\Entity;
use Siesta\Model\Reference;
class MySQLDeleteCollectionManyAssignmentStoredProcedure extends MySQLStoredProcedureBase
{
    const DELETE = "DELETE FROM %s WHERE %s;";
    /**
     * @var CollectionMany
     */
    protected $collectionMany;
    /**
     * @var Entity
     */
    protected $foreignEntity;
    /**
     * @var Reference
     */
    protected $foreignReference;
    /**
     * @var Entity
     */
    protected $mappingEntity;
    /**
     * @var Reference
     */
    protected $mappingReference;
    /**
     *
     */
    protected $reference;
    public function __construct(DataModel $dataModel, Entity $entity, CollectionMany $collectionMany)
    {
        parent::__construct($dataModel, $entity);
        $this->collectionMany = $collectionMany;
        $this->foreignEntity = $collectionMany->getForeignEntity();
        $this->foreignReference = $collectionMany->getForeignReference();
        $this->mappingEntity = $collectionMany->getMappingEntity();
        $this->mappingReference = $collectionMany->getMappingReference();
        $this->buildElements();
    }
    /**
     *
     */
    protected function buildElements()
    {
        $this->modifies = false;
        $this->name = StoredProcedureNaming::getDeleteCollectionManyAssignmentName($this->collectionMany);
        $this->determineTableNames();
        $this->buildSignature();
        $this->buildStatement();
    }
    /**
     * @return void
     */
    protected function buildSignature()
    {
        $signaturePart = [];
        foreach ($this->entity->getPrimaryKeyAttributeList() as $pkAttribute) {
            $parameterName = $this->buildParameterName($this->entity, $pkAttribute);
            $signaturePart[] = $this->buildSignatureParameter($parameterName, $pkAttribute->getDbType());
        }
        foreach ($this->foreignEntity->getPrimaryKeyAttributeList() as $pkAttribute) {
            $parameterName = $this->buildParameterName($this->foreignEntity, $pkAttribute);
            $signaturePart[] = $this->buildSignatureParameter($parameterName, $pkAttribute->getDbType());
        }
        $this->signature = $this->buildSignatureFromList($signaturePart);
    }
    /**
     *
     */
    protected function buildStatement()
    {
        $mappingTable = $this->quote($this->mappingEntity->getTableName());
        $whereStatement = $this->getWhereCondition();
        $this->statement = sprintf(self::DELETE, $mappingTable, $whereStatement);
    }
    /**
     * @return string
     */
    protected function getWhereCondition() : string
    {
        $mappingTable = $this->mappingEntity->getTableName();
        $whereList = [];
        foreach ($this->entity->getPrimaryKeyAttributeList() as $pkAttribute) {
            $mappingAttribute = $this->getCorrespondingColumnForPKAttribute($this->mappingReference, $pkAttribute);
            $parameterName = $this->buildParameterName($this->entity, $pkAttribute);
            $whereList[] = $this->buildTableColumn($mappingTable, $mappingAttribute) . ' = ' . $parameterName;
        }
        foreach ($this->foreignEntity->getPrimaryKeyAttributeList() as $pkAttribute) {
            $mappingAttribute = $this->getCorrespondingColumnForPKAttribute($this->foreignReference, $pkAttribute);
            $mappingAttribute = $this->quote($mappingAttribute);
            $spParam = $this->buildParameterName($this->foreignEntity, $pkAttribute);
            $whereList[] = "($spParam IS NULL OR $mappingAttribute = $spParam)";
        }
        return implode(" AND ", $whereList);
    }
    /**
     * @param Reference $reference
     * @param Attribute $pkAttribute
     *
     * @return string
     */
    protected function getCorrespondingColumnForPKAttribute(Reference $reference, Attribute $pkAttribute) : string
    {
        foreach ($reference->getReferenceMappingList() as $referenceMapping) {
            if ($referenceMapping->getForeignColumnName() === $pkAttribute->getDBName()) {
                return $referenceMapping->getLocalColumnName();
            }
        }
    }
    /**
     * @param string $tableName
     * @param string $columnName
     *
     * @return string
     */
    protected function buildTableColumn(string $tableName, string $columnName) : string
    {
        return $this->quote($tableName) . '.' . $this->quote($columnName);
    }
    /**
     * @param Entity $entity
     * @param Attribute $attribute
     *
     * @return string
     */
    protected function buildParameterName(Entity $entity, Attribute $attribute)
    {
        return 'P_' . $entity->getTableName() . '_' . $attribute->getDBName();
    }
}