LCOV - code coverage report
Current view: top level - src/ctlib/unittests - ct_diagclient.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 67 124 54.0 %
Date: 2024-03-23 09:12:27 Functions: 1 1 100.0 %

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

Generated by: LCOV version 1.13