LCOV - code coverage report
Current view: top level - src/dblib/unittests - t0003.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 51 67 76.1 %
Date: 2025-04-19 15:09:27 Functions: 1 1 100.0 %

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

Generated by: LCOV version 1.13