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-01-18 11:50:39 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           8 : main(int argc, char **argv)
      13             : {
      14           8 :         const int rows_to_add = 50;
      15             :         LOGINREC *login;
      16             :         DBPROCESS *dbproc;
      17             :         int i;
      18             :         char teststr[1024];
      19             :         DBINT testint;
      20             : 
      21           8 :         set_malloc_options();
      22             : 
      23           8 :         read_login_info(argc, argv);
      24           8 :         printf("Starting %s\n", argv[0]);
      25             : 
      26             :         /* Fortify_EnterScope(); */
      27           8 :         dbinit();
      28             : 
      29           8 :         dberrhandle(syb_err_handler);
      30           8 :         dbmsghandle(syb_msg_handler);
      31             : 
      32           8 :         printf("About to logon\n");
      33             : 
      34           8 :         login = dblogin();
      35           8 :         DBSETLPWD(login, PASSWORD);
      36           8 :         DBSETLUSER(login, USER);
      37           8 :         DBSETLAPP(login, "t0018");
      38             : 
      39           8 :         printf("About to open\n");
      40             : 
      41           8 :         dbproc = dbopen(login, SERVER);
      42           8 :         if (strlen(DATABASE))
      43           8 :                 dbuse(dbproc, DATABASE);
      44           8 :         dbloginfree(login);
      45             : 
      46           8 :         printf("creating table\n");
      47           8 :         sql_cmd(dbproc);
      48           8 :         dbsqlexec(dbproc);
      49           8 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
      50             :                 /* nop */
      51             :         }
      52             : 
      53           8 :         printf("insert\n");
      54         408 :         for (i = 0; i < rows_to_add; i++) {
      55         400 :                 sql_cmd(dbproc);
      56         400 :                 dbsqlexec(dbproc);
      57         400 :                 while (dbresults(dbproc) != NO_MORE_RESULTS) {
      58             :                         /* nop */
      59             :                 }
      60         400 :                 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           8 :         printf("select\n");
      68           8 :         sql_cmd(dbproc);
      69           8 :         dbsqlexec(dbproc);
      70             : 
      71           8 :         printf("Checking for an empty result set.\n");
      72           8 :         if (dbresults(dbproc) != SUCCEED) {
      73           0 :                 failed = 1;
      74           0 :                 printf("Was expecting a result set.\n");
      75           0 :                 exit(1);
      76             :         }
      77             : 
      78           8 :         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           8 :         printf("Checking for a result set with content.\n");
      85           8 :         if (dbresults(dbproc) != SUCCEED) {
      86           0 :                 failed = 1;
      87           0 :                 printf("Was expecting a result set.");
      88           0 :                 exit(1);
      89             :         }
      90             : 
      91          16 :         for (i = 1; i <= dbnumcols(dbproc); i++)
      92          16 :                 printf("col %d is %s\n", i, dbcolname(dbproc, i));
      93             : 
      94           8 :         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           8 :         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         400 :         for (i = 0; i < rows_to_add; i++) {
     106             :         char expected[1024];
     107             : 
     108         400 :                 sprintf(expected, "row %03d", i);
     109             : 
     110         400 :                 if (REG_ROW != dbnextrow(dbproc)) {
     111           0 :                         failed = 1;
     112           0 :                         fprintf(stderr, "Failed.  Expected a row\n");
     113           0 :                         exit(1);
     114             :                 }
     115         400 :                 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         400 :                 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         400 :                 printf("Read a row of data -> %d %s\n", (int) testint, teststr);
     126             :         }
     127             : 
     128           8 :         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           8 :         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           8 :         sql_cmd(dbproc);
     140           8 :         dbsqlexec(dbproc);
     141           8 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
     142             :                 /* nop */
     143             :         }
     144           8 :         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           8 :                 printf("Number of rows affected by update = %d.\n", DBCOUNT(dbproc));
     150             :         }
     151             : 
     152           8 :         dbexit();
     153             : 
     154           8 :         printf("%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
     155           8 :         return failed ? 1 : 0;
     156             : }

Generated by: LCOV version 1.13