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: 2024-04-18 20:40:06 Functions: 1 1 100.0 %

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

Generated by: LCOV version 1.13