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

Generated by: LCOV version 1.13