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

Generated by: LCOV version 1.13