00001 <?php
00025 require_once 'Zend/Db/Statement.php';
00026
00036 class Zend_Db_Statement_Sqlsrv extends Zend_Db_Statement
00037 {
00038
00042 protected $_originalSQL;
00043
00047 protected $_keys;
00048
00052 protected $_executed = false;
00053
00061 protected function _prepare($sql)
00062 {
00063 $connection = $this->_adapter->getConnection();
00064
00065 $this->_stmt = sqlsrv_prepare($connection, $sql);
00066
00067 if (!$this->_stmt) {
00068 require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
00069 throw new Zend_Db_Statement_Sqlsrv_Exception(sqlsrv_errors());
00070 }
00071
00072 $this->_originalSQL = $sql;
00073 }
00074
00086 protected function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
00087 {
00088
00089 return true;
00090 }
00091
00097 public function closeCursor()
00098 {
00099 if (!$this->_stmt) {
00100 return false;
00101 }
00102
00103 sqlsrv_free_stmt($this->_stmt);
00104 $this->_stmt = false;
00105 return true;
00106 }
00107
00114 public function columnCount()
00115 {
00116 if ($this->_stmt && $this->_executed) {
00117 return sqlsrv_num_fields($this->_stmt);
00118 }
00119
00120 return 0;
00121 }
00122
00123
00130 public function errorCode()
00131 {
00132 if (!$this->_stmt) {
00133 return false;
00134 }
00135
00136 $error = sqlsrv_errors();
00137 if (!$error) {
00138 return false;
00139 }
00140
00141 return $error[0]['code'];
00142 }
00143
00144
00151 public function errorInfo()
00152 {
00153 if (!$this->_stmt) {
00154 return false;
00155 }
00156
00157 $error = sqlsrv_errors();
00158 if (!$error) {
00159 return false;
00160 }
00161
00162 return array(
00163 $error[0]['code'],
00164 $error[0]['message'],
00165 );
00166 }
00167
00168
00176 public function _execute(array $params = null)
00177 {
00178 $connection = $this->_adapter->getConnection();
00179 if (!$this->_stmt) {
00180 return false;
00181 }
00182
00183 if ($params !== null) {
00184 if (!is_array($params)) {
00185 $params = array($params);
00186 }
00187 $error = false;
00188
00189
00190 $params_ = array();
00191 $temp = array();
00192 $i = 1;
00193 foreach ($params as $param) {
00194 $temp[$i] = $param;
00195 $params_[] = &$temp[$i];
00196 $i++;
00197 }
00198 $params = $params_;
00199 }
00200
00201 $this->_stmt = sqlsrv_query($connection, $this->_originalSQL, $params);
00202
00203 if (!$this->_stmt) {
00204 require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
00205 throw new Zend_Db_Statement_Sqlsrv_Exception(sqlsrv_errors());
00206 }
00207
00208 $this->_executed = true;
00209
00210 return (!$this->_stmt);
00211 }
00212
00222 public function fetch($style = null, $cursor = null, $offset = null)
00223 {
00224 if (!$this->_stmt) {
00225 return false;
00226 }
00227
00228 if (null === $style) {
00229 $style = $this->_fetchMode;
00230 }
00231
00232 $values = sqlsrv_fetch_array($this->_stmt, SQLSRV_FETCH_ASSOC);
00233
00234 if (!$values && (null !== $error = sqlsrv_errors())) {
00235 require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
00236 throw new Zend_Db_Statement_Sqlsrv_Exception($error);
00237 }
00238
00239 if (null === $values) {
00240 return null;
00241 }
00242
00243 if (!$this->_keys) {
00244 foreach ($values as $key => $value) {
00245 $this->_keys[] = $this->_adapter->foldCase($key);
00246 }
00247 }
00248
00249 $values = array_values($values);
00250
00251 $row = false;
00252 switch ($style) {
00253 case Zend_Db::FETCH_NUM:
00254 $row = $values;
00255 break;
00256 case Zend_Db::FETCH_ASSOC:
00257 $row = array_combine($this->_keys, $values);
00258 break;
00259 case Zend_Db::FETCH_BOTH:
00260 $assoc = array_combine($this->_keys, $values);
00261 $row = array_merge($values, $assoc);
00262 break;
00263 case Zend_Db::FETCH_OBJ:
00264 $row = (object) array_combine($this->_keys, $values);
00265 break;
00266 case Zend_Db::FETCH_BOUND:
00267 $assoc = array_combine($this->_keys, $values);
00268 $row = array_merge($values, $assoc);
00269 $row = $this->_fetchBound($row);
00270 break;
00271 default:
00272 require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
00273 throw new Zend_Db_Statement_Sqlsrv_Exception("Invalid fetch mode '$style' specified");
00274 break;
00275 }
00276
00277 return $row;
00278 }
00279
00287 public function fetchColumn($col = 0)
00288 {
00289 if (!$this->_stmt) {
00290 return false;
00291 }
00292
00293 if (!sqlsrv_fetch($this->_stmt)) {
00294 if (null !== $error = sqlsrv_errors()) {
00295 require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
00296 throw new Zend_Db_Statement_Sqlsrv_Exception($error);
00297 }
00298
00299
00300 return false;
00301 }
00302
00303 $data = sqlsrv_get_field($this->_stmt, $col);
00304 if ($data === false) {
00305 require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
00306 throw new Zend_Db_Statement_Sqlsrv_Exception(sqlsrv_errors());
00307 }
00308
00309 return $data;
00310 }
00311
00320 public function fetchObject($class = 'stdClass', array $config = array())
00321 {
00322 if (!$this->_stmt) {
00323 return false;
00324 }
00325
00326 $obj = sqlsrv_fetch_object($this->_stmt);
00327
00328 if ($error = sqlsrv_errors()) {
00329 require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
00330 throw new Zend_Db_Statement_Sqlsrv_Exception($error);
00331 }
00332
00333
00334
00335 if (null === $obj) {
00336 return false;
00337 }
00338
00339 return $obj;
00340 }
00341
00349 public function getColumnMeta($column)
00350 {
00351 $fields = sqlsrv_field_metadata($this->_stmt);
00352
00353 if (!$fields) {
00354 throw new Zend_Db_Statement_Sqlsrv_Exception('Column metadata can not be fetched');
00355 }
00356
00357 if (!isset($fields[$column])) {
00358 throw new Zend_Db_Statement_Sqlsrv_Exception('Column index does not exist in statement');
00359 }
00360
00361 return $fields[$column];
00362 }
00363
00372 public function nextRowset()
00373 {
00374 if (sqlsrv_next_result($this->_stmt) === false) {
00375 require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
00376 throw new Zend_Db_Statement_Sqlsrv_Exception(sqlsrv_errors());
00377 }
00378
00379
00380 }
00381
00390 public function rowCount()
00391 {
00392 if (!$this->_stmt) {
00393 return false;
00394 }
00395
00396 if (!$this->_executed) {
00397 return 0;
00398 }
00399
00400 $num_rows = sqlsrv_rows_affected($this->_stmt);
00401
00402
00403 if ($num_rows === false) {
00404 require_once 'Zend/Db/Statement/Sqlsrv/Exception.php';
00405 throw new Zend_Db_Statement_Sqlsrv_Exception(sqlsrv_errors());
00406 }
00407
00408 return $num_rows;
00409 }
00410 }