LCOV - code coverage report
Current view: top level - src/ctlib/unittests - data.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 56 64 87.5 %
Date: 2025-01-18 12:13:41 Functions: 1 1 100.0 %

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

Generated by: LCOV version 1.13