Test Failed
Pull Request — master (#126)
by Wilmer
05:01
created

ConnectionPDO::getLastInsertID()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql;
6
7
use Yiisoft\Db\Driver\PDO\CommandPDOInterface;
8
use Yiisoft\Db\Driver\PDO\ConnectionPDO as AbstractConnectionPDO;
9
use Yiisoft\Db\Exception\Exception;
10
use Yiisoft\Db\Exception\InvalidConfigException;
11
use Yiisoft\Db\Query\BatchQueryResultInterface;
12
use Yiisoft\Db\Query\QueryInterface;
13
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
14
use Yiisoft\Db\Schema\QuoterInterface;
15
use Yiisoft\Db\Schema\SchemaInterface;
16
use Yiisoft\Db\Transaction\TransactionInterface;
17
18
/**
19
 * Database connection class prefilled for Microsoft SQL Server.
20
 * The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php).
21
 */
22
final class ConnectionPDO extends AbstractConnectionPDO
23
{
24 3
    public function createBatchQueryResult(QueryInterface $query, bool $each = false): BatchQueryResultInterface
25
    {
26 3
        return new BatchQueryResult($query, $each);
27
    }
28
29 233
    public function createCommand(string $sql = null, array $params = []): CommandPDOInterface
30
    {
31 233
        $command = new CommandPDO($this, $this->queryCache);
32
33 233
        if ($sql !== null) {
34 232
            $command->setSql($sql);
35
        }
36
37 233
        if ($this->logger !== null) {
38 233
            $command->setLogger($this->logger);
39
        }
40
41 233
        if ($this->profiler !== null) {
42 233
            $command->setProfiler($this->profiler);
43
        }
44
45 233
        return $command->bindValues($params);
46
    }
47
48 12
    public function createTransaction(): TransactionInterface
49
    {
50 12
        return new TransactionPDO($this);
51
    }
52
53
    /**
54
     * Returns value of the last inserted ID.
55
     *
56 394
     * SQLSERVER driver implements {@see PDO::lastInsertId()} method but with a single peculiarity:
57
     *
58 394
     * When `$sequence` value is a null or an empty string it returns the last inserted ID for table with an identity
59 394
     * column.
60 394
     * But when parameter is not specified it works as expected and returns actual last inserted ID (like the other PDO
61 394
     * drivers).
62
     * if `$sequenceName` is the table name return empty string.
63
     *
64
     * @param string|null $sequenceNAme the sequence name. Defaults to null.
65 394
     *
66
     * @return string last inserted ID value.
67
     */
68 449
    public function getLastInsertID(string $sequenceName = null): string
69
    {
70 449
        if ($this->isActive() && $this->pdo) {
71 449
            return $this->pdo->lastInsertID($sequenceName);
72
        }
73
74 449
        throw new InvalidCallException('DB Connection is not active.');
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Mssql\InvalidCallException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
75
    }
76
77 397
    /**
78
     * @throws Exception|InvalidConfigException
79 397
     */
80 397
    public function getQueryBuilder(): QueryBuilderInterface
81
    {
82
        if ($this->queryBuilder === null) {
83 397
            $this->queryBuilder = new QueryBuilder(
84
                $this->getQuoter(),
85
                $this->getSchema(),
86
            );
87
        }
88
89
        return $this->queryBuilder;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->queryBuilder could return the type null which is incompatible with the type-hinted return Yiisoft\Db\QueryBuilder\QueryBuilderInterface. Consider adding an additional type-check to rule them out.
Loading history...
90
    }
91
92
    public function getQuoter(): QuoterInterface
93
    {
94
        if ($this->quoter === null) {
95
            $this->quoter = new Quoter(['[', ']'], ['[', ']'], $this->getTablePrefix());
96
        }
97 228
98
        return $this->quoter;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->quoter could return the type null which is incompatible with the type-hinted return Yiisoft\Db\Schema\QuoterInterface. Consider adding an additional type-check to rule them out.
Loading history...
99 228
    }
100
101
    public function getSchema(): SchemaInterface
102
    {
103
        if ($this->schema === null) {
104
            $this->schema = new Schema($this, $this->schemaCache);
105
        }
106
107
        return $this->schema;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->schema could return the type null which is incompatible with the type-hinted return Yiisoft\Db\Schema\SchemaInterface. Consider adding an additional type-check to rule them out.
Loading history...
108
    }
109
110
    /**
111
     * Initializes the DB connection.
112
     *
113
     * This method is invoked right after the DB connection is established.
114
     *
115
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
116
     *
117
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
118
     *
119
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
120
     */
121
    protected function initConnection(): void
122
    {
123
        $this->pdo = $this->driver->createConnection();
124
    }
125
}
126