LCOV - code coverage report
Current view: top level - src/ctlib/unittests - variant.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 53 81 65.4 %
Date: 2025-07-02 09:40:27 Functions: 1 1 100.0 %

          Line data    Source code
       1             : #include "common.h"
       2             : 
       3             : /* Testing: Retrieving SQL_VARIANT */
       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_RETCODE results_ret;
      13             :         CS_INT result_type;
      14             :         CS_INT num_cols;
      15             : 
      16             :         CS_DATAFMT datafmt;
      17             :         CS_INT datalength;
      18             :         CS_SMALLINT ind;
      19          10 :         CS_INT count, row_count = 0;
      20             : 
      21             :         CS_CHAR select[1024];
      22             : 
      23             :         CS_CHAR col1[128];
      24             :         const char *expected[10];
      25          10 :         unsigned num_expected = 0;
      26             : 
      27          10 :         unsigned rows = 0;
      28             : 
      29          10 :         memset(expected, 0, sizeof(expected));
      30             : 
      31          10 :         printf("%s: Retrieve SQL_VARIANT column\n", __FILE__);
      32             :         if (verbose) {
      33             :                 printf("Trying login\n");
      34             :         }
      35          10 :         check_call(try_ctlogin, (&ctx, &conn, &cmd, verbose));
      36             : 
      37          10 :         strcpy(select, "CREATE TABLE #ctlib0009 (n int, col1 sql_variant null)");
      38             : 
      39          10 :         check_call(ct_command, (cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED));
      40             : 
      41          10 :         check_call(ct_send, (cmd));
      42             : 
      43          10 :         check_call(ct_results, (cmd, &result_type));
      44             : 
      45          10 :         switch (result_type) {
      46           2 :         case CS_CMD_FAIL:
      47           2 :                 fprintf(stderr, "ct_results() result_type CS_CMD_FAIL, probably not MSSQL.\n");
      48           2 :                 try_ctlogout(ctx, conn, cmd, verbose);
      49           2 :                 return 0;
      50             :         case CS_CMD_SUCCEED:
      51             :                 break;
      52           0 :         default:
      53           0 :                 fprintf(stderr, "ct_results() unexpected return %d.\n", result_type);
      54           0 :                 try_ctlogout(ctx, conn, cmd, verbose);
      55           0 :                 return 1;
      56             :         }
      57             : 
      58           8 :         check_call(run_command, (cmd, "insert into #ctlib0009 values (1, 123)"));
      59           8 :         expected[num_expected++] = "123";
      60             : 
      61           8 :         check_call(run_command, (cmd, "insert into #ctlib0009 values (2, NULL)"));
      62           8 :         expected[num_expected++] = "";
      63             : 
      64           8 :         check_call(run_command, (cmd, "insert into #ctlib0009 values (3, 'hello')"));
      65           8 :         expected[num_expected++] = "hello";
      66             : 
      67           8 :         check_call(run_command, (cmd, "insert into #ctlib0009 values (4, 123.456)"));
      68           8 :         expected[num_expected++] = "123.456";
      69             : 
      70           8 :         strcpy(select, "select col1 from #ctlib0009 order by n");
      71             : 
      72           8 :         check_call(ct_command, (cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED));
      73             : 
      74           8 :         check_call(ct_send, (cmd));
      75             : 
      76           8 :         ct_callback(ctx, NULL, CS_SET, CS_SERVERMSG_CB, (CS_VOID *) servermsg_cb);
      77           8 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
      78          16 :                 printf("ct_results returned %s type\n", res_type_str(result_type));
      79          16 :                 switch ((int) result_type) {
      80             :                 case CS_CMD_SUCCEED:
      81             :                         break;
      82             :                 case CS_CMD_DONE:
      83             :                         break;
      84           0 :                 case CS_CMD_FAIL:
      85           0 :                         fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
      86           0 :                         return 1;
      87           8 :                 case CS_ROW_RESULT:
      88           8 :                         check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
      89           8 :                         if (num_cols != 1) {
      90           0 :                                 fprintf(stderr, "num_cols %d != 1", num_cols);
      91           0 :                                 return 1;
      92             :                         }
      93             : 
      94           8 :                         check_call(ct_describe, (cmd, 1, &datafmt));
      95           8 :                         datafmt.format = CS_FMT_UNUSED;
      96           8 :                         if (datafmt.maxlength > sizeof(col1)) {
      97           8 :                                 datafmt.maxlength = sizeof(col1);
      98             :                         }
      99           8 :                         check_call(ct_bind, (cmd, 1, &datafmt, col1, &datalength, &ind));
     100             : 
     101          48 :                         while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
     102           8 :                                || (ret == CS_ROW_FAIL)) {
     103          32 :                                 row_count += count;
     104          32 :                                 if (ret == CS_ROW_FAIL) {
     105           0 :                                         fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
     106           0 :                                         return 1;
     107             :                                 } else {        /* ret == CS_SUCCEED */
     108          32 :                                         col1[datalength] = 0;
     109          32 :                                         printf("col1 = %s\n", col1);
     110          32 :                                         assert(strcmp(col1, expected[rows]) == 0);
     111          32 :                                         ++rows;
     112             :                                 }
     113             :                         }
     114             : 
     115             : 
     116           8 :                         switch ((int) ret) {
     117             :                         case CS_END_DATA:
     118             :                                 break;
     119           0 :                         case CS_FAIL:
     120           0 :                                 fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
     121           0 :                                 return 1;
     122           0 :                         default:
     123           0 :                                 fprintf(stderr, "ct_fetch() unexpected return.\n");
     124           0 :                                 return 1;
     125             :                         }
     126             :                         break;
     127             : 
     128           0 :                 default:
     129           0 :                         fprintf(stderr, "ct_results() unexpected result_type.\n");
     130           0 :                         return 1;
     131             :                 }
     132             :         }
     133           8 :         switch ((int) results_ret) {
     134             :         case CS_END_RESULTS:
     135             :                 break;
     136           0 :         case CS_FAIL:
     137           0 :                 fprintf(stderr, "ct_results() failed.\n");
     138           0 :                 return 1;
     139             :                 break;
     140           0 :         default:
     141           0 :                 fprintf(stderr, "ct_results() unexpected return.\n");
     142           0 :                 return 1;
     143             :         }
     144             : 
     145           8 :         if (rows != 4) {
     146           0 :                 fprintf(stderr, "wrong number of rows: normal %u, expected 4\n", rows);
     147           0 :                 return 1;
     148             :         }
     149             : 
     150             :         if (verbose) {
     151             :                 printf("Trying logout\n");
     152             :         }
     153           8 :         check_call(try_ctlogout, (ctx, conn, cmd, verbose));
     154             : 
     155           8 :         return 0;
     156             : }

Generated by: LCOV version 1.13