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

Generated by: LCOV version 1.13