Browse Source

Allow SqlServer to execute procs correctly.

Both SELECT and EXECUTE statements should go through
the parent method as they could fetch results.

Fixes #2558
mark_story 14 years ago
parent
commit
0cfec525b2

+ 1 - 1
lib/Cake/Model/Datasource/Database/Sqlserver.php

@@ -745,7 +745,7 @@ class Sqlserver extends DboSource {
  */
 	protected function _execute($sql, $params = array(), $prepareOptions = array()) {
 		$this->_lastAffected = false;
-		if (strncasecmp($sql, 'SELECT', 6) == 0) {
+		if (strncasecmp($sql, 'SELECT', 6) == 0 || preg_match('/^EXEC(?:UCUTE)?\s/m', $sql) > 0) {
 			$prepareOptions += array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL);
 			return parent::_execute($sql, $params, $prepareOptions);
 		}

+ 27 - 0
lib/Cake/Test/Case/Model/Datasource/Database/SqlserverTest.php

@@ -633,4 +633,31 @@ class SqlserverTest extends CakeTestCase {
 		$this->assertEquals('nate', $results[1]['User']['user']);
 	}
 
+/**
+ * Test that the return of stored procedures is honoured
+ *
+ * @return void
+ */
+	public function testStoredProcedureReturn() {
+		$sql = <<<SQL
+CREATE PROCEDURE cake_test_procedure
+AS
+BEGIN
+RETURN 2;
+END
+SQL;
+		$this->Dbo->execute($sql);
+
+		$sql = <<<SQL
+DECLARE @return_value int
+EXEC @return_value = [cake_test_procedure]
+SELECT 'value' = @return_value
+SQL;
+		$query = $this->Dbo->execute($sql);
+		$this->Dbo->execute('DROP PROC cake_test_procedure');
+
+		$result = $query->fetch();
+		$this->assertEquals(2, $result['value']);
+	}
+
 }