Passed
Push — remove-test-schema-trait ( 2f86f9...0a3cc3 )
by Wilmer
35:59
created

ConnectionPDO   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 93.94%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 76
ccs 31
cts 33
cp 0.9394
rs 10
c 0
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
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
A getQueryBuilder() 0 10 2
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 244
    public function createCommand(string $sql = null, array $params = []): CommandPDOInterface
26
    {
27 244
        $command = new CommandPDO($this, $this->queryCache);
28
29 244
        if ($sql !== null) {
30 223
            $command->setSql($sql);
31
        }
32
33 244
        if ($this->logger !== null) {
34 22
            $command->setLogger($this->logger);
35
        }
36
37 244
        if ($this->profiler !== null) {
38 22
            $command->setProfiler($this->profiler);
39
        }
40
41 244
        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 462
    public function getQueryBuilder(): QueryBuilderInterface
72
    {
73 462
        if ($this->queryBuilder === null) {
74 462
            $this->queryBuilder = new QueryBuilder(
75 462
                $this->getQuoter(),
76 462
                $this->getSchema(),
77 462
            );
78
        }
79
80 462
        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 497
    public function getQuoter(): QuoterInterface
84
    {
85 497
        if ($this->quoter === null) {
86 497
            $this->quoter = new Quoter('"', '"', $this->getTablePrefix());
87
        }
88
89 497
        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 487
    public function getSchema(): SchemaInterface
93
    {
94 487
        if ($this->schema === null) {
95 487
            $this->schema = new Schema($this, $this->schemaCache, strtoupper($this->driver->getUsername()));
96
        }
97
98 487
        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