LCOV - code coverage report
Current view: top level - src/dblib/unittests - t0006.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 60 73 82.2 %
Date: 2024-04-18 20:40:06 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* 
       2             :  * Purpose: Test fetching 50 rows in two result sets with a 5000-row buffer.
       3             :  * Functions: dbbind dbcmd dbnextrow dbnumcols dbresults dbsetopt dbsqlexec
       4             :  */
       5             : 
       6             : #include "common.h"
       7             : 
       8             : static char teststr[1024];
       9             : static DBINT testint;
      10             : 
      11             : static int failed = 0;
      12             : 
      13             : 
      14             : static void
      15          16 : get_results(DBPROCESS * dbproc, int start)
      16             : {
      17          16 :         int current = start - 1;
      18             : 
      19         624 :         while (REG_ROW == dbnextrow(dbproc)) {
      20             :         char expected[1024];
      21             : 
      22         592 :                 current++;
      23         592 :                 sprintf(expected, "row %04d", current);
      24             : 
      25         592 :                 if (testint != current) {
      26           0 :                         fprintf(stderr, "Failed.  Expected i to be %d, was %d\n", current, (int) testint);
      27           0 :                         abort();
      28             :                 }
      29         592 :                 if (0 != strncmp(teststr, expected, strlen(expected))) {
      30           0 :                         printf("Failed.  Expected s to be |%s|, was |%s|\n", expected, teststr);
      31           0 :                         abort();
      32             :                 }
      33         592 :                 printf("Read a row of data -> %d %s\n", (int) testint, teststr);
      34             :         }
      35          16 : }
      36             : 
      37             : 
      38             : int
      39           8 : main(int argc, char **argv)
      40             : {
      41             :         RETCODE rc;
      42           8 :         const int rows_to_add = 50;
      43             :         LOGINREC *login;
      44             :         DBPROCESS *dbproc;
      45             :         int i;
      46             : 
      47           8 :         set_malloc_options();
      48             : 
      49           8 :         read_login_info(argc, argv);
      50           8 :         printf("Starting %s\n", argv[0]);
      51             : 
      52             :         /* Fortify_EnterScope(); */
      53           8 :         dbinit();
      54             : 
      55           8 :         dberrhandle(syb_err_handler);
      56           8 :         dbmsghandle(syb_msg_handler);
      57             : 
      58           8 :         printf("About to logon\n");
      59             : 
      60           8 :         login = dblogin();
      61           8 :         DBSETLPWD(login, PASSWORD);
      62           8 :         DBSETLUSER(login, USER);
      63           8 :         DBSETLAPP(login, "t0006");
      64           8 :         DBSETLHOST(login, "ntbox.dntis.ro");
      65             : 
      66           8 :         printf("About to open\n");
      67             : 
      68           8 :         dbproc = dbopen(login, SERVER);
      69           8 :         if (strlen(DATABASE))
      70           8 :                 dbuse(dbproc, DATABASE);
      71           8 :         dbloginfree(login);
      72             : 
      73             : #ifdef MICROSOFT_DBLIB
      74             :         dbsetopt(dbproc, DBBUFFER, "5000");
      75             : #else
      76           8 :         dbsetopt(dbproc, DBBUFFER, "5000", 0);
      77             : #endif
      78             : 
      79           8 :         printf("creating table\n");
      80           8 :         sql_cmd(dbproc);
      81           8 :         dbsqlexec(dbproc);
      82           8 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
      83             :                 /* nop */
      84             :         }
      85             : 
      86           8 :         printf("insert\n");
      87         400 :         for (i = 1; i < rows_to_add; i++) {
      88         392 :                 sql_cmd(dbproc);
      89         392 :                 dbsqlexec(dbproc);
      90         392 :                 while (dbresults(dbproc) != NO_MORE_RESULTS) {
      91             :                         /* nop */
      92             :                 }
      93             :         }
      94             : 
      95           8 :         printf("first select\n");
      96           8 :         if (SUCCEED != sql_cmd(dbproc)) {
      97           0 :                 fprintf(stderr, "%s:%d: dbcmd failed\n", __FILE__, __LINE__);
      98           0 :                 failed = 1;
      99             :         }
     100           8 :         if (SUCCEED != dbsqlexec(dbproc)) {
     101           0 :                 fprintf(stderr, "%s:%d: dbsqlexec failed\n", __FILE__, __LINE__);
     102           0 :                 failed = 1;
     103             :         }
     104             : 
     105             : 
     106           8 :         if (dbresults(dbproc) != SUCCEED) {
     107           0 :                 printf("%s:%d: Was expecting a result set.", __FILE__, __LINE__);
     108           0 :                 failed = 1;
     109           0 :                 exit(1);
     110             :         }
     111             : 
     112          16 :         for (i = 1; i <= dbnumcols(dbproc); i++)
     113          16 :                 printf("col %d is %s\n", i, dbcolname(dbproc, i));
     114             : 
     115           8 :         dbbind(dbproc, 1, INTBIND, 0, (BYTE *) & testint);
     116           8 :         dbbind(dbproc, 2, STRINGBIND, 0, (BYTE *) teststr);
     117             : 
     118           8 :         get_results(dbproc, 1);
     119             : 
     120           8 :         testint = -1;
     121           8 :         strcpy(teststr, "bogus");
     122           8 :         printf("second select\n");
     123           8 :         sql_cmd(dbproc);
     124           8 :         dbsqlexec(dbproc);
     125             : 
     126           8 :         if ((rc = dbresults(dbproc)) != SUCCEED) {
     127           0 :                 printf("%s:%d: Was expecting a result set. (rc=%d)\n", __FILE__, __LINE__, rc);
     128           0 :                 failed = 1;
     129             :         }
     130             : 
     131           8 :         if (!failed) {
     132           8 :                 dbbind(dbproc, 1, INTBIND, 0, (BYTE *) & testint);
     133           8 :                 dbbind(dbproc, 2, STRINGBIND, 0, (BYTE *) teststr);
     134             : 
     135           8 :                 get_results(dbproc, 25);
     136             :         }
     137           8 :         dbexit();
     138             : 
     139           8 :         printf("%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
     140           8 :         return failed ? 1 : 0;
     141             : }

Generated by: LCOV version 1.13