📄 rpc_ct_param.c
字号:
#if HAVE_CONFIG_H#include <config.h>#endif /* HAVE_CONFIG_H */#include <stdio.h>#if HAVE_STDLIB_H#include <stdlib.h>#endif /* HAVE_STRING_H */#if HAVE_STRING_H#include <string.h>#endif /* HAVE_STRING_H */#include <ctpublic.h>#include "common.h"#define MAX(X,Y) (((X) > (Y)) ? (X) : (Y))#define MIN(X,Y) (((X) < (Y)) ? (X) : (Y))static char software_version[] = "$Id: rpc_ct_param.c,v 1.8 2006/12/29 19:00:33 freddy77 Exp $";static void *no_unused_var_warn[] = { software_version, no_unused_var_warn };CS_RETCODE ex_clientmsg_cb(CS_CONTEXT * context, CS_CONNECTION * connection, CS_CLIENTMSG * errmsg);CS_RETCODE ex_servermsg_cb(CS_CONTEXT * context, CS_CONNECTION * connection, CS_SERVERMSG * errmsg);static CS_INT ex_display_dlen(CS_DATAFMT * column);static CS_RETCODE ex_display_header(CS_INT numcols, CS_DATAFMT columns[]);typedef struct _ex_column_data{ CS_SMALLINT indicator; CS_CHAR *value; CS_INT valuelen;}EX_COLUMN_DATA;/* Testing: array binding of result set */intmain(int argc, char *argv[]){ CS_CONTEXT *ctx; CS_CONNECTION *conn; CS_COMMAND *cmd; int verbose = 0; CS_RETCODE ret; CS_INT res_type; CS_INT num_cols; CS_CHAR cmdbuf[4096]; 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; CS_INT i; CS_INT j; CS_INT row_count = 0; CS_INT rows_read; CS_INT disp_len; EX_COLUMN_DATA *coldata; CS_DATAFMT *outdatafmt; CS_SMALLINT msg_id; fprintf(stdout, "%s: submit a stored procedure using ct_param \n", __FILE__); if (verbose) { fprintf(stdout, "Trying login\n"); } ret = try_ctlogin(&ctx, &conn, &cmd, verbose); if (ret != CS_SUCCEED) { fprintf(stderr, "Login failed\n"); return 1; } ct_callback(ctx, NULL, CS_SET, CS_CLIENTMSG_CB, (CS_VOID *) ex_clientmsg_cb); ct_callback(ctx, NULL, CS_SET, CS_SERVERMSG_CB, (CS_VOID *) ex_servermsg_cb); /* do not test error */ ret = run_command(cmd, "IF EXISTS(SELECT * FROM SYSOBJECTS WHERE name = 'sample_rpc' AND type = 'P') DROP PROCEDURE sample_rpc"); strcpy(cmdbuf, "create proc sample_rpc (@intparam int, \ @sintparam smallint output, @floatparam float output, \ @moneyparam money output, \ @dateparam datetime output, @charparam char(20) output, \ @binaryparam binary(20) output) \ as "); strcat(cmdbuf, "select @intparam, @sintparam, @floatparam, @moneyparam, \ @dateparam, @charparam, @binaryparam \ select @sintparam = @sintparam + @intparam \ select @floatparam = @floatparam + @intparam \ select @moneyparam = @moneyparam + convert(money, @intparam) \ select @dateparam = getdate() \ select @charparam = \'The char parameters\' \ select @binaryparam = @binaryparam \ print \'This is the message printed out by sample_rpc.\'"); ret = run_command(cmd, cmdbuf); if (ret != CS_SUCCEED) { fprintf(stderr, "create proc failed\n"); return 1; } /* * 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 ((ret = ct_cmd_props(cmd, CS_GET, CS_PARENT_HANDLE, &conn, CS_UNUSED, NULL)) != CS_SUCCEED) { fprintf(stderr, "ct_cmd_props() failed"); return 1; } if ((ret = ct_con_props(conn, CS_GET, CS_PARENT_HANDLE, &ctx, CS_UNUSED, NULL)) != CS_SUCCEED) { fprintf(stderr, "ct_con_props() failed"); return 1; } ret = cs_convert(ctx, &srcfmt, (CS_VOID *) moneystring, &destfmt, &moneyvar, &destlen); if (ret != CS_SUCCEED) { fprintf(stderr, "cs_convert() failed"); return 1; } /* * Send the RPC command for our stored procedure. */ if ((ret = ct_command(cmd, CS_RPC_CMD, rpc_name, CS_NULLTERM, CS_NO_RECOMPILE)) != CS_SUCCEED) { fprintf(stderr, "ct_command(CS_RPC_CMD) failed"); return 1; } /* * 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 ((ret = ct_param(cmd, &datafmt, (CS_VOID *) & intvar, CS_SIZEOF(CS_INT), 0)) != CS_SUCCEED) { fprintf(stderr, "ct_param(int) failed"); return 1; } strcpy(datafmt.name, "@sintparam"); datafmt.namelen = CS_NULLTERM; datafmt.datatype = CS_SMALLINT_TYPE; datafmt.maxlength = 255; datafmt.status = CS_RETURN; datafmt.locale = NULL; if ((ret = ct_param(cmd, &datafmt, (CS_VOID *) & smallintvar, CS_SIZEOF(CS_SMALLINT), 0)) != CS_SUCCEED) { fprintf(stderr, "ct_param(smallint) failed"); return 1; } strcpy(datafmt.name, "@floatparam"); datafmt.namelen = CS_NULLTERM; datafmt.datatype = CS_FLOAT_TYPE; datafmt.maxlength = 255; datafmt.status = CS_RETURN; datafmt.locale = NULL; if ((ret = ct_param(cmd, &datafmt, (CS_VOID *) & floatvar, CS_SIZEOF(CS_FLOAT), 0)) != CS_SUCCEED) { fprintf(stderr, "ct_param(float) failed"); return 1; } strcpy(datafmt.name, "@moneyparam"); datafmt.namelen = CS_NULLTERM; datafmt.datatype = CS_MONEY_TYPE; datafmt.maxlength = 255; datafmt.status = CS_RETURN; datafmt.locale = NULL; if ((ret = ct_param(cmd, &datafmt, (CS_VOID *) & moneyvar, CS_SIZEOF(CS_MONEY), 0)) != CS_SUCCEED) { fprintf(stderr, "ct_param(money) failed"); return 1; } strcpy(datafmt.name, "@dateparam"); datafmt.namelen = CS_NULLTERM; datafmt.datatype = CS_DATETIME4_TYPE; datafmt.maxlength = 255; 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 ((ret = ct_param(cmd, &datafmt, NULL, 0, -1)) != CS_SUCCEED) { fprintf(stderr, "ct_param(datetime4) failed"); return 1; } strcpy(datafmt.name, "@charparam"); datafmt.namelen = CS_NULLTERM; datafmt.datatype = CS_CHAR_TYPE; datafmt.maxlength = 60; 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 ((ret = ct_param(cmd, &datafmt, NULL, 0, -1)) != CS_SUCCEED) { fprintf(stderr, "ct_param(char) failed"); return 1; } strcpy(datafmt.name, "@binaryparam"); datafmt.namelen = CS_NULLTERM; datafmt.datatype = CS_BINARY_TYPE; datafmt.maxlength = 255; datafmt.status = CS_RETURN; datafmt.locale = NULL; if ((ret = ct_param(cmd, &datafmt, (CS_VOID *) & binaryvar, CS_SIZEOF(CS_BINARY), 0)) != CS_SUCCEED) { fprintf(stderr, "ct_param(binary) failed"); return 1; } /* * Send the command to the server */ if (ct_send(cmd) != CS_SUCCEED) { fprintf(stderr, "ct_send(RPC) failed"); return 1; } /* * Process the results of the RPC. */ while ((ret = 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 */ /* * Find out how many columns there are in this result set. */ ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_res_info(CS_NUMDATA) failed"); return 1; } /* * Make sure we have at least one column */ if (num_cols <= 0) { fprintf(stderr, "ct_res_info(CS_NUMDATA) returned zero columns"); return 1; } /* * Our program variable, called 'coldata', is an array of * EX_COLUMN_DATA structures. Each array element represents * one column. Each array element will re-used for each row. * * First, allocate memory for the data element to process. */ coldata = (EX_COLUMN_DATA *) malloc(num_cols * sizeof(EX_COLUMN_DATA)); if (coldata == NULL) { fprintf(stderr, "malloc coldata failed \n"); return 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -