Test Failed
Pull Request — dev (#72)
by Def
08:22 queued 04:52
created

ConnectionPDOOracle::initConnection()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
ccs 1
cts 1
cp 1
crap 3
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
    /**
54
     * Override base behaviour
55 7
     */
56
    public function getLastInsertID(string $sequenceName = null): string
57
    {
58
        if ($sequenceName === null) {
59
            throw new InvalidArgumentException('Oracle not support lastInsertId without sequence name');
60
        }
61 5
62
        if ($this->isActive()) {
63 5
            /* get the last insert id from connection */
64 1
            $sequenceName = $this->getQuoter()->quoteSimpleTableName($sequenceName);
65
66
            return (string) $this->createCommand("SELECT $sequenceName.CURRVAL FROM DUAL")->queryScalar();
67 4
        }
68
69 3
        throw new InvalidCallException('DB Connection is not active.');
70
    }
71 3
72
    /**
73
     * @throws Exception|InvalidConfigException
74 1
     */
75
    public function getQueryBuilder(): QueryBuilderInterface
76
    {
77
        if ($this->queryBuilder === null) {
78
            $this->queryBuilder = new QueryBuilderPDOOracle(
79
                $this->getQuoter(),
80 344
                $this->getSchema(),
81
            );
82 344
        }
83 344
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 344
    }
86
87
    public function getQuoter(): QuoterInterface
88
    {
89 344
        if ($this->quoter === null) {
90
            $this->quoter = new Quoter('"', '"', $this->getTablePrefix());
91
        }
92 357
93
        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 357
    }
95 357
96
    public function getSchema(): SchemaInterface
97
    {
98 357
        if ($this->schema === null) {
99
            $this->schema = new Schema($this, $this->schemaCache, strtoupper($this->driver->getUsername()));
100
        }
101 358
102
        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 358
    }
104 358
105
    /**
106
     * Initializes the DB connection.
107 358
     *
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
    protected function initConnection(): void
117
    {
118
        if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
119
            $this->driver->attributes([PDO::ATTR_EMULATE_PREPARES => $this->getEmulatePrepare()]);
120
        }
121 185
122
        $this->pdo = $this->driver->createConnection();
123 185
    }
124
}
125