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-01-18 11:50:39 Functions: 1 1 100.0 %

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

Generated by: LCOV version 1.13