PDOConnection.php 3.59 KB
Newer Older
romanb's avatar
romanb committed
1
<?php
2 3 4

namespace Doctrine\DBAL\Driver;

5
use Doctrine\DBAL\ParameterType;
Benjamin Morel's avatar
Benjamin Morel committed
6
use PDO;
Sergei Morozov's avatar
Sergei Morozov committed
7
use function assert;
8

9
/**
10
 * PDO implementation of the Connection interface.
11
 *
12 13
 * Used by all PDO-based drivers.
 */
14
class PDOConnection implements ServerInfoAwareConnection
romanb's avatar
romanb committed
15
{
16 17 18
    /** @var PDO */
    private $connection;

Benjamin Morel's avatar
Benjamin Morel committed
19
    /**
20 21 22 23
     * @param string       $dsn
     * @param string|null  $user
     * @param string|null  $password
     * @param mixed[]|null $options
24
     *
25
     * @throws PDOException In case of an error.
Benjamin Morel's avatar
Benjamin Morel committed
26
     */
27
    public function __construct($dsn, $user = null, $password = null, ?array $options = null)
romanb's avatar
romanb committed
28
    {
29
        try {
30 31
            $this->connection = new PDO($dsn, (string) $user, (string) $password, (array) $options);
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
32 33 34
        } catch (\PDOException $exception) {
            throw new PDOException($exception);
        }
romanb's avatar
romanb committed
35
    }
36

37 38 39
    /**
     * {@inheritdoc}
     */
40
    public function exec(string $statement) : int
41 42
    {
        try {
43
            return $this->connection->exec($statement);
44 45 46 47 48
        } catch (\PDOException $exception) {
            throw new PDOException($exception);
        }
    }

49 50 51 52 53
    /**
     * {@inheritdoc}
     */
    public function getServerVersion()
    {
54
        return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
55 56
    }

57 58 59
    /**
     * {@inheritdoc}
     */
60
    public function prepare(string $sql) : Statement
61
    {
62
        try {
63
            return $this->createStatement(
64
                $this->connection->prepare($sql)
65
            );
66 67 68
        } catch (\PDOException $exception) {
            throw new PDOException($exception);
        }
69 70 71 72 73
    }

    /**
     * {@inheritdoc}
     */
74
    public function query(string $sql) : ResultStatement
75
    {
76
        try {
77
            $stmt = $this->connection->query($sql);
Sergei Morozov's avatar
Sergei Morozov committed
78
            assert($stmt instanceof \PDOStatement);
javer's avatar
javer committed
79

80
            return $this->createStatement($stmt);
81 82 83
        } catch (\PDOException $exception) {
            throw new PDOException($exception);
        }
84 85 86 87 88
    }

    /**
     * {@inheritdoc}
     */
89
    public function quote($input, $type = ParameterType::STRING)
90
    {
91
        return $this->connection->quote($input, $type);
92 93 94 95 96 97 98
    }

    /**
     * {@inheritdoc}
     */
    public function lastInsertId($name = null)
    {
99
        try {
Sergei Morozov's avatar
Sergei Morozov committed
100
            if ($name === null) {
101
                return $this->connection->lastInsertId();
Sergei Morozov's avatar
Sergei Morozov committed
102 103
            }

104
            return $this->connection->lastInsertId($name);
105 106 107
        } catch (\PDOException $exception) {
            throw new PDOException($exception);
        }
108
    }
109 110 111 112 113 114 115 116

    /**
     * {@inheritdoc}
     */
    public function requiresQueryForServerVersion()
    {
        return false;
    }
117 118 119 120

    /**
     * Creates a wrapped statement
     */
121
    protected function createStatement(\PDOStatement $stmt) : PDOStatement
122 123 124
    {
        return new PDOStatement($stmt);
    }
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

    /**
     * {@inheritDoc}
     */
    public function beginTransaction()
    {
        return $this->connection->beginTransaction();
    }

    /**
     * {@inheritDoc}
     */
    public function commit()
    {
        return $this->connection->commit();
    }

    /**
     * {@inheritDoc}
     */
    public function rollBack()
    {
        return $this->connection->rollBack();
    }

    /**
     * {@inheritDoc}
     */
    public function errorCode()
    {
        return $this->connection->errorCode();
    }

    /**
     * {@inheritDoc}
     */
    public function errorInfo()
    {
        return $this->connection->errorInfo();
    }

    public function getWrappedConnection() : PDO
    {
        return $this->connection;
    }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
170
}