SQLSrvConnection.php 4.21 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Driver\SQLSrv;

Steve Müller's avatar
Steve Müller committed
5
use Doctrine\DBAL\Driver\Connection;
6
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
7
use Doctrine\DBAL\ParameterType;
8 9 10 11 12 13 14 15 16 17 18 19 20 21
use function func_get_args;
use function is_float;
use function is_int;
use function sprintf;
use function sqlsrv_begin_transaction;
use function sqlsrv_commit;
use function sqlsrv_configure;
use function sqlsrv_connect;
use function sqlsrv_errors;
use function sqlsrv_query;
use function sqlsrv_rollback;
use function sqlsrv_rows_affected;
use function sqlsrv_server_info;
use function str_replace;
Grégoire Paris's avatar
Grégoire Paris committed
22
use const SQLSRV_ERR_ERRORS;
Steve Müller's avatar
Steve Müller committed
23

24 25 26
/**
 * SQL Server implementation for the Connection interface.
 */
27
class SQLSrvConnection implements Connection, ServerInfoAwareConnection
28
{
29
    /** @var resource */
30 31
    protected $conn;

32
    /** @var LastInsertId */
33 34
    protected $lastInsertId;

Benjamin Morel's avatar
Benjamin Morel committed
35
    /**
36 37
     * @param string  $serverName
     * @param mixed[] $connectionOptions
Benjamin Morel's avatar
Benjamin Morel committed
38
     *
39
     * @throws SQLSrvException
Benjamin Morel's avatar
Benjamin Morel committed
40
     */
41 42
    public function __construct($serverName, $connectionOptions)
    {
43
        if (! sqlsrv_configure('WarningsReturnAsErrors', 0)) {
44 45 46
            throw SQLSrvException::fromSqlSrvErrors();
        }

Sergei Morozov's avatar
Sergei Morozov committed
47 48 49
        $conn = sqlsrv_connect($serverName, $connectionOptions);

        if ($conn === false) {
50 51
            throw SQLSrvException::fromSqlSrvErrors();
        }
Sergei Morozov's avatar
Sergei Morozov committed
52 53

        $this->conn         = $conn;
54
        $this->lastInsertId = new LastInsertId();
55 56
    }

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    /**
     * {@inheritdoc}
     */
    public function getServerVersion()
    {
        $serverInfo = sqlsrv_server_info($this->conn);

        return $serverInfo['SQLServerVersion'];
    }

    /**
     * {@inheritdoc}
     */
    public function requiresQueryForServerVersion()
    {
        return false;
    }

75 76 77 78 79
    /**
     * {@inheritDoc}
     */
    public function prepare($sql)
    {
Benjamin Eberlei's avatar
Benjamin Eberlei committed
80
        return new SQLSrvStatement($this->conn, $sql, $this->lastInsertId);
81 82 83 84 85 86 87 88
    }

    /**
     * {@inheritDoc}
     */
    public function query()
    {
        $args = func_get_args();
89
        $sql  = $args[0];
90 91
        $stmt = $this->prepare($sql);
        $stmt->execute();
Benjamin Morel's avatar
Benjamin Morel committed
92

93 94 95 96 97 98
        return $stmt;
    }

    /**
     * {@inheritDoc}
     */
99
    public function quote($value, $type = ParameterType::STRING)
100 101 102
    {
        if (is_int($value)) {
            return $value;
103 104 105
        }

        if (is_float($value)) {
106 107 108 109 110 111 112 113 114 115 116
            return sprintf('%F', $value);
        }

        return "'" . str_replace("'", "''", $value) . "'";
    }

    /**
     * {@inheritDoc}
     */
    public function exec($statement)
    {
117 118
        $stmt = sqlsrv_query($this->conn, $statement);

119
        if ($stmt === false) {
120 121
            throw SQLSrvException::fromSqlSrvErrors();
        }
Benjamin Morel's avatar
Benjamin Morel committed
122

Sergei Morozov's avatar
Sergei Morozov committed
123 124 125 126 127 128 129
        $rowsAffected = sqlsrv_rows_affected($stmt);

        if ($rowsAffected === false) {
            throw SQLSrvException::fromSqlSrvErrors();
        }

        return $rowsAffected;
130 131 132 133 134 135 136
    }

    /**
     * {@inheritDoc}
     */
    public function lastInsertId($name = null)
    {
137
        if ($name !== null) {
138
            $stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
139
            $stmt->execute([$name]);
140 141
        } else {
            $stmt = $this->query('SELECT @@IDENTITY');
142 143
        }

144
        return $stmt->fetchColumn();
145 146 147 148 149 150 151
    }

    /**
     * {@inheritDoc}
     */
    public function beginTransaction()
    {
152
        if (! sqlsrv_begin_transaction($this->conn)) {
153 154
            throw SQLSrvException::fromSqlSrvErrors();
        }
155 156

        return true;
157 158 159 160 161 162 163
    }

    /**
     * {@inheritDoc}
     */
    public function commit()
    {
164
        if (! sqlsrv_commit($this->conn)) {
165 166
            throw SQLSrvException::fromSqlSrvErrors();
        }
167 168

        return true;
169 170 171 172 173 174 175
    }

    /**
     * {@inheritDoc}
     */
    public function rollBack()
    {
176
        if (! sqlsrv_rollback($this->conn)) {
177 178
            throw SQLSrvException::fromSqlSrvErrors();
        }
179 180

        return true;
181 182 183 184 185 186 187 188 189 190 191
    }

    /**
     * {@inheritDoc}
     */
    public function errorCode()
    {
        $errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
        if ($errors) {
            return $errors[0]['code'];
        }
Benjamin Morel's avatar
Benjamin Morel committed
192

193 194 195 196 197 198 199 200
        return false;
    }

    /**
     * {@inheritDoc}
     */
    public function errorInfo()
    {
Sergei Morozov's avatar
Sergei Morozov committed
201
        return (array) sqlsrv_errors(SQLSRV_ERR_ERRORS);
202 203
    }
}