1<?php 2/* 3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 14 * 15 * This software consists of voluntary contributions made by many individuals 16 * and is licensed under the MIT license. For more information, see 17 * <http://www.doctrine-project.org>. 18 */ 19 20namespace Doctrine\DBAL\Driver\SQLSrv; 21 22/** 23 * SQL Server implementation for the Connection interface. 24 * 25 * @since 2.3 26 * @author Benjamin Eberlei <kontakt@beberlei.de> 27 */ 28class SQLSrvConnection implements \Doctrine\DBAL\Driver\Connection 29{ 30 /** 31 * @var resource 32 */ 33 protected $conn; 34 35 /** 36 * @var LastInsertId 37 */ 38 protected $lastInsertId; 39 40 41 public function __construct($serverName, $connectionOptions) 42 { 43 $this->conn = sqlsrv_connect($serverName, $connectionOptions); 44 if ( ! $this->conn) { 45 throw SQLSrvException::fromSqlSrvErrors(); 46 } 47 $this->lastInsertId = new LastInsertId(); 48 } 49 50 /** 51 * {@inheritDoc} 52 */ 53 public function prepare($sql) 54 { 55 return new SQLSrvStatement($this->conn, $sql, $this->lastInsertId); 56 } 57 58 /** 59 * {@inheritDoc} 60 */ 61 public function query() 62 { 63 $args = func_get_args(); 64 $sql = $args[0]; 65 $stmt = $this->prepare($sql); 66 $stmt->execute(); 67 return $stmt; 68 } 69 70 /** 71 * {@inheritDoc} 72 * @license New BSD, code from Zend Framework 73 */ 74 public function quote($value, $type=\PDO::PARAM_STR) 75 { 76 if (is_int($value)) { 77 return $value; 78 } else if (is_float($value)) { 79 return sprintf('%F', $value); 80 } 81 82 return "'" . str_replace("'", "''", $value) . "'"; 83 } 84 85 /** 86 * {@inheritDoc} 87 */ 88 public function exec($statement) 89 { 90 $stmt = $this->prepare($statement); 91 $stmt->execute(); 92 return $stmt->rowCount(); 93 } 94 95 /** 96 * {@inheritDoc} 97 */ 98 public function lastInsertId($name = null) 99 { 100 if ($name !== null) { 101 $sql = "SELECT IDENT_CURRENT(".$this->quote($name).") AS LastInsertId"; 102 $stmt = $this->prepare($sql); 103 $stmt->execute(); 104 105 return $stmt->fetchColumn(); 106 } 107 108 return $this->lastInsertId->getId(); 109 } 110 111 /** 112 * {@inheritDoc} 113 */ 114 public function beginTransaction() 115 { 116 if ( ! sqlsrv_begin_transaction($this->conn)) { 117 throw SQLSrvException::fromSqlSrvErrors(); 118 } 119 } 120 121 /** 122 * {@inheritDoc} 123 */ 124 public function commit() 125 { 126 if ( ! sqlsrv_commit($this->conn)) { 127 throw SQLSrvException::fromSqlSrvErrors(); 128 } 129 } 130 131 /** 132 * {@inheritDoc} 133 */ 134 public function rollBack() 135 { 136 if ( ! sqlsrv_rollback($this->conn)) { 137 throw SQLSrvException::fromSqlSrvErrors(); 138 } 139 } 140 141 /** 142 * {@inheritDoc} 143 */ 144 public function errorCode() 145 { 146 $errors = sqlsrv_errors(SQLSRV_ERR_ERRORS); 147 if ($errors) { 148 return $errors[0]['code']; 149 } 150 return false; 151 } 152 153 /** 154 * {@inheritDoc} 155 */ 156 public function errorInfo() 157 { 158 return sqlsrv_errors(SQLSRV_ERR_ERRORS); 159 } 160} 161 162