Permalink
Browse files

Fixed the error check in queries and storing the right value to last …

…affected in non-select queries.
  • Loading branch information...
1 parent 39320ef commit 7bae41c4617637053cd0285770ac8e70f32a58a3 @jrbasso jrbasso committed May 23, 2011
Showing with 28 additions and 1 deletion.
  1. +28 −1 lib/Cake/Model/Datasource/Database/Sqlserver.php
@@ -58,6 +58,13 @@ class Sqlserver extends DboSource {
protected $_fieldMappings = array();
/**
+ * Storing the last affected value
+ *
+ * @var mixed
+ */
+ protected $_lastAffected = false;
+
+/**
* Base configuration settings for MS SQL driver
*
* @var array
@@ -700,6 +707,19 @@ protected function _getPrimaryKey($model) {
}
/**
+ * Returns number of affected rows in previous database operation. If no previous operation exists,
+ * this returns false.
+ *
+ * @return integer Number of affected rows
+ */
+ public function lastAffected() {
+ $affected = parent::lastAffected();
+ if ($affected === null && $this->_lastAffected !== false) {
+ return $this->_lastAffected;
+ }
+ return $affected;
+ }
+/**
* Executes given SQL statement.
*
* @param string $sql SQL statement
@@ -709,12 +729,19 @@ protected function _getPrimaryKey($model) {
* query returning no rows, suchs as a CREATE statement, false otherwise
*/
protected function _execute($sql, $params = array(), $prepareOptions = array()) {
+ $this->_lastAffected = false;
if (strncasecmp($sql, 'SELECT', 6) == 0) {
$prepareOptions += array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL);
return parent::_execute($sql, $params, $prepareOptions);
}
try {
- $this->_connection->exec($sql);
+ $this->_lastAffected = $this->_connection->exec($sql);
+ if ($this->_lastAffected === false) {
+ $this->_results = null;
+ $error = $this->_connection->errorInfo();
+ $this->error = $error[2];
+ return false;
+ }
return true;
} catch (PDOException $e) {
$this->_results = null;

0 comments on commit 7bae41c

Please sign in to comment.