LTP GCOV extension - code coverage report
Current view: directory - dblib/unittests - t0016.c
Test: FreeTDS coverage
Date: 2008-08-29 Instrumented lines: 81
Code covered: 84.0 % Executed lines: 68

       1                 : /* 
       2                 :  * Purpose: Test bcp in and out, and specifically bcp_colfmt()
       3                 :  * Functions: bcp_colfmt bcp_columns bcp_exec bcp_init 
       4                 :  */
       5                 : 
       6                 : #if HAVE_CONFIG_H
       7                 : #include <config.h>
       8                 : #endif /* HAVE_CONFIG_H */
       9                 : 
      10                 : #include <stdio.h>
      11                 : 
      12                 : #if HAVE_STDLIB_H
      13                 : #include <stdlib.h>
      14                 : #endif /* HAVE_STDLIB_H */
      15                 : 
      16                 : #if HAVE_STRING_H
      17                 : #include <string.h>
      18                 : #endif /* HAVE_STRING_H */
      19                 : 
      20                 : #include <sqlfront.h>
      21                 : #include <sqldb.h>
      22                 : 
      23                 : #include "common.h"
      24                 : 
      25                 : static char software_version[] = "$Id: t0016.c,v 1.23 2005/05/23 08:06:25 freddy77 Exp $";
      26                 : static void *no_unused_var_warn[] = { software_version, no_unused_var_warn };
      27                 : int failed = 0;
      28                 : 
      29                 : 
      30                 : int
      31                 : main(int argc, char *argv[])
      32               2 : {
      33                 :         LOGINREC *login;
      34                 :         DBPROCESS *dbproc;
      35                 :         int i;
      36                 :         char sqlCmd[256];
      37                 :         RETCODE ret;
      38               2 :         const char *out_file = "t0016.out";
      39               2 :         const char *in_file = FREETDS_SRCDIR "/t0016.in";
      40               2 :         const char *err_file = "t0016.err";
      41                 :         DBINT rows_copied;
      42               2 :         int num_cols = 0;
      43                 : 
      44               2 :         set_malloc_options();
      45                 : 
      46               2 :         read_login_info(argc, argv);
      47               2 :         fprintf(stdout, "Start\n");
      48               2 :         dbinit();
      49                 : 
      50               2 :         dberrhandle(syb_err_handler);
      51               2 :         dbmsghandle(syb_msg_handler);
      52                 : 
      53               2 :         fprintf(stdout, "About to logon\n");
      54                 : 
      55               2 :         login = dblogin();
      56               2 :         BCP_SETL(login, TRUE);
      57               2 :         DBSETLPWD(login, PASSWORD);
      58               2 :         DBSETLUSER(login, USER);
      59               2 :         DBSETLAPP(login, "t0016");
      60                 : 
      61               2 :         dbproc = dbopen(login, SERVER);
      62               2 :         if (strlen(DATABASE)) {
      63               2 :                 dbuse(dbproc, DATABASE);
      64                 :         }
      65               2 :         dbloginfree(login);
      66               2 :         fprintf(stdout, "After logon\n");
      67                 : 
      68               2 :         fprintf(stdout, "Creating table\n");
      69               2 :         strcpy(sqlCmd, "create table #dblib0016 (f1 int not null, s1 int null, f2 numeric(10,2) null, ");
      70               2 :         strcat(sqlCmd, "f3 varchar(255) not null, f4 datetime null) ");
      71               2 :         dbcmd(dbproc, sqlCmd);
      72               2 :         dbsqlexec(dbproc);
      73               4 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
      74                 :                 /* nop */
      75                 :         }
      76                 : 
      77                 :         /* BCP in */
      78                 : 
      79               2 :         ret = bcp_init(dbproc, "#dblib0016", in_file, err_file, DB_IN);
      80               2 :         if (ret != SUCCEED)
      81               0 :                 failed = 1;
      82                 : 
      83               2 :         fprintf(stdout, "return from bcp_init = %d\n", ret);
      84                 : 
      85               2 :         ret = dbcmd(dbproc, "select * from #dblib0016 where 0=1");
      86               2 :         fprintf(stdout, "return from dbcmd = %d\n", ret);
      87                 : 
      88               2 :         ret = dbsqlexec(dbproc);
      89               2 :         fprintf(stdout, "return from dbsqlexec = %d\n", ret);
      90                 : 
      91               2 :         if (dbresults(dbproc) != FAIL) {
      92               2 :                 num_cols = dbnumcols(dbproc);
      93               2 :                 fprintf(stdout, "Number of columns = %d\n", num_cols);
      94                 : 
      95               2 :                 while (dbnextrow(dbproc) != NO_MORE_ROWS) {
      96                 :                 }
      97                 :         }
      98                 : 
      99               2 :         ret = bcp_columns(dbproc, num_cols);
     100               2 :         if (ret != SUCCEED)
     101               0 :                 failed = 1;
     102               2 :         fprintf(stdout, "return from bcp_columns = %d\n", ret);
     103                 : 
     104              10 :         for (i = 1; i < num_cols; i++) {
     105               8 :                 if ((ret = bcp_colfmt(dbproc, i, SYBCHAR, 0, -1, (const BYTE *) "\t", sizeof(char), i)) == FAIL) {
     106               0 :                         fprintf(stdout, "return from bcp_colfmt = %d\n", ret);
     107               0 :                         failed = 1;
     108                 :                 }
     109                 :         }
     110                 : 
     111               2 :         if ((ret = bcp_colfmt(dbproc, num_cols, SYBCHAR, 0, -1, (const BYTE *) "\n", sizeof(char), num_cols)) == FAIL) {
     112               0 :                 fprintf(stdout, "return from bcp_colfmt = %d\n", ret);
     113               0 :                 failed = 1;
     114                 :         }
     115                 : 
     116                 : 
     117               2 :         ret = bcp_exec(dbproc, &rows_copied);
     118               2 :         if (ret != SUCCEED)
     119               0 :                 failed = 1;
     120                 : 
     121               2 :         fprintf(stdout, "%d rows copied in\n", rows_copied);
     122                 : 
     123                 :         /* BCP out */
     124                 : 
     125               2 :         rows_copied = 0;
     126               2 :         ret = bcp_init(dbproc, "#dblib0016", out_file, err_file, DB_OUT);
     127               2 :         if (ret != SUCCEED)
     128               0 :                 failed = 1;
     129                 : 
     130               2 :         printf("select\n");
     131               2 :         dbcmd(dbproc, "select * from #dblib0016 where 0=1");
     132               2 :         dbsqlexec(dbproc);
     133                 : 
     134               2 :         if (dbresults(dbproc) != FAIL) {
     135               2 :                 num_cols = dbnumcols(dbproc);
     136               2 :                 while (dbnextrow(dbproc) != NO_MORE_ROWS) {
     137                 :                 }
     138                 :         }
     139                 : 
     140               2 :         ret = bcp_columns(dbproc, num_cols);
     141                 : 
     142              10 :         for (i = 1; i < num_cols; i++) {
     143               8 :                 if ((ret = bcp_colfmt(dbproc, i, SYBCHAR, 0, -1, (const BYTE *) "\t", sizeof(char), i)) == FAIL) {
     144               0 :                         fprintf(stdout, "return from bcp_colfmt = %d\n", ret);
     145               0 :                         failed = 1;
     146                 :                 }
     147                 :         }
     148                 : 
     149               2 :         if ((ret = bcp_colfmt(dbproc, num_cols, SYBCHAR, 0, -1, (const BYTE *) "\n", sizeof(char), num_cols)) == FAIL) {
     150               0 :                 fprintf(stdout, "return from bcp_colfmt = %d\n", ret);
     151               0 :                 failed = 1;
     152                 :         }
     153                 : 
     154               2 :         ret = bcp_exec(dbproc, &rows_copied);
     155               2 :         if (ret != SUCCEED)
     156               0 :                 failed = 1;
     157                 : 
     158               2 :         fprintf(stdout, "%d rows copied out\n", rows_copied);
     159               2 :         dbclose(dbproc);
     160               2 :         dbexit();
     161                 : 
     162               2 :         fprintf(stdout, "dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__);
     163               2 :         return failed ? 1 : 0;
     164                 : }

Generated by: LTP GCOV extension version 1.6