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

Generated by: LCOV version 1.13