1<?php
2
3namespace Doctrine\DBAL\Driver;
4
5use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
6use Doctrine\DBAL\Driver\PDO\Exception;
7use Doctrine\DBAL\Driver\PDO\Statement;
8use Doctrine\DBAL\ParameterType;
9use Doctrine\Deprecations\Deprecation;
10use PDO;
11use PDOException;
12use PDOStatement;
13use ReturnTypeWillChange;
14
15use function assert;
16
17/**
18 * PDO implementation of the Connection interface.
19 * Used by all PDO-based drivers.
20 *
21 * @deprecated Use {@link Connection} instead
22 */
23class PDOConnection extends PDO implements ConnectionInterface, ServerInfoAwareConnection
24{
25    use PDOQueryImplementation;
26
27    /**
28     * @internal The connection can be only instantiated by its driver.
29     *
30     * @param string       $dsn
31     * @param string|null  $user
32     * @param string|null  $password
33     * @param mixed[]|null $options
34     *
35     * @throws PDOException In case of an error.
36     */
37    public function __construct($dsn, $user = null, $password = null, ?array $options = null)
38    {
39        try {
40            parent::__construct($dsn, (string) $user, (string) $password, (array) $options);
41            $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, [Statement::class, []]);
42            $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
43        } catch (PDOException $exception) {
44            throw Exception::new($exception);
45        }
46    }
47
48    /**
49     * {@inheritdoc}
50     */
51    #[ReturnTypeWillChange]
52    public function exec($sql)
53    {
54        try {
55            $result = parent::exec($sql);
56            assert($result !== false);
57
58            return $result;
59        } catch (PDOException $exception) {
60            throw Exception::new($exception);
61        }
62    }
63
64    /**
65     * {@inheritdoc}
66     */
67    public function getServerVersion()
68    {
69        return PDO::getAttribute(PDO::ATTR_SERVER_VERSION);
70    }
71
72    /**
73     * @param string          $sql
74     * @param array<int, int> $driverOptions
75     *
76     * @return PDOStatement
77     */
78    #[ReturnTypeWillChange]
79    public function prepare($sql, $driverOptions = [])
80    {
81        try {
82            $statement = parent::prepare($sql, $driverOptions);
83            assert($statement instanceof PDOStatement);
84
85            return $statement;
86        } catch (PDOException $exception) {
87            throw Exception::new($exception);
88        }
89    }
90
91    /**
92     * {@inheritdoc}
93     */
94    #[ReturnTypeWillChange]
95    public function quote($value, $type = ParameterType::STRING)
96    {
97        return parent::quote($value, $type);
98    }
99
100    /**
101     * {@inheritdoc}
102     *
103     * @param string|null $name
104     *
105     * @return string|int|false
106     */
107    #[ReturnTypeWillChange]
108    public function lastInsertId($name = null)
109    {
110        try {
111            if ($name === null) {
112                return parent::lastInsertId();
113            }
114
115            return parent::lastInsertId($name);
116        } catch (PDOException $exception) {
117            throw Exception::new($exception);
118        }
119    }
120
121    /**
122     * {@inheritdoc}
123     */
124    public function requiresQueryForServerVersion()
125    {
126        Deprecation::triggerIfCalledFromOutside(
127            'doctrine/dbal',
128            'https://github.com/doctrine/dbal/pull/4114',
129            'ServerInfoAwareConnection::requiresQueryForServerVersion() is deprecated and removed in DBAL 3.'
130        );
131
132        return false;
133    }
134
135    /**
136     * @param mixed ...$args
137     */
138    private function doQuery(...$args): PDOStatement
139    {
140        try {
141            $stmt = parent::query(...$args);
142        } catch (PDOException $exception) {
143            throw Exception::new($exception);
144        }
145
146        assert($stmt instanceof PDOStatement);
147
148        return $stmt;
149    }
150}
151