LCOV - code coverage report
Current view: top level - src/ctlib/unittests - array_bind.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 49 72 68.1 %
Date: 2025-01-18 11:50:39 Functions: 1 1 100.0 %

          Line data    Source code
       1             : #include "common.h"
       2             : 
       3             : /* Testing: array binding of result set */
       4             : int
       5           8 : main(void)
       6             : {
       7             :         CS_CONTEXT *ctx;
       8             :         CS_CONNECTION *conn;
       9             :         CS_COMMAND *cmd;
      10           8 :         int verbose = 0;
      11             : 
      12             :         CS_RETCODE ret;
      13             :         CS_RETCODE results_ret;
      14             :         CS_INT result_type;
      15             :         CS_INT num_cols;
      16             : 
      17             :         CS_DATAFMT datafmt;
      18             :         CS_INT datalength[2];
      19             :         CS_SMALLINT ind[2];
      20           8 :         CS_INT count, row_count = 0;
      21             :         CS_INT cv;
      22             : 
      23             :         CS_CHAR select[1024];
      24             : 
      25             :         CS_INT col1[2];
      26             :         CS_CHAR col2[2][5];
      27             :         CS_CHAR col3[2][32];
      28             : 
      29             : 
      30           8 :         printf("%s: Retrieve data using array binding \n", __FILE__);
      31             :         if (verbose) {
      32             :                 printf("Trying login\n");
      33             :         }
      34           8 :         check_call(try_ctlogin, (&ctx, &conn, &cmd, verbose));
      35             : 
      36           8 :         check_call(run_command, (cmd, "CREATE TABLE #ctlibarray (col1 int not null,  col2 char(4) not null, col3 datetime not null)"));
      37           8 :         check_call(run_command, (cmd, "insert into #ctlibarray values (1, 'AAAA', 'Jan  1 2002 10:00:00AM')"));
      38           8 :         check_call(run_command, (cmd, "insert into #ctlibarray values (2, 'BBBB', 'Jan  2 2002 10:00:00AM')"));
      39           8 :         check_call(run_command, (cmd, "insert into #ctlibarray values (3, 'CCCC', 'Jan  3 2002 10:00:00AM')"));
      40           8 :         check_call(run_command, (cmd, "insert into #ctlibarray values (8, 'DDDD', 'Jan  4 2002 10:00:00AM')"));
      41           8 :         check_call(run_command, (cmd, "insert into #ctlibarray values (9, 'EEEE', 'Jan  5 2002 10:00:00AM')"));
      42             : 
      43             : 
      44           8 :         strcpy(select, "select col1, col2, col3 from #ctlibarray order by col1 ");
      45             : 
      46           8 :         check_call(ct_command, (cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED));
      47             : 
      48           8 :         check_call(ct_send, (cmd));
      49             : 
      50          32 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
      51          16 :                 switch ((int) result_type) {
      52             :                 case CS_CMD_SUCCEED:
      53             :                         break;
      54             :                 case CS_CMD_DONE:
      55             :                         break;
      56           0 :                 case CS_CMD_FAIL:
      57           0 :                         fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
      58           0 :                         return 1;
      59           8 :                 case CS_ROW_RESULT:
      60             : 
      61           8 :                         check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
      62           8 :                         if (num_cols != 3) {
      63           0 :                                 fprintf(stderr, "num_cols %d != 3", num_cols);
      64           0 :                                 return 1;
      65             :                         }
      66             : 
      67           8 :                         check_call(ct_describe, (cmd, 1, &datafmt));
      68           8 :                         datafmt.format = CS_FMT_UNUSED;
      69           8 :                         if (datafmt.maxlength > 1024) {
      70           0 :                                 datafmt.maxlength = 1024;
      71             :                         }
      72             : 
      73           8 :                         datafmt.count = 2;
      74             : 
      75           8 :                         check_call(ct_bind, (cmd, 1, &datafmt, &col1[0], datalength, ind));
      76             : 
      77           8 :                         check_call(ct_describe, (cmd, 2, &datafmt));
      78             : 
      79           8 :                         datafmt.format = CS_FMT_NULLTERM;
      80           8 :                         datafmt.maxlength = 5;
      81           8 :                         datafmt.count = 2;
      82             : 
      83           8 :                         check_call(ct_bind, (cmd, 2, &datafmt, &col2[0], datalength, ind));
      84             : 
      85           8 :                         check_call(ct_describe, (cmd, 3, &datafmt));
      86             : 
      87           8 :                         datafmt.datatype = CS_CHAR_TYPE;
      88           8 :                         datafmt.format = CS_FMT_NULLTERM;
      89           8 :                         datafmt.maxlength = 32;
      90           8 :                         datafmt.count = 2;
      91             : 
      92           8 :                         check_call(ct_bind, (cmd, 3, &datafmt, &col3[0], datalength, ind));
      93             : 
      94           8 :                         count = 0;
      95          40 :                         while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
      96           8 :                                || (ret == CS_ROW_FAIL)) {
      97          24 :                                 row_count += count;
      98          24 :                                 if (ret == CS_ROW_FAIL) {
      99           0 :                                         fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
     100           0 :                                         return 1;
     101             :                                 } else {        /* ret == CS_SUCCEED */
     102          24 :                                         printf("ct_fetch returned %d rows\n", count);
     103          64 :                                         for (cv = 0; cv < count; cv++)
     104          40 :                                                 printf("col1 = %d col2= '%s', col3 = '%s'\n", col1[cv], col2[cv],
     105          40 :                                                         col3[cv]);
     106             :                                 }
     107          24 :                                 count = 0;
     108             :                         }
     109             : 
     110             : 
     111           8 :                         switch ((int) ret) {
     112             :                         case CS_END_DATA:
     113             :                                 break;
     114           0 :                         case CS_FAIL:
     115           0 :                                 fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
     116           0 :                                 return 1;
     117           0 :                         default:
     118           0 :                                 fprintf(stderr, "ct_fetch() unexpected return.\n");
     119           0 :                                 return 1;
     120             :                         }
     121             :                         break;
     122             : 
     123           0 :                 default:
     124           0 :                         fprintf(stderr, "ct_results() unexpected result_type.\n");
     125           0 :                         return 1;
     126             :                 }
     127             :         }
     128           8 :         switch ((int) results_ret) {
     129             :         case CS_END_RESULTS:
     130             :                 break;
     131           0 :         case CS_FAIL:
     132           0 :                 fprintf(stderr, "ct_results() failed.\n");
     133           0 :                 return 1;
     134             :                 break;
     135           0 :         default:
     136           0 :                 fprintf(stderr, "ct_results() unexpected return.\n");
     137           0 :                 return 1;
     138             :         }
     139             : 
     140             :         if (verbose) {
     141             :                 printf("Trying logout\n");
     142             :         }
     143           8 :         check_call(try_ctlogout, (ctx, conn, cmd, verbose));
     144             : 
     145           8 :         return 0;
     146             : }

Generated by: LCOV version 1.13