📄 rpc.c
字号:
{
CS_CONNECTION *connection;
CS_CONTEXT *context;
CS_RETCODE retcode;
CS_DATAFMT datafmt;
CS_DATAFMT srcfmt;
CS_DATAFMT destfmt;
CS_INT intvar;
CS_SMALLINT smallintvar;
CS_FLOAT floatvar;
CS_MONEY moneyvar;
CS_BINARY binaryvar;
char moneystring[10];
char rpc_name[15];
CS_INT destlen;
/*
** Assign values to the variables used for parameter passing.
*/
intvar = 2;
smallintvar = 234;
floatvar = 0.12;
binaryvar = (CS_BINARY)0xff;
strcpy(rpc_name, "sample_rpc");
strcpy(moneystring, "300.90");
/*
** Clear and setup the CS_DATAFMT structures used to convert datatypes.
*/
memset(&srcfmt, 0, sizeof (CS_DATAFMT));
srcfmt.datatype = CS_CHAR_TYPE;
srcfmt.maxlength = strlen(moneystring);
srcfmt.precision = 5;
srcfmt.scale = 2;
srcfmt.locale = NULL;
memset(&destfmt, 0, sizeof (CS_DATAFMT));
destfmt.datatype = CS_MONEY_TYPE;
destfmt.maxlength = sizeof(CS_MONEY);
destfmt.precision = 5;
destfmt.scale = 2;
destfmt.locale = NULL;
/*
** Convert the string representing the money value
** to a CS_MONEY variable. Since this routine does not have the
** context handle, we use the property functions to get it.
*/
if ((retcode = ct_cmd_props(cmd, CS_GET, CS_PARENT_HANDLE,
&connection, CS_UNUSED, NULL)) != CS_SUCCEED)
{
ex_error("BuildRpcCommand: ct_cmd_props() failed");
return retcode;
}
if ((retcode = ct_con_props(connection, CS_GET, CS_PARENT_HANDLE,
&context, CS_UNUSED, NULL)) != CS_SUCCEED)
{
ex_error("BuildRpcCommand: ct_con_props() failed");
return retcode;
}
retcode = cs_convert(context, &srcfmt, (CS_VOID *)moneystring,
&destfmt, &moneyvar, &destlen);
if (retcode != CS_SUCCEED)
{
ex_error("BuildRpcCommand: cs_convert() failed");
return retcode;
}
/*
** Send the RPC command for our stored procedure.
*/
if ((retcode = ct_command(cmd, CS_RPC_CMD, rpc_name, CS_NULLTERM,
CS_NO_RECOMPILE)) != CS_SUCCEED)
{
ex_error("BuildRpcCommand: ct_command() failed");
return retcode;
}
/*
** Clear and setup the CS_DATAFMT structure, then pass
** each of the parameters for the RPC.
*/
memset(&datafmt, 0, sizeof (datafmt));
strcpy(datafmt.name, "@intparam");
datafmt.namelen = CS_NULLTERM;
datafmt.datatype = CS_INT_TYPE;
datafmt.maxlength = CS_UNUSED;
datafmt.status = CS_INPUTVALUE;
datafmt.locale = NULL;
if ((retcode = ct_param(cmd, &datafmt, (CS_VOID *)&intvar,
CS_SIZEOF(CS_INT), CS_UNUSED)) != CS_SUCCEED)
{
ex_error("BuildRpcCommand: ct_param(int) failed");
return retcode;
}
strcpy(datafmt.name, "@sintparam");
datafmt.namelen = CS_NULLTERM;
datafmt.datatype = CS_SMALLINT_TYPE;
datafmt.maxlength = EX_MAXSTRINGLEN;
datafmt.status = CS_RETURN;
datafmt.locale = NULL;
if ((retcode = ct_param(cmd, &datafmt, (CS_VOID *)&smallintvar,
CS_SIZEOF(CS_SMALLINT), CS_UNUSED)) != CS_SUCCEED)
{
ex_error("BuildRpcCommand: ct_param(smallint) failed");
return retcode;
}
strcpy(datafmt.name, "@floatparam");
datafmt.namelen = CS_NULLTERM;
datafmt.datatype = CS_FLOAT_TYPE;
datafmt.maxlength = EX_MAXSTRINGLEN;
datafmt.status = CS_RETURN;
datafmt.locale = NULL;
if((retcode = ct_param(cmd, &datafmt, (CS_VOID *)&floatvar,
CS_SIZEOF(CS_FLOAT), CS_UNUSED)) != CS_SUCCEED)
{
ex_error("BuildRpcCommand: ct_param(float) failed");
return retcode;
}
strcpy(datafmt.name, "@moneyparam");
datafmt.namelen = CS_NULLTERM;
datafmt.datatype = CS_MONEY_TYPE;
datafmt.maxlength = EX_MAXSTRINGLEN;
datafmt.status = CS_RETURN;
datafmt.locale = NULL;
if((retcode = ct_param(cmd, &datafmt, (CS_VOID *)&moneyvar,
CS_SIZEOF(CS_MONEY), CS_UNUSED)) != CS_SUCCEED)
{
ex_error("BuildRpcCommand: ct_param(money) failed");
return retcode;
}
strcpy(datafmt.name, "@dateparam");
datafmt.namelen = CS_NULLTERM;
datafmt.datatype = CS_DATETIME4_TYPE;
datafmt.maxlength = EX_MAXSTRINGLEN;
datafmt.status = CS_RETURN;
datafmt.locale = NULL;
/*
** The datetime variable is filled in by the RPC so pass NULL for
** the data, 0 for data length, and -1 for the indicator arguments.
*/
if((retcode = ct_param(cmd, &datafmt, NULL, 0, -1)) != CS_SUCCEED)
{
ex_error("BuildRpcCommand: ct_param(datetime4) failed");
return retcode;
}
strcpy(datafmt.name, "@charparam");
datafmt.namelen = CS_NULLTERM;
datafmt.datatype = CS_CHAR_TYPE;
datafmt.maxlength = EX_MAXSTRINGLEN;
datafmt.status = CS_RETURN;
datafmt.locale = NULL;
/*
** The character string variable is filled in by the RPC so pass NULL
** for the data 0 for data length, and -1 for the indicator arguments.
*/
if((retcode = ct_param(cmd, &datafmt, NULL, 0, -1)) != CS_SUCCEED)
{
ex_error("BuildRpcCommand: ct_param(char) failed");
return retcode;
}
strcpy(datafmt.name, "@binaryparam");
datafmt.namelen = CS_NULLTERM;
datafmt.datatype = CS_BINARY_TYPE;
datafmt.maxlength = EX_MAXSTRINGLEN;
datafmt.status = CS_RETURN;
datafmt.locale = NULL;
if((retcode = ct_param(cmd, &datafmt, (CS_VOID *)&binaryvar,
CS_SIZEOF(CS_BINARY), CS_UNUSED)) != CS_SUCCEED)
{
ex_error("BuildRpcCommand: ct_param(binary) failed");
return retcode;
}
return retcode;
}
/*
** DoRpc()
**
** Type of function:
** rpc program internal api
**
** Purpose:
** This routine passes the parameters and runs
** the sample RPC on the server.
**
** Parameters:
** connection - Pointer to CS_CONNECTION structure.
**
** Return:
** CS_SUCCEED if rpc was executed.
** Otherwise a Client-Library failure code.
**
*/
CS_STATIC CS_RETCODE
DoRpc(connection)
CS_CONNECTION *connection;
{
CS_RETCODE retcode;
CS_COMMAND *cmd;
CS_SMALLINT msg_id;
CS_INT res_type; /* result type from ct_results */
/*
** Use the new database
*/
if ((retcode = ex_use_db(connection, Ex_dbname)) != CS_SUCCEED)
{
ex_error("DoRpc: ex_use_db() failed");
return retcode;
}
if ((retcode = ct_cmd_alloc(connection, &cmd)) != CS_SUCCEED)
{
ex_error("DoRpc: ct_cmd_alloc() failed");
return retcode;
}
if ((retcode = BuildRpcCommand(cmd)) != CS_SUCCEED)
{
ex_error("DoRpc: BuildRpcCommand() failed");
return retcode;
}
/*
** Send the command to the server
*/
if (ct_send(cmd) != CS_SUCCEED)
{
ex_error("DoCompute: ct_send() failed");
return retcode;
}
/*
** Process the results of the RPC.
*/
while ((retcode = ct_results(cmd, &res_type)) == CS_SUCCEED)
{
switch ((int)res_type)
{
case CS_ROW_RESULT:
case CS_PARAM_RESULT:
case CS_STATUS_RESULT:
/*
** Print the result header based on the result type.
*/
switch ((int)res_type)
{
case CS_ROW_RESULT:
fprintf(stdout, "\nROW RESULTS\n");
break;
case CS_PARAM_RESULT:
fprintf(stdout, "\nPARAMETER RESULTS\n");
break;
case CS_STATUS_RESULT:
fprintf(stdout, "\nSTATUS RESULTS\n");
break;
}
fflush(stdout);
/*
** All three of these result types are fetchable.
** Since the result model for rpcs and rows have
** been unified in the New Client-Library, we
** will use the same routine to display them
*/
retcode = ex_fetch_data(cmd);
if (retcode != CS_SUCCEED)
{
ex_error("DoRpc: ex_fetch_data() failed");
return retcode;
}
break;
case CS_MSG_RESULT:
/*
**
*/
retcode = ct_res_info(cmd, CS_MSGTYPE,
(CS_VOID *)&msg_id, CS_UNUSED, NULL);
if (retcode != CS_SUCCEED)
{
ex_error("DoRpc: ct_res_info(msgtype) failed");
return retcode;
}
fprintf(stdout, "ct_result returned CS_MSG_RESULT where msg id = %d.\n", msg_id);
fflush(stdout);
break;
case CS_CMD_SUCCEED:
/*
** This means no rows were returned.
*/
break;
case CS_CMD_DONE:
/*
** Done with result set.
*/
break;
case CS_CMD_FAIL:
/*
** The server encountered an error while
** processing our command.
*/
ex_error("DoRpc: ct_results returned CS_CMD_FAIL.");
break;
default:
/*
** We got something unexpected.
*/
ex_error("DoRpc: ct_results returned unexpected result type");
return CS_FAIL;
}
}
/*
** We're done processing results. Let's check the
** return value of ct_results() to see if everything
** went ok.
*/
switch ((int)retcode)
{
case CS_END_RESULTS:
/*
** Everything went fine.
*/
retcode = CS_SUCCEED;
break;
case CS_FAIL:
/*
** Something failed happened.
*/
ex_error("DoRpc: ct_results() failed");
break;
default:
/*
** We got an unexpected return value.
*/
ex_error("DoRpc: ct_results returned unexpected result type");
break;
}
return retcode;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -