Passed
Pull Request — dev (#77)
by Def
38:49 queued 06:58
created

ConnectionPDO::getQuoter()   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;
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 183
    public function createCommand(?string $sql = null, array $params = []): CommandPDOInterface
26
    {
27 183
        $command = new CommandPDO($this, $this->queryCache);
28
29 183
        if ($sql !== null) {
30 183
            $command->setSql($sql);
31
        }
32
33 183
        if ($this->logger !== null) {
34 183
            $command->setLogger($this->logger);
35
        }
36
37 183
        if ($this->profiler !== null) {
38 183
            $command->setProfiler($this->profiler);
39
        }
40
41 183
        return $command->bindValues($params);
42
    }
43
44 11
    public function createTransaction(): TransactionInterface
45
    {
46 11
        return new TransactionPDO($this);
47
    }
48
49
    /**
50
     * Override base behaviour
51
     */
52 5
    public function getLastInsertID(string $sequenceName = null): string
53
    {
54 5
        if ($sequenceName === null) {
55 1
            throw new InvalidArgumentException('Oracle not support lastInsertId without sequence name');
56
        }
57
58 4
        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 1
        throw new InvalidCallException('DB Connection is not active.');
66
    }
67
68
    /**
69
     * @throws Exception|InvalidConfigException
70
     */
71 345
    public function getQueryBuilder(): QueryBuilderInterface
72
    {
73 345
        if ($this->queryBuilder === null) {
74 345
            $this->queryBuilder = new QueryBuilder(
75 345
                $this->getQuoter(),
76 345
                $this->getSchema(),
77
            );
78
        }
79
80 345
        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 358
    public function getQuoter(): QuoterInterface
84
    {
85 358
        if ($this->quoter === null) {
86 358
            $this->quoter = new Quoter('"', '"', $this->getTablePrefix());
87
        }
88
89 358
        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 359
    public function getSchema(): SchemaInterface
93
    {
94 359
        if ($this->schema === null) {
95 359
            $this->schema = new Schema($this, $this->schemaCache, strtoupper($this->driver->getUsername()));
96
        }
97
98 359
        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
    /**
102
     * Initializes the DB connection.
103
     *
104
     * This method is invoked right after the DB connection is established.
105
     *
106
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
107
     *
108
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
109
     *
110
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
111
     */
112 187
    protected function initConnection(): void
113
    {
114 187
        if ($this->getEmulatePrepare() !== null) {
115
            $this->driver->attributes([PDO::ATTR_EMULATE_PREPARES => $this->getEmulatePrepare()]);
116
        }
117
118 187
        $this->pdo = $this->driver->createConnection();
119
    }
120
}
121