LCOV - code coverage report
Current view: top level - src/ctlib/unittests - array_bind.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 65 110 59.1 %
Date: 2024-04-18 21:21:48 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(int argc, char *argv[])
      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 :         ret = try_ctlogin(&ctx, &conn, &cmd, verbose);
      45           8 :         if (ret != CS_SUCCEED) {
      46           0 :                 fprintf(stderr, "Login failed\n");
      47           0 :                 return 1;
      48             :         }
      49             : 
      50           8 :         ret = run_command(cmd, "CREATE TABLE #ctlibarray (col1 int not null,  col2 char(4) not null, col3 datetime not null)");
      51           8 :         if (ret != CS_SUCCEED)
      52             :                 return 1;
      53             : 
      54           8 :         ret = run_command(cmd, "insert into #ctlibarray values (1, 'AAAA', 'Jan  1 2002 10:00:00AM')");
      55           8 :         if (ret != CS_SUCCEED)
      56             :                 return 1;
      57             : 
      58           8 :         ret = run_command(cmd, "insert into #ctlibarray values (2, 'BBBB', 'Jan  2 2002 10:00:00AM')");
      59           8 :         if (ret != CS_SUCCEED)
      60             :                 return 1;
      61             : 
      62           8 :         ret = run_command(cmd, "insert into #ctlibarray values (3, 'CCCC', 'Jan  3 2002 10:00:00AM')");
      63           8 :         if (ret != CS_SUCCEED)
      64             :                 return 1;
      65             : 
      66           8 :         ret = run_command(cmd, "insert into #ctlibarray values (8, 'DDDD', 'Jan  4 2002 10:00:00AM')");
      67           8 :         if (ret != CS_SUCCEED)
      68             :                 return 1;
      69             : 
      70           8 :         ret = run_command(cmd, "insert into #ctlibarray values (9, 'EEEE', 'Jan  5 2002 10:00:00AM')");
      71           8 :         if (ret != CS_SUCCEED)
      72             :                 return 1;
      73             : 
      74             : 
      75           8 :         strcpy(select, "select col1, col2, col3 from #ctlibarray order by col1 ");
      76             : 
      77           8 :         ret = ct_command(cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED);
      78             : 
      79           8 :         if (ret != CS_SUCCEED) {
      80           0 :                 fprintf(stderr, "ct_command(%s) failed\n", select);
      81           0 :                 return 1;
      82             :         }
      83             : 
      84           8 :         ret = ct_send(cmd);
      85           8 :         if (ret != CS_SUCCEED) {
      86           0 :                 fprintf(stderr, "ct_send() failed\n");
      87           0 :                 return 1;
      88             :         }
      89             : 
      90          24 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
      91          16 :                 switch ((int) result_type) {
      92             :                 case CS_CMD_SUCCEED:
      93             :                         break;
      94             :                 case CS_CMD_DONE:
      95             :                         break;
      96           0 :                 case CS_CMD_FAIL:
      97           0 :                         fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
      98           0 :                         return 1;
      99           8 :                 case CS_ROW_RESULT:
     100             : 
     101           8 :                         ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);
     102           8 :                         if (ret != CS_SUCCEED) {
     103           0 :                                 fprintf(stderr, "ct_res_info() failed");
     104           0 :                                 return 1;
     105             :                         }
     106           8 :                         if (num_cols != 3) {
     107           0 :                                 fprintf(stderr, "num_cols %d != 3", num_cols);
     108           0 :                                 return 1;
     109             :                         }
     110             : 
     111           8 :                         ret = ct_describe(cmd, 1, &datafmt);
     112           8 :                         if (ret != CS_SUCCEED) {
     113           0 :                                 fprintf(stderr, "ct_describe() failed");
     114           0 :                                 return 1;
     115             :                         }
     116           8 :                         datafmt.format = CS_FMT_UNUSED;
     117           8 :                         if (datafmt.maxlength > 1024) {
     118           0 :                                 datafmt.maxlength = 1024;
     119             :                         }
     120             : 
     121           8 :                         datafmt.count = 2;
     122             : 
     123           8 :                         ret = ct_bind(cmd, 1, &datafmt, &col1[0], datalength, ind);
     124           8 :                         if (ret != CS_SUCCEED) {
     125           0 :                                 fprintf(stderr, "ct_bind() failed\n");
     126           0 :                                 return 1;
     127             :                         }
     128             : 
     129           8 :                         ret = ct_describe(cmd, 2, &datafmt);
     130           8 :                         if (ret != CS_SUCCEED) {
     131           0 :                                 fprintf(stderr, "ct_describe() failed");
     132           0 :                                 return 1;
     133             :                         }
     134             : 
     135           8 :                         datafmt.format = CS_FMT_NULLTERM;
     136           8 :                         datafmt.maxlength = 5;
     137           8 :                         datafmt.count = 2;
     138             : 
     139           8 :                         ret = ct_bind(cmd, 2, &datafmt, &col2[0], datalength, ind);
     140           8 :                         if (ret != CS_SUCCEED) {
     141           0 :                                 fprintf(stderr, "ct_bind() failed\n");
     142           0 :                                 return 1;
     143             :                         }
     144             : 
     145           8 :                         ret = ct_describe(cmd, 3, &datafmt);
     146           8 :                         if (ret != CS_SUCCEED) {
     147           0 :                                 fprintf(stderr, "ct_describe() failed");
     148           0 :                                 return 1;
     149             :                         }
     150             : 
     151           8 :                         datafmt.datatype = CS_CHAR_TYPE;
     152           8 :                         datafmt.format = CS_FMT_NULLTERM;
     153           8 :                         datafmt.maxlength = 32;
     154           8 :                         datafmt.count = 2;
     155             : 
     156           8 :                         ret = ct_bind(cmd, 3, &datafmt, &col3[0], datalength, ind);
     157           8 :                         if (ret != CS_SUCCEED) {
     158           0 :                                 fprintf(stderr, "ct_bind() failed\n");
     159           0 :                                 return 1;
     160             :                         }
     161             : 
     162           8 :                         count = 0;
     163          40 :                         while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
     164           8 :                                || (ret == CS_ROW_FAIL)) {
     165          24 :                                 row_count += count;
     166          24 :                                 if (ret == CS_ROW_FAIL) {
     167           0 :                                         fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
     168           0 :                                         return 1;
     169             :                                 } else {        /* ret == CS_SUCCEED */
     170          24 :                                         printf("ct_fetch returned %d rows\n", count);
     171          64 :                                         for (cv = 0; cv < count; cv++)
     172          40 :                                                 printf("col1 = %d col2= '%s', col3 = '%s'\n", col1[cv], col2[cv],
     173          40 :                                                         col3[cv]);
     174             :                                 }
     175          24 :                                 count = 0;
     176             :                         }
     177             : 
     178             : 
     179           8 :                         switch ((int) ret) {
     180             :                         case CS_END_DATA:
     181             :                                 break;
     182           0 :                         case CS_FAIL:
     183           0 :                                 fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
     184           0 :                                 return 1;
     185           0 :                         default:
     186           0 :                                 fprintf(stderr, "ct_fetch() unexpected return.\n");
     187           0 :                                 return 1;
     188             :                         }
     189             :                         break;
     190             : 
     191           0 :                 default:
     192           0 :                         fprintf(stderr, "ct_results() unexpected result_type.\n");
     193           0 :                         return 1;
     194             :                 }
     195             :         }
     196           8 :         switch ((int) results_ret) {
     197             :         case CS_END_RESULTS:
     198             :                 break;
     199           0 :         case CS_FAIL:
     200           0 :                 fprintf(stderr, "ct_results() failed.\n");
     201           0 :                 return 1;
     202             :                 break;
     203           0 :         default:
     204           0 :                 fprintf(stderr, "ct_results() unexpected return.\n");
     205           0 :                 return 1;
     206             :         }
     207             : 
     208             :         if (verbose) {
     209             :                 printf("Trying logout\n");
     210             :         }
     211           8 :         ret = try_ctlogout(ctx, conn, cmd, verbose);
     212           8 :         if (ret != CS_SUCCEED) {
     213           0 :                 fprintf(stderr, "Logout failed\n");
     214           0 :                 return 1;
     215             :         }
     216             : 
     217             :         return 0;
     218             : }

Generated by: LCOV version 1.13