 |
|
SQL Server Tips by Burleson |
Implement encapsulation by using C++ classes
To provide a C++ encapsulation of XP’s, the key class provided by
the framework is CExtendedStoredProcedure. A specified instance of
the class is created in the exporting function using the
IMPLEMENT_XP macro. Then the virtual function
CExtendedStoredProcedure::main is called with the SRV_PROC pointer
as passed to the exported function along with the name of the
exported function itself. IMPLEMENT_XP is implemented as:
#define IMPLEMENT_XP(xpName,
class) \
extern "C" RETCODE __declspec(dllexport) xpName(SRV_PROC* srvproc) \
{ \
class _xp; \
return _xp.main(srvproc, #xpName); \
}
These values are then stored as member variables in the
CExtendedStoredProcedure instance, so that they can be accessed from
other methods of the class. To customize the functionality of the
XP, various virtual functions are provided so that you can modify
the standard behaviour by overriding the appropriate function in a
derived class. The two key functions which a derived class should
override are BOOL
CExtendedStoredProcedure::Run()
and void
CExtendedStoredProcedure::DisplayUsage(). ‘Run’ is the
method where you should implement the code specific to your XP.
Returning TRUE from this function indicates that the XP ran
successfully, while FALSE indicates that an error occurred. You
should override ‘DisplayUsage’ to display usage information about
what parameters your XP takes. The framework calls this function
automatically when the XP is called with the incorrect number of
parameters.
The above book excerpt is from:
Super SQL
Server Systems
Turbocharge Database Performance with C++ External Procedures
ISBN:
0-9761573-2-2
Joseph Gama, P. J. Naughter
http://www.rampant-books.com/book_2005_2_sql_server_external_procedures.htm |