PDOConnection.php 3.23 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
    public function exec(string $statement) : int
38 39
    {
        try {
40
            return $this->connection->exec($statement);
41 42 43 44 45
        } catch (\PDOException $exception) {
            throw new PDOException($exception);
        }
    }

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

54
    public function prepare(string $sql) : Statement
55
    {
56
        try {
57
            return $this->createStatement(
58
                $this->connection->prepare($sql)
59
            );
60 61 62
        } catch (\PDOException $exception) {
            throw new PDOException($exception);
        }
63 64
    }

65
    public function query(string $sql) : ResultStatement
66
    {
67
        try {
68
            $stmt = $this->connection->query($sql);
Sergei Morozov's avatar
Sergei Morozov committed
69
            assert($stmt instanceof \PDOStatement);
javer's avatar
javer committed
70

71
            return $this->createStatement($stmt);
72 73 74
        } catch (\PDOException $exception) {
            throw new PDOException($exception);
        }
75 76 77 78 79
    }

    /**
     * {@inheritdoc}
     */
80
    public function quote($input, $type = ParameterType::STRING)
81
    {
82
        return $this->connection->quote($input, $type);
83 84 85 86 87 88 89
    }

    /**
     * {@inheritdoc}
     */
    public function lastInsertId($name = null)
    {
90
        try {
Sergei Morozov's avatar
Sergei Morozov committed
91
            if ($name === null) {
92
                return $this->connection->lastInsertId();
Sergei Morozov's avatar
Sergei Morozov committed
93 94
            }

95
            return $this->connection->lastInsertId($name);
96 97 98
        } catch (\PDOException $exception) {
            throw new PDOException($exception);
        }
99
    }
100 101 102 103 104 105 106 107

    /**
     * {@inheritdoc}
     */
    public function requiresQueryForServerVersion()
    {
        return false;
    }
108 109 110 111

    /**
     * Creates a wrapped statement
     */
112
    protected function createStatement(\PDOStatement $stmt) : PDOStatement
113 114 115
    {
        return new PDOStatement($stmt);
    }
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

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

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

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

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