Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
621 lars 1
<?php
2
 
3
namespace Doctrine\DBAL\Driver\PgSQL;
4
 
5
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
6
use Doctrine\DBAL\ParameterType;
7
use Doctrine\DBAL\SQL\Parser;
8
use Doctrine\Deprecations\Deprecation;
9
use PgSql\Connection as PgSqlConnection;
10
use TypeError;
11
 
12
use function assert;
13
use function get_class;
14
use function gettype;
15
use function is_object;
16
use function is_resource;
17
use function pg_close;
18
use function pg_escape_bytea;
19
use function pg_escape_literal;
20
use function pg_get_result;
21
use function pg_last_error;
22
use function pg_result_error;
23
use function pg_send_prepare;
24
use function pg_send_query;
25
use function pg_version;
26
use function sprintf;
27
use function uniqid;
28
 
29
final class Connection implements ServerInfoAwareConnection
30
{
31
    /** @var PgSqlConnection|resource */
32
    private $connection;
33
 
34
    private Parser $parser;
35
 
36
    /** @param PgSqlConnection|resource $connection */
37
    public function __construct($connection)
38
    {
39
        if (! is_resource($connection) && ! $connection instanceof PgSqlConnection) {
40
            throw new TypeError(sprintf(
41
                'Expected connection to be a resource or an instance of %s, got %s.',
42
                PgSqlConnection::class,
43
                is_object($connection) ? get_class($connection) : gettype($connection),
44
            ));
45
        }
46
 
47
        $this->connection = $connection;
48
        $this->parser     = new Parser(false);
49
    }
50
 
51
    public function __destruct()
52
    {
53
        if (! isset($this->connection)) {
54
            return;
55
        }
56
 
57
        @pg_close($this->connection);
58
    }
59
 
60
    public function prepare(string $sql): Statement
61
    {
62
        $visitor = new ConvertParameters();
63
        $this->parser->parse($sql, $visitor);
64
 
65
        $statementName = uniqid('dbal', true);
66
        if (@pg_send_prepare($this->connection, $statementName, $visitor->getSQL()) !== true) {
67
            throw new Exception(pg_last_error($this->connection));
68
        }
69
 
70
        $result = @pg_get_result($this->connection);
71
        assert($result !== false);
72
 
73
        if ((bool) pg_result_error($result)) {
74
            throw Exception::fromResult($result);
75
        }
76
 
77
        return new Statement($this->connection, $statementName, $visitor->getParameterMap());
78
    }
79
 
80
    public function query(string $sql): Result
81
    {
82
        if (@pg_send_query($this->connection, $sql) !== true) {
83
            throw new Exception(pg_last_error($this->connection));
84
        }
85
 
86
        $result = @pg_get_result($this->connection);
87
        assert($result !== false);
88
 
89
        if ((bool) pg_result_error($result)) {
90
            throw Exception::fromResult($result);
91
        }
92
 
93
        return new Result($result);
94
    }
95
 
96
    /** {@inheritdoc} */
97
    public function quote($value, $type = ParameterType::STRING)
98
    {
99
        if ($type === ParameterType::BINARY || $type === ParameterType::LARGE_OBJECT) {
100
            return sprintf("'%s'", pg_escape_bytea($this->connection, $value));
101
        }
102
 
103
        return pg_escape_literal($this->connection, $value);
104
    }
105
 
106
    public function exec(string $sql): int
107
    {
108
        return $this->query($sql)->rowCount();
109
    }
110
 
111
    /** {@inheritdoc} */
112
    public function lastInsertId($name = null)
113
    {
114
        if ($name !== null) {
115
            Deprecation::triggerIfCalledFromOutside(
116
                'doctrine/dbal',
117
                'https://github.com/doctrine/dbal/issues/4687',
118
                'The usage of Connection::lastInsertId() with a sequence name is deprecated.',
119
            );
120
 
121
            return $this->query(sprintf('SELECT CURRVAL(%s)', $this->quote($name)))->fetchOne();
122
        }
123
 
124
        return $this->query('SELECT LASTVAL()')->fetchOne();
125
    }
126
 
127
    /** @return true */
128
    public function beginTransaction(): bool
129
    {
130
        $this->exec('BEGIN');
131
 
132
        return true;
133
    }
134
 
135
    /** @return true */
136
    public function commit(): bool
137
    {
138
        $this->exec('COMMIT');
139
 
140
        return true;
141
    }
142
 
143
    /** @return true */
144
    public function rollBack(): bool
145
    {
146
        $this->exec('ROLLBACK');
147
 
148
        return true;
149
    }
150
 
151
    public function getServerVersion(): string
152
    {
153
        return (string) pg_version($this->connection)['server'];
154
    }
155
 
156
    /** @return PgSqlConnection|resource */
157
    public function getNativeConnection()
158
    {
159
        return $this->connection;
160
    }
161
}