LCOV - code coverage report
Current view: top level - src/ctlib/unittests - ct_diagall.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 77 136 56.6 %
Date: 2024-03-23 09:12:27 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(int argc, char *argv[])
      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 :         ret = try_ctlogin(&ctx, &conn, &cmd, verbose);
      46           8 :         if (ret != CS_SUCCEED) {
      47           0 :                 fprintf(stderr, "Login failed\n");
      48           0 :                 return 1;
      49             :         }
      50             : 
      51           8 :         if (ct_diag(conn, CS_INIT, CS_UNUSED, CS_UNUSED, NULL) != CS_SUCCEED) {
      52           0 :                 fprintf(stderr, "ct_diag(CS_INIT) failed\n");
      53           0 :                 return 1;
      54             :         }
      55             : 
      56           8 :         totmsgs = 5;
      57           8 :         if (ct_diag(conn, CS_MSGLIMIT, CS_ALLMSG_TYPE, CS_UNUSED, &totmsgs) != CS_SUCCEED) {
      58           0 :                 fprintf(stderr, "ct_diag(CS_MSGLIMIT) failed\n");
      59           0 :                 return 1;
      60             :         }
      61             : 
      62           8 :         printf("Maximum message limit is set to: %d\n", totmsgs);
      63             : 
      64           8 :         ret = run_command(cmd, "CREATE TABLE #ctlibarray (col1 int not null,  col2 char(4) not null, col3 datetime not null)");
      65           8 :         if (ret != CS_SUCCEED)
      66             :                 return 1;
      67             : 
      68           8 :         ret = run_command(cmd, "insert into #ctlibarray values (1, 'AAAA', 'Jan  1 2002 10:00:00AM')");
      69           8 :         if (ret != CS_SUCCEED)
      70             :                 return 1;
      71             : 
      72           8 :         ret = run_command(cmd, "insert into #ctlibarray values (2, 'BBBB', 'Jan  2 2002 10:00:00AM')");
      73           8 :         if (ret != CS_SUCCEED)
      74             :                 return 1;
      75             : 
      76           8 :         strcpy(select, "select col1, col2 from #ctlibarray order by col1 ");
      77             : 
      78           8 :         ret = ct_command(cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED);
      79             : 
      80           8 :         if (ret != CS_SUCCEED) {
      81           0 :                 fprintf(stderr, "ct_command(%s) failed\n", select);
      82           0 :                 return 1;
      83             :         }
      84             : 
      85           8 :         ret = ct_send(cmd);
      86           8 :         if (ret != CS_SUCCEED) {
      87           0 :                 fprintf(stderr, "ct_send() failed\n");
      88           0 :                 return 1;
      89             :         }
      90             : 
      91          24 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
      92          16 :                 switch ((int) result_type) {
      93             :                 case CS_CMD_SUCCEED:
      94             :                         break;
      95             :                 case CS_CMD_DONE:
      96             :                         break;
      97           0 :                 case CS_CMD_FAIL:
      98           0 :                         fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
      99           0 :                         return 1;
     100           8 :                 case CS_ROW_RESULT:
     101             : 
     102           8 :                         ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);
     103           8 :                         if (ret != CS_SUCCEED) {
     104           0 :                                 fprintf(stderr, "ct_res_info() failed");
     105           0 :                                 return 1;
     106             :                         }
     107           8 :                         if (num_cols != 2) {
     108           0 :                                 fprintf(stderr, "num_cols %d != 2", num_cols);
     109           0 :                                 return 1;
     110             :                         }
     111             : 
     112           8 :                         ret = ct_describe(cmd, 1, &datafmt);
     113           8 :                         if (ret != CS_SUCCEED) {
     114           0 :                                 fprintf(stderr, "ct_describe() failed");
     115           0 :                                 return 1;
     116             :                         }
     117           8 :                         datafmt.format = CS_FMT_UNUSED;
     118           8 :                         if (datafmt.maxlength > 1024) {
     119           0 :                                 datafmt.maxlength = 1024;
     120             :                         }
     121             : 
     122           8 :                         datafmt.count = 2;
     123             : 
     124           8 :                         ret = ct_bind(cmd, 1, &datafmt, &col1[0], datalength, ind);
     125           8 :                         if (ret != CS_SUCCEED) {
     126           0 :                                 fprintf(stderr, "ct_bind() failed\n");
     127           0 :                                 return 1;
     128             :                         }
     129             : 
     130           8 :                         ret = ct_describe(cmd, 2, &datafmt);
     131           8 :                         if (ret != CS_SUCCEED) {
     132           0 :                                 fprintf(stderr, "ct_describe() failed");
     133           0 :                                 return 1;
     134             :                         }
     135             : 
     136           8 :                         datafmt.format = CS_FMT_NULLTERM;
     137           8 :                         datafmt.maxlength = 5;
     138           8 :                         datafmt.count = 4;
     139             : 
     140           8 :                         ret = ct_bind(cmd, 2, &datafmt, &col2[0], datalength, ind);
     141             : 
     142           8 :                         if (ret != CS_SUCCEED) {
     143             : 
     144           8 :                                 datafmt.format = CS_FMT_NULLTERM;
     145           8 :                                 datafmt.maxlength = 5;
     146           8 :                                 datafmt.count = 2;
     147             : 
     148           8 :                                 ret = ct_bind(cmd, 2, &datafmt, &col2[0], datalength, ind);
     149             : 
     150             :                         }
     151             : 
     152           8 :                         if (ret != CS_SUCCEED) {
     153             :                                 return 1;
     154             :                         }
     155             : 
     156           8 :                         count = 0;
     157          24 :                         while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
     158           8 :                                || (ret == CS_ROW_FAIL)) {
     159           8 :                                 row_count += count;
     160           8 :                                 if (ret == CS_ROW_FAIL) {
     161           0 :                                         fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
     162           0 :                                         return 1;
     163             :                                 } else {        /* ret == CS_SUCCEED */
     164           8 :                                         printf("ct_fetch returned %d rows\n", count);
     165          24 :                                         for (cv = 0; cv < count; cv++)
     166          16 :                                                 printf("col1 = %d col2= '%s'\n", col1[cv], col2[cv]);
     167             :                                 }
     168           8 :                                 count = 0;
     169             :                         }
     170             : 
     171             : 
     172           8 :                         switch ((int) ret) {
     173             :                         case CS_END_DATA:
     174             :                                 break;
     175           0 :                         case CS_FAIL:
     176           0 :                                 fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
     177           0 :                                 return 1;
     178           0 :                         default:
     179           0 :                                 fprintf(stderr, "ct_fetch() unexpected return.\n");
     180           0 :                                 return 1;
     181             :                         }
     182             :                         break;
     183             : 
     184           0 :                 default:
     185           0 :                         fprintf(stderr, "ct_results() unexpected result_type.\n");
     186           0 :                         return 1;
     187             :                 }
     188             :         }
     189           8 :         switch ((int) results_ret) {
     190             :         case CS_END_RESULTS:
     191             :                 break;
     192           0 :         case CS_FAIL:
     193           0 :                 fprintf(stderr, "ct_results() failed.\n");
     194           0 :                 return 1;
     195             :                 break;
     196           0 :         default:
     197           0 :                 fprintf(stderr, "ct_results() unexpected return.\n");
     198           0 :                 return 1;
     199             :         }
     200             : 
     201           8 :         ret = run_command(cmd, "DROP TABLE #ctlibarray3");
     202           8 :         ret = run_command(cmd, "DROP TABLE #ctlibarray4");
     203           8 :         ret = run_command(cmd, "DROP TABLE #ctlibarray5");
     204             : 
     205           8 :         if (ct_diag(conn, CS_STATUS, CS_ALLMSG_TYPE, CS_UNUSED, &num_msgs) != CS_SUCCEED) {
     206           0 :                 fprintf(stderr, "ct_diag(CS_STATUS) failed\n");
     207           0 :                 return 1;
     208             :         }
     209             : 
     210           8 :         printf("Total number of client/server messages = %d \n", num_msgs);
     211             : 
     212           8 :         if (ct_diag(conn, CS_STATUS, CS_CLIENTMSG_TYPE, CS_UNUSED, &num_msgs) != CS_SUCCEED) {
     213           0 :                 fprintf(stderr, "ct_diag(CS_STATUS) failed\n");
     214           0 :                 return 1;
     215             :         }
     216             : 
     217           8 :         printf("Number of client messages returned: %d\n", num_msgs);
     218             : 
     219          16 :         for (i = 0; i < num_msgs; i++) {
     220             : 
     221           8 :                 if (ct_diag(conn, CS_GET, CS_CLIENTMSG_TYPE, i + 1, &client_message) != CS_SUCCEED) {
     222           0 :                         fprintf(stderr, "cs_diag(CS_GET) failed\n");
     223           0 :                         return 1;
     224             :                 }
     225             : 
     226           8 :                 clientmsg_cb(ctx, conn, &client_message);
     227             : 
     228             :         }
     229             : 
     230           8 :         if (ct_diag(conn, CS_STATUS, CS_SERVERMSG_TYPE, CS_UNUSED, &num_msgs) != CS_SUCCEED) {
     231           0 :                 fprintf(stderr, "ct_diag(CS_STATUS) failed\n");
     232           0 :                 return 1;
     233             :         }
     234             : 
     235           8 :         printf("Number of server messages returned: %d\n", num_msgs);
     236             : 
     237          32 :         for (i = 0; i < num_msgs; i++) {
     238             : 
     239          24 :                 if (ct_diag(conn, CS_GET, CS_SERVERMSG_TYPE, i + 1, &server_message) != CS_SUCCEED) {
     240           0 :                         fprintf(stderr, "cs_diag(CS_GET) failed\n");
     241           0 :                         return 1;
     242             :                 }
     243             : 
     244          24 :                 servermsg_cb(ctx, conn, &server_message);
     245             : 
     246             :         }
     247             : 
     248           8 :         if (ct_diag(conn, CS_CLEAR, CS_ALLMSG_TYPE, CS_UNUSED, NULL) != CS_SUCCEED) {
     249           0 :                 fprintf(stderr, "cs_diag(CS_CLEAR) failed\n");
     250           0 :                 return 1;
     251             :         }
     252             : 
     253           8 :         if (ct_diag(conn, CS_STATUS, CS_ALLMSG_TYPE, CS_UNUSED, &num_msgs) != CS_SUCCEED) {
     254           0 :                 fprintf(stderr, "cs_diag(CS_STATUS) failed\n");
     255           0 :                 return 1;
     256             :         }
     257           8 :         if (num_msgs != 0) {
     258           0 :                 fprintf(stderr, "cs_diag(CS_CLEAR) failed there are still %d messages on queue\n", num_msgs);
     259           0 :                 return 1;
     260             :         }
     261             : 
     262             :         if (verbose) {
     263             :                 printf("Trying logout\n");
     264             :         }
     265           8 :         ret = try_ctlogout(ctx, conn, cmd, verbose);
     266           8 :         if (ret != CS_SUCCEED) {
     267           0 :                 fprintf(stderr, "Logout failed\n");
     268           0 :                 return 1;
     269             :         }
     270             : 
     271             :         return 0;
     272             : }

Generated by: LCOV version 1.13