LCOV - code coverage report
Current view: top level - src/ctlib/unittests - data.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 49 55 89.1 %
Date: 2025-01-18 11:50:39 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           8 : main(int argc, char *argv[])
       6             : {
       7             :         CS_CONTEXT *ctx;
       8             :         CS_CONNECTION *conn;
       9             :         CS_COMMAND *cmd;
      10           8 :         int verbose = 0;
      11             : 
      12             :         CS_RETCODE ret;
      13             :         CS_INT result_type;
      14             :         CS_INT num_cols;
      15             : 
      16             :         CS_DATAFMT datafmt;
      17           8 :         CS_INT copied = 0;
      18           8 :         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           8 :         printf("%s: test data from server\n", __FILE__);
      28             :         if (verbose) {
      29             :                 printf("Trying login\n");
      30             :         }
      31           8 :         check_call(try_ctlogin_with_options, (argc, argv, &ctx, &conn, &cmd, verbose));
      32           8 :         verbose += common_pwd.fverbose;
      33             : 
      34           8 :         ret = ct_con_props(conn, CS_GET, CS_TDS_VERSION, &tds_version, CS_UNUSED, NULL);
      35           8 :         if (ret == CS_SUCCEED) {
      36           8 :                 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           4 :         select = "select cast('<a b=\"aaa\"><b>ciao</b>hi</a>' as xml) as name";
      46           4 :         printf("%s\n", select);
      47             : 
      48           4 :         check_call(ct_command, (cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED));
      49             : 
      50           4 :         check_call(ct_send, (cmd));
      51             : 
      52           4 :         check_call(ct_results, (cmd, &result_type));
      53             : 
      54           4 :         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           2 :         check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
      67           2 :         assert(num_cols == 1);
      68             : 
      69           2 :         check_call(ct_describe, (cmd, 1, &datafmt));
      70             : 
      71           2 :         assert(strcmp(datafmt.name, "name") == 0);
      72           2 :         assert(datafmt.datatype == CS_LONGCHAR_TYPE);
      73           2 :         assert(datafmt.maxlength == 0x7fffffff);
      74           2 :         assert(datafmt.scale == 0);
      75           2 :         assert(datafmt.precision == 0);
      76             : 
      77           2 :         datafmt.format = CS_FMT_NULLTERM;
      78           2 :         datafmt.maxlength = 100;
      79             : 
      80           2 :         printf("binding column 1\n");
      81           2 :         check_call(ct_bind, (cmd, 1, &datafmt, buffer, &copied, &ind));
      82             : 
      83           2 :         printf("fetching rows.\n");
      84           6 :         while ((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED) {
      85           2 :                 fprintf(stderr, "ct_fetch() returned %d.\n", (int) ret);
      86           2 :                 buffer[copied] = '\0';
      87           2 :                 fprintf(stderr, "copied %d bytes: [%s]\n", copied, buffer);
      88             :         }
      89           2 :         assert(ret == CS_END_DATA);
      90             : 
      91             :         do {
      92           4 :                 ret = ct_results(cmd, &result_type);
      93           4 :         } while (ret == CS_SUCCEED);
      94             : 
      95           2 :         ret = ct_results(cmd, &result_type);
      96           2 :         assert(ret == CS_END_RESULTS);
      97             : 
      98           2 :         if (verbose) {
      99           0 :                 printf("Trying logout\n");
     100             :         }
     101           2 :         check_call(try_ctlogout, (ctx, conn, cmd, verbose));
     102             : 
     103           2 :         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