Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | 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
 
27
use const DB2_AUTOCOMMIT_OFF;
28
use const DB2_AUTOCOMMIT_ON;
29
 
30
final class Connection implements ServerInfoAwareConnection
31
{
32
    /** @var resource */
33
    private $connection;
34
 
35
    /**
36
     * @internal The connection can be only instantiated by its driver.
37
     *
38
     * @param resource $connection
39
     */
40
    public function __construct($connection)
41
    {
42
        $this->connection = $connection;
43
    }
44
 
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getServerVersion()
49
    {
50
        $serverInfo = db2_server_info($this->connection);
51
        assert($serverInfo instanceof stdClass);
52
 
53
        return $serverInfo->DBMS_VER;
54
    }
55
 
56
    public function prepare(string $sql): DriverStatement
57
    {
58
        $stmt = @db2_prepare($this->connection, $sql);
59
 
60
        if ($stmt === false) {
61
            throw PrepareFailed::new(error_get_last());
62
        }
63
 
64
        return new Statement($stmt);
65
    }
66
 
67
    public function query(string $sql): ResultInterface
68
    {
69
        return $this->prepare($sql)->execute();
70
    }
71
 
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function quote($value, $type = ParameterType::STRING)
76
    {
77
        $value = db2_escape_string($value);
78
 
79
        if ($type === ParameterType::INTEGER) {
80
            return $value;
81
        }
82
 
83
        return "'" . $value . "'";
84
    }
85
 
86
    public function exec(string $sql): int
87
    {
88
        $stmt = @db2_exec($this->connection, $sql);
89
 
90
        if ($stmt === false) {
91
            throw StatementError::new();
92
        }
93
 
94
        return db2_num_rows($stmt);
95
    }
96
 
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function lastInsertId($name = null)
101
    {
102
        if ($name !== null) {
103
            Deprecation::triggerIfCalledFromOutside(
104
                'doctrine/dbal',
105
                'https://github.com/doctrine/dbal/issues/4687',
106
                'The usage of Connection::lastInsertId() with a sequence name is deprecated.',
107
            );
108
        }
109
 
110
        return db2_last_insert_id($this->connection) ?? false;
111
    }
112
 
113
    public function beginTransaction(): bool
114
    {
688 lars 115
        return db2_autocommit($this->connection, DB2_AUTOCOMMIT_OFF);
148 lars 116
    }
117
 
118
    public function commit(): bool
119
    {
120
        if (! db2_commit($this->connection)) {
121
            throw ConnectionError::new($this->connection);
122
        }
123
 
688 lars 124
        return db2_autocommit($this->connection, DB2_AUTOCOMMIT_ON);
148 lars 125
    }
126
 
127
    public function rollBack(): bool
128
    {
129
        if (! db2_rollback($this->connection)) {
130
            throw ConnectionError::new($this->connection);
131
        }
132
 
688 lars 133
        return db2_autocommit($this->connection, DB2_AUTOCOMMIT_ON);
148 lars 134
    }
135
 
136
    /** @return resource */
137
    public function getNativeConnection()
138
    {
139
        return $this->connection;
140
    }
141
}