LCOV - code coverage report
Current view: top level - src/ctlib/unittests - rpc_ct_param.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 214 310 69.0 %
Date: 2024-04-18 21:21:48 Functions: 4 5 80.0 %

          Line data    Source code
       1             : #include <config.h>
       2             : 
       3             : #include <stdio.h>
       4             : 
       5             : #if HAVE_STDLIB_H
       6             : #include <stdlib.h>
       7             : #endif /* HAVE_STRING_H */
       8             : 
       9             : #if HAVE_STRING_H
      10             : #include <string.h>
      11             : #endif /* HAVE_STRING_H */
      12             : 
      13             : #include <ctpublic.h>
      14             : #include "common.h"
      15             : 
      16             : #define MAX(X,Y)      (((X) > (Y)) ? (X) : (Y))
      17             : #define MIN(X,Y)      (((X) < (Y)) ? (X) : (Y))
      18             : 
      19             : CS_RETCODE ex_clientmsg_cb(CS_CONTEXT * context, CS_CONNECTION * connection, CS_CLIENTMSG * errmsg);
      20             : CS_RETCODE ex_servermsg_cb(CS_CONTEXT * context, CS_CONNECTION * connection, CS_SERVERMSG * errmsg);
      21             : static CS_INT ex_display_dlen(CS_DATAFMT * column);
      22             : static CS_RETCODE ex_display_header(CS_INT numcols, CS_DATAFMT columns[]);
      23             : 
      24             : typedef struct _ex_column_data
      25             : {
      26             :         CS_SMALLINT indicator;
      27             :         CS_CHAR *value;
      28             :         CS_INT valuelen;
      29             : }
      30             : EX_COLUMN_DATA;
      31             : 
      32             : /* Testing: array binding of result set */
      33             : int
      34           8 : main(int argc, char *argv[])
      35             : {
      36             :         CS_CONTEXT *ctx;
      37             :         CS_CONNECTION *conn;
      38             :         CS_COMMAND *cmd;
      39           8 :         int verbose = 0;
      40             : 
      41             :         CS_RETCODE ret;
      42             :         CS_INT res_type;
      43             :         CS_INT num_cols;
      44             : 
      45             :         CS_CHAR cmdbuf[4096];
      46             : 
      47             :         CS_DATAFMT datafmt;
      48             :         CS_DATAFMT srcfmt;
      49             :         CS_DATAFMT destfmt;
      50             :         CS_INT intvar;
      51             :         CS_SMALLINT smallintvar;
      52             :         CS_FLOAT floatvar;
      53             :         CS_MONEY moneyvar;
      54             :         CS_BINARY binaryvar;
      55           8 :         CS_BIT bitvar = 1;
      56             :         char moneystring[10];
      57             :         char rpc_name[15];
      58             :         CS_INT destlen;
      59             :         CS_INT i;
      60             :         CS_INT j;
      61           8 :         CS_INT row_count = 0;
      62             :         CS_INT rows_read;
      63             :         CS_INT disp_len;
      64             :         EX_COLUMN_DATA *coldata;
      65             :         CS_DATAFMT *outdatafmt;
      66             :         CS_SMALLINT msg_id;
      67             : 
      68             : 
      69             : 
      70           8 :         printf("%s: submit a stored procedure using ct_param \n", __FILE__);
      71             :         if (verbose) {
      72             :                 printf("Trying login\n");
      73             :         }
      74           8 :         ret = try_ctlogin(&ctx, &conn, &cmd, verbose);
      75           8 :         if (ret != CS_SUCCEED) {
      76           0 :                 fprintf(stderr, "Login failed\n");
      77           0 :                 return 1;
      78             :         }
      79             : 
      80           8 :         ct_callback(ctx, NULL, CS_SET, CS_CLIENTMSG_CB, (CS_VOID *) ex_clientmsg_cb);
      81             : 
      82           8 :         ct_callback(ctx, NULL, CS_SET, CS_SERVERMSG_CB, (CS_VOID *) ex_servermsg_cb);
      83             : 
      84             :         /* do not test error */
      85           8 :         ret = run_command(cmd, "IF OBJECT_ID('sample_rpc') IS NOT NULL DROP PROCEDURE sample_rpc");
      86             : 
      87           8 :         strcpy(cmdbuf, "create proc sample_rpc (@intparam int, \
      88             :         @sintparam smallint output, @floatparam float output, \
      89             :         @moneyparam money output,  \
      90             :         @dateparam datetime output, @charparam char(20) output, @empty varchar(20) output, \
      91             :         @binaryparam    binary(20) output, @bitparam bit) \
      92             :         as ");
      93             : 
      94           8 :         strcat(cmdbuf, "select @intparam, @sintparam, @floatparam, @moneyparam, \
      95             :         @dateparam, @charparam, @binaryparam \
      96             :         select @sintparam = @sintparam + @intparam \
      97             :         select @floatparam = @floatparam + @intparam \
      98             :         select @moneyparam = @moneyparam + convert(money, @intparam) \
      99             :         select @dateparam = getdate() \
     100             :         select @charparam = \'The char parameters\' \
     101             :         select @empty = \'\' \
     102             :         select @binaryparam = @binaryparam \
     103             :         print \'This is the message printed out by sample_rpc.\'");
     104             : 
     105           8 :         ret = run_command(cmd, cmdbuf);
     106             : 
     107           8 :         if (ret != CS_SUCCEED) {
     108           0 :                 fprintf(stderr, "create proc failed\n");
     109           0 :                 return 1;
     110             :         }
     111             : 
     112             :         /*
     113             :          * Assign values to the variables used for parameter passing.
     114             :          */
     115             : 
     116           8 :         intvar = 2;
     117           8 :         smallintvar = 234;
     118           8 :         floatvar = 0.12;
     119           8 :         binaryvar = (CS_BINARY) 0xff;
     120           8 :         strcpy(rpc_name, "sample_rpc");
     121           8 :         strcpy(moneystring, "300.90");
     122             : 
     123             :         /*
     124             :          * Clear and setup the CS_DATAFMT structures used to convert datatypes.
     125             :          */
     126             : 
     127           8 :         memset(&srcfmt, 0, sizeof(CS_DATAFMT));
     128             :         srcfmt.datatype = CS_CHAR_TYPE;
     129           8 :         srcfmt.maxlength = strlen(moneystring);
     130           8 :         srcfmt.precision = 5;
     131           8 :         srcfmt.scale = 2;
     132             :         srcfmt.locale = NULL;
     133             : 
     134           8 :         memset(&destfmt, 0, sizeof(CS_DATAFMT));
     135           8 :         destfmt.datatype = CS_MONEY_TYPE;
     136           8 :         destfmt.maxlength = sizeof(CS_MONEY);
     137           8 :         destfmt.precision = 5;
     138           8 :         destfmt.scale = 2;
     139             :         destfmt.locale = NULL;
     140             : 
     141             :         /*
     142             :          * Convert the string representing the money value
     143             :          * to a CS_MONEY variable. Since this routine does not have the
     144             :          * context handle, we use the property functions to get it.
     145             :          */
     146           8 :         if ((ret = ct_cmd_props(cmd, CS_GET, CS_PARENT_HANDLE, &conn, CS_UNUSED, NULL)) != CS_SUCCEED) {
     147           0 :                 fprintf(stderr, "ct_cmd_props() failed");
     148           0 :                 return 1;
     149             :         }
     150           8 :         if ((ret = ct_con_props(conn, CS_GET, CS_PARENT_HANDLE, &ctx, CS_UNUSED, NULL)) != CS_SUCCEED) {
     151           0 :                 fprintf(stderr, "ct_con_props() failed");
     152           0 :                 return 1;
     153             :         }
     154           8 :         ret = cs_convert(ctx, &srcfmt, (CS_VOID *) moneystring, &destfmt, &moneyvar, &destlen);
     155           8 :         if (ret != CS_SUCCEED) {
     156           0 :                 fprintf(stderr, "cs_convert() failed");
     157           0 :                 return 1;
     158             :         }
     159             : 
     160             :         /*
     161             :          * Send the RPC command for our stored procedure.
     162             :          */
     163           8 :         if ((ret = ct_command(cmd, CS_RPC_CMD, rpc_name, CS_NULLTERM, CS_NO_RECOMPILE)) != CS_SUCCEED) {
     164           0 :                 fprintf(stderr, "ct_command(CS_RPC_CMD) failed");
     165           0 :                 return 1;
     166             :         }
     167             : 
     168             :         /*
     169             :          * Clear and setup the CS_DATAFMT structure, then pass
     170             :          * each of the parameters for the RPC.
     171             :          */
     172           8 :         memset(&datafmt, 0, sizeof(datafmt));
     173           8 :         strcpy(datafmt.name, "@intparam");
     174           8 :         datafmt.namelen = CS_NULLTERM;
     175           8 :         datafmt.datatype = CS_INT_TYPE;
     176           8 :         datafmt.maxlength = CS_UNUSED;
     177           8 :         datafmt.status = CS_INPUTVALUE;
     178             :         datafmt.locale = NULL;
     179             : 
     180           8 :         if ((ret = ct_param(cmd, &datafmt, (CS_VOID *) & intvar, CS_SIZEOF(CS_INT), 0)) != CS_SUCCEED) {
     181           0 :                 fprintf(stderr, "ct_param(int) failed");
     182           0 :                 return 1;
     183             :         }
     184             : 
     185           8 :         strcpy(datafmt.name, "@sintparam");
     186           8 :         datafmt.namelen = CS_NULLTERM;
     187           8 :         datafmt.datatype = CS_SMALLINT_TYPE;
     188           8 :         datafmt.maxlength = 255;
     189           8 :         datafmt.status = CS_RETURN;
     190           8 :         datafmt.locale = NULL;
     191             : 
     192           8 :         if ((ret = ct_param(cmd, &datafmt, (CS_VOID *) & smallintvar, CS_SIZEOF(CS_SMALLINT), 0)) != CS_SUCCEED) {
     193           0 :                 fprintf(stderr, "ct_param(smallint) failed");
     194           0 :                 return 1;
     195             :         }
     196             : 
     197           8 :         strcpy(datafmt.name, "@floatparam");
     198           8 :         datafmt.namelen = CS_NULLTERM;
     199           8 :         datafmt.datatype = CS_FLOAT_TYPE;
     200           8 :         datafmt.maxlength = 255;
     201           8 :         datafmt.status = CS_RETURN;
     202           8 :         datafmt.locale = NULL;
     203             : 
     204           8 :         if ((ret = ct_param(cmd, &datafmt, (CS_VOID *) & floatvar, CS_SIZEOF(CS_FLOAT), 0)) != CS_SUCCEED) {
     205           0 :                 fprintf(stderr, "ct_param(float) failed");
     206           0 :                 return 1;
     207             :         }
     208             : 
     209             : 
     210           8 :         strcpy(datafmt.name, "@moneyparam");
     211           8 :         datafmt.namelen = CS_NULLTERM;
     212           8 :         datafmt.datatype = CS_MONEY_TYPE;
     213           8 :         datafmt.maxlength = 255;
     214           8 :         datafmt.status = CS_RETURN;
     215           8 :         datafmt.locale = NULL;
     216             : 
     217           8 :         if ((ret = ct_param(cmd, &datafmt, (CS_VOID *) & moneyvar, CS_SIZEOF(CS_MONEY), 0)) != CS_SUCCEED) {
     218           0 :                 fprintf(stderr, "ct_param(money) failed");
     219           0 :                 return 1;
     220             :         }
     221             : 
     222           8 :         strcpy(datafmt.name, "@dateparam");
     223           8 :         datafmt.namelen = CS_NULLTERM;
     224           8 :         datafmt.datatype = CS_DATETIME4_TYPE;
     225           8 :         datafmt.maxlength = 255;
     226           8 :         datafmt.status = CS_RETURN;
     227           8 :         datafmt.locale = NULL;
     228             : 
     229             :         /*
     230             :          * The datetime variable is filled in by the RPC so pass NULL for
     231             :          * the data, 0 for data length, and -1 for the indicator arguments.
     232             :          */
     233           8 :         if ((ret = ct_param(cmd, &datafmt, NULL, 0, -1)) != CS_SUCCEED) {
     234           0 :                 fprintf(stderr, "ct_param(datetime4) failed");
     235           0 :                 return 1;
     236             :         }
     237           8 :         strcpy(datafmt.name, "@charparam");
     238           8 :         datafmt.namelen = CS_NULLTERM;
     239           8 :         datafmt.datatype = CS_CHAR_TYPE;
     240           8 :         datafmt.maxlength = 60;
     241           8 :         datafmt.status = CS_RETURN;
     242           8 :         datafmt.locale = NULL;
     243             : 
     244             :         /*
     245             :          * The character string variable is filled in by the RPC so pass NULL
     246             :          * for the data 0 for data length, and -1 for the indicator arguments.
     247             :          */
     248           8 :         if ((ret = ct_param(cmd, &datafmt, NULL, 0, -1)) != CS_SUCCEED) {
     249           0 :                 fprintf(stderr, "ct_param(char) failed");
     250           0 :                 return 1;
     251             :         }
     252             : 
     253           8 :         strcpy(datafmt.name, "@empty");
     254           8 :         datafmt.namelen = CS_NULLTERM;
     255           8 :         datafmt.datatype = CS_VARCHAR_TYPE;
     256           8 :         datafmt.maxlength = 60;
     257           8 :         datafmt.status = CS_RETURN;
     258           8 :         datafmt.locale = NULL;
     259             : 
     260             :         /*
     261             :          * The character string variable is filled in by the RPC so pass NULL
     262             :          * for the data 0 for data length, and -1 for the indicator arguments.
     263             :          */
     264           8 :         if ((ret = ct_param(cmd, &datafmt, NULL, 0, -1)) != CS_SUCCEED) {
     265           0 :                 fprintf(stderr, "ct_param(char) failed");
     266           0 :                 return 1;
     267             :         }
     268             : 
     269           8 :         strcpy(datafmt.name, "@binaryparam");
     270           8 :         datafmt.namelen = CS_NULLTERM;
     271           8 :         datafmt.datatype = CS_BINARY_TYPE;
     272           8 :         datafmt.maxlength = 255;
     273           8 :         datafmt.status = CS_RETURN;
     274           8 :         datafmt.locale = NULL;
     275             : 
     276           8 :         if ((ret = ct_param(cmd, &datafmt, (CS_VOID *) & binaryvar, CS_SIZEOF(CS_BINARY), 0)) != CS_SUCCEED) {
     277           0 :                 fprintf(stderr, "ct_param(binary) failed");
     278           0 :                 return 1;
     279             :         }
     280             : 
     281           8 :         strcpy(datafmt.name, "@bitparam");
     282           8 :         datafmt.namelen = CS_NULLTERM;
     283           8 :         datafmt.datatype = CS_BIT_TYPE;
     284           8 :         datafmt.maxlength = 1;
     285           8 :         datafmt.status = 0;
     286           8 :         datafmt.locale = NULL;
     287             : 
     288           8 :         if ((ret = ct_param(cmd, &datafmt, (CS_VOID *) & bitvar, CS_SIZEOF(CS_BIT), 0)) != CS_SUCCEED) {
     289           0 :                 fprintf(stderr, "ct_param(binary) failed");
     290           0 :                 return 1;
     291             :         }
     292             : 
     293             :         /*
     294             :          * Send the command to the server
     295             :          */
     296           8 :         if (ct_send(cmd) != CS_SUCCEED) {
     297           0 :                 fprintf(stderr, "ct_send(RPC) failed");
     298           0 :                 return 1;
     299             :         }
     300             : 
     301             :         /*
     302             :          * Process the results of the RPC.
     303             :          */
     304          56 :         while ((ret = ct_results(cmd, &res_type)) == CS_SUCCEED) {
     305          48 :                 switch ((int) res_type) {
     306          24 :                 case CS_ROW_RESULT:
     307             :                 case CS_PARAM_RESULT:
     308             :                 case CS_STATUS_RESULT:
     309             :                         /*
     310             :                          * Print the result header based on the result type.
     311             :                          */
     312          24 :                         switch ((int) res_type) {
     313           8 :                         case CS_ROW_RESULT:
     314           8 :                                 printf("\nROW RESULTS\n");
     315           8 :                                 break;
     316             : 
     317           8 :                         case CS_PARAM_RESULT:
     318           8 :                                 printf("\nPARAMETER RESULTS\n");
     319           8 :                                 break;
     320             : 
     321           8 :                         case CS_STATUS_RESULT:
     322           8 :                                 printf("\nSTATUS RESULTS\n");
     323           8 :                                 break;
     324             :                         }
     325          24 :                         fflush(stdout);
     326             : 
     327             :                         /*
     328             :                          * All three of these result types are fetchable.
     329             :                          * Since the result model for rpcs and rows have
     330             :                          * been unified in the New Client-Library, we
     331             :                          * will use the same routine to display them
     332             :                          */
     333             : 
     334             :                         /*
     335             :                          * Find out how many columns there are in this result set.
     336             :                          */
     337          24 :                         ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);
     338          24 :                         if (ret != CS_SUCCEED) {
     339           0 :                                 fprintf(stderr, "ct_res_info(CS_NUMDATA) failed");
     340           0 :                                 return 1;
     341             :                         }
     342             : 
     343             :                         /*
     344             :                          * Make sure we have at least one column
     345             :                          */
     346          24 :                         if (num_cols <= 0) {
     347           0 :                                 fprintf(stderr, "ct_res_info(CS_NUMDATA) returned zero columns");
     348           0 :                                 return 1;
     349             :                         }
     350             : 
     351             :                         /*
     352             :                          * Our program variable, called 'coldata', is an array of
     353             :                          * EX_COLUMN_DATA structures. Each array element represents
     354             :                          * one column.  Each array element will re-used for each row.
     355             :                          * 
     356             :                          * First, allocate memory for the data element to process.
     357             :                          */
     358          24 :                         coldata = (EX_COLUMN_DATA *) malloc(num_cols * sizeof(EX_COLUMN_DATA));
     359          24 :                         if (coldata == NULL) {
     360           0 :                                 fprintf(stderr, "malloc coldata failed \n");
     361           0 :                                 return 1;
     362             :                         }
     363             : 
     364          24 :                         outdatafmt = (CS_DATAFMT *) malloc(num_cols * sizeof(CS_DATAFMT));
     365          24 :                         if (outdatafmt == NULL) {
     366           0 :                                 fprintf(stderr, "malloc outdatafmt failed \n");
     367           0 :                                 return 1;
     368             :                         }
     369             : 
     370         144 :                         for (i = 0; i < num_cols; i++) {
     371         120 :                                 ret = ct_describe(cmd, (i + 1), &outdatafmt[i]);
     372         120 :                                 if (ret != CS_SUCCEED) {
     373           0 :                                         fprintf(stderr, "ct_describe failed \n");
     374           0 :                                         return 1;
     375             :                                 }
     376             : 
     377         120 :                                 outdatafmt[i].maxlength = ex_display_dlen(&outdatafmt[i]) + 1;
     378         120 :                                 outdatafmt[i].datatype = CS_CHAR_TYPE;
     379         120 :                                 outdatafmt[i].format = CS_FMT_NULLTERM;
     380             : 
     381         120 :                                 coldata[i].value = (CS_CHAR *) malloc(outdatafmt[i].maxlength);
     382         120 :                                 if (coldata[i].value == NULL) {
     383           0 :                                         fprintf(stderr, "malloc coldata.value failed \n");
     384           0 :                                         return 1;
     385             :                                 }
     386         120 :                                 coldata[i].value[0] = 0;
     387             : 
     388         120 :                                 ret = ct_bind(cmd, (i + 1), &outdatafmt[i], coldata[i].value, &coldata[i].valuelen,
     389             :                                               & coldata[i].indicator);
     390         120 :                                 if (ret != CS_SUCCEED) {
     391           0 :                                         fprintf(stderr, "ct_bind failed \n");
     392           0 :                                         return 1;
     393             :                                 }
     394             :                         }
     395          24 :                         if (ret != CS_SUCCEED) {
     396           0 :                                 for (j = 0; j < i; j++) {
     397           0 :                                         free(coldata[j].value);
     398             :                                 }
     399           0 :                                 free(coldata);
     400           0 :                                 free(outdatafmt);
     401           0 :                                 return 1;
     402             :                         }
     403             : 
     404          24 :                         ex_display_header(num_cols, outdatafmt);
     405             : 
     406          72 :                         while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED,
     407          24 :                                                 &rows_read)) == CS_SUCCEED) || (ret == CS_ROW_FAIL)) {
     408             :                                 /*
     409             :                                  * Increment our row count by the number of rows just fetched.
     410             :                                  */
     411          24 :                                 row_count = row_count + rows_read;
     412             : 
     413             :                                 /*
     414             :                                  * Check if we hit a recoverable error.
     415             :                                  */
     416          24 :                                 if (ret == CS_ROW_FAIL) {
     417           0 :                                         printf("Error on row %d.\n", row_count);
     418           0 :                                         fflush(stdout);
     419             :                                 }
     420             : 
     421             :                                 /*
     422             :                                  * We have a row.  Loop through the columns displaying the
     423             :                                  * column values.
     424             :                                  */
     425         120 :                                 for (i = 0; i < num_cols; i++) {
     426             :                                         /*
     427             :                                          * Display the column value
     428             :                                          */
     429         120 :                                         printf("%s", coldata[i].value);
     430         120 :                                         fflush(stdout);
     431             : 
     432             :                                         /*
     433             :                                          * If not last column, Print out spaces between this
     434             :                                          * column and next one.
     435             :                                          */
     436         120 :                                         if (i != num_cols - 1) {
     437          96 :                                                 disp_len = ex_display_dlen(&outdatafmt[i]);
     438          96 :                                                 disp_len -= coldata[i].valuelen - 1;
     439        1822 :                                                 for (j = 0; j < disp_len; j++) {
     440        1726 :                                                         fputc(' ', stdout);
     441             :                                                 }
     442             :                                         }
     443             :                                 }
     444          24 :                                 printf("\n");
     445          24 :                                 fflush(stdout);
     446             :                         }
     447             : 
     448             :                         /*
     449             :                          * Free allocated space.
     450             :                          */
     451         120 :                         for (i = 0; i < num_cols; i++) {
     452         120 :                                 free(coldata[i].value);
     453             :                         }
     454          24 :                         free(coldata);
     455          24 :                         free(outdatafmt);
     456             : 
     457             :                         /*
     458             :                          * We're done processing rows.  Let's check the final return
     459             :                          * value of ct_fetch().
     460             :                          */
     461          24 :                         switch ((int) ret) {
     462          24 :                         case CS_END_DATA:
     463             :                                 /*
     464             :                                  * Everything went fine.
     465             :                                  */
     466          24 :                                 printf("All done processing rows.\n");
     467          24 :                                 fflush(stdout);
     468             :                                 break;
     469             : 
     470           0 :                         case CS_FAIL:
     471             :                                 /*
     472             :                                  * Something terrible happened.
     473             :                                  */
     474           0 :                                 fprintf(stderr, "ct_fetch returned CS_FAIL\n");
     475           0 :                                 return 1;
     476             :                                 break;
     477             : 
     478           0 :                         default:
     479             :                                 /*
     480             :                                  * We got an unexpected return value.
     481             :                                  */
     482           0 :                                 fprintf(stderr, "ct_fetch returned %d\n", ret);
     483           0 :                                 return 1;
     484             :                                 break;
     485             : 
     486             :                         }
     487          24 :                         break;
     488             : 
     489           0 :                 case CS_MSG_RESULT:
     490           0 :                         ret = ct_res_info(cmd, CS_MSGTYPE, (CS_VOID *) & msg_id, CS_UNUSED, NULL);
     491           0 :                         if (ret != CS_SUCCEED) {
     492           0 :                                 fprintf(stderr, "ct_res_info(msg_id) failed");
     493           0 :                                 return 1;
     494             :                         }
     495           0 :                         printf("ct_result returned CS_MSG_RESULT where msg id = %d.\n", msg_id);
     496           0 :                         fflush(stdout);
     497           0 :                         break;
     498             : 
     499             :                 case CS_CMD_SUCCEED:
     500             :                         /*
     501             :                          * This means no rows were returned.
     502             :                          */
     503             :                         break;
     504             : 
     505             :                 case CS_CMD_DONE:
     506             :                         /*
     507             :                          * Done with result set.
     508             :                          */
     509             :                         break;
     510             : 
     511           0 :                 case CS_CMD_FAIL:
     512             :                         /*
     513             :                          * The server encountered an error while
     514             :                          * processing our command.
     515             :                          */
     516           0 :                         fprintf(stderr, "ct_results returned CS_CMD_FAIL.");
     517           0 :                         break;
     518             : 
     519           0 :                 default:
     520             :                         /*
     521             :                          * We got something unexpected.
     522             :                          */
     523           0 :                         fprintf(stderr, "ct_results returned unexpected result type.");
     524           0 :                         return CS_FAIL;
     525             :                 }
     526             :         }
     527             : 
     528             :         /*
     529             :          * We're done processing results. Let's check the
     530             :          * return value of ct_results() to see if everything
     531             :          * went ok.
     532             :          */
     533           8 :         switch ((int) ret) {
     534             :         case CS_END_RESULTS:
     535             :                 /*
     536             :                  * Everything went fine.
     537             :                  */
     538             :                 break;
     539             : 
     540           0 :         case CS_FAIL:
     541             :                 /*
     542             :                  * Something failed happened.
     543             :                  */
     544           0 :                 fprintf(stderr, "ct_results failed.");
     545           0 :                 break;
     546             : 
     547           0 :         default:
     548             :                 /*
     549             :                  * We got an unexpected return value.
     550             :                  */
     551           0 :                 fprintf(stderr, "ct_results returned unexpected result type.");
     552           0 :                 break;
     553             :         }
     554             : 
     555           8 :         run_command(cmd, "DROP PROCEDURE sample_rpc");
     556             : 
     557             :         if (verbose) {
     558             :                 printf("Trying logout\n");
     559             :         }
     560           8 :         ret = try_ctlogout(ctx, conn, cmd, verbose);
     561           8 :         if (ret != CS_SUCCEED) {
     562           0 :                 fprintf(stderr, "Logout failed\n");
     563           0 :                 return 1;
     564             :         }
     565             : 
     566             :         return 0;
     567             : }
     568             : 
     569             : static CS_INT
     570         456 : ex_display_dlen(CS_DATAFMT * column)
     571             : {
     572             : CS_INT len;
     573             : 
     574         456 :         switch ((int) column->datatype) {
     575         360 :         case CS_CHAR_TYPE:
     576             :         case CS_VARCHAR_TYPE:
     577             :         case CS_TEXT_TYPE:
     578             :         case CS_IMAGE_TYPE:
     579         360 :                 len = MIN(column->maxlength, 1024);
     580         360 :                 break;
     581             : 
     582          16 :         case CS_BINARY_TYPE:
     583             :         case CS_VARBINARY_TYPE:
     584          16 :                 len = MIN((2 * column->maxlength) + 2, 1024);
     585          16 :                 break;
     586             : 
     587             :         case CS_BIT_TYPE:
     588             :         case CS_TINYINT_TYPE:
     589             :                 len = 3;
     590             :                 break;
     591             : 
     592          16 :         case CS_SMALLINT_TYPE:
     593          16 :                 len = 6;
     594          16 :                 break;
     595             : 
     596          16 :         case CS_INT_TYPE:
     597          16 :                 len = 11;
     598          16 :                 break;
     599             : 
     600          16 :         case CS_REAL_TYPE:
     601             :         case CS_FLOAT_TYPE:
     602          16 :                 len = 20;
     603          16 :                 break;
     604             : 
     605          16 :         case CS_MONEY_TYPE:
     606             :         case CS_MONEY4_TYPE:
     607          16 :                 len = 24;
     608          16 :                 break;
     609             : 
     610          16 :         case CS_DATETIME_TYPE:
     611             :         case CS_DATETIME4_TYPE:
     612          16 :                 len = 30;
     613          16 :                 break;
     614             : 
     615           0 :         case CS_NUMERIC_TYPE:
     616             :         case CS_DECIMAL_TYPE:
     617           0 :                 len = (CS_MAX_PREC + 2);
     618           0 :                 break;
     619             : 
     620           0 :         default:
     621           0 :                 len = 12;
     622           0 :                 break;
     623             :         }
     624             : 
     625         456 :         return MAX((CS_INT) (strlen(column->name) + 1), len);
     626             : }
     627             : 
     628             : static CS_RETCODE
     629          24 : ex_display_header(CS_INT numcols, CS_DATAFMT columns[])
     630             : {
     631             : CS_INT i;
     632             : CS_INT l;
     633             : CS_INT j;
     634             : CS_INT disp_len;
     635             : 
     636          24 :         fputc('\n', stdout);
     637         144 :         for (i = 0; i < numcols; i++) {
     638         120 :                 disp_len = ex_display_dlen(&columns[i]);
     639         120 :                 printf("%s", columns[i].name);
     640         120 :                 fflush(stdout);
     641         120 :                 l = disp_len - strlen(columns[i].name);
     642        6728 :                 for (j = 0; j < l; j++) {
     643        6608 :                         fputc(' ', stdout);
     644        6608 :                         fflush(stdout);
     645             :                 }
     646             :         }
     647          24 :         fputc('\n', stdout);
     648          24 :         fflush(stdout);
     649         144 :         for (i = 0; i < numcols; i++) {
     650         120 :                 disp_len = ex_display_dlen(&columns[i]);
     651         120 :                 l = disp_len - 1;
     652        7168 :                 for (j = 0; j < l; j++) {
     653        7048 :                         fputc('-', stdout);
     654             :                 }
     655         120 :                 fputc(' ', stdout);
     656             :         }
     657          24 :         fputc('\n', stdout);
     658             : 
     659          24 :         return CS_SUCCEED;
     660             : }
     661             : 
     662             : CS_RETCODE
     663           0 : ex_clientmsg_cb(CS_CONTEXT * context, CS_CONNECTION * connection, CS_CLIENTMSG * errmsg)
     664             : {
     665           0 :         printf("\nOpen Client Message:\n");
     666           0 :         printf("Message number: LAYER = (%ld) ORIGIN = (%ld) ", (long)CS_LAYER(errmsg->msgnumber), (long)CS_ORIGIN(errmsg->msgnumber));
     667           0 :         printf("SEVERITY = (%ld) NUMBER = (%ld)\n", (long)CS_SEVERITY(errmsg->msgnumber), (long)CS_NUMBER(errmsg->msgnumber));
     668           0 :         printf("Message String: %s\n", errmsg->msgstring);
     669           0 :         if (errmsg->osstringlen > 0) {
     670           0 :                 printf("Operating System Error: %s\n", errmsg->osstring);
     671             :         }
     672           0 :         fflush(stdout);
     673             : 
     674           0 :         return CS_SUCCEED;
     675             : }
     676             : 
     677             : CS_RETCODE
     678           8 : ex_servermsg_cb(CS_CONTEXT * context, CS_CONNECTION * connection, CS_SERVERMSG * srvmsg)
     679             : {
     680           8 :         printf("\nServer message:\n");
     681           8 :         printf("Message number: %ld, Severity %ld, ", (long) srvmsg->msgnumber, (long) srvmsg->severity);
     682           8 :         printf("State %ld, Line %ld\n", (long) srvmsg->state, (long) srvmsg->line);
     683             : 
     684           8 :         if (srvmsg->svrnlen > 0) {
     685           8 :                 printf("Server '%s'\n", srvmsg->svrname);
     686             :         }
     687             : 
     688           8 :         if (srvmsg->proclen > 0) {
     689           8 :                 printf(" Procedure '%s'\n", srvmsg->proc);
     690             :         }
     691             : 
     692           8 :         printf("Message String: %s\n", srvmsg->text);
     693           8 :         fflush(stdout);
     694             : 
     695           8 :         return CS_SUCCEED;
     696             : }

Generated by: LCOV version 1.13