Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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