LCOV - code coverage report
Current view: top level - src/ctlib/unittests - t0002.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 74 132 56.1 %
Date: 2025-07-02 09:40:27 Functions: 2 2 100.0 %

          Line data    Source code
       1             : #include "common.h"
       2             : 
       3             : static int sp_who(CS_COMMAND *cmd);
       4             : 
       5             : /*
       6             :  * ct_send SQL |select name = @@servername|
       7             :  * ct_bind variable
       8             :  * ct_fetch and print results
       9             :  */
      10          10 : TEST_MAIN()
      11             : {
      12             :         CS_CONTEXT *ctx;
      13             :         CS_CONNECTION *conn;
      14             :         CS_COMMAND *cmd;
      15          10 :         int verbose = 0;
      16             : 
      17             :         CS_RETCODE ret;
      18             :         CS_RETCODE results_ret;
      19             :         CS_DATAFMT datafmt;
      20             :         CS_INT datalength;
      21             :         CS_SMALLINT ind;
      22          10 :         CS_INT count, row_count = 0;
      23             :         CS_INT result_type;
      24             :         CS_CHAR name[256];
      25             : 
      26          10 :         printf("%s: Testing bind & select\n", __FILE__);
      27             :         if (verbose) {
      28             :                 printf("Trying login\n");
      29             :         }
      30          10 :         check_call(try_ctlogin, (&ctx, &conn, &cmd, verbose));
      31             : 
      32          10 :         check_call(ct_command, (cmd, CS_LANG_CMD, "select name = @@servername", CS_NULLTERM, CS_UNUSED));
      33          10 :         check_call(ct_send, (cmd));
      34          40 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
      35          20 :                 switch ((int) result_type) {
      36             :                 case CS_CMD_SUCCEED:
      37             :                         break;
      38             :                 case CS_CMD_DONE:
      39             :                         break;
      40           0 :                 case CS_CMD_FAIL:
      41           0 :                         fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
      42           0 :                         return 1;
      43          10 :                 case CS_ROW_RESULT:
      44          10 :                         datafmt.datatype = CS_CHAR_TYPE;
      45          10 :                         datafmt.format = CS_FMT_NULLTERM;
      46          10 :                         datafmt.maxlength = 256;
      47          10 :                         datafmt.count = 1;
      48          10 :                         datafmt.locale = NULL;
      49          10 :                         check_call(ct_bind, (cmd, 1, &datafmt, name, &datalength, &ind));
      50             : 
      51          30 :                         while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
      52          10 :                                || (ret == CS_ROW_FAIL)) {
      53          10 :                                 row_count += count;
      54          10 :                                 if (ret == CS_ROW_FAIL) {
      55           0 :                                         fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
      56           0 :                                         return 1;
      57          10 :                                 } else if (ret == CS_SUCCEED) {
      58             :                                         if (verbose) {
      59             :                                                 printf("server name = %s\n", name);
      60             :                                         }
      61             :                                 } else {
      62             :                                         break;
      63             :                                 }
      64             :                         }
      65          10 :                         switch ((int) ret) {
      66             :                         case CS_END_DATA:
      67             :                                 break;
      68           0 :                         case CS_FAIL:
      69           0 :                                 fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
      70           0 :                                 return 1;
      71           0 :                         default:
      72           0 :                                 fprintf(stderr, "ct_fetch() unexpected return.\n");
      73           0 :                                 return 1;
      74             :                         }
      75             :                         break;
      76           0 :                 case CS_COMPUTE_RESULT:
      77           0 :                         fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
      78           0 :                         return 1;
      79           0 :                 default:
      80           0 :                         fprintf(stderr, "ct_results() unexpected result_type.\n");
      81           0 :                         return 1;
      82             :                 }
      83             :         }
      84          10 :         switch ((int) results_ret) {
      85             :         case CS_END_RESULTS:
      86             :                 break;
      87           0 :         case CS_FAIL:
      88           0 :                 fprintf(stderr, "ct_results() failed.\n");
      89           0 :                 return 1;
      90             :                 break;
      91           0 :         default:
      92           0 :                 fprintf(stderr, "ct_results() unexpected return.\n");
      93           0 :                 return 1;
      94             :         }
      95             : 
      96             :         /*
      97             :          * test parameter return code processing with sp_who
      98             :          */
      99          10 :         sp_who(cmd);
     100             : 
     101             :         if (verbose) {
     102             :                 printf("Trying logout\n");
     103             :         }
     104          10 :         check_call(try_ctlogout, (ctx, conn, cmd, verbose));
     105             : 
     106          10 :         return 0;
     107             : }
     108             : 
     109             : int 
     110          10 : sp_who(CS_COMMAND *cmd)
     111             : {
     112             :         enum {maxcol=10, colsize=260};
     113             :         
     114             :         struct _col { 
     115             :                 CS_DATAFMT datafmt;
     116             :                 CS_INT datalength;
     117             :                 CS_SMALLINT ind;
     118             :                 CS_CHAR data[colsize];
     119             :         } col[maxcol];
     120             :         
     121             :         CS_INT num_cols;
     122          10 :         CS_INT count, row_count = 0;
     123             :         CS_INT result_type;
     124             :         CS_RETCODE ret;
     125             :         CS_RETCODE results_ret;
     126             :         int i;
     127          10 :         int is_status_result=0;
     128             : 
     129          10 :         check_call(ct_command, (cmd, CS_LANG_CMD, "exec sp_who", CS_NULLTERM, CS_UNUSED));
     130          10 :         check_call(ct_send, (cmd));
     131          10 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
     132          58 :                 is_status_result = 0;
     133          58 :                 switch ((int) result_type) {
     134             :                 case CS_CMD_SUCCEED:
     135             :                         break;
     136             :                 case CS_CMD_DONE:
     137             :                         break;
     138           0 :                 case CS_CMD_FAIL:
     139           0 :                         fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
     140           0 :                         return 1;
     141          10 :                 case CS_STATUS_RESULT:
     142          10 :                         printf("ct_results: CS_STATUS_RESULT detected for sp_who\n");
     143          10 :                         is_status_result = 1;
     144             :                         /* fall through */
     145          20 :                 case CS_ROW_RESULT:
     146          20 :                         check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
     147          20 :                         if (num_cols > maxcol) {
     148           0 :                                 fprintf(stderr, "ct_res_info() failed\n");
     149           0 :                                 return 1;
     150             :                         }
     151             : 
     152          20 :                         if (num_cols <= 0) {
     153           0 :                                 fprintf(stderr, "ct_res_info() return strange values\n");
     154           0 :                                 return 1;
     155             :                         }
     156             : 
     157          20 :                         if (is_status_result && num_cols != 1) {
     158           0 :                                 fprintf(stderr, "CS_STATUS_RESULT return more than 1 column\n");
     159           0 :                                 return 1;
     160             :                         }
     161             :                         
     162         100 :                         for (i=0; i < num_cols; i++) {
     163             : 
     164             :                                 /* here we can finally test for the return status column */
     165         100 :                                 check_call(ct_describe, (cmd, i+1, &col[i].datafmt));
     166             :                                 
     167         100 :                                 if (col[i].datafmt.status & CS_RETURN) {
     168           0 :                                         printf("ct_describe() indicates a return code in column %d for sp_who\n", i);
     169             :                                         
     170             :                                         /*
     171             :                                          * other possible values:
     172             :                                          * CS_CANBENULL
     173             :                                          * CS_HIDDEN
     174             :                                          * CS_IDENTITY
     175             :                                          * CS_KEY
     176             :                                          * CS_VERSION_KEY
     177             :                                          * CS_TIMESTAMP
     178             :                                          * CS_UPDATABLE
     179             :                                          * CS_UPDATECOL
     180             :                                          */
     181             :                                 }
     182             :                                 
     183         100 :                                 col[i].datafmt.datatype = CS_CHAR_TYPE;
     184         100 :                                 col[i].datafmt.format = CS_FMT_NULLTERM;
     185         100 :                                 col[i].datafmt.maxlength = colsize;
     186         100 :                                 col[i].datafmt.count = 1;
     187         100 :                                 col[i].datafmt.locale = NULL;
     188             : 
     189         100 :                                 check_call(ct_bind, (cmd, i+1, &col[i].datafmt, &col[i].data, &col[i].datalength, &col[i].ind));
     190             :                         }
     191             : 
     192             :                         row_count = 0;
     193         306 :                         while ((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED) {
     194         286 :                                 if( row_count == 0) { /* titles */
     195         100 :                                         for (i=0; i < num_cols; i++) {
     196             :                                                 char fmt[40];
     197         100 :                                                 if (col[i].datafmt.namelen == 0) {
     198          10 :                                                         printf("unnamed%d ",i+1);
     199          10 :                                                         continue;
     200             :                                                 }
     201          90 :                                                 sprintf(fmt, "%%-%d.%ds  ", col[i].datafmt.namelen, col[i].datafmt.maxlength);
     202          90 :                                                 printf(fmt, col[i].datafmt.name);
     203             :                                         }
     204          20 :                                         printf("\n");
     205             :                                 }
     206             :                                 
     207        2489 :                                 for (i=0; i < num_cols; i++) { /* data */
     208             :                                         char fmt[40];
     209        2489 :                                         if (col[i].ind)
     210         126 :                                                 continue;
     211        2363 :                                         sprintf(fmt, "%%-%d.%ds  ", col[i].datalength, col[i].datafmt.maxlength);
     212        2363 :                                         printf(fmt, col[i].data);
     213        2363 :                                         if (is_status_result && strcmp(col[i].data,"0")) {
     214           0 :                                                 fprintf(stderr, "CS_STATUS_RESULT should return 0 as result\n");
     215           0 :                                                 return 1;
     216             :                                         }
     217             :                                 }
     218             : 
     219         286 :                                 printf("\n");
     220             : 
     221         286 :                                 row_count += count;
     222             :                         }
     223          20 :                         if (is_status_result && row_count != 1) {
     224           0 :                                 fprintf(stderr, "CS_STATUS_RESULT should return a row\n");
     225           0 :                                 return 1;
     226             :                         }
     227             :                         
     228          20 :                         switch ((int) ret) {
     229          20 :                         case CS_END_DATA:
     230          20 :                                 printf("ct_results fetched %d rows.\n", row_count);
     231             :                                 break;
     232           0 :                         case CS_ROW_FAIL:
     233           0 :                                 fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
     234           0 :                                 return 1;
     235           0 :                         case CS_FAIL:
     236           0 :                                 fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
     237           0 :                                 return 1;
     238           0 :                         default:
     239           0 :                                 fprintf(stderr, "ct_fetch() unexpected return.\n");
     240           0 :                                 return 1;
     241             :                         }
     242          20 :                         break;
     243           0 :                 case CS_COMPUTE_RESULT:
     244           0 :                         fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
     245           0 :                         return 1;
     246           0 :                 default:
     247           0 :                         fprintf(stderr, "ct_results() unexpected result_type.\n");
     248           0 :                         return 1;
     249             :                 }
     250             :         }
     251             :         
     252          10 :         switch ((int) results_ret) {
     253             :         case CS_END_RESULTS:
     254             :                 break;
     255           0 :         case CS_FAIL:
     256           0 :                 fprintf(stderr, "ct_results() failed.\n");
     257           0 :                 return 1;
     258             :                 break;
     259           0 :         default:
     260           0 :                 fprintf(stderr, "ct_results() unexpected return.\n");
     261           0 :                 return 1;
     262             :         }
     263             :         
     264             :         return 0;
     265             : }

Generated by: LCOV version 1.13