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

Generated by: LCOV version 1.13