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

Generated by: LCOV version 1.13