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

Generated by: LCOV version 1.13