Passed
Pull Request — dev (#60)
by Def
15:02 queued 07:01
created

ConnectionPDOOracle::getLastInsertID()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 3
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\Connection\ConnectionPDO;
9
use Yiisoft\Db\Exception\Exception;
10
use Yiisoft\Db\Exception\InvalidArgumentException;
11
use Yiisoft\Db\Exception\InvalidCallException;
12
use Yiisoft\Db\Exception\InvalidConfigException;
13
use Yiisoft\Db\Oracle\Quoter;
14
use Yiisoft\Db\Query\QueryBuilderInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Query\QueryBuilderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Yiisoft\Db\Schema\QuoterInterface;
16
use Yiisoft\Db\Schema\SchemaInterface;
17
use Yiisoft\Db\Transaction\TransactionInterface;
18
19
use function constant;
20
21
/**
22
 * Database connection class prefilled for Oracle SQL Server.
23
 * The class Connection represents a connection to a database via [PDO](https://secure.php.net/manual/en/book.pdo.php).
24
 */
25
final class ConnectionPDOOracle extends ConnectionPDO
26
{
27 336
    public function createCommand(?string $sql = null, array $params = []): CommandPDOOracle
28
    {
29 336
        $command = new CommandPDOOracle($this, $this->queryCache);
30
31 336
        if ($sql !== null) {
32 174
            $command->setSql($sql);
33
        }
34
35 336
        if ($this->logger !== null) {
36 336
            $command->setLogger($this->logger);
37
        }
38
39 336
        if ($this->profiler !== null) {
40 336
            $command->setProfiler($this->profiler);
41
        }
42
43 336
        return $command->bindValues($params);
44
    }
45
46 8
    public function createTransaction(): TransactionInterface
47
    {
48 8
        return new TransactionPDOOracle($this);
49
    }
50
51 7
    public function getDriverName(): string
52
    {
53 7
        return 'oci';
54
    }
55
56
    /**
57
     * Override base behaviour
58
     */
59 5
    public function getLastInsertID(string $sequenceName = null): string
60
    {
61 5
        if ($sequenceName === null) {
62 1
            throw new InvalidArgumentException('Oracle not support lastInsertId without sequence name');
63
        }
64
65 4
        if ($this->isActive()) {
66
            /* get the last insert id from connection */
67 3
            $sequenceName = $this->getQuoter()->quoteSimpleTableName($sequenceName);
68
69 3
            return (string) $this->createCommand("SELECT $sequenceName.CURRVAL FROM DUAL")->queryScalar();
70
        }
71
72 1
        throw new InvalidCallException('DB Connection is not active.');
73
    }
74
75
    /**
76
     * @throws Exception|InvalidConfigException
77
     */
78 336
    public function getQueryBuilder(): QueryBuilderInterface
79
    {
80 336
        if ($this->queryBuilder === null) {
81 336
            $this->queryBuilder = new QueryBuilderPDOOracle(
82 336
                $this->createCommand(),
83 336
                $this->getQuoter(),
84 336
                $this->getSchema(),
85
            );
86
        }
87
88 336
        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...
89
    }
90
91 349
    public function getQuoter(): QuoterInterface
92
    {
93 349
        if ($this->quoter === null) {
94 349
            $this->quoter = new Quoter('"', '"', $this->getTablePrefix());
95
        }
96
97 349
        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...
98
    }
99
100 350
    public function getSchema(): SchemaInterface
101
    {
102 350
        if ($this->schema === null) {
103 350
            $this->schema = new SchemaPDOOracle($this, $this->schemaCache, strtoupper($this->driver->getUsername()));
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Oracle\PDO\SchemaPDOOracle was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
104
        }
105
106 350
        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...
107
    }
108
109
    /**
110
     * Initializes the DB connection.
111
     *
112
     * This method is invoked right after the DB connection is established.
113
     *
114
     * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES`.
115
     *
116
     * if {@see emulatePrepare} is true, and sets the database {@see charset} if it is not empty.
117
     *
118
     * It then triggers an {@see EVENT_AFTER_OPEN} event.
119
     */
120 177
    protected function initConnection(): void
121
    {
122 177
        $this->pdo = $this->driver->createConnection();
123 177
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
124
125 177
        if ($this->getEmulatePrepare() !== null && constant('PDO::ATTR_EMULATE_PREPARES')) {
126
            $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, $this->getEmulatePrepare());
127
        }
128
    }
129
}
130