LCOV - code coverage report
Current view: top level - src/dblib/unittests - t0018.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 57 93 61.3 %
Date: 2025-07-02 09:40:27 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* 
       2             :  * Purpose: Test behaviour of DBCOUNT() for updates and inserts
       3             :  * Functions: DBCOUNT dbbind dbnextrow dbnumcols dbopen dbresults dbsqlexec 
       4             :  */
       5             : 
       6             : #include "common.h"
       7             : 
       8             : #include <freetds/bool.h>
       9             : 
      10          10 : TEST_MAIN()
      11             : {
      12          10 :         bool failed = false;
      13          10 :         const int rows_to_add = 50;
      14             :         LOGINREC *login;
      15             :         DBPROCESS *dbproc;
      16             :         int i;
      17             :         char teststr[1024];
      18             :         DBINT testint;
      19             : 
      20          10 :         set_malloc_options();
      21             : 
      22          10 :         read_login_info(argc, argv);
      23          10 :         printf("Starting %s\n", argv[0]);
      24             : 
      25             :         /* Fortify_EnterScope(); */
      26          10 :         dbinit();
      27             : 
      28          10 :         dberrhandle(syb_err_handler);
      29          10 :         dbmsghandle(syb_msg_handler);
      30             : 
      31          10 :         printf("About to logon\n");
      32             : 
      33          10 :         login = dblogin();
      34          10 :         DBSETLPWD(login, PASSWORD);
      35          10 :         DBSETLUSER(login, USER);
      36          10 :         DBSETLAPP(login, "t0018");
      37             : 
      38          10 :         printf("About to open\n");
      39             : 
      40          10 :         dbproc = dbopen(login, SERVER);
      41          10 :         if (strlen(DATABASE))
      42          10 :                 dbuse(dbproc, DATABASE);
      43          10 :         dbloginfree(login);
      44             : 
      45          10 :         printf("creating table\n");
      46          10 :         sql_cmd(dbproc);
      47          10 :         dbsqlexec(dbproc);
      48          10 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
      49             :                 /* nop */
      50             :         }
      51             : 
      52          10 :         printf("insert\n");
      53         510 :         for (i = 0; i < rows_to_add; i++) {
      54         500 :                 sql_cmd(dbproc);
      55         500 :                 dbsqlexec(dbproc);
      56         500 :                 while (dbresults(dbproc) != NO_MORE_RESULTS) {
      57             :                         /* nop */
      58             :                 }
      59         500 :                 if (DBCOUNT(dbproc) != 1) {
      60           0 :                         failed = true;
      61           0 :                         printf("Was expecting a rows affect to be 1.");
      62           0 :                         exit(1);
      63             :                 }
      64             :         }
      65             : 
      66          10 :         printf("select\n");
      67          10 :         sql_cmd(dbproc);
      68          10 :         dbsqlexec(dbproc);
      69             : 
      70          10 :         printf("Checking for an empty result set.\n");
      71          10 :         if (dbresults(dbproc) != SUCCEED) {
      72           0 :                 failed = true;
      73           0 :                 printf("Was expecting a result set.\n");
      74           0 :                 exit(1);
      75             :         }
      76             : 
      77          10 :         if(DBROWS(dbproc) != FAIL) {
      78           0 :                 failed = true;
      79           0 :                 printf("Was expecting no rows to be available.\n");
      80           0 :                 exit(1);
      81             :         }
      82             : 
      83          10 :         printf("Checking for a result set with content.\n");
      84          10 :         if (dbresults(dbproc) != SUCCEED) {
      85           0 :                 failed = true;
      86           0 :                 printf("Was expecting a result set.");
      87           0 :                 exit(1);
      88             :         }
      89             : 
      90          20 :         for (i = 1; i <= dbnumcols(dbproc); i++)
      91          20 :                 printf("col %d is %s\n", i, dbcolname(dbproc, i));
      92             : 
      93          10 :         if (SUCCEED != dbbind(dbproc, 1, INTBIND, 0, (BYTE *) & testint)) {
      94           0 :                 failed = true;
      95           0 :                 fprintf(stderr, "Had problem with bind\n");
      96           0 :                 abort();
      97             :         }
      98          10 :         if (SUCCEED != dbbind(dbproc, 2, STRINGBIND, 0, (BYTE *) teststr)) {
      99           0 :                 failed = true;
     100           0 :                 fprintf(stderr, "Had problem with bind\n");
     101           0 :                 abort();
     102             :         }
     103             : 
     104         500 :         for (i = 0; i < rows_to_add; i++) {
     105             :         char expected[1024];
     106             : 
     107         500 :                 sprintf(expected, "row %03d", i);
     108             : 
     109         500 :                 if (REG_ROW != dbnextrow(dbproc)) {
     110           0 :                         failed = true;
     111           0 :                         fprintf(stderr, "Failed.  Expected a row\n");
     112           0 :                         exit(1);
     113             :                 }
     114         500 :                 if (testint != i) {
     115           0 :                         failed = true;
     116           0 :                         fprintf(stderr, "Failed.  Expected i to be %d, was %d\n", i, (int) testint);
     117           0 :                         abort();
     118             :                 }
     119         500 :                 if (0 != strncmp(teststr, expected, strlen(expected))) {
     120           0 :                         failed = true;
     121           0 :                         printf("Failed.  Expected s to be |%s|, was |%s|\n", expected, teststr);
     122           0 :                         abort();
     123             :                 }
     124         500 :                 printf("Read a row of data -> %d %s\n", (int) testint, teststr);
     125             :         }
     126             : 
     127          10 :         if (dbnextrow(dbproc) != NO_MORE_ROWS) {
     128           0 :                 failed = true;
     129           0 :                 fprintf(stderr, "Was expecting no more rows\n");
     130           0 :                 exit(1);
     131             :         }
     132          10 :         if (DBCOUNT(dbproc) != rows_to_add) {
     133           0 :                 failed = true;
     134           0 :                 printf("Was expecting a rows affect to be %d was %d.\n", rows_to_add, DBCOUNT(dbproc));
     135           0 :                 exit(1);
     136             :         }
     137             : 
     138          10 :         sql_cmd(dbproc);
     139          10 :         dbsqlexec(dbproc);
     140          10 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
     141             :                 /* nop */
     142             :         }
     143          10 :         if (DBCOUNT(dbproc) != rows_to_add) {
     144           0 :                 failed = true;
     145           0 :                 printf("Was expecting a rows affect to be %d was %d.\n", rows_to_add, DBCOUNT(dbproc));
     146           0 :                 exit(1);
     147             :         } else {
     148          10 :                 printf("Number of rows affected by update = %d.\n", DBCOUNT(dbproc));
     149             :         }
     150             : 
     151          10 :         dbexit();
     152             : 
     153          10 :         printf("%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
     154          10 :         return failed ? 1 : 0;
     155             : }

Generated by: LCOV version 1.13