Part of the EllisLab Network
   
 
Support for error numbers, insert_id() and error strings in MSSQL driver
Posted: 11 February 2007 07:25 PM   [ Ignore ]  
Summer Student
Total Posts:  9
Joined  02-04-2007

Hi,

I fixed the issues in the subject line.  Note that the change to DB_driver.php is required so that the call to _error_number() does not screw up the _error_message() result.  Here is the patch:

Index: DB_driver.php
===================================================================
---
DB_driver.php       (revision 1)
+++
DB_driver.php       (revision 8)
@@ -
277,11 +277,13 @@

                        if (
$this->db_debug)
                        
{
-                               log_message('error', 'Query error: '.$this->_error_message());
+                               
$err_msg = $this->_error_message();
+                               
log_message('error', 'Query error: '.$err_msg);
+
                                return
$this->display_error(
                                                                                array(
                                                                                                
'Error Number: '.$this->_error_number(),
-                                                                                               
$this->_error_message(),
+                                                                                               
$err_msg,
                                                                                                
$sql
                                                                                        
)
                                                                                );
@@ -
1042,7 +1044,7 @@
                
$LANG->load('db');

                
$heading = 'MySQL Error';
-
+
                if (
$native == TRUE)
                
{
                        $message
= $error;
Index: drivers/mssql/mssql_driver.php
===================================================================
---
drivers/mssql/mssql_driver.php      (revision 1)
+++
drivers/mssql/mssql_driver.php      (revision 8)
@@ -
229,8 +229,10 @@
         */
        function
insert_id()
        
{
-               // Not supported in MS SQL?
-               return 0;
+               
$query = $this->query("SELECT @@IDENTITY AS insert_id");
+
+               
$row = $query->row();
+               return
$row->insert_id;
        
}

        
// --------------------------------------------------------------------
@@ -316,8 +318,10 @@
         */
        function
_error_message()
        
{
-               // Are errros even supported in MS SQL?
-               return '';
+               
// Note: only returns the last line of the last message.  If reporting an
+               // error, take care to fetch the message BEFORE the error number, as getting
+               // the error number involves performing a query.
+               return mssql_get_last_message();
        
}

        
// --------------------------------------------------------------------
@@ -330,8 +334,10 @@
         */
        function
_error_number()
        
{
-               // Are error numbers supported?
-               return '';
+               
$query = $this->query("SELECT @@ERROR AS error_number");
+
+               
$row = $query->row();
+               return
$row->error_number;
        
}

        
// --------------------------------------------------------------------

Profile
 
 
Posted: 22 May 2007 04:04 PM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  5
Joined  04-15-2007

Thanks for this, I just applied this patch and it works nicely…

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 64453 Total Logged-in Users: 25
Total Topics: 80958 Total Anonymous Users: 0
Total Replies: 435680 Total Guests: 185
Total Posts: 516638    
Members ( View Memberlist )