Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
34 / 34
MySQLDeleteByPKStoredProcedure
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
7 / 7
13
100.00% covered (success)
100.00%
34 / 34
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 buildElements
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
6 / 6
 getCreateProcedureStatement
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 buildSignature
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 buildStatement
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
6 / 6
 buildDeleteSQL
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 buildDelimitDeleteSQL
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
6 / 6
<?php
declare(strict_types=1);
namespace Siesta\Driver\MySQL\StoredProcedure;
use Siesta\Database\StoredProcedureNaming;
use Siesta\Model\DataModel;
use Siesta\Model\DelimitAttributeList;
use Siesta\Model\Entity;
/**
 * @author Gregor Müller
 */
class MySQLDeleteByPKStoredProcedure extends MySQLStoredProcedureBase
{
    const DELIMIT_DELETE = "UPDATE %s SET %s = NOW() WHERE %s AND %s IS NULL;";
    /**
     * MySQLDeleteStoredProcedure constructor.
     *
     * @param DataModel $dataModel
     * @param Entity $entity
     */
    public function __construct(DataModel $dataModel, Entity $entity)
    {
        parent::__construct($dataModel, $entity);
        $this->buildElements();
    }
    /**
     *
     */
    protected function buildElements()
    {
        $this->modifies = true;
        $this->name = StoredProcedureNaming::getDeleteByPrimaryKeyName($this->entity);
        $this->determineTableNames();
        $this->buildSignature();
        $this->buildStatement();
    }
    /**
     * @return null|string
     */
    public function getCreateProcedureStatement()
    {
        if (!$this->entity->hasPrimaryKey()) {
            return null;
        }
        return parent::getCreateProcedureStatement();
    }
    /**
     * @return void
     */
    protected function buildSignature()
    {
        $parameterList = [];
        foreach ($this->entity->getPrimaryKeyAttributeList() as $attribute) {
            $parameterList[] = $this->buildSignatureParameter($attribute->getStoredProcedureParameterName(), $attribute->getDbType());
        }
        $this->signature = $this->buildSignatureFromList($parameterList);
    }
    /**
     *
     */
    protected function buildStatement()
    {
        $this->statement = $this->buildDeleteSQL($this->tableName);
        if ($this->entity->getIsDelimit()) {
            $this->statement .= $this->buildDelimitDeleteSQL();
        }
        if ($this->isReplication) {
            $this->statement .= $this->buildDeleteSQL($this->replicationTableName);
        }
    }
    /**
     * @param string $tableName
     *
     * @return string
     */
    protected function buildDeleteSQL($tableName)
    {
        $whereList = [];
        foreach ($this->entity->getPrimaryKeyAttributeList() as $attribute) {
            $whereList[] = $this->buildWherePart($attribute);
        }
        $where = $this->buildWhereAndSnippet($whereList);
        return sprintf(self::DELETE_WHERE, $tableName, $where);
    }
    /**
     * @return string
     */
    protected function buildDelimitDeleteSQL()
    {
        $whereList = [];
        foreach ($this->entity->getPrimaryKeyAttributeList() as $attribute) {
            $whereList[] = $this->buildWherePart($attribute);
        }
        $where = $this->buildWhereAndSnippet($whereList);
        $validUntilColumn = $this->quote(DelimitAttributeList::COLUMN_VALID_UNTIL);
        return sprintf(self::DELIMIT_DELETE, $this->delimitTable, $validUntilColumn, $where, $validUntilColumn);
    }
}