LCOV - code coverage report
Current view: top level - src/dblib/unittests - t0017.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 116 128 90.6 %
Date: 2025-04-24 11:49:38 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* 
       2             :  * Purpose: Test bcp in, with dbvarylen()
       3             :  * Functions: bcp_colfmt bcp_columns bcp_exec bcp_init dbvarylen 
       4             :  */
       5             : 
       6             : #include "common.h"
       7             : #include <assert.h>
       8             : 
       9          10 : TEST_MAIN()
      10             : {
      11          10 :         int failed = 0;
      12             :         LOGINREC *login;
      13             :         DBPROCESS *dbproc;
      14             :         int i;
      15             :         RETCODE ret;
      16          10 :         int big_endian = 1;
      17             : 
      18          10 :         char *out_file = "t0017.out";
      19             :         static const char in_file_le[] = FREETDS_SRCDIR "/t0017.in";
      20             :         static const char in_file_be[] = FREETDS_SRCDIR "/t0017.in.be";
      21          10 :         const char *in_file = in_file_le;
      22          10 :         const char *err_file = "t0017.err";
      23             :         DBINT rows_copied;
      24          10 :         int num_cols = 0;
      25             :         int col_type[256];
      26             :         DBBOOL col_varylen[256];
      27             :         int prefix_len;
      28             : 
      29             :         if (((char *) &big_endian)[0] == 1)
      30          10 :                 big_endian = 0;
      31             :         if (big_endian)
      32             :                 in_file = in_file_be;
      33             : 
      34          10 :         setbuf(stdout, NULL);
      35          10 :         setbuf(stderr, NULL);
      36             : 
      37          10 :         set_malloc_options();
      38             : 
      39          10 :         read_login_info(argc, argv);
      40          10 :         printf("Starting %s\n", argv[0]);
      41          10 :         dbinit();
      42             : 
      43          10 :         dberrhandle(syb_err_handler);
      44          10 :         dbmsghandle(syb_msg_handler);
      45             : 
      46          10 :         printf("About to logon ... ");
      47             : 
      48          10 :         login = dblogin();
      49          10 :         assert(login);
      50          10 :         BCP_SETL(login, TRUE);
      51          10 :         DBSETLPWD(login, PASSWORD);
      52          10 :         DBSETLUSER(login, USER);
      53          10 :         DBSETLAPP(login, "t0017");
      54          10 :         printf("done\n");
      55             : 
      56          10 :         printf("Opening \"%s\" for \"%s\" ... ", SERVER, USER);
      57          10 :         dbproc = dbopen(login, SERVER);
      58          10 :         assert(dbproc);
      59          10 :         if (strlen(DATABASE)) {
      60          10 :                 dbuse(dbproc, DATABASE);
      61             :         }
      62          10 :         dbloginfree(login);
      63          10 :         printf("done\n");
      64             : 
      65          10 :         printf("Creating table ... ");
      66          10 :         sql_cmd(dbproc);
      67          10 :         dbsqlexec(dbproc);
      68          10 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
      69             :                 /* nop */
      70             :         }
      71          10 :         printf("done\n");
      72             : 
      73          10 :         sql_cmd(dbproc);
      74          10 :         dbsqlexec(dbproc);
      75          10 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
      76             :                 /* nop */
      77             :         }
      78             : 
      79             :         /* BCP out */
      80          10 :         printf("bcp_init... ");
      81          10 :         ret = bcp_init(dbproc, "#dblib0017", out_file, err_file, DB_OUT);
      82          10 :         if (ret != SUCCEED)
      83           0 :                 failed = 1;
      84          10 :         printf("done\n");
      85             : 
      86          10 :         printf("Issuing SELECT ... ");
      87          10 :         sql_cmd(dbproc);
      88          10 :         dbsqlexec(dbproc);
      89          10 :         printf("done\nFetching metadata ... ");
      90          10 :         if (dbresults(dbproc) != FAIL) {
      91          10 :                 num_cols = dbnumcols(dbproc);
      92          30 :                 for (i = 0; i < num_cols; ++i) {
      93          20 :                         col_type[i] = dbcoltype(dbproc, i + 1);
      94          20 :                         col_varylen[i] = dbvarylen(dbproc, i + 1);
      95             :                 }
      96          10 :                 while (dbnextrow(dbproc) != NO_MORE_ROWS) {
      97             :                 }
      98             :         }
      99          10 :         printf("done\n");
     100             : 
     101          10 :         printf("bcp_columns ... ");
     102          10 :         ret = bcp_columns(dbproc, num_cols);
     103          10 :         if (ret != SUCCEED)
     104           0 :                 failed = 1;
     105          30 :         for (i = 0; i < num_cols; i++) {
     106          20 :                 prefix_len = 0;
     107          20 :                 if (col_type[i] == SYBIMAGE || col_type[i] == SYBTEXT) {
     108             :                         prefix_len = 4;
     109          10 :                 } else if (col_varylen[i]) {
     110          10 :                         prefix_len = 1;
     111             :                 }
     112          20 :                 printf("bind %d prefix %d col_type %s\n", i, prefix_len, col_type[i] == SYBIMAGE ? "image" : "other");
     113          20 :                 ret = bcp_colfmt(dbproc, i + 1, col_type[i], prefix_len, -1, NULL, 0, i + 1);
     114          20 :                 if (ret == FAIL) {
     115           0 :                         fprintf(stderr, "return from bcp_colfmt = %d\n", ret);
     116           0 :                         failed = 1;
     117             :                 }
     118             :         }
     119          10 :         printf("done\n");
     120             : 
     121          10 :         rows_copied = -1;
     122          10 :         printf("bcp_exec ... ");
     123          10 :         ret = bcp_exec(dbproc, &rows_copied);
     124          10 :         if (ret != SUCCEED || rows_copied != 1)
     125           0 :                 failed = 1;
     126             : 
     127          10 :         printf("%d rows copied\n", rows_copied);
     128             : 
     129             :         /* delete rows */
     130          10 :         sql_cmd(dbproc);
     131          10 :         dbsqlexec(dbproc);
     132          10 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
     133             :                 /* nop */
     134             :         }
     135             : 
     136             :         /* 
     137             :          * BCP in 
     138             :          */
     139          10 :         printf("bcp_init... ");
     140          10 :         ret = bcp_init(dbproc, "#dblib0017", in_file, err_file, DB_IN);
     141          10 :         if (ret != SUCCEED)
     142           0 :                 failed = 1;
     143          10 :         printf("done\n");
     144             : 
     145          10 :         printf("Issuing SELECT ... ");
     146          10 :         sql_cmd(dbproc);
     147          10 :         dbsqlexec(dbproc);
     148          10 :         printf("done\nFetching metadata ... ");
     149          10 :         if (dbresults(dbproc) != FAIL) {
     150          10 :                 num_cols = dbnumcols(dbproc);
     151          30 :                 for (i = 0; i < num_cols; i++) {
     152          20 :                         col_type[i] = dbcoltype(dbproc, i + 1);
     153          20 :                         col_varylen[i] = dbvarylen(dbproc, i + 1);
     154             :                 }
     155          10 :                 while (dbnextrow(dbproc) != NO_MORE_ROWS) {
     156             :                 }
     157             :         }
     158          10 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
     159             :                 /* nop */
     160             :         }
     161          10 :         printf("done\n");
     162             : 
     163          10 :         printf("bcp_columns ... ");
     164          10 :         ret = bcp_columns(dbproc, num_cols);
     165          10 :         if (ret != SUCCEED)
     166           0 :                 failed = 1;
     167          30 :         for (i = 0; i < num_cols; i++) {
     168          20 :                 prefix_len = 0;
     169          20 :                 if (col_type[i] == SYBIMAGE || col_type[i] == SYBTEXT) {
     170             :                         prefix_len = 4;
     171          10 :                 } else if (col_varylen[i]) {
     172          10 :                         prefix_len = 1;
     173             :                 }
     174          20 :                 ret = bcp_colfmt(dbproc, i + 1, col_type[i], prefix_len, -1, NULL, 0, i + 1);
     175          20 :                 if (ret == FAIL) {
     176           0 :                         fprintf(stderr, "return from bcp_colfmt = %d\n", ret);
     177           0 :                         failed = 1;
     178             :                 }
     179             :         }
     180          10 :         printf("done\n");
     181             : 
     182          10 :         printf("bcp_exec ... ");
     183          10 :         rows_copied = -1;
     184          10 :         ret = bcp_exec(dbproc, &rows_copied);
     185          10 :         if (ret != SUCCEED || rows_copied != 1)
     186           0 :                 failed = 1;
     187          10 :         printf("done\n");
     188             : 
     189             : 
     190             :         /* test we inserted correctly row */
     191          10 :         if (!failed) {
     192          10 :                 sql_cmd(dbproc);
     193          10 :                 dbsqlexec(dbproc);
     194          54 :                 while (dbresults(dbproc) != NO_MORE_RESULTS) {
     195          34 :                         while ((ret=dbnextrow(dbproc)) != NO_MORE_ROWS) {
     196           0 :                                 fprintf(stderr, "Invalid dbnextrow result %d executing query\n", ret);
     197           0 :                                 failed = 1;
     198             :                         }
     199             :                 }
     200             :         }
     201             : 
     202          10 :         printf("%d rows copied\n", rows_copied);
     203          10 :         dbclose(dbproc);
     204          10 :         dbexit();
     205             : 
     206          10 :         printf("dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__);
     207          10 :         return failed ? 1 : 0;
     208             : }

Generated by: LCOV version 1.13