LCOV - code coverage report
Current view: top level - src/ctlib/unittests - data.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 48 54 88.9 %
Date: 2025-02-21 09:36:06 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* Test some data from server. Currently tests MS XML type */
       2             : #include "common.h"
       3             : 
       4             : int
       5          10 : main(int argc, char *argv[])
       6             : {
       7             :         CS_CONTEXT *ctx;
       8             :         CS_CONNECTION *conn;
       9             :         CS_COMMAND *cmd;
      10          10 :         int verbose = 0;
      11             : 
      12             :         CS_RETCODE ret;
      13             :         CS_INT result_type;
      14             :         CS_INT num_cols;
      15             : 
      16             :         CS_DATAFMT datafmt;
      17          10 :         CS_INT copied = 0;
      18          10 :         CS_SMALLINT ind = 0;
      19             :         CS_INT count;
      20             : 
      21             :         const char *select;
      22             : 
      23             :         char buffer[128];
      24             : 
      25             :         int tds_version;
      26             : 
      27          10 :         printf("%s: test data from server\n", __FILE__);
      28             :         if (verbose) {
      29             :                 printf("Trying login\n");
      30             :         }
      31          10 :         check_call(try_ctlogin_with_options, (argc, argv, &ctx, &conn, &cmd, verbose));
      32          10 :         verbose += common_pwd.fverbose;
      33             : 
      34          10 :         ret = ct_con_props(conn, CS_GET, CS_TDS_VERSION, &tds_version, CS_UNUSED, NULL);
      35          10 :         if (ret == CS_SUCCEED) {
      36          10 :                 switch (tds_version) {
      37           4 :                 case CS_TDS_70:
      38             :                 case CS_TDS_71:
      39           4 :                         fprintf(stderr, "This TDS version does not support XML.\n");
      40           4 :                         try_ctlogout(ctx, conn, cmd, verbose);
      41           4 :                         return 0;
      42             :                 }
      43             :         }
      44             : 
      45           6 :         select = "select cast('<a b=\"aaa\"><b>ciao</b>hi</a>' as xml) as name";
      46           6 :         printf("%s\n", select);
      47             : 
      48           6 :         check_call(ct_command, (cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED));
      49             : 
      50           6 :         check_call(ct_send, (cmd));
      51             : 
      52           6 :         check_call(ct_results, (cmd, &result_type));
      53             : 
      54           6 :         switch (result_type) {
      55           2 :         case CS_CMD_FAIL:
      56           2 :                 fprintf(stderr, "ct_results() result_type CS_CMD_FAIL, probably not MSSQL.\n");
      57           2 :                 try_ctlogout(ctx, conn, cmd, verbose);
      58           2 :                 return 0;
      59             :         case CS_ROW_RESULT:
      60             :                 break;
      61           0 :         default:
      62           0 :                 fprintf(stderr, "ct_results() unexpected return %d.\n", result_type);
      63             :                 goto Cleanup;
      64             :         }
      65             : 
      66           4 :         check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
      67           4 :         assert(num_cols == 1);
      68             : 
      69           4 :         check_call(ct_describe, (cmd, 1, &datafmt));
      70             : 
      71           4 :         assert(strcmp(datafmt.name, "name") == 0);
      72           4 :         assert(datafmt.datatype == CS_LONGCHAR_TYPE);
      73           4 :         assert(datafmt.maxlength == 0x7fffffff);
      74           4 :         assert(datafmt.scale == 0);
      75           4 :         assert(datafmt.precision == 0);
      76             : 
      77           4 :         datafmt.format = CS_FMT_NULLTERM;
      78           4 :         datafmt.maxlength = 100;
      79             : 
      80           4 :         printf("binding column 1\n");
      81           4 :         check_call(ct_bind, (cmd, 1, &datafmt, buffer, &copied, &ind));
      82             : 
      83           4 :         printf("fetching rows.\n");
      84          12 :         while ((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED) {
      85           4 :                 fprintf(stderr, "ct_fetch() returned %d.\n", (int) ret);
      86           4 :                 buffer[copied] = '\0';
      87           4 :                 fprintf(stderr, "copied %d bytes: [%s]\n", copied, buffer);
      88             :         }
      89           4 :         assert(ret == CS_END_DATA);
      90             : 
      91             :         do {
      92           8 :                 ret = ct_results(cmd, &result_type);
      93           8 :         } while (ret == CS_SUCCEED);
      94             : 
      95           4 :         assert(ret == CS_END_RESULTS);
      96             : 
      97           4 :         if (verbose) {
      98           0 :                 printf("Trying logout\n");
      99             :         }
     100           4 :         check_call(try_ctlogout, (ctx, conn, cmd, verbose));
     101             : 
     102           4 :         return 0;
     103             : 
     104           0 : Cleanup:
     105           0 :         try_ctlogout(ctx, conn, cmd, verbose);
     106           0 :         return 1;
     107             : }

Generated by: LCOV version 1.13