Checking if the last query returned a error, tests added. Fixes #72.

This commit is contained in:
renan.saddam 2010-02-17 22:31:00 -03:00
parent fc499ac48f
commit 63f7900ba1
2 changed files with 38 additions and 5 deletions

View file

@ -99,6 +99,13 @@ class DboMssql extends DboSource {
'commit' => 'COMMIT',
'rollback' => 'ROLLBACK'
);
/**
* Define if the last query had error
*
* @var string
* @access private
*/
var $__lastQueryHadError = false;
/**
* MS SQL DBO driver constructor; sets SQL Server error reporting defaults
*
@ -177,7 +184,9 @@ class DboMssql extends DboSource {
* @access protected
*/
function _execute($sql) {
return mssql_query($sql, $this->connection);
$result = @mssql_query($sql, $this->connection);
$this->__lastQueryHadError = ($result === false);
return $result;
}
/**
* Returns an array of sources (tables) in the database.
@ -411,10 +420,9 @@ class DboMssql extends DboSource {
* @return string Error message with error number
*/
function lastError() {
$error = mssql_get_last_message();
if ($error) {
if (!preg_match('/contexto de la base de datos a|contesto di database|changed database|contexte de la base de don|datenbankkontext/i', $error)) {
if ($this->__lastQueryHadError) {
$error = mssql_get_last_message();
if ($error && !preg_match('/contexto de la base de datos a|contesto di database|changed database|contexte de la base de don|datenbankkontext/i', $error)) {
return $error;
}
}

View file

@ -627,5 +627,30 @@ class DboMssqlTest extends CakeTestCase {
);
$this->assertEqual($result, $expected);
}
/**
* testLastError
*
* @return void
* @access public
*/
function testLastError() {
$debug = Configure::read('debug');
Configure::write('debug', 0);
$this->db->simulate = false;
$query = 'SELECT [name] FROM [categories]';
$this->assertTrue($this->db->execute($query) !== false);
$this->assertNull($this->db->lastError());
$query = 'SELECT [inexistent_field] FROM [categories]';
$this->assertFalse($this->db->execute($query));
$this->assertNotNull($this->db->lastError());
$query = 'SELECT [name] FROM [categories]';
$this->assertTrue($this->db->execute($query) !== false);
$this->assertNull($this->db->lastError());
Configure::write('debug', $debug);
}
}
?>