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

Generated by: LCOV version 1.13