Skip to content
Permalink
Browse files

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

  • Loading branch information...
renan committed Feb 18, 2010
1 parent fc499ac commit 63f7900ba1647e2111b85d9445d3973f746f93f5
@@ -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 @@ function disconnect() {
* @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 @@ function update(&$model, $fields = array(), $values = null, $conditions = null)
* @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;
}
}
@@ -627,5 +627,30 @@ function testInsertMulti() {
);
$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);
}
}
?>

0 comments on commit 63f7900

Please sign in to comment.
You can’t perform that action at this time.