LCOV - code coverage report
Current view: top level - src/ctlib/unittests - t0009.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 83 137 60.6 %
Date: 2025-01-18 11:50:39 Functions: 1 2 50.0 %

          Line data    Source code
       1             : #include "common.h"
       2             : 
       3             : static CS_RETCODE ex_servermsg_cb(CS_CONTEXT * context, CS_CONNECTION * connection, CS_SERVERMSG * errmsg);
       4             : static int compute_supported = 1;
       5             : 
       6             : /* Testing: Retrieve compute results */
       7             : int
       8           8 : main(void)
       9             : {
      10             :         CS_CONTEXT *ctx;
      11             :         CS_CONNECTION *conn;
      12             :         CS_COMMAND *cmd;
      13           8 :         int verbose = 0;
      14             : 
      15             :         CS_RETCODE ret;
      16             :         CS_RETCODE results_ret;
      17             :         CS_INT result_type;
      18             :         CS_INT num_cols, compute_id;
      19             : 
      20             :         CS_DATAFMT datafmt;
      21             :         CS_INT datalength;
      22             :         CS_SMALLINT ind;
      23           8 :         CS_INT count, row_count = 0;
      24             : 
      25             :         CS_CHAR select[1024];
      26             : 
      27             :         CS_INT col1;
      28             :         CS_CHAR col2[2];
      29             :         CS_CHAR col3[32];
      30             : 
      31             :         CS_INT compute_col1;
      32           8 :         CS_CHAR compute_col3[32] = "";
      33             : 
      34           8 :         unsigned rows[3] = { 0, 0, 0 };
      35             : 
      36           8 :         printf("%s: Retrieve compute results processing\n", __FILE__);
      37             :         if (verbose) {
      38             :                 printf("Trying login\n");
      39             :         }
      40           8 :         check_call(try_ctlogin, (&ctx, &conn, &cmd, verbose));
      41             : 
      42           8 :         check_call(run_command,
      43             :                    (cmd, "CREATE TABLE #ctlib0009 (col1 int not null,  col2 char(1) not null, col3 datetime not null)"));
      44             : 
      45           8 :         check_call(run_command, (cmd, "insert into #ctlib0009 values (1, 'A', 'Jan  1 2002 10:00:00AM')"));
      46           8 :         check_call(run_command, (cmd, "insert into #ctlib0009 values (2, 'A', 'Jan  2 2002 10:00:00AM')"));
      47           8 :         check_call(run_command, (cmd, "insert into #ctlib0009 values (3, 'A', 'Jan  3 2002 10:00:00AM')"));
      48           8 :         check_call(run_command, (cmd, "insert into #ctlib0009 values (8, 'B', 'Jan  4 2002 10:00:00AM')"));
      49           8 :         check_call(run_command, (cmd, "insert into #ctlib0009 values (9, 'B', 'Jan  5 2002 10:00:00AM')"));
      50             : 
      51           8 :         strcpy(select, "select col1, col2, col3 from #ctlib0009 order by col2 ");
      52           8 :         strcat(select, "compute sum(col1) by col2 ");
      53           8 :         strcat(select, "compute max(col3)");
      54             : 
      55           8 :         check_call(ct_command, (cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED));
      56             : 
      57           8 :         check_call(ct_send, (cmd));
      58             : 
      59           8 :         ct_callback(ctx, NULL, CS_SET, CS_SERVERMSG_CB, (CS_VOID *) ex_servermsg_cb);
      60          64 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
      61          48 :                 printf("ct_results returned %s type\n", res_type_str(result_type));
      62          48 :                 switch ((int) result_type) {
      63             :                 case CS_CMD_SUCCEED:
      64             :                         break;
      65             :                 case CS_CMD_DONE:
      66             :                         break;
      67           0 :                 case CS_CMD_FAIL:
      68           0 :                         if (!compute_supported) {
      69           0 :                                 try_ctlogout(ctx, conn, cmd, verbose);
      70           0 :                                 return 0;
      71             :                         }
      72           0 :                         fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
      73           0 :                         return 1;
      74          16 :                 case CS_ROW_RESULT:
      75          16 :                         check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
      76          16 :                         if (num_cols != 3) {
      77           0 :                                 fprintf(stderr, "num_cols %d != 3", num_cols);
      78           0 :                                 return 1;
      79             :                         }
      80             : 
      81          16 :                         check_call(ct_describe, (cmd, 1, &datafmt));
      82          16 :                         datafmt.format = CS_FMT_UNUSED;
      83          16 :                         if (datafmt.maxlength > 1024) {
      84           0 :                                 datafmt.maxlength = 1024;
      85             :                         }
      86          16 :                         check_call(ct_bind, (cmd, 1, &datafmt, &col1, &datalength, &ind));
      87             : 
      88          16 :                         check_call(ct_describe, (cmd, 2, &datafmt));
      89             : 
      90          16 :                         datafmt.format = CS_FMT_NULLTERM;
      91          16 :                         datafmt.maxlength = 2;
      92             : 
      93          16 :                         check_call(ct_bind, (cmd, 2, &datafmt, col2, &datalength, &ind));
      94             : 
      95          16 :                         check_call(ct_describe, (cmd, 3, &datafmt));
      96             : 
      97          16 :                         datafmt.datatype = CS_CHAR_TYPE;
      98          16 :                         datafmt.format = CS_FMT_NULLTERM;
      99          16 :                         datafmt.maxlength = 32;
     100             : 
     101          16 :                         check_call(ct_bind, (cmd, 3, &datafmt, col3, &datalength, &ind));
     102             : 
     103          72 :                         while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
     104          16 :                                || (ret == CS_ROW_FAIL)) {
     105          40 :                                 row_count += count;
     106          40 :                                 if (ret == CS_ROW_FAIL) {
     107           0 :                                         fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
     108           0 :                                         return 1;
     109             :                                 } else {        /* ret == CS_SUCCEED */
     110          40 :                                         printf("col1 = %d col2= '%s', col3 = '%s'\n", col1, col2, col3);
     111          40 :                                         ++rows[0];
     112             :                                 }
     113             :                         }
     114             : 
     115             : 
     116          16 :                         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          24 :                 case CS_COMPUTE_RESULT:
     129             : 
     130          24 :                         printf("testing compute_result\n");
     131             : 
     132          24 :                         check_call(ct_compute_info, (cmd, CS_COMP_ID, CS_UNUSED, &compute_id, CS_UNUSED, NULL));
     133             : 
     134          24 :                         if (compute_id != 1 && compute_id != 2) {
     135           0 :                                 fprintf(stderr, "invalid compute_id value");
     136           0 :                                 return 1;
     137             :                         }
     138             : 
     139          24 :                         if (compute_id == 1) {
     140          16 :                                 check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
     141          16 :                                 if (num_cols != 1) {
     142           0 :                                         fprintf(stderr, "compute_id %d num_cols %d != 1", compute_id, num_cols);
     143           0 :                                         return 1;
     144             :                                 }
     145             : 
     146          16 :                                 check_call(ct_describe, (cmd, 1, &datafmt));
     147          16 :                                 datafmt.format = CS_FMT_UNUSED;
     148          16 :                                 if (datafmt.maxlength > 1024) {
     149           0 :                                         datafmt.maxlength = 1024;
     150             :                                 }
     151          16 :                                 compute_col1 = -1;
     152          16 :                                 check_call(ct_bind, (cmd, 1, &datafmt, &compute_col1, &datalength, &ind));
     153             :                         }
     154          24 :                         if (compute_id == 2) {
     155           8 :                                 check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
     156           8 :                                 if (num_cols != 1) {
     157           0 :                                         fprintf(stderr, "compute_id %d num_cols %d != 1", compute_id, num_cols);
     158           0 :                                         return 1;
     159             :                                 }
     160             : 
     161           8 :                                 check_call(ct_describe, (cmd, 1, &datafmt));
     162             : 
     163           8 :                                 datafmt.datatype = CS_CHAR_TYPE;
     164           8 :                                 datafmt.format = CS_FMT_NULLTERM;
     165           8 :                                 datafmt.maxlength = 32;
     166             : 
     167           8 :                                 compute_col3[0] = 0;
     168           8 :                                 check_call(ct_bind, (cmd, 1, &datafmt, compute_col3, &datalength, &ind));
     169             :                         }
     170             : 
     171             : 
     172          48 :                         while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
     173          24 :                                || (ret == CS_ROW_FAIL)) {
     174          24 :                                 row_count += count;
     175          24 :                                 if (ret == CS_ROW_FAIL) {
     176           0 :                                         fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
     177           0 :                                         return 1;
     178             :                                 } else {        /* ret == CS_SUCCEED */
     179          24 :                                         if (compute_id == 1) {
     180          16 :                                                 printf("compute_col1 = %d \n", compute_col1);
     181          16 :                                                 if (compute_col1 != 6 && compute_col1 != 17) {
     182           0 :                                                         fprintf(stderr, "(should be 6 or 17)\n");
     183           0 :                                                         return 1;
     184             :                                                 }
     185             :                                         }
     186          24 :                                         if (compute_id == 2) {
     187           8 :                                                 printf("compute_col3 = '%s'\n", compute_col3);
     188           8 :                                                 if (strcmp("Jan  5 2002 10:00:00AM", compute_col3)
     189           8 :                                                     && strcmp("Jan 05 2002 10:00AM", compute_col3)
     190           0 :                                                     && strcmp("Jan  5 2002 10:00AM", compute_col3)) {
     191           0 :                                                         fprintf(stderr, "(should be \"Jan  5 2002 10:00:00AM\")\n");
     192           0 :                                                         return 1;
     193             :                                                 }
     194             :                                         }
     195          24 :                                         ++rows[compute_id];
     196             :                                 }
     197             :                         }
     198             : 
     199             : 
     200          24 :                         switch ((int) ret) {
     201             :                         case CS_END_DATA:
     202             :                                 break;
     203           0 :                         case CS_FAIL:
     204           0 :                                 fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
     205           0 :                                 return 1;
     206           0 :                         default:
     207           0 :                                 fprintf(stderr, "ct_fetch() unexpected return.\n");
     208           0 :                                 return 1;
     209             :                         }
     210             :                         break;
     211             : 
     212           0 :                 default:
     213           0 :                         fprintf(stderr, "ct_results() unexpected result_type.\n");
     214           0 :                         return 1;
     215             :                 }
     216             :         }
     217           8 :         switch ((int) results_ret) {
     218             :         case CS_END_RESULTS:
     219             :                 break;
     220           0 :         case CS_FAIL:
     221           0 :                 fprintf(stderr, "ct_results() failed.\n");
     222           0 :                 return 1;
     223             :                 break;
     224           0 :         default:
     225           0 :                 fprintf(stderr, "ct_results() unexpected return.\n");
     226           0 :                 return 1;
     227             :         }
     228             : 
     229           8 :         if (rows[0] != 5 || rows[1] != 2 || rows[2] != 1) {
     230           0 :                 fprintf(stderr, "wrong number of rows: normal %u compute_1 %u compute_2 %u, expected 5 2 1\n",
     231             :                         rows[0], rows[1], rows[2]);
     232           0 :                 return 1;
     233             :         }
     234             : 
     235             :         if (verbose) {
     236             :                 printf("Trying logout\n");
     237             :         }
     238           8 :         check_call(try_ctlogout, (ctx, conn, cmd, verbose));
     239             : 
     240           8 :         return 0;
     241             : }
     242             : 
     243             : static CS_RETCODE
     244           0 : ex_servermsg_cb(CS_CONTEXT * context, CS_CONNECTION * connection, CS_SERVERMSG * srvmsg)
     245             : {
     246           0 :         if (strstr(srvmsg->text, "compute")) {
     247           0 :                 compute_supported = 0;
     248           0 :                 printf("Server does not support compute\n");
     249           0 :                 return CS_SUCCEED;
     250             :         }
     251           0 :         return servermsg_cb(context, connection, srvmsg);
     252             : }

Generated by: LCOV version 1.13