Passed
Push — master ( 87a26d...c76764 )
by Def
28:26 queued 06:00
created

ConnectionPDO   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 14
eloc 27
dl 0
loc 76
ccs 30
cts 32
cp 0.9375
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getQueryBuilder() 0 10 2
A createCommand() 0 17 4
A getQuoter() 0 7 2
A createTransaction() 0 3 1
A getSchema() 0 7 2
A getLastInsertID() 0 14 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Oracle;
6
7
use PDO;
8
use Yiisoft\Db\Driver\PDO\CommandPDOInterface;
9
use Yiisoft\Db\Driver\PDO\ConnectionPDO as AbstractConnectionPDO;
10
use Yiisoft\Db\Exception\Exception;
11
use Yiisoft\Db\Exception\InvalidArgumentException;
12
use Yiisoft\Db\Exception\InvalidCallException;
13
use Yiisoft\Db\Exception\InvalidConfigException;
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 Oracle 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 234
    public function createCommand(string $sql = null, array $params = []): CommandPDOInterface
26
    {
27 234
        $command = new CommandPDO($this, $this->queryCache);
28
29 234
        if ($sql !== null) {
30 213
            $command->setSql($sql);
31
        }
32
33 234
        if ($this->logger !== null) {
34 95
            $command->setLogger($this->logger);
35
        }
36
37 234
        if ($this->profiler !== null) {
38 95
            $command->setProfiler($this->profiler);
39
        }
40
41 234
        return $command->bindValues($params);
42
    }
43
44 12
    public function createTransaction(): TransactionInterface
45
    {
46 12
        return new TransactionPDO($this);
47
    }
48
49
    /**
50
     * Override base behaviour
51
     */
52 3
    public function getLastInsertID(string $sequenceName = null): string
53
    {
54 3
        if ($sequenceName === null) {
55
            throw new InvalidArgumentException('Oracle not support lastInsertId without sequence name');
56
        }
57
58 3
        if ($this->isActive()) {
59
            /* get the last insert id from connection */
60 3
            $sequenceName = $this->getQuoter()->quoteSimpleTableName($sequenceName);
61
62 3
            return (string) $this->createCommand("SELECT $sequenceName.CURRVAL FROM DUAL")->queryScalar();
63
        }
64
65
        throw new InvalidCallException('DB Connection is not active.');
66
    }
67
68
    /**
69
     * @throws Exception|InvalidConfigException
70
     */
71 451
    public function getQueryBuilder(): QueryBuilderInterface
72
    {
73 451
        if ($this->queryBuilder === null) {
74 451
            $this->queryBuilder = new QueryBuilder(
75 451
                $this->getQuoter(),
76 451
                $this->getSchema(),
77
            );
78
        }
79
80 451
        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...
81
    }
82
83 493
    public function getQuoter(): QuoterInterface
84
    {
85 493
        if ($this->quoter === null) {
86 493
            $this->quoter = new Quoter('"', '"', $this->getTablePrefix());
87
        }
88
89 493
        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...
90
    }
91
92 472
    public function getSchema(): SchemaInterface
93
    {
94 472
        if ($this->schema === null) {
95 472
            $this->schema = new Schema($this, $this->schemaCache, strtoupper($this->driver->getUsername()));
96
        }
97
98 472
        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...
99
    }
100
}
101