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-04-26 20:22:58 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          10 :                 switch (tds_version) {
      36           4 :                 case CS_TDS_70:
      37             :                 case CS_TDS_71:
      38           4 :                         fprintf(stderr, "This TDS version does not support XML.\n");
      39           4 :                         try_ctlogout(ctx, conn, cmd, verbose);
      40           4 :                         return 0;
      41             :                 }
      42             :         }
      43             : 
      44           6 :         select = "select cast('<a b=\"aaa\"><b>ciao</b>hi</a>' as xml) as name";
      45           6 :         printf("%s\n", select);
      46             : 
      47           6 :         check_call(ct_command, (cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED));
      48             : 
      49           6 :         check_call(ct_send, (cmd));
      50             : 
      51           6 :         check_call(ct_results, (cmd, &result_type));
      52             : 
      53           6 :         switch (result_type) {
      54           2 :         case CS_CMD_FAIL:
      55           2 :                 fprintf(stderr, "ct_results() result_type CS_CMD_FAIL, probably not MSSQL.\n");
      56           2 :                 try_ctlogout(ctx, conn, cmd, verbose);
      57           2 :                 return 0;
      58             :         case CS_ROW_RESULT:
      59             :                 break;
      60           0 :         default:
      61           0 :                 fprintf(stderr, "ct_results() unexpected return %d.\n", result_type);
      62             :                 goto Cleanup;
      63             :         }
      64             : 
      65           4 :         check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
      66           4 :         assert(num_cols == 1);
      67             : 
      68           4 :         check_call(ct_describe, (cmd, 1, &datafmt));
      69             : 
      70           4 :         assert(strcmp(datafmt.name, "name") == 0);
      71           4 :         assert(datafmt.datatype == CS_LONGCHAR_TYPE);
      72           4 :         assert(datafmt.maxlength == 0x7fffffff);
      73           4 :         assert(datafmt.scale == 0);
      74           4 :         assert(datafmt.precision == 0);
      75             : 
      76           4 :         datafmt.format = CS_FMT_NULLTERM;
      77           4 :         datafmt.maxlength = 100;
      78             : 
      79           4 :         printf("binding column 1\n");
      80           4 :         check_call(ct_bind, (cmd, 1, &datafmt, buffer, &copied, &ind));
      81             : 
      82           4 :         printf("fetching rows.\n");
      83          12 :         while ((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED) {
      84           4 :                 fprintf(stderr, "ct_fetch() returned %d.\n", (int) ret);
      85           4 :                 buffer[copied] = '\0';
      86           4 :                 fprintf(stderr, "copied %d bytes: [%s]\n", copied, buffer);
      87             :         }
      88           4 :         assert(ret == CS_END_DATA);
      89             : 
      90             :         do {
      91           8 :                 ret = ct_results(cmd, &result_type);
      92           8 :         } while (ret == CS_SUCCEED);
      93             : 
      94           4 :         assert(ret == CS_END_RESULTS);
      95             : 
      96           4 :         if (verbose) {
      97           0 :                 printf("Trying logout\n");
      98             :         }
      99           4 :         check_call(try_ctlogout, (ctx, conn, cmd, verbose));
     100             : 
     101           4 :         return 0;
     102             : 
     103           0 : Cleanup:
     104           0 :         try_ctlogout(ctx, conn, cmd, verbose);
     105           0 :         return 1;
     106             : }

Generated by: LCOV version 1.13