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

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

Generated by: LCOV version 1.13