Failed Conditions
Pull Request — develop (#3523)
by
unknown
65:32
created

Statement::execute()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 27
ccs 0
cts 0
cp 0
rs 9.2222
c 0
b 0
f 0
cc 6
nc 5
nop 1
crap 42
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver\PDOSqlsrv;
6
7
use Doctrine\DBAL\Driver\PDOStatement;
8
use Doctrine\DBAL\ParameterType;
9
use PDO;
10
11
/**
12
 * PDO SQL Server Statement
13
 */
14
class Statement extends PDOStatement
15
{
16
    /**
17
     * The last insert ID.
18
     *
19
     * @var LastInsertId|null
20
     */
21
    private $lastInsertId;
22
23
    /**
24
     * The affected number of rows
25
     *
26
     * @var int|null
27
     */
28
    private $rowCount;
29
30
    public function __construct(\PDOStatement $stmt, ?LastInsertId $lastInsertId = null)
31
    {
32
        parent::__construct($stmt);
33
        $this->lastInsertId = $lastInsertId;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function bindParam($param, &$variable, int $type = ParameterType::STRING, ?int $length = null, $driverOptions = null) : void
40
    {
41
        if (($type === ParameterType::LARGE_OBJECT || $type === ParameterType::BINARY)
42
            && $driverOptions === null
43
        ) {
44
            $driverOptions = PDO::SQLSRV_ENCODING_BINARY;
45
        }
46
47
        parent::bindParam($param, $variable, $type, $length, $driverOptions);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function bindValue($param, $value, int $type = ParameterType::STRING) : void
54
    {
55
        $this->bindParam($param, $value, $type);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function execute(?array $params = null) : void
62
    {
63
        parent::execute($params);
64
        $this->rowCount = $this->rowCount();
65
66
        if (! $this->lastInsertId) {
67
            return;
68
        }
69
70
        $id = null;
71
        $this->nextRowset();
72
73
        if ($this->columnCount() > 0) {
74
            $id = $this->fetchColumn();
75
        }
76
77
        if (! $id) {
78
            while ($this->nextRowset()) {
79
                if ($this->columnCount() === 0) {
80
                    continue;
81
                }
82
83
                $id = $this->fetchColumn();
84
            }
85
        }
86
87
        $this->lastInsertId->setId($id);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function rowCount() : int
94
    {
95
        return $this->rowCount ?: parent::rowCount();
96
    }
97
}
98