Db2
2 years ago
Mysqli
2 years ago
Oracle
2 years ago
Pdo
2 years ago
Sqlsrv
2 years ago
Db2.php
1 year ago
Exception.php
2 years ago
Interface.php
2 years ago
Mysqli.php
1 year ago
Oracle.php
1 year ago
Pdo.php
1 year ago
Sqlsrv.php
1 year ago
Sqlsrv.php
378 lines
| 1 | <?php |
| 2 | |
| 3 | namespace { |
| 4 | /** |
| 5 | * Zend Framework |
| 6 | * |
| 7 | * LICENSE |
| 8 | * |
| 9 | * This source file is subject to the new BSD license that is bundled |
| 10 | * with this package in the file LICENSE.txt. |
| 11 | * It is also available through the world-wide-web at this URL: |
| 12 | * http://framework.zend.com/license/new-bsd |
| 13 | * If you did not receive a copy of the license and are unable to |
| 14 | * obtain it through the world-wide-web, please send an email |
| 15 | * to license@zend.com so we can send you a copy immediately. |
| 16 | * |
| 17 | * @category Zend |
| 18 | * @package Zend_Db |
| 19 | * @subpackage Statement |
| 20 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 21 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 22 | * @version $Id: Sqlsrv.php 23775 2011-03-01 17:25:24Z ralph $ |
| 23 | */ |
| 24 | /** |
| 25 | * @see Zend_Db_Statement |
| 26 | */ |
| 27 | // require_once 'Zend/Db/Statement.php'; |
| 28 | /** |
| 29 | * Extends for Microsoft SQL Server Driver for PHP |
| 30 | * |
| 31 | * @category Zend |
| 32 | * @package Zend_Db |
| 33 | * @subpackage Statement |
| 34 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 35 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 36 | */ |
| 37 | class Zend_Db_Statement_Sqlsrv extends \Zend_Db_Statement |
| 38 | { |
| 39 | /** |
| 40 | * The connection_stmt object original string. |
| 41 | */ |
| 42 | protected $_originalSQL; |
| 43 | /** |
| 44 | * Column names. |
| 45 | */ |
| 46 | protected $_keys; |
| 47 | /** |
| 48 | * Query executed |
| 49 | */ |
| 50 | protected $_executed = \false; |
| 51 | /** |
| 52 | * Prepares statement handle |
| 53 | * |
| 54 | * @param string $sql |
| 55 | * @return void |
| 56 | * @throws Zend_Db_Statement_Sqlsrv_Exception |
| 57 | */ |
| 58 | protected function _prepare($sql) |
| 59 | { |
| 60 | $connection = $this->_adapter->getConnection(); |
| 61 | $this->_stmt = \sqlsrv_prepare($connection, $sql); |
| 62 | if (!$this->_stmt) { |
| 63 | // require_once 'Zend/Db/Statement/Sqlsrv/Exception.php'; |
| 64 | throw new \Zend_Db_Statement_Sqlsrv_Exception(\sqlsrv_errors()); |
| 65 | } |
| 66 | $this->_originalSQL = $sql; |
| 67 | } |
| 68 | /** |
| 69 | * Binds a parameter to the specified variable name. |
| 70 | * |
| 71 | * @param mixed $parameter Name the parameter, either integer or string. |
| 72 | * @param mixed $variable Reference to PHP variable containing the value. |
| 73 | * @param mixed $type OPTIONAL Datatype of SQL parameter. |
| 74 | * @param mixed $length OPTIONAL Length of SQL parameter. |
| 75 | * @param mixed $options OPTIONAL Other options. |
| 76 | * @return bool |
| 77 | * @throws Zend_Db_Statement_Exception |
| 78 | */ |
| 79 | protected function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null) |
| 80 | { |
| 81 | //Db server doesn't support bind by name |
| 82 | return \true; |
| 83 | } |
| 84 | /** |
| 85 | * Closes the cursor, allowing the statement to be executed again. |
| 86 | * |
| 87 | * @return bool |
| 88 | */ |
| 89 | public function closeCursor() |
| 90 | { |
| 91 | if (!$this->_stmt) { |
| 92 | return \false; |
| 93 | } |
| 94 | \sqlsrv_free_stmt($this->_stmt); |
| 95 | $this->_stmt = \false; |
| 96 | return \true; |
| 97 | } |
| 98 | /** |
| 99 | * Returns the number of columns in the result set. |
| 100 | * Returns null if the statement has no result set metadata. |
| 101 | * |
| 102 | * @return int The number of columns. |
| 103 | */ |
| 104 | public function columnCount() |
| 105 | { |
| 106 | if ($this->_stmt && $this->_executed) { |
| 107 | return \sqlsrv_num_fields($this->_stmt); |
| 108 | } |
| 109 | return 0; |
| 110 | } |
| 111 | /** |
| 112 | * Retrieves the error code, if any, associated with the last operation on |
| 113 | * the statement handle. |
| 114 | * |
| 115 | * @return string error code. |
| 116 | */ |
| 117 | public function errorCode() |
| 118 | { |
| 119 | if (!$this->_stmt) { |
| 120 | return \false; |
| 121 | } |
| 122 | $error = \sqlsrv_errors(); |
| 123 | if (!$error) { |
| 124 | return \false; |
| 125 | } |
| 126 | return $error[0]['code']; |
| 127 | } |
| 128 | /** |
| 129 | * Retrieves an array of error information, if any, associated with the |
| 130 | * last operation on the statement handle. |
| 131 | * |
| 132 | * @return array |
| 133 | */ |
| 134 | public function errorInfo() |
| 135 | { |
| 136 | if (!$this->_stmt) { |
| 137 | return \false; |
| 138 | } |
| 139 | $error = \sqlsrv_errors(); |
| 140 | if (!$error) { |
| 141 | return \false; |
| 142 | } |
| 143 | return array($error[0]['code'], $error[0]['message']); |
| 144 | } |
| 145 | /** |
| 146 | * Executes a prepared statement. |
| 147 | * |
| 148 | * @param array $params OPTIONAL Values to bind to parameter placeholders. |
| 149 | * @return bool |
| 150 | * @throws Zend_Db_Statement_Exception |
| 151 | */ |
| 152 | public function _execute(?array $params = null) |
| 153 | { |
| 154 | $connection = $this->_adapter->getConnection(); |
| 155 | if (!$this->_stmt) { |
| 156 | return \false; |
| 157 | } |
| 158 | if ($params !== null) { |
| 159 | if (!\is_array($params)) { |
| 160 | $params = array($params); |
| 161 | } |
| 162 | $error = \false; |
| 163 | // make all params passed by reference |
| 164 | $params_ = array(); |
| 165 | $temp = array(); |
| 166 | $i = 1; |
| 167 | foreach ($params as $param) { |
| 168 | $temp[$i] = $param; |
| 169 | $params_[] =& $temp[$i]; |
| 170 | $i++; |
| 171 | } |
| 172 | $params = $params_; |
| 173 | } |
| 174 | $this->_stmt = \sqlsrv_query($connection, $this->_originalSQL, $params); |
| 175 | if (!$this->_stmt) { |
| 176 | // require_once 'Zend/Db/Statement/Sqlsrv/Exception.php'; |
| 177 | throw new \Zend_Db_Statement_Sqlsrv_Exception(\sqlsrv_errors()); |
| 178 | } |
| 179 | $this->_executed = \true; |
| 180 | return !$this->_stmt; |
| 181 | } |
| 182 | /** |
| 183 | * Fetches a row from the result set. |
| 184 | * |
| 185 | * @param int $style OPTIONAL Fetch mode for this fetch operation. |
| 186 | * @param int $cursor OPTIONAL Absolute, relative, or other. |
| 187 | * @param int $offset OPTIONAL Number for absolute or relative cursors. |
| 188 | * @return mixed Array, object, or scalar depending on fetch mode. |
| 189 | * @throws Zend_Db_Statement_Exception |
| 190 | */ |
| 191 | public function fetch($style = null, $cursor = null, $offset = null) |
| 192 | { |
| 193 | if (!$this->_stmt) { |
| 194 | return \false; |
| 195 | } |
| 196 | if (null === $style) { |
| 197 | $style = $this->_fetchMode; |
| 198 | } |
| 199 | $values = \sqlsrv_fetch_array($this->_stmt, \SQLSRV_FETCH_ASSOC); |
| 200 | if (!$values && null !== ($error = \sqlsrv_errors())) { |
| 201 | // require_once 'Zend/Db/Statement/Sqlsrv/Exception.php'; |
| 202 | throw new \Zend_Db_Statement_Sqlsrv_Exception($error); |
| 203 | } |
| 204 | if (null === $values) { |
| 205 | return null; |
| 206 | } |
| 207 | if (!$this->_keys) { |
| 208 | foreach ($values as $key => $value) { |
| 209 | $this->_keys[] = $this->_adapter->foldCase($key); |
| 210 | } |
| 211 | } |
| 212 | $values = \array_values($values); |
| 213 | $row = \false; |
| 214 | switch ($style) { |
| 215 | case \Zend_Db::FETCH_NUM: |
| 216 | $row = $values; |
| 217 | break; |
| 218 | case \Zend_Db::FETCH_ASSOC: |
| 219 | $row = \array_combine($this->_keys, $values); |
| 220 | break; |
| 221 | case \Zend_Db::FETCH_BOTH: |
| 222 | $assoc = \array_combine($this->_keys, $values); |
| 223 | $row = \array_merge($values, $assoc); |
| 224 | break; |
| 225 | case \Zend_Db::FETCH_OBJ: |
| 226 | $row = (object) \array_combine($this->_keys, $values); |
| 227 | break; |
| 228 | case \Zend_Db::FETCH_BOUND: |
| 229 | $assoc = \array_combine($this->_keys, $values); |
| 230 | $row = \array_merge($values, $assoc); |
| 231 | $row = $this->_fetchBound($row); |
| 232 | break; |
| 233 | default: |
| 234 | // require_once 'Zend/Db/Statement/Sqlsrv/Exception.php'; |
| 235 | throw new \Zend_Db_Statement_Sqlsrv_Exception("Invalid fetch mode '{$style}' specified"); |
| 236 | break; |
| 237 | } |
| 238 | return $row; |
| 239 | } |
| 240 | /** |
| 241 | * Returns a single column from the next row of a result set. |
| 242 | * |
| 243 | * @param int $col OPTIONAL Position of the column to fetch. |
| 244 | * @return string |
| 245 | * @throws Zend_Db_Statement_Exception |
| 246 | */ |
| 247 | public function fetchColumn($col = 0) |
| 248 | { |
| 249 | if (!$this->_stmt) { |
| 250 | return \false; |
| 251 | } |
| 252 | if (!\sqlsrv_fetch($this->_stmt)) { |
| 253 | if (null !== ($error = \sqlsrv_errors())) { |
| 254 | // require_once 'Zend/Db/Statement/Sqlsrv/Exception.php'; |
| 255 | throw new \Zend_Db_Statement_Sqlsrv_Exception($error); |
| 256 | } |
| 257 | // If no error, there is simply no record |
| 258 | return \false; |
| 259 | } |
| 260 | $data = \sqlsrv_get_field($this->_stmt, $col); |
| 261 | //0-based |
| 262 | if ($data === \false) { |
| 263 | // require_once 'Zend/Db/Statement/Sqlsrv/Exception.php'; |
| 264 | throw new \Zend_Db_Statement_Sqlsrv_Exception(\sqlsrv_errors()); |
| 265 | } |
| 266 | return $data; |
| 267 | } |
| 268 | /** |
| 269 | * Fetches the next row and returns it as an object. |
| 270 | * |
| 271 | * @param string $class OPTIONAL Name of the class to create. |
| 272 | * @param array $config OPTIONAL Constructor arguments for the class. |
| 273 | * @return mixed One object instance of the specified class. |
| 274 | * @throws Zend_Db_Statement_Exception |
| 275 | */ |
| 276 | public function fetchObject($class = 'stdClass', array $config = array()) |
| 277 | { |
| 278 | if (!$this->_stmt) { |
| 279 | return \false; |
| 280 | } |
| 281 | $obj = \sqlsrv_fetch_object($this->_stmt); |
| 282 | if ($error = \sqlsrv_errors()) { |
| 283 | // require_once 'Zend/Db/Statement/Sqlsrv/Exception.php'; |
| 284 | throw new \Zend_Db_Statement_Sqlsrv_Exception($error); |
| 285 | } |
| 286 | /* @todo XXX handle parameters */ |
| 287 | if (null === $obj) { |
| 288 | return \false; |
| 289 | } |
| 290 | return $obj; |
| 291 | } |
| 292 | /** |
| 293 | * Returns metadata for a column in a result set. |
| 294 | * |
| 295 | * @param int $column |
| 296 | * @return mixed |
| 297 | * @throws Zend_Db_Statement_Sqlsrv_Exception |
| 298 | */ |
| 299 | public function getColumnMeta($column) |
| 300 | { |
| 301 | $fields = \sqlsrv_field_metadata($this->_stmt); |
| 302 | if (!$fields) { |
| 303 | throw new \Zend_Db_Statement_Sqlsrv_Exception('Column metadata can not be fetched'); |
| 304 | } |
| 305 | if (!isset($fields[$column])) { |
| 306 | throw new \Zend_Db_Statement_Sqlsrv_Exception('Column index does not exist in statement'); |
| 307 | } |
| 308 | return $fields[$column]; |
| 309 | } |
| 310 | /** |
| 311 | * Retrieves the next rowset (result set) for a SQL statement that has |
| 312 | * multiple result sets. An example is a stored procedure that returns |
| 313 | * the results of multiple queries. |
| 314 | * |
| 315 | * @return bool |
| 316 | * @throws Zend_Db_Statement_Exception |
| 317 | */ |
| 318 | public function nextRowset() |
| 319 | { |
| 320 | if (\sqlsrv_next_result($this->_stmt) === \false) { |
| 321 | // require_once 'Zend/Db/Statement/Sqlsrv/Exception.php'; |
| 322 | throw new \Zend_Db_Statement_Sqlsrv_Exception(\sqlsrv_errors()); |
| 323 | } |
| 324 | // reset column keys |
| 325 | $this->_keys = null; |
| 326 | return \true; |
| 327 | } |
| 328 | /** |
| 329 | * Returns the number of rows affected by the execution of the |
| 330 | * last INSERT, DELETE, or UPDATE statement executed by this |
| 331 | * statement object. |
| 332 | * |
| 333 | * @return int The number of rows affected. |
| 334 | * @throws Zend_Db_Statement_Exception |
| 335 | */ |
| 336 | public function rowCount() |
| 337 | { |
| 338 | if (!$this->_stmt) { |
| 339 | return \false; |
| 340 | } |
| 341 | if (!$this->_executed) { |
| 342 | return 0; |
| 343 | } |
| 344 | $num_rows = \sqlsrv_rows_affected($this->_stmt); |
| 345 | // Strict check is necessary; 0 is a valid return value |
| 346 | if ($num_rows === \false) { |
| 347 | // require_once 'Zend/Db/Statement/Sqlsrv/Exception.php'; |
| 348 | throw new \Zend_Db_Statement_Sqlsrv_Exception(\sqlsrv_errors()); |
| 349 | } |
| 350 | return $num_rows; |
| 351 | } |
| 352 | /** |
| 353 | * Returns an array containing all of the result set rows. |
| 354 | * |
| 355 | * @param int $style OPTIONAL Fetch mode. |
| 356 | * @param int $col OPTIONAL Column number, if fetch mode is by column. |
| 357 | * @return array Collection of rows, each in a format by the fetch mode. |
| 358 | * |
| 359 | * Behaves like parent, but if limit() |
| 360 | * is used, the final result removes the extra column |
| 361 | * 'zend_db_rownum' |
| 362 | */ |
| 363 | public function fetchAll($style = null, $col = null) |
| 364 | { |
| 365 | $data = parent::fetchAll($style, $col); |
| 366 | $results = array(); |
| 367 | $remove = $this->_adapter->foldCase('ZEND_DB_ROWNUM'); |
| 368 | foreach ($data as $row) { |
| 369 | if (\is_array($row) && \array_key_exists($remove, $row)) { |
| 370 | unset($row[$remove]); |
| 371 | } |
| 372 | $results[] = $row; |
| 373 | } |
| 374 | return $results; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 |