📄 proc.cpp
字号:
// proc.cpp
// xp_base64encode extended stored procedure
//
// Usage: xp_base64encode @param1 [, @param2 OUTPUT]
//
// If only @param1 is specified, it is Base64-encoded and returned as a SQL result set.
// If @param2 OUTPUT is specified, the result of the encoding is returned in @param2.
//
// Michael Coles, MCDBA
// 7/2005
//
// Ver. 0.9.0.0: Updated 1/19/2006 to fix memory fragmentation problems that show up
// when run on very large datasets
//
#include "../common/stdafx/stdafx.h"
#include "../common/params/params.h"
#include "../common/base64/base64.h"
#define XP_NOERROR 0
#define XP_ERROR 1
#define MAXCOLNAME 25
#define MAXNAME 25
#define MAXTEXT 255
// Error messages
#define ERR_USAGE "Usage: EXEC xp_base64encode @param1 [, @param2 OUTPUT]"
#define ERR_OUTPUT "If @param2 is specified, it must be specified as OUTPUT"
#ifdef __cplusplus
extern "C" {
#endif
RETCODE __declspec(dllexport) xp_base64encode(SRV_PROC *srvproc);
#ifdef __cplusplus
}
#endif
void PrintError (SRV_PROC *srvproc, char *ErrorMsg);
RETCODE __declspec(dllexport) xp_base64encode(SRV_PROC *srvproc)
{
BYTE *result = NULL;
SRVRETCODE sqlerr = XP_NOERROR;
// First we define our two SQL Server Parameters
params *P1 = new params;
params *P2 = new params;
// Now we get the actual count of parameters passed in
int j = params::getparamcount(srvproc);
ULONG outlength = 0;
// Next we retrieve parameters 1 and 2. Don't worry if only one was passed in
// our P2->success flag will just be set to FAIL. No biggie.
params::getparam(srvproc, 1, P1);
params::getparam(srvproc, 2, P2);
// Make sure we have at least one good parameter
if (j > 2 || j < 1) { // Need at least one or two parameters
PrintError(srvproc, ERR_USAGE);
sqlerr = XP_ERROR;
}
else
{
// If first parameter is NULL, return NULL
BOOL isnull = P1->isnull;
// Now we calculate the Encode Buffer Size based on the length of the parameter
// passed in
outlength = CBase64Utils::CalculateEncodeBufferSize(P1->length);
// Then we define our result array of BYTEs and zero it out
result = new BYTE[outlength + (10 * sizeof(BYTE))];
memset(result, 0, outlength + (10 * sizeof(BYTE)));
// Next we encode it
CBase64Utils::Encode(P1->cdata, result, P1->length);
if (isnull) outlength = 0; // Set the output length to 0 (required to return a NULL)
// Now we decide what to do with the output. We can either: 1) return it in an
// output parameter, or 2) return it as a data column (if no 2nd parameter was
// given)
if (P2->success == FAIL) { // Only one parameter was passed in
DBCHAR colname[MAXCOLNAME + 10];
//Set up the column names
_snprintf(colname, MAXCOLNAME, "ENCODED");
srv_describe(srvproc, 1, colname, SRV_NULLTERM, SRVVARCHAR, outlength, SRVVARCHAR, 0, 0);
// Update column 1
srv_setcoldata(srvproc, 1, result);
srv_setcollen(srvproc, 1, static_cast<int>(outlength));
srv_sendrow(srvproc);
srv_senddone(srvproc, SRV_DONE_MORE | SRV_DONE_COUNT, (DBUSMALLINT)0, (DBINT)1);
} else {
if (P2->isoutput == FALSE) { // The second parameter is not OUTPUT
PrintError(srvproc, ERR_OUTPUT);
sqlerr = XP_ERROR;
} else { // Output to the OUTPUT parameter
srv_paramsetoutput(srvproc, 2, result, outlength, isnull);
srv_senddone(srvproc, SRV_DONE_MORE, (DBUSMALLINT)0, (DBINT)0);
}
}
}
// A little clean-up
if (result != NULL) {
memset(result, 0, outlength);
delete [] result;
result = NULL;
}
if (P1 != NULL) {
delete P1;
P1 = NULL;
}
if (P2 != NULL) {
delete P2;
P2 = NULL;
}
// Return the error code
return sqlerr;
}
// This routine sends an error message to the SQL Server Output Window
//
// Parameters:
// SRV_PROC *srvproc The server process pointer passed in by SQL Server
// char *ErrorMsg The error message string to print
//
void PrintError (SRV_PROC *srvproc, char *ErrorMsg)
{
// Print the error message on the SQL Server output screen
srv_sendmsg(srvproc, SRV_MSG_ERROR, XP_ERROR, SRV_INFO, 1,
NULL, 0, (DBUSMALLINT) __LINE__,
ErrorMsg,
SRV_NULLTERM);
// Alert SQL Server that an error has occurred
srv_senddone(srvproc, (SRV_DONE_ERROR | SRV_DONE_MORE), 0, 0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -