Passed
Pull Request — master (#3808)
by Sergei
09:45
created

OCI8Connection::getExecuteMode()   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.037

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1.037
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver\OCI8;
6
7
use Doctrine\DBAL\Driver\Connection;
8
use Doctrine\DBAL\Driver\ResultStatement;
9
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
10
use Doctrine\DBAL\Driver\Statement as DriverStatement;
11
use UnexpectedValueException;
12
use const OCI_DEFAULT;
13
use function addcslashes;
14
use function oci_commit;
15
use function oci_connect;
16
use function oci_error;
17
use function oci_pconnect;
18
use function oci_rollback;
19
use function oci_server_version;
20
use function preg_match;
21
use function sprintf;
22
use function str_replace;
23
24
/**
25
 * OCI8 implementation of the Connection interface.
26
 */
27
final class OCI8Connection implements Connection, ServerInfoAwareConnection
28
{
29
    /** @var resource */
30
    protected $dbh;
31
32
    /** @var ExecutionMode */
33
    private $executionMode;
34
35
    /**
36
     * Creates a Connection to an Oracle Database using oci8 extension.
37
     *
38
     * @throws OCI8Exception
39
     */
40 30
    public function __construct(
41
        string $username,
42
        string $password,
43
        string $db,
44
        string $charset = '',
45
        int $sessionMode = OCI_DEFAULT,
46
        bool $persistent = false
47
    ) {
48 30
        $dbh = $persistent
49
            ? @oci_pconnect($username, $password, $db, $charset, $sessionMode)
50 30
            : @oci_connect($username, $password, $db, $charset, $sessionMode);
51
52 30
        if ($dbh === false) {
53 3
            throw OCI8Exception::fromErrorInfo(oci_error());
54
        }
55
56 27
        $this->dbh           = $dbh;
57 27
        $this->executionMode = new ExecutionMode();
58 27
    }
59
60
    /**
61
     * {@inheritdoc}
62
     *
63
     * @throws UnexpectedValueException If the version string returned by the database server
64
     *                                  does not contain a parsable version number.
65
     */
66
    public function getServerVersion() : string
67
    {
68
        $version = oci_server_version($this->dbh);
69
70
        if ($version === false) {
71
            throw OCI8Exception::fromErrorInfo(oci_error($this->dbh));
72
        }
73
74
        if (! preg_match('/\s+(\d+\.\d+\.\d+\.\d+\.\d+)\s+/', $version, $matches)) {
75
            throw new UnexpectedValueException(
76
                sprintf(
77
                    'Unexpected database version string "%s". Cannot parse an appropriate version number from it. ' .
78
                    'Please report this database version string to the Doctrine team.',
79
                    $version
80
                )
81
            );
82
        }
83
84
        return $matches[1];
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 356
    public function prepare(string $sql) : DriverStatement
91
    {
92 356
        return new OCI8Statement($this->dbh, $sql, $this->executionMode);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 208
    public function query(string $sql) : ResultStatement
99
    {
100 208
        $stmt = $this->prepare($sql);
101 208
        $stmt->execute();
102
103 205
        return $stmt;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 2
    public function quote(string $input) : string
110
    {
111 2
        return "'" . addcslashes(str_replace("'", "''", $input), "\000\n\r\\\032") . "'";
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 177
    public function exec(string $statement) : int
118
    {
119 177
        $stmt = $this->prepare($statement);
120 177
        $stmt->execute();
121
122 171
        return $stmt->rowCount();
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 4
    public function lastInsertId(?string $name = null) : string
129
    {
130 4
        if ($name === null) {
131 1
            throw new OCI8Exception('The driver does not support identity columns.');
132
        }
133
134 3
        $sql    = 'SELECT ' . $name . '.CURRVAL FROM DUAL';
135 3
        $stmt   = $this->query($sql);
136 3
        $result = $stmt->fetchColumn();
137
138 3
        if ($result === false) {
139
            throw new OCI8Exception('lastInsertId failed: Query was executed but no result was returned.');
140
        }
141
142 3
        return $result;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 16
    public function beginTransaction() : void
149
    {
150 16
        $this->executionMode->disableAutoCommit();
151 16
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156 7
    public function commit() : void
157
    {
158 7
        if (! oci_commit($this->dbh)) {
159
            throw OCI8Exception::fromErrorInfo(oci_error($this->dbh));
160
        }
161
162 7
        $this->executionMode->enableAutoCommit();
163 7
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168 10
    public function rollBack() : void
169
    {
170 10
        if (! oci_rollback($this->dbh)) {
171
            throw OCI8Exception::fromErrorInfo(oci_error($this->dbh));
172
        }
173
174 10
        $this->executionMode->enableAutoCommit();
175 10
    }
176
}
177