Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Doctrine\DBAL\Driver\OCI8;
4
 
5
use Doctrine\DBAL\Driver\Exception;
6
use Doctrine\DBAL\Driver\OCI8\Exception\Error;
7
use Doctrine\DBAL\Driver\OCI8\Exception\SequenceDoesNotExist;
8
use Doctrine\DBAL\Driver\Result as ResultInterface;
9
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
10
use Doctrine\DBAL\Driver\Statement as DriverStatement;
11
use Doctrine\DBAL\ParameterType;
12
use Doctrine\DBAL\SQL\Parser;
13
use Doctrine\Deprecations\Deprecation;
14
 
15
use function addcslashes;
16
use function assert;
17
use function is_float;
18
use function is_int;
19
use function is_resource;
20
use function oci_commit;
21
use function oci_parse;
22
use function oci_rollback;
23
use function oci_server_version;
24
use function preg_match;
25
use function str_replace;
26
 
27
final class Connection implements ServerInfoAwareConnection
28
{
29
    /** @var resource */
30
    private $connection;
31
 
32
    private Parser $parser;
33
    private ExecutionMode $executionMode;
34
 
35
    /**
36
     * @internal The connection can be only instantiated by its driver.
37
     *
38
     * @param resource $connection
39
     */
40
    public function __construct($connection)
41
    {
42
        $this->connection    = $connection;
43
        $this->parser        = new Parser(false);
44
        $this->executionMode = new ExecutionMode();
45
    }
46
 
47
    public function getServerVersion(): string
48
    {
49
        $version = oci_server_version($this->connection);
50
 
51
        if ($version === false) {
52
            throw Error::new($this->connection);
53
        }
54
 
55
        $result = preg_match('/\s+(\d+\.\d+\.\d+\.\d+\.\d+)\s+/', $version, $matches);
56
        assert($result === 1);
57
 
58
        return $matches[1];
59
    }
60
 
61
    /** @throws Parser\Exception */
62
    public function prepare(string $sql): DriverStatement
63
    {
64
        $visitor = new ConvertPositionalToNamedPlaceholders();
65
 
66
        $this->parser->parse($sql, $visitor);
67
 
68
        $statement = oci_parse($this->connection, $visitor->getSQL());
69
        assert(is_resource($statement));
70
 
71
        return new Statement($this->connection, $statement, $visitor->getParameterMap(), $this->executionMode);
72
    }
73
 
74
    /**
75
     * @throws Exception
76
     * @throws Parser\Exception
77
     */
78
    public function query(string $sql): ResultInterface
79
    {
80
        return $this->prepare($sql)->execute();
81
    }
82
 
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function quote($value, $type = ParameterType::STRING)
87
    {
88
        if (is_int($value) || is_float($value)) {
89
            return $value;
90
        }
91
 
92
        $value = str_replace("'", "''", $value);
93
 
94
        return "'" . addcslashes($value, "\000\n\r\\\032") . "'";
95
    }
96
 
97
    /**
98
     * @throws Exception
99
     * @throws Parser\Exception
100
     */
101
    public function exec(string $sql): int
102
    {
103
        return $this->prepare($sql)->execute()->rowCount();
104
    }
105
 
106
    /**
107
     * {@inheritdoc}
108
     *
109
     * @param string|null $name
110
     *
111
     * @return int|false
112
     *
113
     * @throws Parser\Exception
114
     */
115
    public function lastInsertId($name = null)
116
    {
117
        if ($name === null) {
118
            return false;
119
        }
120
 
121
        Deprecation::triggerIfCalledFromOutside(
122
            'doctrine/dbal',
123
            'https://github.com/doctrine/dbal/issues/4687',
124
            'The usage of Connection::lastInsertId() with a sequence name is deprecated.',
125
        );
126
 
127
        $result = $this->query('SELECT ' . $name . '.CURRVAL FROM DUAL')->fetchOne();
128
 
129
        if ($result === false) {
130
            throw SequenceDoesNotExist::new();
131
        }
132
 
133
        return (int) $result;
134
    }
135
 
136
    public function beginTransaction(): bool
137
    {
138
        $this->executionMode->disableAutoCommit();
139
 
140
        return true;
141
    }
142
 
143
    public function commit(): bool
144
    {
145
        if (! oci_commit($this->connection)) {
146
            throw Error::new($this->connection);
147
        }
148
 
149
        $this->executionMode->enableAutoCommit();
150
 
151
        return true;
152
    }
153
 
154
    public function rollBack(): bool
155
    {
156
        if (! oci_rollback($this->connection)) {
157
            throw Error::new($this->connection);
158
        }
159
 
160
        $this->executionMode->enableAutoCommit();
161
 
162
        return true;
163
    }
164
 
165
    /** @return resource */
166
    public function getNativeConnection()
167
    {
168
        return $this->connection;
169
    }
170
}