LCOV - code coverage report
Current view: top level - src/dblib/unittests - batch_stmt_ins_upd.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 96 106 90.6 %
Date: 2025-02-21 09:36:06 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Purpose: this will test what is returned from a batch of queries that do not return any rows
       3             :  * This is related to a bug first identified in PHPs PDO library https://bugs.php.net/bug.php?id=72969
       4             :  * Functions: dbbind dbcmd dbcolname dberrhandle dbisopt dbmsghandle dbnextrow dbnumcols dbopen dbresults dbsetlogintime dbsqlexec dbuse
       5             :  */
       6             : 
       7             : #include "common.h"
       8             : 
       9             : int
      10          10 : main(int argc, char **argv)
      11             : {
      12             :         LOGINREC *login;
      13             :         DBPROCESS *dbproc;
      14             :         int i;
      15             :         DBINT erc;
      16             : 
      17             :         RETCODE ret;
      18             :         int rowcount;
      19             :         int colcount;
      20             : 
      21          10 :         set_malloc_options();
      22             : 
      23          10 :         read_login_info(argc, argv);
      24          10 :         if (argc > 1) {
      25           0 :                 argc -= optind;
      26           0 :                 argv += optind;
      27             :         }
      28             : 
      29          10 :         printf("Starting %s\n", argv[0]);
      30             : 
      31             :         /* Fortify_EnterScope(); */
      32          10 :         dbinit();
      33             : 
      34          10 :         dberrhandle(syb_err_handler);
      35          10 :         dbmsghandle(syb_msg_handler);
      36             : 
      37          10 :         printf("About to logon as \"%s\"\n", USER);
      38             : 
      39          10 :         login = dblogin();
      40          10 :         DBSETLPWD(login, PASSWORD);
      41          10 :         DBSETLUSER(login, USER);
      42          10 :         DBSETLAPP(login, "batch_stmt_ins_upd");
      43             : 
      44          10 :         if (argc > 1) {
      45           0 :                 printf("server and login timeout overrides (%s and %s) detected\n", argv[0], argv[1]);
      46           0 :                 strcpy(SERVER, argv[0]);
      47           0 :                 i = atoi(argv[1]);
      48           0 :                 if (i) {
      49           0 :                         i = dbsetlogintime(i);
      50           0 :                         printf("dbsetlogintime returned %s.\n", (i == SUCCEED) ? "SUCCEED" : "FAIL");
      51             :                 }
      52             :         }
      53             : 
      54          10 :         printf("About to open \"%s\"\n", SERVER);
      55             : 
      56          10 :         dbproc = dbopen(login, SERVER);
      57          10 :         if (!dbproc) {
      58           0 :                 fprintf(stderr, "Unable to connect to %s\n", SERVER);
      59           0 :                 return 1;
      60             :         }
      61          10 :         dbloginfree(login);
      62             : 
      63          10 :         printf("Using database \"%s\"\n", DATABASE);
      64          10 :         if (strlen(DATABASE)) {
      65          10 :                 erc = dbuse(dbproc, DATABASE);
      66          10 :                 assert(erc == SUCCEED);
      67             :         }
      68             : 
      69          10 :         sql_cmd(dbproc);
      70          10 :         dbsqlexec(dbproc);
      71          10 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
      72             :                 /* nop */
      73             :         }
      74             : 
      75             :         /*
      76             :          * This test is written to simulate how dblib is used in PDO
      77             :          * functions are called in the same order they would be if doing
      78             :          * PDO::query followed by some number of PDO::statement->nextRowset
      79             :          */
      80             : 
      81             :         /*
      82             :          * First, call everything that happens in PDO::query
      83             :          * this will return the results of the CREATE TABLE statement
      84             :          */
      85          10 :         dbcancel(dbproc);
      86             : 
      87          10 :         printf("using sql_cmd\n");
      88          10 :         sql_cmd(dbproc);
      89          10 :         dbsqlexec(dbproc);
      90             : 
      91          10 :         ret = dbresults(dbproc);
      92          10 :         rowcount = DBCOUNT(dbproc);
      93          10 :         colcount = dbnumcols(dbproc);
      94             : 
      95          10 :         printf("RETCODE: %d\n", ret);
      96          10 :         printf("ROWCOUNT: %d\n", rowcount);
      97          10 :         printf("COLCOUNT: %d\n\n", colcount);
      98             : 
      99             :         /* check the results of the create table statement */
     100          10 :         assert(ret == SUCCEED);
     101          10 :         assert(rowcount == -1);
     102          10 :         assert(colcount == 0);
     103             : 
     104             :         /* now simulate calling nextRowset() for each remaining statement in our batch */
     105             : 
     106             :         /*
     107             :          * INSERT
     108             :          */
     109          10 :         ret = dbnextrow(dbproc);
     110          10 :         assert(ret == NO_MORE_ROWS);
     111             : 
     112          10 :         ret = dbresults(dbproc);
     113          10 :         rowcount = DBCOUNT(dbproc);
     114          10 :         colcount = dbnumcols(dbproc);
     115             : 
     116          10 :         printf("RETCODE: %d\n", ret);
     117          10 :         printf("ROWCOUNT: %d\n", rowcount);
     118          10 :         printf("COLCOUNT: %d\n\n", colcount);
     119             : 
     120          10 :         assert(ret == SUCCEED);
     121          10 :         assert(rowcount == 3);
     122          10 :         assert(colcount == 0);
     123             : 
     124             :         /*
     125             :          * UPDATE
     126             :          */
     127          10 :         ret = dbnextrow(dbproc);
     128          10 :         assert(ret == NO_MORE_ROWS);
     129             : 
     130          10 :         ret = dbresults(dbproc);
     131          10 :         rowcount = DBCOUNT(dbproc);
     132          10 :         colcount = dbnumcols(dbproc);
     133             : 
     134          10 :         printf("RETCODE: %d\n", ret);
     135          10 :         printf("ROWCOUNT: %d\n", rowcount);
     136          10 :         printf("COLCOUNT: %d\n\n", colcount);
     137             : 
     138          10 :         assert(ret == SUCCEED);
     139          10 :         assert(rowcount == 3);
     140          10 :         assert(colcount == 0);
     141             : 
     142             :         /*
     143             :          * INSERT
     144             :          */
     145          10 :         ret = dbnextrow(dbproc);
     146          10 :         assert(ret == NO_MORE_ROWS);
     147             : 
     148          10 :         ret = dbresults(dbproc);
     149          10 :         rowcount = DBCOUNT(dbproc);
     150          10 :         colcount = dbnumcols(dbproc);
     151             : 
     152          10 :         printf("RETCODE: %d\n", ret);
     153          10 :         printf("ROWCOUNT: %d\n", rowcount);
     154          10 :         printf("COLCOUNT: %d\n\n", colcount);
     155             : 
     156          10 :         assert(ret == SUCCEED);
     157          10 :         assert(rowcount == 1);
     158          10 :         assert(colcount == 0);
     159             : 
     160             :         /*
     161             :          * DROP
     162             :          */
     163          10 :         ret = dbnextrow(dbproc);
     164          10 :         assert(ret == NO_MORE_ROWS);
     165             : 
     166          10 :         ret = dbresults(dbproc);
     167          10 :         rowcount = DBCOUNT(dbproc);
     168          10 :         colcount = dbnumcols(dbproc);
     169             : 
     170          10 :         printf("RETCODE: %d\n", ret);
     171          10 :         printf("ROWCOUNT: %d\n", rowcount);
     172          10 :         printf("COLCOUNT: %d\n\n", colcount);
     173             : 
     174          10 :         assert(ret == SUCCEED);
     175          10 :         assert(rowcount == -1);
     176          10 :         assert(colcount == 0);
     177             : 
     178             :         /* Call one more time to be sure we get NO_MORE_RESULTS */
     179          10 :         ret = dbnextrow(dbproc);
     180          10 :         assert(ret == NO_MORE_ROWS);
     181             : 
     182          10 :         ret = dbresults(dbproc);
     183          10 :         rowcount = DBCOUNT(dbproc);
     184          10 :         colcount = dbnumcols(dbproc);
     185             : 
     186          10 :         printf("RETCODE: %d\n", ret);
     187          10 :         printf("ROWCOUNT: %d\n", rowcount);
     188          10 :         printf("COLCOUNT: %d\n\n", colcount);
     189             : 
     190          10 :         assert(ret == NO_MORE_RESULTS);
     191          10 :         assert(rowcount == -1);
     192          10 :         assert(colcount == 0);
     193             : 
     194          10 :         dbexit();
     195             : 
     196          10 :         printf("%s OK\n", __FILE__);
     197          10 :         return 0;
     198             : }

Generated by: LCOV version 1.13