LCOV - code coverage report
Current view: top level - src/ctlib/unittests - ct_cursors.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 94 143 65.7 %
Date: 2025-07-26 13:53:51 Functions: 1 1 100.0 %

          Line data    Source code
       1             : #include "common.h"
       2             : 
       3          10 : TEST_MAIN()
       4             : {
       5             :         CS_CONTEXT *ctx;
       6             :         CS_CONNECTION *conn;
       7             :         CS_COMMAND *cmd;
       8             :         CS_COMMAND *cmd2;
       9             :         CS_COMMAND *cmd3;
      10             :         CS_RETCODE ret;
      11             :         CS_RETCODE ret2;
      12             :         CS_RETCODE results_ret;
      13             :         CS_INT result_type;
      14          10 :         CS_INT count, count2, row_count = 0;
      15             :         CS_DATAFMT datafmt;
      16             :         CS_DATAFMT datafmt2;
      17             :         CS_SMALLINT ind;
      18          10 :         int verbose = 1;
      19          10 :         CS_CHAR *name = "c1";
      20          10 :         CS_CHAR *name2 = "c2";
      21             :         CS_CHAR col1[6];
      22             :         CS_CHAR col2[6];
      23             :         CS_INT datalength;
      24             :         CS_CHAR text[128];
      25             :         CS_INT num_cols, i;
      26             : 
      27          10 :         memset(col1, 0, sizeof(col1));
      28          10 :         memset(col2, 0, sizeof(col2));
      29             : 
      30          10 :         printf("%s: use multiple cursors on the same connection\n", __FILE__);
      31             : 
      32             :         if (verbose) {
      33          10 :                 printf("Trying login\n");
      34             :         }
      35          10 :         check_call(try_ctlogin, (&ctx, &conn, &cmd, verbose));
      36             : 
      37          10 :         check_call(ct_cmd_alloc, (conn, &cmd2));
      38             : 
      39          10 :         check_call(ct_cmd_alloc, (conn, &cmd3));
      40             : 
      41          10 :         check_call(run_command, (cmd, "CREATE TABLE #test_table (col1 char(4))"));
      42          10 :         check_call(run_command, (cmd, "INSERT #test_table (col1) VALUES ('AAA')"));
      43          10 :         check_call(run_command, (cmd, "INSERT #test_table (col1) VALUES ('BBB')"));
      44          10 :         check_call(run_command, (cmd, "INSERT #test_table (col1) VALUES ('CCC')"));
      45             : 
      46          10 :         check_call(run_command, (cmd, "CREATE TABLE #test_table2 (col1 char(4))"));
      47          10 :         check_call(run_command, (cmd, "INSERT #test_table2 (col1) VALUES ('XXX')"));
      48          10 :         check_call(run_command, (cmd, "INSERT #test_table2 (col1) VALUES ('YYY')"));
      49          10 :         check_call(run_command, (cmd, "INSERT #test_table2 (col1) VALUES ('ZZZ')"));
      50             : 
      51             :         if (verbose) {
      52          10 :                 printf("opening first cursor on connection\n");
      53             :         }
      54             : 
      55          10 :         strcpy(text, "select col1 from #test_table order by col1");
      56             : 
      57          10 :         check_call(ct_cursor, (cmd, CS_CURSOR_DECLARE, name, CS_NULLTERM, text, CS_NULLTERM, CS_UNUSED));
      58             : 
      59          10 :         check_call(ct_cursor, (cmd, CS_CURSOR_ROWS, NULL, CS_UNUSED, NULL, CS_UNUSED, (CS_INT) 1));
      60             : 
      61          10 :         check_call(ct_cursor, (cmd, CS_CURSOR_OPEN, NULL, CS_UNUSED, NULL, CS_UNUSED, CS_UNUSED));
      62             : 
      63          10 :         check_call(ct_send, (cmd));
      64             : 
      65          38 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
      66          18 :                 switch ((int) result_type) {
      67             : 
      68           0 :                 case CS_CMD_FAIL:
      69           0 :                         fprintf(stderr, "ct_results failed\n");
      70           0 :                         return 1;
      71             :                 case CS_CMD_SUCCEED:
      72             :                 case CS_CMD_DONE:
      73             :                 case CS_STATUS_RESULT:
      74             :                         break;
      75             : 
      76          10 :                 case CS_CURSOR_RESULT:
      77          10 :                         check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
      78             : 
      79          10 :                         if (num_cols != 1) {
      80           0 :                                 fprintf(stderr, "unexpected num of columns =  %d \n", num_cols);
      81           0 :                                 return 1;
      82             :                         }
      83             : 
      84          10 :                         for (i = 0; i < num_cols; i++) {
      85             : 
      86             :                                 /* here we can finally test for the return status column */
      87          10 :                                 check_call(ct_describe, (cmd, i + 1, &datafmt));
      88             : 
      89          10 :                                 if (datafmt.status & CS_RETURN) {
      90           0 :                                         printf("ct_describe() column %d \n", i);
      91             :                                 }
      92             : 
      93          10 :                                 datafmt.datatype = CS_CHAR_TYPE;
      94          10 :                                 datafmt.format = CS_FMT_NULLTERM;
      95          10 :                                 datafmt.maxlength = 6;
      96          10 :                                 datafmt.count = 1;
      97          10 :                                 datafmt.locale = NULL;
      98          10 :                                 check_call(ct_bind, (cmd, 1, &datafmt, col1, &datalength, &ind));
      99             : 
     100             :                         }
     101             :                         break;
     102             : 
     103           0 :                 case CS_COMPUTE_RESULT:
     104           0 :                         fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
     105           0 :                         return 1;
     106           0 :                 default:
     107           0 :                         fprintf(stderr, "ct_results() unexpected result_type.\n");
     108           0 :                         return 1;
     109             :                 }
     110             :         }
     111             : 
     112             :         if (verbose) {
     113          10 :                 printf("opening second cursor on connection\n");
     114             :         }
     115             : 
     116          10 :         strcpy(text, "select col1 from #test_table2 order by col1");
     117             : 
     118          10 :         check_call(ct_cursor, (cmd2, CS_CURSOR_DECLARE, name2, CS_NULLTERM, text, CS_NULLTERM, CS_UNUSED));
     119             : 
     120          10 :         check_call(ct_cursor, (cmd2, CS_CURSOR_ROWS, NULL, CS_UNUSED, NULL, CS_UNUSED, (CS_INT) 1));
     121             : 
     122          10 :         check_call(ct_cursor, (cmd2, CS_CURSOR_OPEN, NULL, CS_UNUSED, NULL, CS_UNUSED, CS_UNUSED));
     123             : 
     124          10 :         check_call(ct_send, (cmd2));
     125             : 
     126          38 :         while ((results_ret = ct_results(cmd2, &result_type)) == CS_SUCCEED) {
     127          18 :                 switch ((int) result_type) {
     128             : 
     129           0 :                 case CS_CMD_FAIL:
     130           0 :                         fprintf(stderr, "ct_results failed\n");
     131           0 :                         return 1;
     132             :                 case CS_CMD_SUCCEED:
     133             :                 case CS_CMD_DONE:
     134             :                 case CS_STATUS_RESULT:
     135             :                         break;
     136             : 
     137          10 :                 case CS_CURSOR_RESULT:
     138          10 :                         check_call(ct_res_info, (cmd2, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
     139             : 
     140          10 :                         if (num_cols != 1) {
     141           0 :                                 fprintf(stderr, "unexpected num of columns =  %d \n", num_cols);
     142           0 :                                 return 1;
     143             :                         }
     144             : 
     145          10 :                         for (i = 0; i < num_cols; i++) {
     146             : 
     147             :                                 /* here we can finally test for the return status column */
     148          10 :                                 check_call(ct_describe, (cmd2, i + 1, &datafmt2));
     149             : 
     150          10 :                                 if (datafmt2.status & CS_RETURN) {
     151           0 :                                         printf("ct_describe() column %d \n", i);
     152             :                                 }
     153             : 
     154          10 :                                 datafmt2.datatype = CS_CHAR_TYPE;
     155          10 :                                 datafmt2.format = CS_FMT_NULLTERM;
     156          10 :                                 datafmt2.maxlength = 6;
     157          10 :                                 datafmt2.count = 1;
     158          10 :                                 datafmt2.locale = NULL;
     159          10 :                                 check_call(ct_bind, (cmd2, 1, &datafmt2, col2, &datalength, &ind));
     160             :                         }
     161             :                         break;
     162             : 
     163           0 :                 case CS_COMPUTE_RESULT:
     164           0 :                         fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
     165           0 :                         return 1;
     166           0 :                 default:
     167           0 :                         fprintf(stderr, "ct_results() unexpected result_type.\n");
     168           0 :                         return 1;
     169             :                 }
     170             :         }
     171             : 
     172             :         row_count = 0;
     173             : 
     174          40 :         while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
     175          10 :                || (ret == CS_ROW_FAIL)) {
     176             : 
     177          30 :                 ret2 = ct_fetch(cmd2, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count2);
     178          30 :                 if (ret == CS_SUCCEED && ret2 == CS_SUCCEED) {
     179          30 :                         printf("%s\t\t\t%s\n", col1, col2);
     180             :                 }
     181             :         }
     182             : 
     183          10 :         switch ((int) ret) {
     184             :         case CS_END_DATA:
     185             :                 break;
     186           0 :         case CS_ROW_FAIL:
     187           0 :                 fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
     188           0 :                 return 1;
     189           0 :         case CS_FAIL:
     190           0 :                 fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
     191           0 :                 return 1;
     192           0 :         default:
     193           0 :                 fprintf(stderr, "ct_fetch() unexpected return.\n");
     194           0 :                 return 1;
     195             :         }
     196             : 
     197             :         if (verbose) {
     198          10 :                 printf("closing first cursor on connection\n");
     199             :         }
     200             : 
     201          10 :         check_call(ct_cursor, (cmd, CS_CURSOR_CLOSE, NULL, CS_UNUSED, NULL, CS_UNUSED, CS_UNUSED));
     202             : 
     203          10 :         check_call(ct_send, (cmd));
     204             : 
     205          24 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
     206           4 :                 if (result_type == CS_CMD_FAIL) {
     207           0 :                         fprintf(stderr, "ct_results(2) result_type CS_CMD_FAIL.\n");
     208           0 :                         return 1;
     209             :                 }
     210             :         }
     211          10 :         if (results_ret != CS_END_RESULTS) {
     212           0 :                 fprintf(stderr, "ct_results() returned BAD.\n");
     213           0 :                 return 1;
     214             :         }
     215             : 
     216          10 :         check_call(ct_cursor, (cmd, CS_CURSOR_DEALLOC, NULL, CS_UNUSED, NULL, CS_UNUSED, CS_UNUSED));
     217             : 
     218          10 :         check_call(ct_send, (cmd));
     219             : 
     220          24 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
     221           4 :                 if (result_type == CS_CMD_FAIL) {
     222           0 :                         fprintf(stderr, "ct_results(3) result_type CS_CMD_FAIL.\n");
     223           0 :                         return 1;
     224             :                 }
     225             :         }
     226          10 :         if (results_ret != CS_END_RESULTS) {
     227           0 :                 fprintf(stderr, "ct_results() returned BAD.\n");
     228           0 :                 return 1;
     229             :         }
     230             : 
     231             :         if (verbose) {
     232          10 :                 printf("closing second cursor on connection\n");
     233             :         }
     234             : 
     235          10 :         check_call(ct_cursor, (cmd2, CS_CURSOR_CLOSE, NULL, CS_UNUSED, NULL, CS_UNUSED, CS_UNUSED));
     236             : 
     237          10 :         check_call(ct_send, (cmd2));
     238             : 
     239          24 :         while ((results_ret = ct_results(cmd2, &result_type)) == CS_SUCCEED) {
     240           4 :                 if (result_type == CS_CMD_FAIL) {
     241           0 :                         fprintf(stderr, "ct_results(2) result_type CS_CMD_FAIL.\n");
     242           0 :                         return 1;
     243             :                 }
     244             :         }
     245          10 :         if (results_ret != CS_END_RESULTS) {
     246           0 :                 fprintf(stderr, "ct_results() returned BAD.\n");
     247           0 :                 return 1;
     248             :         }
     249             : 
     250          10 :         check_call(ct_cursor, (cmd2, CS_CURSOR_DEALLOC, NULL, CS_UNUSED, NULL, CS_UNUSED, CS_UNUSED));
     251             : 
     252          10 :         check_call(ct_send, (cmd2));
     253             : 
     254          24 :         while ((results_ret = ct_results(cmd2, &result_type)) == CS_SUCCEED) {
     255           4 :                 if (result_type == CS_CMD_FAIL) {
     256           0 :                         fprintf(stderr, "ct_results(3) result_type CS_CMD_FAIL.\n");
     257           0 :                         return 1;
     258             :                 }
     259             :         }
     260          10 :         if (results_ret != CS_END_RESULTS) {
     261           0 :                 fprintf(stderr, "ct_results() returned BAD.\n");
     262           0 :                 return 1;
     263             :         }
     264             : 
     265          10 :         ct_cmd_drop(cmd2);
     266          10 :         ct_cmd_drop(cmd3);
     267             : 
     268             :         if (verbose) {
     269          10 :                 printf("Trying logout\n");
     270             :         }
     271          10 :         check_call(try_ctlogout, (ctx, conn, cmd, verbose));
     272             : 
     273             :         if (verbose) {
     274          10 :                 printf("Test succeded\n");
     275             :         }
     276          10 :         return 0;
     277             : }

Generated by: LCOV version 1.13