Passed
Push — dev ( c992f5...8dd21a )
by Def
35:13 queued 07:22
created

ConnectionPDOOracle::getSchema()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Oracle\PDO;
6
7
use PDO;
8
use Yiisoft\Db\Driver\PDO\CommandPDOInterface;
9
use Yiisoft\Db\Driver\PDO\ConnectionPDO;
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\Oracle\Quoter;
15
use Yiisoft\Db\Oracle\Schema;
16
use Yiisoft\Db\Query\QueryBuilderInterface;
17
use Yiisoft\Db\Schema\QuoterInterface;
18
use Yiisoft\Db\Schema\SchemaInterface;
19
use Yiisoft\Db\Transaction\TransactionInterface;
20
21
use function constant;
22
23
/**
24
 * Database connection class prefilled for Oracle SQL Server.
25
 * The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php).
26
 */
27
final class ConnectionPDOOracle extends ConnectionPDO
28
{
29 182
    public function createCommand(?string $sql = null, array $params = []): CommandPDOInterface
30
    {
31 182
        $command = new CommandPDOOracle($this, $this->queryCache);
32
33 182
        if ($sql !== null) {
34 182
            $command->setSql($sql);
35
        }
36
37 182
        if ($this->logger !== null) {
38 182
            $command->setLogger($this->logger);
39
        }
40
41 182
        if ($this->profiler !== null) {
42 182
            $command->setProfiler($this->profiler);
43
        }
44
45 182
        return $command->bindValues($params);
46
    }
47
48 11
    public function createTransaction(): TransactionInterface
49
    {
50 11
        return new TransactionPDOOracle($this);
51
    }
52
53
    /**
54
     * Override base behaviour
55
     */
56 5
    public function getLastInsertID(string $sequenceName = null): string
57
    {
58 5
        if ($sequenceName === null) {
59 1
            throw new InvalidArgumentException('Oracle not support lastInsertId without sequence name');
60
        }
61
62 4
        if ($this->isActive()) {
63
            /* get the last insert id from connection */
64 3
            $sequenceName = $this->getQuoter()->quoteSimpleTableName($sequenceName);
65
66 3
            return (string) $this->createCommand("SELECT $sequenceName.CURRVAL FROM DUAL")->queryScalar();
67
        }
68
69 1
        throw new InvalidCallException('DB Connection is not active.');
70
    }
71
72
    /**
73
     * @throws Exception|InvalidConfigException
74
     */
75 344
    public function getQueryBuilder(): QueryBuilderInterface
76
    {
77 344
        if ($this->queryBuilder === null) {
78 344
            $this->queryBuilder = new QueryBuilderPDOOracle(
79 344
                $this->getQuoter(),
80 344
                $this->getSchema(),
81
            );
82
        }
83
84 344
        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\Query\QueryBuilderInterface. Consider adding an additional type-check to rule them out.
Loading history...
85
    }
86
87 357
    public function getQuoter(): QuoterInterface
88
    {
89 357
        if ($this->quoter === null) {
90 357
            $this->quoter = new Quoter('"', '"', $this->getTablePrefix());
91
        }
92
93 357
        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...
94
    }
95
96 358
    public function getSchema(): SchemaInterface
97
    {
98 358
        if ($this->schema === null) {
99 358
            $this->schema = new Schema($this, $this->schemaCache, strtoupper($this->driver->getUsername()));
100
        }
101
102 358
        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...
103
    }
104
105
    /**
106
     * Initializes the DB connection.
107
     *
108
     * This method is invoked right after the DB connection is established.
109
     *
110
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
111
     *
112
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
113
     *
114
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
115
     */
116 186
    protected function initConnection(): void
117
    {
118 186
        if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
119
            $this->driver->attributes([PDO::ATTR_EMULATE_PREPARES => $this->getEmulatePrepare()]);
120
        }
121
122 186
        $this->pdo = $this->driver->createConnection();
123
    }
124
}
125