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: 2024-04-18 20:40:06 Functions: 1 1 100.0 %

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

Generated by: LCOV version 1.13