@@ -252,38 +252,46 @@ cdef int msg_handler(DBPROCESS *dbproc, DBINT msgno, int msgstate,
252
252
cdef char * mssql_lastmsgsrv
253
253
cdef char * mssql_lastmsgproc
254
254
cdef int _min_error_severity = min_error_severity
255
+ cdef MSSQLConnection conn = None
255
256
256
257
IF PYMSSQL_DEBUG == 1 :
257
258
fprintf(stderr, " \n +++ msg_handler(dbproc = %p , msgno = %d , " \
258
259
" msgstate = %d , severity = %d , msgtext = '%s ', " \
259
260
" srvname = '%s ', procname = '%s ', line = %d )\n " ,
260
- < void * > dbproc, msgno, msgstate, severity, msgtext, srvname,
261
+ dbproc, msgno, msgstate, severity, msgtext, srvname,
261
262
procname, line);
262
263
fprintf(stderr, " +++ previous max severity = %d \n\n " ,
263
264
_mssql_last_msg_severity);
264
265
266
+ for cnx in connection_object_list:
267
+ if (< MSSQLConnection> cnx).dbproc != dbproc:
268
+ continue
269
+
270
+ conn = < MSSQLConnection> cnx
271
+ break
272
+
273
+ if conn is not None and conn.msghandler is not None :
274
+ conn.msghandler(msgstate, severity, srvname, procname, line, msgtext)
275
+
265
276
if severity < _min_error_severity:
266
277
return INT_CANCEL
267
278
268
- mssql_lastmsgstr = _mssql_last_msg_str
269
- mssql_lastmsgsrv = _mssql_last_msg_srv
270
- mssql_lastmsgproc = _mssql_last_msg_proc
271
- mssql_lastmsgno = & _mssql_last_msg_no
272
- mssql_lastmsgseverity = & _mssql_last_msg_severity
273
- mssql_lastmsgstate = & _mssql_last_msg_state
274
- mssql_lastmsgline = & _mssql_last_msg_line
275
-
276
- for conn in connection_object_list:
277
- if dbproc != (< MSSQLConnection> conn).dbproc:
278
- continue
279
- mssql_lastmsgstr = (< MSSQLConnection> conn).last_msg_str
280
- mssql_lastmsgsrv = (< MSSQLConnection> conn).last_msg_srv
281
- mssql_lastmsgproc = (< MSSQLConnection> conn).last_msg_proc
282
- mssql_lastmsgno = & (< MSSQLConnection> conn).last_msg_no
283
- mssql_lastmsgseverity = & (< MSSQLConnection> conn).last_msg_severity
284
- mssql_lastmsgstate = & (< MSSQLConnection> conn).last_msg_state
285
- mssql_lastmsgline = & (< MSSQLConnection> conn).last_msg_line
286
- break
279
+ if conn is not None :
280
+ mssql_lastmsgstr = conn.last_msg_str
281
+ mssql_lastmsgsrv = conn.last_msg_srv
282
+ mssql_lastmsgproc = conn.last_msg_proc
283
+ mssql_lastmsgno = & conn.last_msg_no
284
+ mssql_lastmsgseverity = & conn.last_msg_severity
285
+ mssql_lastmsgstate = & conn.last_msg_state
286
+ mssql_lastmsgline = & conn.last_msg_line
287
+ else :
288
+ mssql_lastmsgstr = _mssql_last_msg_str
289
+ mssql_lastmsgsrv = _mssql_last_msg_srv
290
+ mssql_lastmsgproc = _mssql_last_msg_proc
291
+ mssql_lastmsgno = & _mssql_last_msg_no
292
+ mssql_lastmsgseverity = & _mssql_last_msg_severity
293
+ mssql_lastmsgstate = & _mssql_last_msg_state
294
+ mssql_lastmsgline = & _mssql_last_msg_line
287
295
288
296
# Calculate the maximum severity of all messages in a row
289
297
# Fill the remaining fields as this is going to raise the exception
@@ -561,6 +569,16 @@ cdef class MSSQLConnection:
561
569
clr_err(self )
562
570
return MSSQLRowIterator(self )
563
571
572
+ cpdef set_msghandler(self , object handler):
573
+ """
574
+ set_msghandler(handler) -- set the msghandler for the connection
575
+
576
+ This function allows setting a msghandler for the connection to
577
+ allow a client to gain access to the messages returned from the
578
+ server.
579
+ """
580
+ self .msghandler = handler
581
+
564
582
cpdef cancel(self ):
565
583
"""
566
584
cancel() -- cancel all pending results.
0 commit comments