Failed Conditions
Pull Request — develop (#3335)
by
unknown
12:27
created

OCI8Connection::errorCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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