Passed
Pull Request — master (#126)
by Wilmer
13:56
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\InvalidCallException;
11
use Yiisoft\Db\Exception\InvalidConfigException;
12
use Yiisoft\Db\Query\BatchQueryResultInterface;
13
use Yiisoft\Db\Query\QueryInterface;
14
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
15
use Yiisoft\Db\Schema\QuoterInterface;
16
use Yiisoft\Db\Schema\SchemaInterface;
17
use Yiisoft\Db\Transaction\TransactionInterface;
18
19
/**
20
 * Database connection class prefilled for Microsoft SQL Server.
21
 * The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php).
22
 */
23
final class ConnectionPDO extends AbstractConnectionPDO
24
{
25 4
    public function createBatchQueryResult(QueryInterface $query, bool $each = false): BatchQueryResultInterface
26
    {
27 4
        return new BatchQueryResult($query, $each);
28
    }
29
30 243
    public function createCommand(string $sql = null, array $params = []): CommandPDOInterface
31
    {
32 243
        $command = new CommandPDO($this, $this->queryCache);
33
34 243
        if ($sql !== null) {
35 199
            $command->setSql($sql);
36
        }
37
38 243
        if ($this->logger !== null) {
39 3
            $command->setLogger($this->logger);
40
        }
41
42 243
        if ($this->profiler !== null) {
43 3
            $command->setProfiler($this->profiler);
44
        }
45
46 243
        return $command->bindValues($params);
47
    }
48
49 9
    public function createTransaction(): TransactionInterface
50
    {
51 9
        return new TransactionPDO($this);
52
    }
53
54
    /**
55
     * Returns value of the last inserted ID.
56
     *
57
     * SQLSERVER driver implements {@see PDO::lastInsertId()} method but with a single peculiarity:
58
     *
59
     * When `$sequence` value is a null or an empty string it returns the last inserted ID for table with an identity
60
     * column.
61
     * But when parameter is not specified it works as expected and returns actual last inserted ID (like the other PDO
62
     * drivers).
63
     * if `$sequenceName` is the table name return empty string.
64
     *
65
     * @param string|null $sequenceNAme the sequence name. Defaults to null.
66
     *
67
     * @return string last inserted ID value.
68
     */
69 5
    public function getLastInsertID(string $sequenceName = null): string
70
    {
71 5
        if ($this->isActive() && $this->pdo) {
72 4
            return $this->pdo->lastInsertID($sequenceName);
73
        }
74
75 1
        throw new InvalidCallException('DB Connection is not active.');
76
    }
77
78
    /**
79
     * @throws Exception|InvalidConfigException
80
     */
81 466
    public function getQueryBuilder(): QueryBuilderInterface
82
    {
83 466
        if ($this->queryBuilder === null) {
84 466
            $this->queryBuilder = new QueryBuilder(
85 466
                $this->getQuoter(),
86 466
                $this->getSchema(),
87
            );
88
        }
89
90 466
        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...
91
    }
92
93 522
    public function getQuoter(): QuoterInterface
94
    {
95 522
        if ($this->quoter === null) {
96 522
            $this->quoter = new Quoter(['[', ']'], ['[', ']'], $this->getTablePrefix());
97
        }
98
99 522
        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...
100
    }
101
102 489
    public function getSchema(): SchemaInterface
103
    {
104 489
        if ($this->schema === null) {
105 489
            $this->schema = new Schema($this, $this->schemaCache);
106
        }
107
108 489
        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...
109
    }
110
111
    /**
112
     * Initializes the DB connection.
113
     *
114
     * This method is invoked right after the DB connection is established.
115
     *
116
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
117
     *
118
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
119
     *
120
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
121
     */
122 326
    protected function initConnection(): void
123
    {
124 326
        $this->pdo = $this->driver->createConnection();
125
    }
126
}
127