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\Mysqli;
4
 
5
use Doctrine\DBAL\Driver\Mysqli\Exception\ConnectionError;
6
use Doctrine\DBAL\Driver\Result as ResultInterface;
7
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
8
use Doctrine\DBAL\Driver\Statement as DriverStatement;
9
use Doctrine\DBAL\ParameterType;
10
use Doctrine\Deprecations\Deprecation;
11
use mysqli;
12
use mysqli_sql_exception;
13
 
14
final class Connection implements ServerInfoAwareConnection
15
{
16
    /**
17
     * Name of the option to set connection flags
18
     */
19
    public const OPTION_FLAGS = 'flags';
20
 
21
    private mysqli $connection;
22
 
23
    /** @internal The connection can be only instantiated by its driver. */
24
    public function __construct(mysqli $connection)
25
    {
26
        $this->connection = $connection;
27
    }
28
 
29
    /**
30
     * Retrieves mysqli native resource handle.
31
     *
32
     * Could be used if part of your application is not using DBAL.
33
     *
34
     * @deprecated Call {@see getNativeConnection()} instead.
35
     */
36
    public function getWrappedResourceHandle(): mysqli
37
    {
38
        Deprecation::trigger(
39
            'doctrine/dbal',
40
            'https://github.com/doctrine/dbal/pull/5037',
41
            '%s is deprecated, call getNativeConnection() instead.',
42
            __METHOD__,
43
        );
44
 
45
        return $this->getNativeConnection();
46
    }
47
 
48
    public function getServerVersion(): string
49
    {
50
        return $this->connection->get_server_info();
51
    }
52
 
53
    public function prepare(string $sql): DriverStatement
54
    {
55
        try {
56
            $stmt = $this->connection->prepare($sql);
57
        } catch (mysqli_sql_exception $e) {
58
            throw ConnectionError::upcast($e);
59
        }
60
 
61
        if ($stmt === false) {
62
            throw ConnectionError::new($this->connection);
63
        }
64
 
65
        return new Statement($stmt);
66
    }
67
 
68
    public function query(string $sql): ResultInterface
69
    {
70
        return $this->prepare($sql)->execute();
71
    }
72
 
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function quote($value, $type = ParameterType::STRING)
77
    {
78
        return "'" . $this->connection->escape_string($value) . "'";
79
    }
80
 
81
    public function exec(string $sql): int
82
    {
83
        try {
84
            $result = $this->connection->query($sql);
85
        } catch (mysqli_sql_exception $e) {
86
            throw ConnectionError::upcast($e);
87
        }
88
 
89
        if ($result === false) {
90
            throw ConnectionError::new($this->connection);
91
        }
92
 
93
        return $this->connection->affected_rows;
94
    }
95
 
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function lastInsertId($name = null)
100
    {
101
        if ($name !== null) {
102
            Deprecation::triggerIfCalledFromOutside(
103
                'doctrine/dbal',
104
                'https://github.com/doctrine/dbal/issues/4687',
105
                'The usage of Connection::lastInsertId() with a sequence name is deprecated.',
106
            );
107
        }
108
 
109
        return $this->connection->insert_id;
110
    }
111
 
112
    public function beginTransaction(): bool
113
    {
114
        $this->connection->begin_transaction();
115
 
116
        return true;
117
    }
118
 
119
    public function commit(): bool
120
    {
121
        try {
122
            return $this->connection->commit();
123
        } catch (mysqli_sql_exception $e) {
124
            return false;
125
        }
126
    }
127
 
128
    public function rollBack(): bool
129
    {
130
        try {
131
            return $this->connection->rollback();
132
        } catch (mysqli_sql_exception $e) {
133
            return false;
134
        }
135
    }
136
 
137
    public function getNativeConnection(): mysqli
138
    {
139
        return $this->connection;
140
    }
141
}