LCOV - code coverage report
Current view: top level - src/ctlib/unittests - ct_diagclient.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 56 83 67.5 %
Date: 2025-01-18 11:50:39 Functions: 1 1 100.0 %

          Line data    Source code
       1             : #include "common.h"
       2             : 
       3             : /* Testing: Client Messages */
       4             : int
       5           8 : main(void)
       6             : {
       7             :         CS_CONTEXT *ctx;
       8             :         CS_CONNECTION *conn;
       9             :         CS_COMMAND *cmd;
      10           8 :         int verbose = 0;
      11             : 
      12             :         CS_RETCODE ret;
      13             :         CS_RETCODE results_ret;
      14             :         CS_INT result_type;
      15             :         CS_INT num_cols;
      16             : 
      17             :         CS_DATAFMT datafmt;
      18             :         CS_INT datalength[2];
      19             :         CS_SMALLINT ind[2];
      20           8 :         CS_INT count, row_count = 0;
      21             :         CS_INT cv;
      22             :         int i;
      23             :         CS_CHAR select[1024];
      24             : 
      25             :         CS_INT col1[2];
      26             :         CS_CHAR col2[2][5];
      27             :         CS_INT num_msgs, totmsgs;
      28             :         CS_CLIENTMSG client_message;
      29           8 :         int result = 1;
      30             : 
      31           8 :         printf("%s: Retrieve data using array binding \n", __FILE__);
      32             :         if (verbose) {
      33             :                 printf("Trying login\n");
      34             :         }
      35           8 :         check_call(try_ctlogin, (&ctx, &conn, &cmd, verbose));
      36             : 
      37           8 :         check_call(ct_diag, (conn, CS_INIT, CS_UNUSED, CS_UNUSED, NULL));
      38             : 
      39           8 :         totmsgs = 1;
      40           8 :         check_call(ct_diag, (conn, CS_MSGLIMIT, CS_CLIENTMSG_TYPE, CS_UNUSED, &totmsgs));
      41             : 
      42           8 :         printf("Maximum message limit is set to: %d\n", totmsgs);
      43             : 
      44           8 :         check_call(run_command, (cmd, "CREATE TABLE #ctlibarray (col1 int not null,  col2 char(4) not null, col3 datetime not null)"));
      45           8 :         check_call(run_command, (cmd, "insert into #ctlibarray values (1, 'AAAA', 'Jan  1 2002 10:00:00AM')"));
      46           8 :         check_call(run_command, (cmd, "insert into #ctlibarray values (2, 'BBBB', 'Jan  2 2002 10:00:00AM')"));
      47             : 
      48           8 :         strcpy(select, "select col1, col2 from #ctlibarray order by col1 ");
      49             : 
      50           8 :         check_call(ct_command, (cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED));
      51             : 
      52           8 :         check_call(ct_send, (cmd));
      53             : 
      54          32 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
      55          16 :                 switch ((int) result_type) {
      56             :                 case CS_CMD_SUCCEED:
      57             :                         break;
      58             :                 case CS_CMD_DONE:
      59             :                         break;
      60           0 :                 case CS_CMD_FAIL:
      61           0 :                         fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
      62           0 :                         return 1;
      63           8 :                 case CS_ROW_RESULT:
      64             : 
      65           8 :                         check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
      66           8 :                         if (num_cols != 2) {
      67           0 :                                 fprintf(stderr, "num_cols %d != 2", num_cols);
      68           0 :                                 return 1;
      69             :                         }
      70             : 
      71           8 :                         check_call(ct_describe, (cmd, 1, &datafmt));
      72           8 :                         datafmt.format = CS_FMT_UNUSED;
      73           8 :                         if (datafmt.maxlength > 1024) {
      74           0 :                                 datafmt.maxlength = 1024;
      75             :                         }
      76             : 
      77           8 :                         datafmt.count = 2;
      78             : 
      79           8 :                         check_call(ct_bind, (cmd, 1, &datafmt, &col1[0], datalength, ind));
      80             : 
      81           8 :                         check_call(ct_describe, (cmd, 2, &datafmt));
      82             : 
      83           8 :                         datafmt.format = CS_FMT_NULLTERM;
      84           8 :                         datafmt.maxlength = 5;
      85           8 :                         datafmt.count = 4;
      86             : 
      87           8 :                         ret = ct_bind(cmd, 2, &datafmt, &col2[0], datalength, ind);
      88           8 :                         if (ret != CS_SUCCEED) {
      89             : 
      90           8 :                                 check_call(ct_diag, (conn, CS_STATUS, CS_CLIENTMSG_TYPE, CS_UNUSED, &num_msgs));
      91             : 
      92           8 :                                 printf("Number of client messages returned: %d\n", num_msgs);
      93             : 
      94          16 :                                 for (i = 0; i < num_msgs; i++) {
      95             : 
      96           8 :                                         check_call(ct_diag, (conn, CS_GET, CS_CLIENTMSG_TYPE, i + 1, &client_message));
      97             : 
      98           8 :                                         clientmsg_cb(ctx, conn, &client_message);
      99             :                                 }
     100             : 
     101           8 :                                 check_call(ct_diag, (conn, CS_CLEAR, CS_CLIENTMSG_TYPE, CS_UNUSED, NULL));
     102             : 
     103           8 :                                 check_call(ct_diag, (conn, CS_STATUS, CS_CLIENTMSG_TYPE, CS_UNUSED, &num_msgs));
     104           8 :                                 if (num_msgs != 0) {
     105           0 :                                         fprintf(stderr, "cs_diag(CS_CLEAR) failed there are still %d messages on queue\n",
     106             :                                                 num_msgs);
     107           0 :                                         return 1;
     108             :                                 }
     109             :                                 
     110             :                                 /* we catch error, good */
     111           8 :                                 result = 0;
     112             :                         } else {
     113           0 :                                 fprintf(stderr, "ct_bind() succeeded while it shouldn't\n");
     114           0 :                                 return 1;
     115             :                         }
     116           8 :                         datafmt.count = 2;
     117           8 :                         check_call(ct_bind, (cmd, 2, &datafmt, &col2[0], datalength, ind));
     118           8 :                         count = 0;
     119          24 :                         while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
     120           8 :                                || (ret == CS_ROW_FAIL)) {
     121           8 :                                 row_count += count;
     122           8 :                                 if (ret == CS_ROW_FAIL) {
     123           0 :                                         fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
     124           0 :                                         return 1;
     125             :                                 } else {        /* ret == CS_SUCCEED */
     126           8 :                                         printf("ct_fetch returned %d rows\n", count);
     127          24 :                                         for (cv = 0; cv < count; cv++)
     128          16 :                                                 printf("col1 = %d col2= '%s'\n", col1[cv], col2[cv]);
     129             :                                 }
     130           8 :                                 count = 0;
     131             :                         }
     132             : 
     133             : 
     134           8 :                         switch ((int) ret) {
     135             :                         case CS_END_DATA:
     136             :                                 break;
     137           0 :                         case CS_FAIL:
     138           0 :                                 fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
     139           0 :                                 return 1;
     140           0 :                         default:
     141           0 :                                 fprintf(stderr, "ct_fetch() unexpected return.\n");
     142           0 :                                 return 1;
     143             :                         }
     144             :                         break;
     145             : 
     146           0 :                 default:
     147           0 :                         fprintf(stderr, "ct_results() unexpected result_type.\n");
     148           0 :                         return 1;
     149             :                 }
     150             :         }
     151           8 :         switch ((int) results_ret) {
     152             :         case CS_END_RESULTS:
     153             :                 break;
     154           0 :         case CS_FAIL:
     155           0 :                 fprintf(stderr, "ct_results() failed.\n");
     156           0 :                 return 1;
     157             :                 break;
     158           0 :         default:
     159           0 :                 fprintf(stderr, "ct_results() unexpected return.\n");
     160           0 :                 return 1;
     161             :         }
     162             : 
     163             :         if (verbose) {
     164             :                 printf("Trying logout\n");
     165             :         }
     166           8 :         check_call(try_ctlogout, (ctx, conn, cmd, verbose));
     167             : 
     168           8 :         return result;
     169             : }

Generated by: LCOV version 1.13