LCOV - code coverage report
Current view: top level - src/dblib/unittests - bcp2.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 83 105 79.0 %
Date: 2025-01-18 11:50:39 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /*
       2             :  * Purpose: Test bcp functions, specifically some NULL behaviour
       3             :  * Functions: bcp_bind bcp_done bcp_init bcp_sendrow
       4             :  */
       5             : 
       6             : #include "common.h"
       7             : 
       8             : #include <assert.h>
       9             : 
      10             : static void
      11           0 : doexit(int value)
      12             : {
      13           0 :         dbexit();            /* always call dbexit before returning to OS */
      14           0 :         exit(value);
      15             : }
      16             : 
      17             : static int
      18           8 : init(DBPROCESS * dbproc, const char *name)
      19             : {
      20             :         RETCODE rc;
      21             : 
      22           8 :         printf("Dropping %s.%s..%s\n", SERVER, DATABASE, name);
      23           8 :         sql_cmd(dbproc);
      24           8 :         dbsqlexec(dbproc);
      25          28 :         while ((rc=dbresults(dbproc)) == SUCCEED)
      26          12 :                 continue;
      27           8 :         if (rc != NO_MORE_RESULTS)
      28             :                 return 1;
      29             : 
      30           8 :         printf("Creating %s.%s..%s\n", SERVER, DATABASE, name);
      31           8 :         sql_cmd(dbproc);
      32             : 
      33           8 :         if (dbsqlexec(dbproc) == FAIL)
      34             :                 return 1;
      35          16 :         while ((rc=dbresults(dbproc)) == SUCCEED)
      36           8 :                 continue;
      37           8 :         if (rc != NO_MORE_RESULTS)
      38             :                 return 1;
      39           8 :         printf("ok\n");
      40           8 :         return 0;
      41             : }
      42             : 
      43             : int
      44           8 : main(int argc, char **argv)
      45             : {
      46             :         LOGINREC *login;
      47             :         DBPROCESS *dbproc;
      48             :         DBINT msgno;
      49           8 :         int num_rows, expected = 2;
      50             :         static const char table_name[] = "bcp_test";
      51             : 
      52             :         /* Variables for host file data to be copied to database. */
      53             :         char s1[11];
      54             :         char s2[11];
      55             :         char s3[11];
      56             : 
      57           8 :         set_malloc_options();
      58             : 
      59           8 :         read_login_info(argc, argv);
      60             : 
      61           8 :         printf("Starting %s\n", argv[0]);
      62             : 
      63           8 :         dbsetversion(DBVERSION_100);
      64           8 :         dbinit();
      65             : 
      66           8 :         dberrhandle(syb_err_handler);
      67           8 :         dbmsghandle(syb_msg_handler);
      68             : 
      69           8 :         printf("About to logon\n");
      70             : 
      71           8 :         login = dblogin();
      72           8 :         DBSETLPWD(login, PASSWORD);
      73           8 :         DBSETLUSER(login, USER);
      74           8 :         DBSETLAPP(login, "bcp2.c unit test");
      75           8 :         BCP_SETL(login, TRUE);
      76             : 
      77           8 :         printf("About to open %s.%s\n", SERVER, DATABASE);
      78             : 
      79           8 :         dbproc = dbopen(login, SERVER);
      80           8 :         if (strlen(DATABASE))
      81           8 :                 dbuse(dbproc, DATABASE);
      82           8 :         dbloginfree(login);
      83             : 
      84           8 :         if (init(dbproc, table_name))
      85           0 :                 doexit(1);
      86             : 
      87             :         /* set up and send the bcp */
      88           8 :         strcpy(s1, "test short");
      89           8 :         strcpy(s2, " ");
      90           8 :         strcpy(s3, "");
      91             : 
      92           8 :         if (bcp_init(dbproc, "bcp_test", NULL, "bcp.errors", DB_IN) != SUCCEED)
      93           0 :                 doexit(1);
      94             : 
      95           8 :         bcp_bind(dbproc, (BYTE *)s1, 0, -1, (BYTE *)"", 1, SYBCHAR, 1);
      96           8 :         bcp_bind(dbproc, (BYTE *)s2, 0, -1, (BYTE *)"", 1, SYBCHAR, 2);
      97           8 :         bcp_bind(dbproc, (BYTE *)s3, 0, -1, (BYTE *)"", 1, SYBCHAR, 3);
      98             : 
      99           8 :         printf("Sending some rows... \n");
     100           8 :         if (bcp_sendrow(dbproc) == FAIL) {
     101           0 :                 fprintf(stderr, "send failed\n");
     102           0 :                 doexit(1);
     103             :         }
     104             : 
     105           8 :         strcpy(s2, "x");
     106           8 :         strcpy(s3, " ");
     107           8 :         if (bcp_sendrow(dbproc) == FAIL) {
     108           0 :                 fprintf(stderr, "send failed\n");
     109           0 :                 doexit(1);
     110             :         }
     111             : 
     112             :         /* In MSSQL the error is reported during bcp_done but all inserts are ignored */
     113           8 :         if (DBTDS_5_0 == DBTDS(dbproc)) {
     114           2 :                 msgno = 20073;
     115           2 :                 dbsetuserdata(dbproc, (BYTE*) &msgno);
     116           2 :                 strcpy(s2, "");
     117           2 :                 strcpy(s3, "");
     118           2 :                 if (bcp_sendrow(dbproc) != FAIL) {
     119           0 :                         fprintf(stderr, "send NULL succeeded\n");
     120           0 :                         doexit(1);
     121             :                 }
     122           2 :                 dbsetuserdata(dbproc, NULL);
     123             : 
     124             :                 /* end bcp. */
     125           2 :                 if (bcp_done(dbproc) != expected) {
     126           0 :                         fprintf(stderr, "Bulk copy unsuccessful.\n");
     127           0 :                         doexit(1);
     128             :                 }
     129             :         } else {
     130             :                 /* end bcp. */
     131           6 :                 if (bcp_done(dbproc) != expected) {
     132           0 :                         fprintf(stderr, "Bulk copy unsuccessful.\n");
     133           0 :                         doexit(1);
     134             :                 }
     135             : 
     136             :                 /* another insert with error */
     137           6 :                 if (bcp_init(dbproc, "bcp_test", NULL, "bcp.errors", DB_IN) != SUCCEED)
     138           0 :                         doexit(1);
     139             : 
     140           6 :                 bcp_bind(dbproc, (BYTE *)s1, 0, -1, (BYTE *)"", 1, SYBCHAR, 1);
     141           6 :                 bcp_bind(dbproc, (BYTE *)s2, 0, -1, (BYTE *)"", 1, SYBCHAR, 2);
     142           6 :                 bcp_bind(dbproc, (BYTE *)s3, 0, -1, (BYTE *)"", 1, SYBCHAR, 3);
     143             : 
     144           6 :                 strcpy(s2, "");
     145           6 :                 strcpy(s3, "");
     146           6 :                 if (bcp_sendrow(dbproc) == FAIL) {
     147           0 :                         fprintf(stderr, "send failed\n");
     148           0 :                         doexit(1);
     149             :                 }
     150             : 
     151             :                 /* end bcp. */
     152           6 :                 msgno = 515;
     153           6 :                 dbsetuserdata(dbproc, (BYTE*) &msgno);
     154           6 :                 num_rows = bcp_done(dbproc);
     155           6 :                 if (num_rows != -1) {
     156           0 :                         fprintf(stderr, "Bulk copy successful. %d rows returned\n", num_rows);
     157           0 :                         doexit(1);
     158             :                 }
     159           6 :                 dbsetuserdata(dbproc, NULL);
     160             :         }
     161             : 
     162           8 :         printf("done\n");
     163             : 
     164             :         /* check we inserted the expected number of rows row */
     165           8 :         num_rows = 0;
     166           8 :         sql_cmd(dbproc);
     167           8 :         dbsqlexec(dbproc);
     168          24 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
     169          24 :                 while (dbnextrow(dbproc) == REG_ROW)
     170          16 :                         num_rows++;
     171             :         }
     172           8 :         if (num_rows != expected) {
     173           0 :                 fprintf(stderr, "Expected %d row(s), got %d\n", expected, num_rows);
     174           0 :                 doexit(1);
     175             :         }
     176             : 
     177           8 :         printf("Dropping table %s\n", table_name);
     178           8 :         sql_cmd(dbproc);
     179           8 :         dbsqlexec(dbproc);
     180           8 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
     181             :                 /* nop */
     182             :         }
     183           8 :         dbexit();
     184             : 
     185           8 :         printf("%s OK\n", __FILE__);
     186             :         return 0;
     187             : }

Generated by: LCOV version 1.13