[freetds] Error/Message handling techniques in C++

James K. Lowden jklowden at freetds.org
Thu Nov 29 21:31:16 EST 2007


Sharan Rao wrote:
> I am using db-lib alongwith C++ code. Are there any alternatives for
> error  retrieval / message retrieval other than using callback functions
> ? Using callbacks is not really enjoyable in C++ code. As the callback
> interface  is not accessible, AFAIK, I'll have to resort to some global
> variable stuff  :( ( Any better methods ? )

You don't need any global variables.  Here's what I do for my C++
framework:

1.  Write two file-scope functions.  These are the callbacks.  Install
them immediately after you call db_init().  
2.  In your C++ class, add a static std::map<DBPROCESS*, your_class*>.
3.  Give the callbacks access to that static map. 
4.  Write instance-level functions for the callbacks to call.  
5.  On connecting to the database, have your object add its 'this' pointer
(and the new DBPROCESS*) to the class's static map.  

When a message is received, the callback can look up the instance in the
static map and call the appropriate function, passing whatever it
received.  

> I found a PHP/Sybase implementation of message handling. 

We could do that (or you could).  It's just a matter of caching the last
message and providing a function to access the cache.  

Callbacks are good IMO.  Once you have the infrastructure in place to
handle one message, you've got it for all of them.  That's *really*
important in database programming because when things go wrong, they tend
to go very wrong.  Relying on the application to verify every call and
check for errors every time is a recipe for missing the crucial first
message.  After which the second won't make sense.  ;-)

I would ask the PHP guys how they handle timeout conditions.  db-lib lets
the application tell the library when to keep trying and when to give up. 
Via the error handler return code.  

HTH.  

--jkl




More information about the FreeTDS mailing list