Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Doctrine\DBAL\Driver\PDO;
4
 
621 lars 5
use Doctrine\DBAL\Driver\PDO\PDOException as DriverPDOException;
148 lars 6
use Doctrine\DBAL\Driver\Result as ResultInterface;
7
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
8
use Doctrine\DBAL\Driver\Statement as StatementInterface;
9
use Doctrine\DBAL\ParameterType;
10
use Doctrine\Deprecations\Deprecation;
11
use PDO;
12
use PDOException;
13
use PDOStatement;
14
 
15
use function assert;
16
 
17
final class Connection implements ServerInfoAwareConnection
18
{
19
    private PDO $connection;
20
 
21
    /** @internal The connection can be only instantiated by its driver. */
22
    public function __construct(PDO $connection)
23
    {
24
        $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
25
 
26
        $this->connection = $connection;
27
    }
28
 
29
    public function exec(string $sql): int
30
    {
31
        try {
32
            $result = $this->connection->exec($sql);
33
 
34
            assert($result !== false);
35
 
36
            return $result;
37
        } catch (PDOException $exception) {
38
            throw Exception::new($exception);
39
        }
40
    }
41
 
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getServerVersion()
46
    {
47
        return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
48
    }
49
 
50
    /**
51
     * {@inheritDoc}
52
     *
53
     * @return Statement
54
     */
55
    public function prepare(string $sql): StatementInterface
56
    {
57
        try {
58
            $stmt = $this->connection->prepare($sql);
59
            assert($stmt instanceof PDOStatement);
60
 
61
            return new Statement($stmt);
62
        } catch (PDOException $exception) {
63
            throw Exception::new($exception);
64
        }
65
    }
66
 
67
    public function query(string $sql): ResultInterface
68
    {
69
        try {
70
            $stmt = $this->connection->query($sql);
71
            assert($stmt instanceof PDOStatement);
72
 
73
            return new Result($stmt);
74
        } catch (PDOException $exception) {
75
            throw Exception::new($exception);
76
        }
77
    }
78
 
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function quote($value, $type = ParameterType::STRING)
83
    {
621 lars 84
        return $this->connection->quote($value, ParameterTypeMap::convertParamType($type));
148 lars 85
    }
86
 
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function lastInsertId($name = null)
91
    {
92
        try {
93
            if ($name === null) {
94
                return $this->connection->lastInsertId();
95
            }
96
 
97
            Deprecation::triggerIfCalledFromOutside(
98
                'doctrine/dbal',
99
                'https://github.com/doctrine/dbal/issues/4687',
100
                'The usage of Connection::lastInsertId() with a sequence name is deprecated.',
101
            );
102
 
103
            return $this->connection->lastInsertId($name);
104
        } catch (PDOException $exception) {
105
            throw Exception::new($exception);
106
        }
107
    }
108
 
109
    public function beginTransaction(): bool
110
    {
621 lars 111
        try {
112
            return $this->connection->beginTransaction();
113
        } catch (PDOException $exception) {
114
            throw DriverPDOException::new($exception);
115
        }
148 lars 116
    }
117
 
118
    public function commit(): bool
119
    {
621 lars 120
        try {
121
            return $this->connection->commit();
122
        } catch (PDOException $exception) {
123
            throw DriverPDOException::new($exception);
124
        }
148 lars 125
    }
126
 
127
    public function rollBack(): bool
128
    {
621 lars 129
        try {
130
            return $this->connection->rollBack();
131
        } catch (PDOException $exception) {
132
            throw DriverPDOException::new($exception);
133
        }
148 lars 134
    }
135
 
136
    public function getNativeConnection(): PDO
137
    {
138
        return $this->connection;
139
    }
140
 
141
    /** @deprecated Call {@see getNativeConnection()} instead. */
142
    public function getWrappedConnection(): PDO
143
    {
144
        Deprecation::triggerIfCalledFromOutside(
145
            'doctrine/dbal',
146
            'https://github.com/doctrine/dbal/pull/5037',
147
            '%s is deprecated, call getNativeConnection() instead.',
148
            __METHOD__,
149
        );
150
 
151
        return $this->getNativeConnection();
152
    }
153
}