LCOV - code coverage report
Current view: top level - src/dblib/unittests - t0005.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 102 146 69.9 %
Date: 2025-04-26 20:22:58 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* 
       2             :  * Purpose: Test data retrieval accuracy and cancelling results
       3             :  * Functions: dbbind dbcancel dbcmd dbcolname 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          10 :         int failed = 0;
      17             :         int expected_error;
      18             : 
      19          10 :         set_malloc_options();
      20             : 
      21          10 :         read_login_info(argc, argv);
      22          10 :         printf("Starting %s\n", argv[0]);
      23             : 
      24          10 :         dbinit();
      25             : 
      26          10 :         dberrhandle(syb_err_handler);
      27          10 :         dbmsghandle(syb_msg_handler);
      28             : 
      29          10 :         printf("About to logon\n");
      30             : 
      31          10 :         login = dblogin();
      32          10 :         DBSETLPWD(login, PASSWORD);
      33          10 :         DBSETLUSER(login, USER);
      34          10 :         DBSETLAPP(login, "t0005");
      35          10 :         DBSETLHOST(login, "ntbox.dntis.ro");
      36             : 
      37          10 :         printf("About to open\n");
      38             : 
      39          10 :         dbproc = dbopen(login, SERVER);
      40          10 :         if (strlen(DATABASE))
      41          10 :                 dbuse(dbproc, DATABASE);
      42          10 :         dbloginfree(login);
      43             : 
      44          10 :         printf("creating table\n");
      45          10 :         if (SUCCEED != sql_cmd(dbproc)) {
      46           0 :                 fprintf(stderr, "%s:%d: dbcmd failed\n", __FILE__, __LINE__);
      47           0 :                 failed = 1;
      48             :         }
      49             : 
      50          10 :         if (SUCCEED != dbsqlexec(dbproc)) {
      51           0 :                 fprintf(stderr, "%s:%d: dbcmd failed\n", __FILE__, __LINE__);
      52           0 :                 failed = 1;
      53             :         }
      54             : 
      55          20 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
      56             :                 /* nop */
      57             :         }
      58             : 
      59          10 :         printf("insert\n");
      60         500 :         for (i = 1; i < rows_to_add; i++) {
      61         490 :                 sql_cmd(dbproc);
      62         490 :                 dbsqlexec(dbproc);
      63         490 :                 while (dbresults(dbproc) != NO_MORE_RESULTS) {
      64             :                         /* nop */
      65             :                 }
      66             :         }
      67             : 
      68             : 
      69          10 :         sql_cmd(dbproc);
      70          10 :         dbsqlexec(dbproc);
      71             : 
      72          10 :         if (dbresults(dbproc) != SUCCEED) {
      73           0 :                 printf("Was expecting a result set.");
      74           0 :                 exit(1);
      75             :         }
      76             : 
      77          20 :         for (i = 1; i <= dbnumcols(dbproc); i++)
      78          20 :                 printf("col %d is %s\n", i, dbcolname(dbproc, i));
      79             : 
      80          10 :         dbbind(dbproc, 1, INTBIND, 0, (BYTE *) & testint);
      81          10 :         dbbind(dbproc, 2, STRINGBIND, 0, (BYTE *) teststr);
      82             : 
      83          50 :         for (i = 1; i < 5; i++) {
      84             :         char expected[1024];
      85             : 
      86          40 :                 sprintf(expected, "row %04d", i);
      87             : 
      88          40 :                 testint = -1;
      89          40 :                 strcpy(teststr, "bogus");
      90             : 
      91          40 :                 if (REG_ROW != dbnextrow(dbproc)) {
      92           0 :                         fprintf(stderr, "Failed.  Expected a row\n");
      93           0 :                         exit(1);
      94             :                 }
      95          40 :                 if (testint != i) {
      96           0 :                         fprintf(stderr, "Failed.  Expected i to be %d, was %d\n", i, (int) testint);
      97           0 :                         abort();
      98             :                 }
      99          40 :                 if (0 != strncmp(teststr, expected, strlen(expected))) {
     100           0 :                         printf("Failed.  Expected s to be |%s|, was |%s|\n", expected, teststr);
     101           0 :                         abort();
     102             :                 }
     103          40 :                 printf("Read a row of data -> %d %s\n", (int) testint, teststr);
     104             :         }
     105             : 
     106          10 :         printf("This query should succeeded as we have fetched the exact\n"
     107             :                         "number of rows in the result set\n");
     108             : 
     109          10 :         expected_error = 20019;
     110          10 :         dbsetuserdata(dbproc, (BYTE*) &expected_error);
     111             : 
     112          10 :         if (SUCCEED != sql_cmd(dbproc)) {
     113           0 :                 fprintf(stderr, "%s:%d: dbcmd failed\n", __FILE__, __LINE__);
     114           0 :                 failed = 1;
     115             :         }
     116          10 :         if (SUCCEED != dbsqlexec(dbproc)) {
     117           0 :                 fprintf(stderr, "%s:%d: dbsqlexec should have succeeded but didn't\n", __FILE__, __LINE__);
     118           0 :                 failed = 1;
     119             :         }
     120             : 
     121          10 :         if (dbresults(dbproc) != SUCCEED) {
     122           0 :                 printf("Was expecting a result set.");
     123           0 :                 exit(1);
     124             :         }
     125             : 
     126          20 :         for (i = 1; i <= dbnumcols(dbproc); i++)
     127          20 :                 printf("col %d is %s\n", i, dbcolname(dbproc, i));
     128             : 
     129          10 :         dbbind(dbproc, 1, INTBIND, 0, (BYTE *) & testint);
     130          10 :         dbbind(dbproc, 2, STRINGBIND, 0, (BYTE *) teststr);
     131             : 
     132          50 :         for (i = 1; i < 5; i++) {
     133             :         char expected[1024];
     134             : 
     135          40 :                 sprintf(expected, "row %04d", i);
     136             : 
     137          40 :                 testint = -1;
     138          40 :                 strcpy(teststr, "bogus");
     139             : 
     140          40 :                 if (REG_ROW != dbnextrow(dbproc)) {
     141           0 :                         fprintf(stderr, "Failed.  Expected a row\n");
     142           0 :                         exit(1);
     143             :                 }
     144          40 :                 if (testint != i) {
     145           0 :                         fprintf(stderr, "Failed.  Expected i to be %d, was %d\n", i, (int) testint);
     146           0 :                         abort();
     147             :                 }
     148          40 :                 if (0 != strncmp(teststr, expected, strlen(expected))) {
     149           0 :                         printf("Failed.  Expected s to be |%s|, was |%s|\n", expected, teststr);
     150           0 :                         abort();
     151             :                 }
     152          40 :                 printf("Read a row of data -> %d %s\n", (int) testint, teststr);
     153             :         }
     154             : 
     155          10 :         printf("Testing anticipated failure\n");
     156             : 
     157          10 :         if (SUCCEED != sql_cmd(dbproc)) {
     158           0 :                 fprintf(stderr, "%s:%d: dbcmd failed\n", __FILE__, __LINE__);
     159           0 :                 failed = 1;
     160             :         }
     161          10 :         if (SUCCEED == dbsqlexec(dbproc)) {
     162           0 :                 fprintf(stderr, "%s:%d: dbsqlexec should have failed but didn't\n", __FILE__, __LINE__);
     163           0 :                 failed = 1;
     164             :         }
     165             : 
     166             : 
     167          10 :         printf("calling dbcancel to flush results\n");
     168          10 :         dbcancel(dbproc);
     169             : 
     170          10 :         printf("Dropping proc\n");
     171          10 :         sql_cmd(dbproc);
     172          10 :         dbsqlexec(dbproc);
     173          10 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
     174             :                 /* nop */
     175             :         }
     176             : 
     177          10 :         printf("creating proc\n");
     178          10 :         sql_cmd(dbproc);
     179          10 :         if (dbsqlexec(dbproc) == FAIL) {
     180           0 :                 fprintf(stderr, "%s:%d: failed to create procedure\n", __FILE__, __LINE__);
     181           0 :                 failed = 1;
     182             :         }
     183          20 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
     184             :                 /* nop */
     185             :         }
     186             : 
     187          10 :         printf("calling proc\n");
     188          10 :         sql_cmd(dbproc);
     189          10 :         if (dbsqlexec(dbproc) == FAIL) {
     190           0 :                 fprintf(stderr, "%s:%d: failed to call procedure\n", __FILE__, __LINE__);
     191           0 :                 failed = 1;
     192             :         }
     193          10 :         if (dbresults(dbproc) != SUCCEED) {
     194           0 :                 printf("Was expecting a result set.");
     195           0 :                 exit(1);
     196             :         }
     197             : 
     198          20 :         for (i = 1; i <= dbnumcols(dbproc); i++)
     199          20 :                 printf("col %d is %s\n", i, dbcolname(dbproc, i));
     200             : 
     201          10 :         dbbind(dbproc, 1, INTBIND, 0, (BYTE *) & testint);
     202          10 :         dbbind(dbproc, 2, STRINGBIND, 0, (BYTE *) teststr);
     203             : 
     204          60 :         for (i = 1; i < 6; i++) {
     205             :         char expected[1024];
     206             : 
     207          50 :                 sprintf(expected, "row %04d", i);
     208             : 
     209          50 :                 testint = -1;
     210          50 :                 strcpy(teststr, "bogus");
     211             : 
     212          50 :                 if (REG_ROW != dbnextrow(dbproc)) {
     213           0 :                         fprintf(stderr, "Failed.  Expected a row\n");
     214           0 :                         exit(1);
     215             :                 }
     216          50 :                 if (testint != i) {
     217           0 :                         fprintf(stderr, "Failed.  Expected i to be %d, was %d\n", i, (int) testint);
     218           0 :                         abort();
     219             :                 }
     220          50 :                 if (0 != strncmp(teststr, expected, strlen(expected))) {
     221           0 :                         printf("Failed.  Expected s to be |%s|, was |%s|\n", expected, teststr);
     222           0 :                         abort();
     223             :                 }
     224          50 :                 printf("Read a row of data -> %d %s\n", (int) testint, teststr);
     225             :         }
     226             : 
     227          10 :         printf("Calling dbsqlexec before dbnextrow returns NO_MORE_ROWS\n");
     228          10 :         printf("The following command should succeed because\n"
     229             :                         "we have fetched the exact number of rows in the result set\n");
     230             : 
     231          10 :         if (SUCCEED != sql_cmd(dbproc)) {
     232           0 :                 fprintf(stderr, "%s:%d: dbcmd failed\n", __FILE__, __LINE__);
     233           0 :                 failed = 1;
     234             :         }
     235          10 :         if (SUCCEED != dbsqlexec(dbproc)) {
     236           0 :                 fprintf(stderr, "%s:%d: dbsqlexec should have succeeded but didn't\n", __FILE__, __LINE__);
     237           0 :                 failed = 1;
     238             :         }
     239             : 
     240          10 :         dbcancel(dbproc);
     241             : 
     242          10 :         sql_cmd(dbproc);
     243          10 :         dbsqlexec(dbproc);
     244          10 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
     245             :                 /* nop */
     246             :         }
     247             : 
     248          10 :         dbexit();
     249             : 
     250          10 :         printf("%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
     251          10 :         return failed ? 1 : 0;
     252             : }

Generated by: LCOV version 1.13