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

          Line data    Source code
       1             : #undef NDEBUG
       2             : #include <config.h>
       3             : 
       4             : #if HAVE_STRING_H
       5             : #include <string.h>
       6             : #endif /* HAVE_STRING_H */
       7             : 
       8             : #include <stdio.h>
       9             : #include <assert.h>
      10             : #include <ctpublic.h>
      11             : #include "common.h"
      12             : 
      13             : /* Testing: Retrieving SQL_VARIANT */
      14             : int
      15           8 : main(int argc, char *argv[])
      16             : {
      17             :         CS_CONTEXT *ctx;
      18             :         CS_CONNECTION *conn;
      19             :         CS_COMMAND *cmd;
      20           8 :         int verbose = 0;
      21             : 
      22             :         CS_RETCODE ret;
      23             :         CS_RETCODE results_ret;
      24             :         CS_INT result_type;
      25             :         CS_INT num_cols;
      26             : 
      27             :         CS_DATAFMT datafmt;
      28             :         CS_INT datalength;
      29             :         CS_SMALLINT ind;
      30           8 :         CS_INT count, row_count = 0;
      31             : 
      32             :         CS_CHAR select[1024];
      33             : 
      34             :         CS_CHAR col1[128];
      35             :         const char *expected[10];
      36           8 :         unsigned num_expected = 0;
      37             : 
      38           8 :         unsigned rows = 0;
      39             : 
      40           8 :         memset(expected, 0, sizeof(expected));
      41             : 
      42           8 :         printf("%s: Retrieve SQL_VARIANT column\n", __FILE__);
      43             :         if (verbose) {
      44             :                 printf("Trying login\n");
      45             :         }
      46           8 :         ret = try_ctlogin(&ctx, &conn, &cmd, verbose);
      47           8 :         if (ret != CS_SUCCEED) {
      48           0 :                 fprintf(stderr, "Login failed\n");
      49           0 :                 return 1;
      50             :         }
      51             : 
      52           8 :         strcpy(select, "CREATE TABLE #ctlib0009 (n int, col1 sql_variant null)");
      53             : 
      54           8 :         ret = ct_command(cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED);
      55           8 :         assert(ret == CS_SUCCEED);
      56             : 
      57           8 :         ret = ct_send(cmd);
      58           8 :         assert(ret == CS_SUCCEED);
      59             : 
      60           8 :         ret = ct_results(cmd, &result_type);
      61           8 :         assert(ret == CS_SUCCEED);
      62             : 
      63           8 :         switch (result_type) {
      64           2 :         case CS_CMD_FAIL:
      65           2 :                 fprintf(stderr, "ct_results() result_type CS_CMD_FAIL, probably not MSSQL.\n");
      66           2 :                 try_ctlogout(ctx, conn, cmd, verbose);
      67           2 :                 return 0;
      68             :         case CS_CMD_SUCCEED:
      69             :                 break;
      70           0 :         default:
      71           0 :                 fprintf(stderr, "ct_results() unexpected return %d.\n", result_type);
      72           0 :                 try_ctlogout(ctx, conn, cmd, verbose);
      73           0 :                 return 1;
      74             :         }
      75             : 
      76           6 :         ret = run_command(cmd, "insert into #ctlib0009 values (1, 123)");
      77           6 :         assert(ret == CS_SUCCEED);
      78           6 :         expected[num_expected++] = "123";
      79             : 
      80           6 :         ret = run_command(cmd, "insert into #ctlib0009 values (2, NULL)");
      81           6 :         assert(ret == CS_SUCCEED);
      82           6 :         expected[num_expected++] = "";
      83             : 
      84           6 :         ret = run_command(cmd, "insert into #ctlib0009 values (3, 'hello')");
      85           6 :         assert(ret == CS_SUCCEED);
      86           6 :         expected[num_expected++] = "hello";
      87             : 
      88           6 :         ret = run_command(cmd, "insert into #ctlib0009 values (4, 123.456)");
      89           6 :         assert(ret == CS_SUCCEED);
      90           6 :         expected[num_expected++] = "123.456";
      91             : 
      92           6 :         strcpy(select, "select col1 from #ctlib0009 order by n");
      93             : 
      94           6 :         ret = ct_command(cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED);
      95             : 
      96           6 :         if (ret != CS_SUCCEED) {
      97           0 :                 fprintf(stderr, "ct_command(%s) failed\n", select);
      98           0 :                 return 1;
      99             :         }
     100             : 
     101           6 :         ret = ct_send(cmd);
     102           6 :         if (ret != CS_SUCCEED) {
     103           0 :                 fprintf(stderr, "ct_send() failed\n");
     104           0 :                 return 1;
     105             :         }
     106             : 
     107           6 :         ct_callback(ctx, NULL, CS_SET, CS_SERVERMSG_CB, (CS_VOID *) servermsg_cb);
     108           6 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
     109          12 :                 printf("ct_results returned %s type\n", res_type_str(result_type));
     110          12 :                 switch ((int) result_type) {
     111             :                 case CS_CMD_SUCCEED:
     112             :                         break;
     113             :                 case CS_CMD_DONE:
     114             :                         break;
     115           0 :                 case CS_CMD_FAIL:
     116           0 :                         fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
     117           0 :                         return 1;
     118           6 :                 case CS_ROW_RESULT:
     119           6 :                         ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);
     120           6 :                         if (ret != CS_SUCCEED) {
     121           0 :                                 fprintf(stderr, "ct_res_info() failed");
     122           0 :                                 return 1;
     123             :                         }
     124           6 :                         if (num_cols != 1) {
     125           0 :                                 fprintf(stderr, "num_cols %d != 1", num_cols);
     126           0 :                                 return 1;
     127             :                         }
     128             : 
     129           6 :                         ret = ct_describe(cmd, 1, &datafmt);
     130           6 :                         if (ret != CS_SUCCEED) {
     131           0 :                                 fprintf(stderr, "ct_describe() failed");
     132           0 :                                 return 1;
     133             :                         }
     134           6 :                         datafmt.format = CS_FMT_UNUSED;
     135           6 :                         if (datafmt.maxlength > sizeof(col1)) {
     136           6 :                                 datafmt.maxlength = sizeof(col1);
     137             :                         }
     138           6 :                         ret = ct_bind(cmd, 1, &datafmt, col1, &datalength, &ind);
     139           6 :                         if (ret != CS_SUCCEED) {
     140           0 :                                 fprintf(stderr, "ct_bind() failed\n");
     141           0 :                                 return 1;
     142             :                         }
     143             : 
     144          30 :                         while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
     145           6 :                                || (ret == CS_ROW_FAIL)) {
     146          24 :                                 row_count += count;
     147          24 :                                 if (ret == CS_ROW_FAIL) {
     148           0 :                                         fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
     149           0 :                                         return 1;
     150             :                                 } else {        /* ret == CS_SUCCEED */
     151          24 :                                         col1[datalength] = 0;
     152          24 :                                         printf("col1 = %s\n", col1);
     153          24 :                                         assert(strcmp(col1, expected[rows]) == 0);
     154          24 :                                         ++rows;
     155             :                                 }
     156             :                         }
     157             : 
     158             : 
     159           6 :                         switch ((int) ret) {
     160             :                         case CS_END_DATA:
     161             :                                 break;
     162           0 :                         case CS_FAIL:
     163           0 :                                 fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
     164           0 :                                 return 1;
     165           0 :                         default:
     166           0 :                                 fprintf(stderr, "ct_fetch() unexpected return.\n");
     167           0 :                                 return 1;
     168             :                         }
     169             :                         break;
     170             : 
     171           0 :                 default:
     172           0 :                         fprintf(stderr, "ct_results() unexpected result_type.\n");
     173           0 :                         return 1;
     174             :                 }
     175             :         }
     176           6 :         switch ((int) results_ret) {
     177             :         case CS_END_RESULTS:
     178             :                 break;
     179           0 :         case CS_FAIL:
     180           0 :                 fprintf(stderr, "ct_results() failed.\n");
     181           0 :                 return 1;
     182             :                 break;
     183           0 :         default:
     184           0 :                 fprintf(stderr, "ct_results() unexpected return.\n");
     185           0 :                 return 1;
     186             :         }
     187             : 
     188           6 :         if (rows != 4) {
     189           0 :                 fprintf(stderr, "wrong number of rows: normal %u, expected 4\n", rows);
     190           0 :                 return 1;
     191             :         }
     192             : 
     193             :         if (verbose) {
     194             :                 printf("Trying logout\n");
     195             :         }
     196           6 :         ret = try_ctlogout(ctx, conn, cmd, verbose);
     197           6 :         if (ret != CS_SUCCEED) {
     198           0 :                 fprintf(stderr, "Logout failed\n");
     199           0 :                 return 1;
     200             :         }
     201             : 
     202             :         return 0;
     203             : }

Generated by: LCOV version 1.13