SQLSrvConnection.php 3.96 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 22
use const SQLSRV_ERR_ERRORS;
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;
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();
        }

47
        $this->conn = sqlsrv_connect($serverName, $connectionOptions);
48
        if (! $this->conn) {
49 50
            throw SQLSrvException::fromSqlSrvErrors();
        }
51
        $this->lastInsertId = new LastInsertId();
52 53
    }

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

        return $serverInfo['SQLServerVersion'];
    }

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

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

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

90 91 92 93 94 95
        return $stmt;
    }

    /**
     * {@inheritDoc}
     */
96
    public function quote($value, $type = ParameterType::STRING)
97 98 99
    {
        if (is_int($value)) {
            return $value;
Steve Müller's avatar
Steve Müller committed
100
        } elseif (is_float($value)) {
101 102 103 104 105 106 107 108 109 110 111
            return sprintf('%F', $value);
        }

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

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

114
        if ($stmt === false) {
115 116
            throw SQLSrvException::fromSqlSrvErrors();
        }
Benjamin Morel's avatar
Benjamin Morel committed
117

118
        return sqlsrv_rows_affected($stmt);
119 120 121 122 123 124 125
    }

    /**
     * {@inheritDoc}
     */
    public function lastInsertId($name = null)
    {
126
        if ($name !== null) {
127
            $stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
128
            $stmt->execute([$name]);
129 130
        } else {
            $stmt = $this->query('SELECT @@IDENTITY');
131 132
        }

133
        return $stmt->fetchColumn();
134 135 136 137 138 139 140
    }

    /**
     * {@inheritDoc}
     */
    public function beginTransaction()
    {
141
        if (! sqlsrv_begin_transaction($this->conn)) {
142 143 144 145 146 147 148 149 150
            throw SQLSrvException::fromSqlSrvErrors();
        }
    }

    /**
     * {@inheritDoc}
     */
    public function commit()
    {
151
        if (! sqlsrv_commit($this->conn)) {
152 153 154 155 156 157 158 159 160
            throw SQLSrvException::fromSqlSrvErrors();
        }
    }

    /**
     * {@inheritDoc}
     */
    public function rollBack()
    {
161
        if (! sqlsrv_rollback($this->conn)) {
162 163 164 165 166 167 168 169 170 171 172 173 174
            throw SQLSrvException::fromSqlSrvErrors();
        }
    }

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

176 177 178 179 180 181 182 183 184 185 186
        return false;
    }

    /**
     * {@inheritDoc}
     */
    public function errorInfo()
    {
        return sqlsrv_errors(SQLSRV_ERR_ERRORS);
    }
}