Passed
Push — implement-getname ( 5df801 )
by Wilmer
56:57 queued 21:37
created

ConnectionPDO::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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 185
    public function createCommand(?string $sql = null, array $params = []): CommandPDOInterface
26
    {
27 185
        $command = new CommandPDO($this, $this->queryCache);
28
29 185
        if ($sql !== null) {
30 184
            $command->setSql($sql);
31
        }
32
33 185
        if ($this->logger !== null) {
34 185
            $command->setLogger($this->logger);
35
        }
36
37 185
        if ($this->profiler !== null) {
38 185
            $command->setProfiler($this->profiler);
39
        }
40
41 185
        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
    public function getName(): string
69
    {
70
        return $this->getDriver()->getName();
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Yiisoft\Db\Driver\PDO\PDODriverInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        return $this->getDriver()->/** @scrutinizer ignore-call */ getName();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
    }
72
73
    /**
74
     * @throws Exception|InvalidConfigException
75
     */
76 346
    public function getQueryBuilder(): QueryBuilderInterface
77
    {
78 346
        if ($this->queryBuilder === null) {
79 346
            $this->queryBuilder = new QueryBuilder(
80 346
                $this->getQuoter(),
81 346
                $this->getSchema(),
82
            );
83
        }
84
85 346
        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...
86
    }
87
88 359
    public function getQuoter(): QuoterInterface
89
    {
90 359
        if ($this->quoter === null) {
91 359
            $this->quoter = new Quoter('"', '"', $this->getTablePrefix());
92
        }
93
94 359
        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...
95
    }
96
97 361
    public function getSchema(): SchemaInterface
98
    {
99 361
        if ($this->schema === null) {
100 361
            $this->schema = new Schema($this, $this->schemaCache, strtoupper($this->driver->getUsername()));
101
        }
102
103 361
        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...
104
    }
105
106
    /**
107
     * Initializes the DB connection.
108
     *
109
     * This method is invoked right after the DB connection is established.
110
     *
111
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
112
     *
113
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
114
     *
115
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
116
     */
117 188
    protected function initConnection(): void
118
    {
119 188
        if ($this->getEmulatePrepare() !== null) {
120
            $this->driver->attributes([PDO::ATTR_EMULATE_PREPARES => $this->getEmulatePrepare()]);
121
        }
122
123 188
        $this->pdo = $this->driver->createConnection();
124
    }
125
}
126