Passed
Push — master ( 8a002b...c640bb )
by Wilmer
05:52 queued 01:51
created

Connection::createTransaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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\AbstractConnectionPDO;
8
use Yiisoft\Db\Driver\PDO\CommandPDOInterface;
9
use Yiisoft\Db\Query\BatchQueryResultInterface;
10
use Yiisoft\Db\Query\QueryInterface;
11
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
12
use Yiisoft\Db\Schema\QuoterInterface;
13
use Yiisoft\Db\Schema\SchemaInterface;
14
use Yiisoft\Db\Transaction\TransactionInterface;
15
16
/**
17
 * Implements a connection to a database via PDO (PHP Data Objects) for MSSQL Server.
18
 *
19
 * @link https://www.php.net/manual/en/ref.pdo-sqlsrv.php
20
 */
21
final class Connection extends AbstractConnectionPDO
22
{
23 4
    public function createBatchQueryResult(QueryInterface $query, bool $each = false): BatchQueryResultInterface
24
    {
25 4
        return new BatchQueryResult($query, $each);
26
    }
27
28 642
    public function createCommand(string $sql = null, array $params = []): CommandPDOInterface
29
    {
30 642
        $command = new Command($this);
31
32 642
        if ($sql !== null) {
33 614
            $command->setSql($sql);
34
        }
35
36 642
        if ($this->logger !== null) {
37 2
            $command->setLogger($this->logger);
38
        }
39
40 642
        if ($this->profiler !== null) {
41 3
            $command->setProfiler($this->profiler);
42
        }
43
44 642
        return $command->bindValues($params);
45
    }
46
47 14
    public function createTransaction(): TransactionInterface
48
    {
49 14
        return new Transaction($this);
50
    }
51
52 880
    public function getQueryBuilder(): QueryBuilderInterface
53
    {
54 880
        if ($this->queryBuilder === null) {
55 880
            $this->queryBuilder = new QueryBuilder(
56 880
                $this->getQuoter(),
57 880
                $this->getSchema(),
58 880
            );
59
        }
60
61 880
        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...
62
    }
63
64 938
    public function getQuoter(): QuoterInterface
65
    {
66 938
        if ($this->quoter === null) {
67 938
            $this->quoter = new Quoter(['[', ']'], ['[', ']'], $this->getTablePrefix());
68
        }
69
70 938
        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...
71
    }
72
73 892
    public function getSchema(): SchemaInterface
74
    {
75 892
        if ($this->schema === null) {
76 892
            $this->schema = new Schema($this, $this->schemaCache);
77
        }
78
79 892
        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...
80
    }
81
82
    /**
83
     * Initializes the DB connection.
84
     *
85
     * This method is invoked right after the DB connection is established.
86
     */
87 631
    protected function initConnection(): void
88
    {
89 631
        $this->pdo = $this->driver->createConnection();
90
    }
91
}
92