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

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

Generated by: LCOV version 1.13