LCOV - code coverage report
Current view: top level - src/dblib/unittests - t0004.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 58 74 78.4 %
Date: 2025-05-05 12:32:54 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* 
       2             :  * Purpose: Test retrieving data and attempting to initiate a new query with results pending
       3             :  * Functions: dbnextrow dbresults dbsqlexec dbgetchar 
       4             :  */
       5             : 
       6             : #include "common.h"
       7             : #include <ctype.h>
       8             : 
       9          10 : TEST_MAIN()
      10             : {
      11          10 :         const int rows_to_add = 50;
      12             :         LOGINREC *login;
      13             :         DBPROCESS *dbproc;
      14             :         int i, expected_error;
      15             :         char *s, teststr[1024];
      16             :         DBINT testint;
      17          10 :         int failed = 0;
      18             : 
      19          10 :         set_malloc_options();
      20             : 
      21          10 :         read_login_info(argc, argv);
      22             : 
      23          10 :         printf("Starting %s\n", argv[0]);
      24             : 
      25          10 :         dbinit();
      26             : 
      27          10 :         dberrhandle(syb_err_handler);
      28          10 :         dbmsghandle(syb_msg_handler);
      29             : 
      30          10 :         printf("About to logon\n");
      31             : 
      32          10 :         login = dblogin();
      33          10 :         DBSETLPWD(login, PASSWORD);
      34          10 :         DBSETLUSER(login, USER);
      35          10 :         DBSETLAPP(login, "t0004");
      36             : 
      37          10 :         printf("About to open\n");
      38             : 
      39          10 :         dbproc = dbopen(login, SERVER);
      40          10 :         if (strlen(DATABASE))
      41          10 :                 dbuse(dbproc, DATABASE);
      42          10 :         dbloginfree(login);
      43             : 
      44          10 :         printf("creating table\n");
      45          10 :         sql_cmd(dbproc);
      46          10 :         dbsqlexec(dbproc);
      47          10 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
      48             :                 /* nop */
      49             :         }
      50             : 
      51          10 :         printf("insert\n");
      52         500 :         for (i = 1; i < rows_to_add; i++) {
      53         490 :                 sql_cmd(dbproc);
      54         490 :                 dbsqlexec(dbproc);
      55         490 :                 while (dbresults(dbproc) != NO_MORE_RESULTS) {
      56             :                         /* nop */
      57             :                 }
      58             :         }
      59             : 
      60          10 :         sql_cmd(dbproc); /* select */
      61          10 :         dbsqlexec(dbproc);
      62             : 
      63          10 :         if (dbresults(dbproc) != SUCCEED) {
      64           0 :                 printf("Was expecting a result set.");
      65           0 :                 exit(1);
      66             :         }
      67             : 
      68          20 :         for (i = 1; i <= dbnumcols(dbproc); i++)
      69          20 :                 printf("col %d is %s\n", i, dbcolname(dbproc, i));
      70             : 
      71          10 :         dbbind(dbproc, 1, INTBIND, 0, (BYTE *) & testint);
      72          10 :         dbbind(dbproc, 2, STRINGBIND, 0, (BYTE *) teststr);
      73             : 
      74         250 :         for (i = 1; i <= 24; i++) {
      75             :         char expected[1024];
      76             : 
      77         240 :                 sprintf(expected, "row %04d", i);
      78             : 
      79         240 :                 if (i % 5 == 0) {
      80          40 :                         dbclrbuf(dbproc, 5);
      81             :                 }
      82             : 
      83             : 
      84         240 :                 testint = -1;
      85         240 :                 strcpy(teststr, "bogus");
      86             : 
      87         240 :                 if (REG_ROW != dbnextrow(dbproc)) {
      88           0 :                         fprintf(stderr, "Failed.  Expected a row\n");
      89           0 :                         exit(1);
      90             :                 }
      91         240 :                 if (testint != i) {
      92           0 :                         fprintf(stderr, "Failed.  Expected i to be %d, was %d\n", i, (int) testint);
      93           0 :                         abort();
      94             :                 }
      95         240 :                 if (0 != strncmp(teststr, expected, strlen(expected))) {
      96           0 :                         printf("Failed.  Expected s to be |%s|, was |%s|\n", expected, teststr);
      97           0 :                         abort();
      98             :                 }
      99         240 :                 printf("Read a row of data -> %d %s\n", (int) testint, teststr);
     100             :         }
     101             : 
     102             : 
     103             : 
     104          10 :         printf("second select\n");
     105          10 :         printf("testing dbgetchar...\n");
     106         490 :         for (i=0; (s = dbgetchar(dbproc, i)) != NULL; i++) {
     107         960 :                 putchar(*s);
     108         480 :                 if (!(isprint((unsigned char)*s) || iscntrl((unsigned char)*s))) {
     109           0 :                         fprintf(stderr, "%s:%d: dbgetchar failed with %x at position %d\n", __FILE__, __LINE__, *s, i);
     110           0 :                         failed = 1;
     111           0 :                         break;
     112             :                 }
     113             :         }
     114          10 :         printf("<== end of command buffer\n");
     115             : 
     116          10 :         if (SUCCEED != sql_cmd(dbproc)) {
     117           0 :                 fprintf(stderr, "%s:%d: dbcmd failed\n", __FILE__, __LINE__);
     118           0 :                 failed = 1;
     119             :         }
     120          10 :         printf("About to exec for the second time.  Should fail\n");
     121          10 :         expected_error = 20019;
     122          10 :         dbsetuserdata(dbproc, (BYTE*) &expected_error);
     123          10 :         if (FAIL != dbsqlexec(dbproc)) {
     124           0 :                 fprintf(stderr, "%s:%d: dbsqlexec should have failed but didn't\n", __FILE__, __LINE__);
     125           0 :                 failed = 1;
     126             :         }
     127           0 :         dbexit();
     128             : 
     129          10 :         printf("%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
     130          10 :         return failed ? 1 : 0;
     131             : }

Generated by: LCOV version 1.13