LCOV - code coverage report
Current view: top level - src/ctlib/unittests - ct_cursor.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 151 266 56.8 %
Date: 2025-04-21 16:22:06 Functions: 2 2 100.0 %

          Line data    Source code
       1             : #include "common.h"
       2             : 
       3             : static int update_second_table(CS_COMMAND * cmd2, char *value);
       4             : 
       5          10 : TEST_MAIN()
       6             : {
       7             :         CS_CONTEXT *ctx;
       8             :         CS_CONNECTION *conn;
       9             :         CS_COMMAND *cmd;
      10             :         CS_COMMAND *cmd2;
      11             :         CS_RETCODE ret;
      12             :         CS_RETCODE results_ret;
      13             :         CS_INT result_type;
      14          10 :         CS_INT count, row_count = 0;
      15             :         CS_DATAFMT datafmt;
      16             :         CS_SMALLINT ind;
      17          10 :         int verbose = 1;
      18             :         CS_CHAR name[3]; 
      19             :         CS_CHAR col1[6];
      20             :         CS_INT datalength;
      21             :         CS_CHAR text[128];
      22             :         CS_INT num_cols, i, j;
      23             :         CS_INT props_value;
      24             : 
      25          10 :         printf("%s: Testing ct_cursor()\n", __FILE__);
      26             : 
      27             :         if (verbose) {
      28          10 :                 printf("Trying login\n");
      29             :         }
      30          10 :         check_call(try_ctlogin, (&ctx, &conn, &cmd, verbose));
      31             : 
      32          10 :         check_call(ct_cmd_alloc, (conn, &cmd2));
      33             : 
      34          10 :         check_call(run_command, (cmd, "CREATE TABLE #test_table (col1 char(4))"));
      35          10 :         check_call(run_command, (cmd, "INSERT #test_table (col1) VALUES ('AAA')"));
      36          10 :         check_call(run_command, (cmd, "INSERT #test_table (col1) VALUES ('BBB')"));
      37          10 :         check_call(run_command, (cmd, "INSERT #test_table (col1) VALUES ('CCC')"));
      38             : 
      39          10 :         check_call(run_command, (cmd2, "CREATE TABLE #test_table2 (col1 char(4))"));
      40          10 :         check_call(run_command, (cmd2, "INSERT #test_table2 (col1) VALUES ('---')"));
      41             : 
      42             :         if (verbose) {
      43          10 :                 printf("Trying declare, rows , open in one SEND\n");
      44             :         }
      45             : 
      46          10 :         strcpy(text, "select col1 from #test_table where 1 = 1");
      47          10 :         strcpy(name, "c1");
      48             : 
      49          10 :         check_call(ct_cursor, (cmd, CS_CURSOR_DECLARE, name, CS_NULLTERM, text, CS_NULLTERM, CS_UNUSED));
      50             : 
      51          10 :         check_call(ct_cursor, (cmd, CS_CURSOR_ROWS, NULL, CS_UNUSED, NULL, CS_UNUSED, (CS_INT) 1));
      52             : 
      53          10 :         check_call(ct_cursor, (cmd, CS_CURSOR_OPEN, NULL, CS_UNUSED, NULL, CS_UNUSED, CS_UNUSED));
      54             : 
      55          10 :         check_call(ct_send, (cmd));
      56             : 
      57          58 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
      58          38 :                 switch ((int) result_type) {
      59             : 
      60             :                 case CS_CMD_SUCCEED:
      61             :                 case CS_CMD_DONE:
      62             :                 case CS_CMD_FAIL:
      63             :                 case CS_STATUS_RESULT:
      64             :                         break;
      65             : 
      66          10 :                 case CS_CURSOR_RESULT:
      67             : 
      68          10 :                         check_call(ct_cmd_props, (cmd, CS_GET, CS_CUR_STATUS, &props_value, sizeof(CS_INT), NULL)); 
      69          10 :                         if (props_value & CS_CURSTAT_DECLARED) {
      70           0 :                                 fprintf(stderr, "ct_cmd_props claims cursor is in DECLARED state when it should be OPEN\n");
      71           0 :                                 return 1;
      72             :                         }
      73          10 :                         if (!(props_value & CS_CURSTAT_OPEN)) {
      74           0 :                                 fprintf(stderr, "ct_cmd_props claims cursor is not in OPEN state when it should be \n");
      75           0 :                                 return 1;
      76             :                         }
      77          10 :                         if (props_value & CS_CURSTAT_CLOSED) {
      78           0 :                                 fprintf(stderr, "ct_cmd_props claims cursor is in CLOSED state when it should be OPEN\n");
      79           0 :                                 return 1;
      80             :                         }
      81             : 
      82          10 :                         check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
      83             : 
      84          10 :                         if (num_cols != 1) {
      85           0 :                                 fprintf(stderr, "unexpected num of columns =  %d \n", num_cols);
      86           0 :                                 return 1;
      87             :                         }
      88             : 
      89          10 :                         for (i = 0; i < num_cols; i++) {
      90             : 
      91             :                                 /* here we can finally test for the return status column */
      92          10 :                                 check_call(ct_describe, (cmd, i + 1, &datafmt));
      93             : 
      94          10 :                                 if (datafmt.status & CS_RETURN) {
      95           0 :                                         printf("ct_describe() column %d \n", i);
      96             :                                 }
      97             : 
      98          10 :                                 datafmt.datatype = CS_CHAR_TYPE;
      99          10 :                                 datafmt.format = CS_FMT_NULLTERM;
     100          10 :                                 datafmt.maxlength = 6;
     101          10 :                                 datafmt.count = 1;
     102          10 :                                 datafmt.locale = NULL;
     103          10 :                                 check_call(ct_bind, (cmd, 1, &datafmt, col1, &datalength, &ind));
     104             :                         }
     105             :                         row_count = 0;
     106          40 :                         while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
     107          10 :                                || (ret == CS_ROW_FAIL)) {
     108             : 
     109          30 :                                 if (row_count == 0) {
     110          10 :                                         for (j = 0; j < num_cols; j++) {
     111          10 :                                                 printf("\n%s\n", datafmt.name);
     112             :                                         }
     113          10 :                                         printf("------\n\n");
     114             :                                 }
     115             : 
     116          30 :                                 for (j = 0; j < num_cols; j++) {
     117          30 :                                         printf("%s\n\n", col1);
     118          30 :                                         row_count++;
     119             :                                 }
     120             : 
     121          30 :                                 ret = update_second_table(cmd2, col1);
     122          30 :                                 if (ret)
     123             :                                         return ret;
     124             :                         }
     125             : 
     126          10 :                         switch ((int) ret) {
     127             :                         case CS_END_DATA:
     128             :                                 break;
     129           0 :                         case CS_ROW_FAIL:
     130           0 :                                 fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
     131           0 :                                 return 1;
     132           0 :                         case CS_FAIL:
     133           0 :                                 fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
     134           0 :                                 return 1;
     135           0 :                         default:
     136           0 :                                 fprintf(stderr, "ct_fetch() unexpected return. %d\n", ret);
     137           0 :                                 return 1;
     138             :                         }
     139             :                         break;
     140             : 
     141           0 :                 case CS_COMPUTE_RESULT:
     142           0 :                         fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
     143           0 :                         return 1;
     144           0 :                 default:
     145           0 :                         fprintf(stderr, "ct_results() unexpected result_type.\n");
     146           0 :                         return 1;
     147             :                 }
     148             :         }
     149             : 
     150             : 
     151          10 :         check_call(ct_cursor, (cmd, CS_CURSOR_CLOSE, NULL, CS_UNUSED, NULL, CS_UNUSED, CS_DEALLOC));
     152             : 
     153          10 :         check_call(ct_send, (cmd));
     154             : 
     155          24 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
     156           4 :                 if (result_type == CS_CMD_FAIL) {
     157           0 :                         fprintf(stderr, "ct_results(2) result_type CS_CMD_FAIL.\n");
     158           0 :                         return 1;
     159             :                 }
     160             :         }
     161          10 :         if (results_ret != CS_END_RESULTS) {
     162           0 :                 fprintf(stderr, "ct_results() returned BAD.\n");
     163           0 :                 return 1;
     164             :         }
     165             : 
     166          10 :         check_call(ct_cmd_props, (cmd, CS_GET, CS_CUR_STATUS, &props_value, sizeof(CS_INT), NULL)); 
     167             : 
     168          10 :         if (props_value != CS_CURSTAT_NONE) {
     169           0 :                 fprintf(stderr, "ct_cmd_props() CS_CUR_STATUS != CS_CURSTAT_NONE \n");
     170           0 :                 return 1;
     171             :         }
     172             : 
     173             :         if (verbose) {
     174          10 :                 printf("Trying declare, rows, open one at a time \n");
     175             :         }
     176             : 
     177          10 :         strcpy(text, "select col1 from #test_table where 2 = 2");
     178             : 
     179          10 :         check_call(ct_cursor, (cmd, CS_CURSOR_DECLARE, name, 3, text, CS_NULLTERM, CS_UNUSED));
     180             : 
     181          10 :         check_call(ct_send, (cmd));
     182             : 
     183          24 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
     184           4 :                 if (result_type == CS_CMD_FAIL) {
     185           0 :                         fprintf(stderr, "ct_results(4) result_type CS_CMD_FAIL.\n");
     186           0 :                         return 1;
     187             :                 }
     188             :         }
     189          10 :         if (results_ret != CS_END_RESULTS) {
     190           0 :                 fprintf(stderr, "ct_results() returned BAD.\n");
     191           0 :                 return 1;
     192             :         }
     193             : 
     194          10 :         check_call(ct_cursor, (cmd, CS_CURSOR_ROWS, NULL, CS_UNUSED, NULL, CS_UNUSED, (CS_INT) 1));
     195             : 
     196          10 :         check_call(ct_send, (cmd));
     197             : 
     198          24 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
     199           4 :                 if (result_type == CS_CMD_FAIL) {
     200           0 :                         fprintf(stderr, "ct_results(5) result_type CS_CMD_FAIL.\n");
     201           0 :                         return 1;
     202             :                 }
     203             :         }
     204          10 :         if (results_ret != CS_END_RESULTS) {
     205           0 :                 fprintf(stderr, "ct_results() returned BAD.\n");
     206           0 :                 return 1;
     207             :         }
     208             : 
     209          10 :         check_call(ct_cursor, (cmd, CS_CURSOR_OPEN, NULL, CS_UNUSED, NULL, CS_UNUSED, CS_UNUSED));
     210             : 
     211          10 :         check_call(ct_send, (cmd));
     212             : 
     213          50 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
     214          30 :                 switch ((int) result_type) {
     215             : 
     216             :                 case CS_CMD_SUCCEED:
     217             :                         break;
     218             :                 case CS_CMD_DONE:
     219             :                         break;
     220           0 :                 case CS_CMD_FAIL:
     221           0 :                         fprintf(stderr, "ct_results(6) result_type CS_CMD_FAIL.\n");
     222           0 :                         break;
     223           0 :                 case CS_STATUS_RESULT:
     224           0 :                         printf("ct_results: CS_STATUS_RESULT detected for sp_who\n");
     225             : 
     226          10 :                 case CS_CURSOR_RESULT:
     227          10 :                         check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
     228             : 
     229          10 :                         if (num_cols != 1) {
     230           0 :                                 fprintf(stderr, "unexpected num of columns =  %d \n", num_cols);
     231           0 :                                 return 1;
     232             :                         }
     233             : 
     234          10 :                         for (i = 0; i < num_cols; i++) {
     235             : 
     236             :                                 /* here we can finally test for the return status column */
     237          10 :                                 check_call(ct_describe, (cmd, i + 1, &datafmt));
     238             : 
     239          10 :                                 if (datafmt.status & CS_RETURN) {
     240           0 :                                         printf("ct_describe() column %d \n", i);
     241             :                                 }
     242             : 
     243          10 :                                 datafmt.datatype = CS_CHAR_TYPE;
     244          10 :                                 datafmt.format = CS_FMT_NULLTERM;
     245          10 :                                 datafmt.maxlength = 6;
     246          10 :                                 datafmt.count = 1;
     247          10 :                                 datafmt.locale = NULL;
     248          10 :                                 check_call(ct_bind, (cmd, 1, &datafmt, col1, &datalength, &ind));
     249             :                         }
     250             :                         row_count = 0;
     251          40 :                         while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
     252          10 :                                || (ret == CS_ROW_FAIL)) {
     253             : 
     254          30 :                                 if (row_count == 0) {
     255          10 :                                         for (j = 0; j < num_cols; j++) {
     256          10 :                                                 printf("\n%s\n", datafmt.name);
     257             :                                         }
     258          10 :                                         printf("------\n\n");
     259             :                                 }
     260             : 
     261          30 :                                 for (j = 0; j < num_cols; j++) {
     262          30 :                                         printf("%s\n\n", col1);
     263          30 :                                         row_count++;
     264             :                                 }
     265             :                         }
     266             : 
     267             : 
     268          10 :                         switch ((int) ret) {
     269             :                         case CS_END_DATA:
     270             :                                 break;
     271           0 :                         case CS_ROW_FAIL:
     272           0 :                                 fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
     273           0 :                                 return 1;
     274           0 :                         case CS_FAIL:
     275           0 :                                 fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
     276           0 :                                 return 1;
     277           0 :                         default:
     278           0 :                                 fprintf(stderr, "ct_fetch() unexpected return. %d\n", ret);
     279           0 :                                 return 1;
     280             :                         }
     281             :                         break;
     282             : 
     283           0 :                 case CS_COMPUTE_RESULT:
     284           0 :                         fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
     285           0 :                         return 1;
     286           0 :                 default:
     287           0 :                         fprintf(stderr, "ct_results() unexpected result_type.\n");
     288           0 :                         return 1;
     289             :                 }
     290             :         }
     291             : 
     292             : 
     293          10 :         check_call(ct_cursor, (cmd, CS_CURSOR_CLOSE, NULL, CS_UNUSED, NULL, CS_UNUSED, CS_UNUSED));
     294             : 
     295          10 :         check_call(ct_send, (cmd));
     296             : 
     297          24 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
     298           4 :                 if (result_type == CS_CMD_FAIL) {
     299           0 :                         fprintf(stderr, "ct_results(7) result_type CS_CMD_FAIL.\n");
     300           0 :                         return 1;
     301             :                 }
     302             :         }
     303          10 :         if (results_ret != CS_END_RESULTS) {
     304           0 :                 fprintf(stderr, "ct_results() returned BAD.\n");
     305           0 :                 return 1;
     306             :         }
     307          10 :         check_call(ct_cursor, (cmd, CS_CURSOR_DEALLOC, NULL, CS_UNUSED, NULL, CS_UNUSED, CS_UNUSED));
     308             : 
     309          10 :         check_call(ct_send, (cmd));
     310             : 
     311          24 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
     312           4 :                 if (result_type == CS_CMD_FAIL) {
     313           0 :                         fprintf(stderr, "ct_results(8) result_type CS_CMD_FAIL.\n");
     314           0 :                         return 1;
     315             :                 }
     316             :         }
     317          10 :         if (results_ret != CS_END_RESULTS) {
     318           0 :                 fprintf(stderr, "ct_results() returned BAD.\n");
     319           0 :                 return 1;
     320             :         }
     321             : 
     322             :         if (verbose) {
     323          10 :                 printf("Running normal select command after cursor operations\n");
     324             :         }
     325             : 
     326          10 :         check_call(ct_command, (cmd, CS_LANG_CMD, "select col1 from #test_table", CS_NULLTERM, CS_UNUSED));
     327          10 :         check_call(ct_send, (cmd));
     328          40 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
     329          20 :                 switch ((int) result_type) {
     330             :                 case CS_CMD_SUCCEED:
     331             :                         break;
     332             :                 case CS_CMD_DONE:
     333             :                         break;
     334           0 :                 case CS_CMD_FAIL:
     335           0 :                         fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
     336           0 :                         return 1;
     337          10 :                 case CS_ROW_RESULT:
     338          10 :                         datafmt.datatype = CS_CHAR_TYPE;
     339          10 :                         datafmt.format = CS_FMT_NULLTERM;
     340          10 :                         datafmt.maxlength = 6;
     341          10 :                         datafmt.count = 1;
     342          10 :                         datafmt.locale = NULL;
     343          10 :                         check_call(ct_bind, (cmd, 1, &datafmt, col1, &datalength, &ind));
     344             : 
     345          50 :                         while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
     346          10 :                                || (ret == CS_ROW_FAIL)) {
     347          30 :                                 row_count += count;
     348          30 :                                 if (ret == CS_ROW_FAIL) {
     349           0 :                                         fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
     350           0 :                                         return 1;
     351          30 :                                 } else if (ret == CS_SUCCEED) {
     352             :                                         ;
     353             :                                 } else {
     354             :                                         break;
     355             :                                 }
     356             :                         }
     357          10 :                         switch ((int) ret) {
     358             :                         case CS_END_DATA:
     359             :                                 break;
     360           0 :                         case CS_FAIL:
     361           0 :                                 fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
     362           0 :                                 return 1;
     363           0 :                         default:
     364           0 :                                 fprintf(stderr, "ct_fetch() unexpected return.%d\n", ret);
     365           0 :                                 return 1;
     366             :                         }
     367             :                         break;
     368           0 :                 case CS_COMPUTE_RESULT:
     369           0 :                         fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
     370           0 :                         return 1;
     371           0 :                 default:
     372           0 :                         fprintf(stderr, "ct_results() unexpected result_type. %d\n", result_type);
     373           0 :                         return 1;
     374             :                 }
     375             :         }
     376          10 :         switch ((int) results_ret) {
     377             :         case CS_END_RESULTS:
     378             :                 break;
     379           0 :         case CS_FAIL:
     380           0 :                 fprintf(stderr, "ct_results() failed.\n");
     381           0 :                 return 1;
     382             :                 break;
     383           0 :         default:
     384           0 :                 fprintf(stderr, "ct_results() unexpected return.\n");
     385           0 :                 return 1;
     386             :         }
     387             :         if (verbose) {
     388          10 :                 printf("Trying logout\n");
     389             :         }
     390             : 
     391          10 :         ct_cmd_drop(cmd2);
     392             : 
     393          10 :         check_call(try_ctlogout, (ctx, conn, cmd, verbose));
     394             : 
     395             :         if (verbose) {
     396          10 :                 printf("Test suceeded\n");
     397             :         }
     398          10 :         return 0;
     399             : }
     400             : 
     401             : static int
     402          30 : update_second_table(CS_COMMAND * cmd2, char *value)
     403             : {
     404             : 
     405             :         CS_RETCODE ret;
     406             :         CS_RETCODE results_ret;
     407             :         CS_INT result_type;
     408          30 :         CS_INT count, row_count = 0;
     409             :         CS_DATAFMT datafmt;
     410             :         CS_SMALLINT ind;
     411             :         CS_CHAR col1[6];
     412             :         CS_INT datalength;
     413             :         CS_CHAR text[128];
     414             : 
     415          30 :         sprintf(text, "update #test_table2 set col1 = '%s' ", value);
     416          30 :         check_call(run_command, (cmd2, text));
     417             : 
     418          30 :         check_call(ct_command, (cmd2, CS_LANG_CMD, "select col1 from #test_table2", CS_NULLTERM, CS_UNUSED));
     419          30 :         check_call(ct_send, (cmd2));
     420         120 :         while ((results_ret = ct_results(cmd2, &result_type)) == CS_SUCCEED) {
     421          60 :                 switch ((int) result_type) {
     422             :                 case CS_CMD_SUCCEED:
     423             :                         break;
     424             :                 case CS_CMD_DONE:
     425             :                         break;
     426           0 :                 case CS_CMD_FAIL:
     427           0 :                         fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
     428           0 :                         return 1;
     429          30 :                 case CS_ROW_RESULT:
     430          30 :                         datafmt.datatype = CS_CHAR_TYPE;
     431          30 :                         datafmt.format = CS_FMT_NULLTERM;
     432          30 :                         datafmt.maxlength = 6;
     433          30 :                         datafmt.count = 1;
     434          30 :                         datafmt.locale = NULL;
     435          30 :                         check_call(ct_bind, (cmd2, 1, &datafmt, col1, &datalength, &ind));
     436             : 
     437          90 :                         while (((ret = ct_fetch(cmd2, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
     438          30 :                                || (ret == CS_ROW_FAIL)) {
     439          30 :                                 row_count += count;
     440          30 :                                 if (ret == CS_ROW_FAIL) {
     441           0 :                                         fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
     442           0 :                                         return 1;
     443          30 :                                 } else if (ret == CS_SUCCEED) {
     444             :                                         ;
     445             :                                 } else {
     446             :                                         break;
     447             :                                 }
     448             :                         }
     449          30 :                         switch ((int) ret) {
     450             :                         case CS_END_DATA:
     451             :                                 break;
     452           0 :                         case CS_FAIL:
     453           0 :                                 fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
     454           0 :                                 return 1;
     455           0 :                         default:
     456           0 :                                 fprintf(stderr, "ct_fetch() unexpected return.%d\n", ret);
     457           0 :                                 return 1;
     458             :                         }
     459             :                         break;
     460           0 :                 case CS_COMPUTE_RESULT:
     461           0 :                         fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
     462           0 :                         return 1;
     463           0 :                 default:
     464           0 :                         fprintf(stderr, "ct_results() unexpected result_type. %d\n", result_type);
     465           0 :                         return 1;
     466             :                 }
     467             :         }
     468          30 :         switch ((int) results_ret) {
     469             :         case CS_END_RESULTS:
     470             :                 break;
     471           0 :         case CS_FAIL:
     472           0 :                 fprintf(stderr, "ct_results() failed.\n");
     473           0 :                 return 1;
     474             :                 break;
     475           0 :         default:
     476           0 :                 fprintf(stderr, "ct_results() unexpected return.\n");
     477           0 :                 return 1;
     478             :         }
     479             :         return 0;
     480             : }
     481             : 

Generated by: LCOV version 1.13