LCOV - code coverage report
Current view: top level - src/ctlib/unittests - t0007.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 53 101 52.5 %
Date: 2025-01-18 11:50:39 Functions: 1 1 100.0 %

          Line data    Source code
       1             : #include "common.h"
       2             : 
       3             : /* Testing: Retrieve CS_TEXT_TYPE using ct_bind() */
       4             : int
       5           8 : main(void)
       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_RETCODE results_ret;
      14             :         CS_INT result_type;
      15             :         CS_INT num_cols;
      16             : 
      17             :         CS_DATAFMT datafmt[4];
      18             :         CS_INT datalength[4];
      19             :         CS_SMALLINT ind[4];
      20           8 :         CS_INT count, row_count = 0;
      21             : 
      22             :         CS_CHAR name[4][1024];
      23             : 
      24           8 :         name[0][0] = 0;
      25           8 :         name[1][0] = 0;
      26           8 :         name[2][0] = 0;
      27           8 :         name[3][0] = 0;
      28             : 
      29           8 :         printf("%s: Retrieve CS_CHAR_TYPE using ct_bind()\n", __FILE__);
      30             :         if (verbose) {
      31             :                 printf("Trying login\n");
      32             :         }
      33           8 :         check_call(try_ctlogin, (&ctx, &conn, &cmd, verbose));
      34             : 
      35           8 :         check_call(ct_command, (cmd, CS_LANG_CMD, "SELECT CONVERT(VARCHAR(7),'1234') AS test, "
      36             :                    "CONVERT(VARCHAR(7),'') AS test2, CONVERT(VARCHAR(7),NULL) AS test3, "
      37             :                    "CONVERT(NUMERIC(38,2), 123.45) as test4", CS_NULLTERM, CS_UNUSED));
      38           8 :         check_call(ct_send, (cmd));
      39           8 :         while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
      40          16 :                 switch ((int) result_type) {
      41             :                 case CS_CMD_SUCCEED:
      42             :                         break;
      43             :                 case CS_CMD_DONE:
      44             :                         break;
      45           0 :                 case CS_CMD_FAIL:
      46           0 :                         fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
      47           0 :                         return 1;
      48           8 :                 case CS_ROW_RESULT:
      49           8 :                         check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
      50           8 :                         if (num_cols != 4) {
      51           0 :                                 fprintf(stderr, "num_cols %d != 4", num_cols);
      52           0 :                                 return 1;
      53             :                         }
      54           8 :                         check_call(ct_describe, (cmd, 1, &datafmt[0]));
      55           8 :                         datafmt[0].format = CS_FMT_NULLTERM;
      56           8 :                         ++datafmt[0].maxlength;
      57           8 :                         if (datafmt[0].maxlength > 1024) {
      58           0 :                                 datafmt[0].maxlength = 1024;
      59             :                         }
      60             : 
      61           8 :                         check_call(ct_describe, (cmd, 2, &datafmt[1]));
      62           8 :                         datafmt[1].format = CS_FMT_NULLTERM;
      63           8 :                         ++datafmt[1].maxlength;
      64           8 :                         if (datafmt[1].maxlength > 1024) {
      65           0 :                                 datafmt[1].maxlength = 1024;
      66             :                         }
      67             : 
      68           8 :                         check_call(ct_describe, (cmd, 3, &datafmt[2]));
      69           8 :                         datafmt[2].format = CS_FMT_NULLTERM;
      70           8 :                         ++datafmt[2].maxlength;
      71           8 :                         if (datafmt[2].maxlength > 1024) {
      72           0 :                                 datafmt[2].maxlength = 1024;
      73             :                         }
      74             : 
      75           8 :                         check_call(ct_describe, (cmd, 4, &datafmt[3]));
      76           8 :                         datafmt[3].format = CS_FMT_NULLTERM;
      77           8 :                         if (datafmt[3].maxlength != sizeof(CS_NUMERIC)) {
      78           0 :                                 fprintf(stderr, "wrong maxlength for numeric\n");
      79           0 :                                 return 1;
      80             :                         }
      81           8 :                         ++datafmt[3].maxlength;
      82             :                         if (datafmt[3].maxlength > 1024) {
      83             :                                 datafmt[3].maxlength = 1024;
      84             :                         }
      85             : 
      86           8 :                         check_call(ct_bind, (cmd, 1, &datafmt[0], name[0], &datalength[0], &ind[0]));
      87           8 :                         check_call(ct_bind, (cmd, 2, &datafmt[1], name[1], &datalength[1], &ind[1]));
      88           8 :                         check_call(ct_bind, (cmd, 3, &datafmt[2], name[2], &datalength[2], &ind[2]));
      89           8 :                         check_call(ct_bind, (cmd, 4, &datafmt[3], name[3], &datalength[3], &ind[3]));
      90             : 
      91           8 :                         while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
      92           8 :                                || (ret == CS_ROW_FAIL)) {
      93           8 :                                 row_count += count;
      94           8 :                                 if (ret == CS_ROW_FAIL) {
      95           0 :                                         fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
      96           0 :                                         return 1;
      97             :                                 } else {        /* ret == CS_SUCCEED */
      98             :                                         if (verbose) {
      99             :                                                 printf("name = '%s'\n", name[0]);
     100             :                                         }
     101           8 :                                         if (ind[0] != 0) {
     102           0 :                                                 fprintf(stderr, "Returned NULL\n");
     103           0 :                                                 return 1;
     104             :                                         }
     105           8 :                                         if (strcmp(name[0], "1234")) {
     106           0 :                                                 fprintf(stderr, "Bad return:\n'%s'\n! =\n'%s'\n", name[0], "1234");
     107           0 :                                                 return 1;
     108             :                                         }
     109           8 :                                         if (datalength[0] != strlen(name[0]) + 1) {
     110           0 :                                                 fprintf(stderr, "Bad length:\n'%ld'\n! =\n'%d'\n", (long) strlen(name[0]) + 1, datalength[0]);
     111           0 :                                                 return 1;
     112             :                                         }
     113           8 :                                         if (ind[1] != 0) {
     114           0 :                                                 fprintf(stderr, "Returned NULL\n");
     115           0 :                                                 return 1;
     116             :                                         }
     117             :                                         /* empty are retunerd as a single space in TDS4.x and TDS5 */
     118           8 :                                         if (strcmp(name[1], "") && strcmp(name[1], " ")) {
     119           0 :                                                 fprintf(stderr, "Bad return:\n'%s'\n! =\n'%s'\n", name[1], "");
     120           0 :                                                 return 1;
     121             :                                         }
     122           8 :                                         if (datalength[1] != strlen(name[1]) + 1) {
     123           0 :                                                 fprintf(stderr, "Col 2 bad length:\n'%ld'\n! =\n'%d'\n", (long) strlen(name[1]) + 1, datalength[1]);
     124           0 :                                                 return 1;
     125             :                                         }
     126           8 :                                         if (ind[2] == 0) {
     127           0 :                                                 fprintf(stderr, "Col 3 returned not NULL (ind %d len %d)\n", (int) ind[2], (int) datalength[2]);
     128           0 :                                                 return 1;
     129             :                                         }
     130           8 :                                         if (strcmp(name[2], "")) {
     131           0 :                                                 fprintf(stderr, "Col 3 bad return:\n'%s'\n! =\n'%s'\n", name[2], "");
     132           0 :                                                 return 1;
     133             :                                         }
     134           8 :                                         if (datalength[2] != 0) {
     135           0 :                                                 fprintf(stderr, "Col 3 bad length:\n'%ld'\n! =\n'%d'\n", (long) 0, datalength[2]);
     136           0 :                                                 return 1;
     137             :                                         }
     138             :                                 }
     139             :                         }
     140           8 :                         switch ((int) ret) {
     141             :                         case CS_END_DATA:
     142             :                                 break;
     143           0 :                         case CS_FAIL:
     144           0 :                                 fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
     145           0 :                                 return 1;
     146           0 :                         default:
     147           0 :                                 fprintf(stderr, "ct_fetch() unexpected return.\n");
     148           0 :                                 return 1;
     149             :                         }
     150             :                         break;
     151           0 :                 case CS_COMPUTE_RESULT:
     152           0 :                         fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
     153           0 :                         return 1;
     154           0 :                 default:
     155           0 :                         fprintf(stderr, "ct_results() unexpected result_type.\n");
     156           0 :                         return 1;
     157             :                 }
     158             :         }
     159           8 :         switch ((int) results_ret) {
     160             :         case CS_END_RESULTS:
     161             :                 break;
     162           0 :         case CS_FAIL:
     163           0 :                 fprintf(stderr, "ct_results() failed.\n");
     164           0 :                 return 1;
     165             :                 break;
     166           0 :         default:
     167           0 :                 fprintf(stderr, "ct_results() unexpected return.\n");
     168           0 :                 return 1;
     169             :         }
     170             : 
     171             :         if (verbose) {
     172             :                 printf("Trying logout\n");
     173             :         }
     174           8 :         check_call(try_ctlogout, (ctx, conn, cmd, verbose));
     175             : 
     176           8 :         return 0;
     177             : }

Generated by: LCOV version 1.13