Passed
Push — dev ( 7ae04d...cd5ace )
by Def
30:02 queued 08:14
created

ConnectionPDOOracle::initConnection()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 5
cp 0.8
crap 3.072
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 7
    public function getDriverName(): string
54
    {
55 7
        return 'oci';
56
    }
57
58
    /**
59
     * Override base behaviour
60
     */
61 5
    public function getLastInsertID(string $sequenceName = null): string
62
    {
63 5
        if ($sequenceName === null) {
64 1
            throw new InvalidArgumentException('Oracle not support lastInsertId without sequence name');
65
        }
66
67 4
        if ($this->isActive()) {
68
            /* get the last insert id from connection */
69 3
            $sequenceName = $this->getQuoter()->quoteSimpleTableName($sequenceName);
70
71 3
            return (string) $this->createCommand("SELECT $sequenceName.CURRVAL FROM DUAL")->queryScalar();
72
        }
73
74 1
        throw new InvalidCallException('DB Connection is not active.');
75
    }
76
77
    /**
78
     * @throws Exception|InvalidConfigException
79
     */
80 344
    public function getQueryBuilder(): QueryBuilderInterface
81
    {
82 344
        if ($this->queryBuilder === null) {
83 344
            $this->queryBuilder = new QueryBuilderPDOOracle(
84 344
                $this->getQuoter(),
85 344
                $this->getSchema(),
86
            );
87
        }
88
89 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...
90
    }
91
92 357
    public function getQuoter(): QuoterInterface
93
    {
94 357
        if ($this->quoter === null) {
95 357
            $this->quoter = new Quoter('"', '"', $this->getTablePrefix());
96
        }
97
98 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...
99
    }
100
101 358
    public function getSchema(): SchemaInterface
102
    {
103 358
        if ($this->schema === null) {
104 358
            $this->schema = new Schema($this, $this->schemaCache, strtoupper($this->driver->getUsername()));
105
        }
106
107 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...
108
    }
109
110
    /**
111
     * Initializes the DB connection.
112
     *
113
     * This method is invoked right after the DB connection is established.
114
     *
115
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
116
     *
117
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
118
     *
119
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
120
     */
121 185
    protected function initConnection(): void
122
    {
123 185
        $this->pdo = $this->driver->createConnection();
124 185
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
125
126 185
        if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
127
            $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
128
        }
129
    }
130
}
131