Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

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