LCOV - code coverage report
Current view: top level - src/dblib/unittests - t0015.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 52 74 70.3 %
Date: 2024-03-23 08:24:27 Functions: 1 1 100.0 %

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

Generated by: LCOV version 1.13