Passed
Push — master ( d70a5d...abf1ec )
by Wilmer
10:23 queued 03:57
created

Connection::createTransaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 Throwable;
8
use Yiisoft\Db\Driver\PDO\AbstractConnectionPDO;
9
use Yiisoft\Db\Driver\PDO\CommandPDOInterface;
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
 * Implements a connection to a database via PDO (PHP Data Objects) for Oracle Server.
21
 *
22
 * @link https://www.php.net/manual/en/ref.pdo-oci.php
23
 */
24
final class Connection extends AbstractConnectionPDO
25
{
26 268
    public function createCommand(string $sql = null, array $params = []): CommandPDOInterface
27
    {
28 268
        $command = new Command($this);
29
30 268
        if ($sql !== null) {
31 243
            $command->setSql($sql);
32
        }
33
34 268
        if ($this->logger !== null) {
35 2
            $command->setLogger($this->logger);
36
        }
37
38 268
        if ($this->profiler !== null) {
39 3
            $command->setProfiler($this->profiler);
40
        }
41
42 268
        return $command->bindValues($params);
43
    }
44
45 13
    public function createTransaction(): TransactionInterface
46
    {
47 13
        return new Transaction($this);
48
    }
49
50
    /**
51
     * Override base behaviour to support Oracle sequences.
52
     *
53
     * @throws Exception
54
     * @throws InvalidConfigException
55
     * @throws InvalidCallException
56
     * @throws Throwable
57
     */
58 5
    public function getLastInsertID(string $sequenceName = null): string
59
    {
60 5
        if ($sequenceName === null) {
61 1
            throw new InvalidArgumentException('Oracle not support lastInsertId without sequence name.');
62
        }
63
64 4
        if ($this->isActive()) {
65
            // get the last insert id from connection
66 4
            $sequenceName = $this->getQuoter()->quoteSimpleTableName($sequenceName);
67
68 4
            return (string) $this->createCommand("SELECT $sequenceName.CURRVAL FROM DUAL")->queryScalar();
69
        }
70
71
        throw new InvalidCallException('DB Connection is not active.');
72
    }
73
74 510
    public function getQueryBuilder(): QueryBuilderInterface
75
    {
76 510
        if ($this->queryBuilder === null) {
77 510
            $this->queryBuilder = new QueryBuilder($this->getQuoter(), $this->getSchema());
78
        }
79
80 510
        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 565
    public function getQuoter(): QuoterInterface
84
    {
85 565
        if ($this->quoter === null) {
86 565
            $this->quoter = new Quoter('"', '"', $this->getTablePrefix());
87
        }
88
89 565
        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 535
    public function getSchema(): SchemaInterface
93
    {
94 535
        if ($this->schema === null) {
95 535
            $this->schema = new Schema($this, $this->schemaCache, strtoupper($this->driver->getUsername()));
96
        }
97
98 535
        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