LCOV - code coverage report
Current view: top level - src/ctlib/unittests - blk_in.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 65 81 80.2 %
Date: 2025-01-18 12:13:41 Functions: 3 3 100.0 %

          Line data    Source code
       1             : #include <config.h>
       2             : 
       3             : #include <stdio.h>
       4             : #include <assert.h>
       5             : 
       6             : #if HAVE_STDLIB_H
       7             : #include <stdlib.h>
       8             : #endif /* HAVE_STDLIB_H */
       9             : 
      10             : #if HAVE_SYS_STAT_H
      11             : #include <sys/stat.h>
      12             : #endif /* HAVE_SYS_STAT_H */
      13             : 
      14             : #if HAVE_STRING_H
      15             : #include <string.h>
      16             : #endif /* HAVE_STRING_H */
      17             : 
      18             : 
      19             : #include <ctpublic.h>
      20             : #include <bkpublic.h>
      21             : #include "common.h"
      22             : #include "blk_in.h"
      23             : 
      24             : static CS_RETCODE
      25             : do_bind(CS_BLKDESC * blkdesc, int colnum, CS_INT host_format, CS_INT host_type, CS_INT host_maxlen, 
      26             :         void        *var_addr,
      27             :         CS_INT      *var_len_addr,
      28             :         CS_SMALLINT *var_ind_addr );
      29             : 
      30             : static char command[512];
      31             : 
      32             : /*
      33             :  * Static data for insertion
      34             :  */
      35             : int  not_null_bit = 1;
      36             : CS_INT      l_not_null_bit = 4;
      37             : CS_SMALLINT i_not_null_bit = 0;
      38             : 
      39             : char not_null_char[] = "a char";
      40             : CS_INT      l_not_null_char = 6;
      41             : CS_SMALLINT i_not_null_char = 0;
      42             : 
      43             : char not_null_varchar[] = "a varchar";
      44             : CS_INT      l_not_null_varchar = 9;
      45             : CS_SMALLINT i_not_null_varchar = 0;
      46             : 
      47             : char not_null_datetime[]                = "Dec 17 2003  3:44PM";
      48             : CS_INT      l_not_null_datetime = 19; 
      49             : CS_SMALLINT i_not_null_datetime = 0;
      50             : 
      51             : char not_null_smalldatetime[]   = "Dec 17 2003  3:44PM";
      52             : CS_INT      l_not_null_smalldatetime = 19;
      53             : CS_SMALLINT i_not_null_smalldatetime = 0;
      54             : 
      55             : char not_null_money[] = "12.34";
      56             : CS_INT      l_not_null_money = 5;
      57             : CS_SMALLINT i_not_null_money = 0;
      58             : 
      59             : char not_null_smallmoney[] = "12.34";
      60             : CS_INT      l_not_null_smallmoney = 5;
      61             : CS_SMALLINT i_not_null_smallmoney = 0;
      62             : 
      63             : char not_null_float[] = "12.34";
      64             : CS_INT      l_not_null_float = 5;
      65             : CS_SMALLINT i_not_null_float = 0;
      66             : 
      67             : char not_null_real[] = "12.34";
      68             : CS_INT      l_not_null_real = 5;
      69             : CS_SMALLINT i_not_null_real = 0;
      70             : 
      71             : char not_null_decimal[] = "12.34";
      72             : CS_INT      l_not_null_decimal = 5;
      73             : CS_SMALLINT i_not_null_decimal = 0;
      74             : 
      75             : char not_null_numeric[] = "12.34";
      76             : CS_INT      l_not_null_numeric = 5;
      77             : CS_SMALLINT i_not_null_numeric = 0;
      78             : 
      79             : int  not_null_int        = 1234;
      80             : CS_INT      l_not_null_int = 4;
      81             : CS_SMALLINT i_not_null_int = 0;
      82             : 
      83             : int  not_null_smallint   = 1234;
      84             : CS_INT      l_not_null_smallint = 4;
      85             : CS_SMALLINT i_not_null_smallint = 0;
      86             : 
      87             : int  not_null_tinyint    = 123;
      88             : CS_INT      l_not_null_tinyint = 4;
      89             : CS_SMALLINT i_not_null_tinyint = 0;
      90             : 
      91             : CS_INT      l_null_char = 0;
      92             : CS_SMALLINT i_null_char = -1;
      93             : 
      94             : CS_INT      l_null_varchar = 0;
      95             : CS_SMALLINT i_null_varchar = -1;
      96             : 
      97             : CS_INT      l_null_datetime = 0;
      98             : CS_SMALLINT i_null_datetime = -1;
      99             : 
     100             : CS_INT      l_null_smalldatetime = 0;
     101             : CS_SMALLINT i_null_smalldatetime = -1;
     102             : 
     103             : CS_INT      l_null_money = 0;
     104             : CS_SMALLINT i_null_money = -1;
     105             : 
     106             : CS_INT      l_null_smallmoney = 0;
     107             : CS_SMALLINT i_null_smallmoney = -1;
     108             : 
     109             : CS_INT      l_null_float = 0;
     110             : CS_SMALLINT i_null_float = -1;
     111             : 
     112             : CS_INT      l_null_real = 0;
     113             : CS_SMALLINT i_null_real = -1;
     114             : 
     115             : CS_INT      l_null_decimal = 0;
     116             : CS_SMALLINT i_null_decimal = -1;
     117             : 
     118             : CS_INT      l_null_numeric = 0;
     119             : CS_SMALLINT i_null_numeric = -1;
     120             : 
     121             : CS_INT      l_null_int = 0;
     122             : CS_SMALLINT i_null_int = -1;
     123             : 
     124             : CS_INT      l_null_smallint = 0;
     125             : CS_SMALLINT i_null_smallint = -1;
     126             : 
     127             : CS_INT      l_null_tinyint = 0;
     128             : CS_SMALLINT i_null_tinyint = -1;
     129             : 
     130             : static void
     131           8 : do_binds(CS_BLKDESC * blkdesc)
     132             : {
     133             :         enum { prefixlen = 0 };
     134             :         enum { termlen = 0 };
     135             :         enum NullValue { IsNull, IsNotNull };
     136             : 
     137             :         /* non nulls */
     138             : 
     139           8 :         do_bind(blkdesc, 1, CS_FMT_UNUSED,   CS_INT_TYPE,   4,  &not_null_bit, &l_not_null_bit, &i_not_null_bit);
     140           8 :         do_bind(blkdesc, 2, CS_FMT_NULLTERM, CS_CHAR_TYPE,  7,  not_null_char, &l_not_null_char, &i_not_null_char);
     141           8 :         do_bind(blkdesc, 3, CS_FMT_NULLTERM, CS_CHAR_TYPE,  10, not_null_varchar, &l_not_null_varchar, &i_not_null_varchar);
     142           8 :         do_bind(blkdesc, 4, CS_FMT_NULLTERM, CS_CHAR_TYPE,  20, not_null_datetime, &l_not_null_datetime, &i_not_null_datetime);
     143           8 :         do_bind(blkdesc, 5, CS_FMT_NULLTERM, CS_CHAR_TYPE,  20, not_null_smalldatetime, &l_not_null_smalldatetime, &i_not_null_smalldatetime);
     144           8 :         do_bind(blkdesc, 6, CS_FMT_NULLTERM, CS_CHAR_TYPE,  6,  not_null_money, &l_not_null_money, &i_not_null_money);
     145           8 :         do_bind(blkdesc, 7, CS_FMT_NULLTERM, CS_CHAR_TYPE,  6,  not_null_smallmoney, &l_not_null_smallmoney, &i_not_null_smallmoney);
     146           8 :         do_bind(blkdesc, 8, CS_FMT_NULLTERM, CS_CHAR_TYPE,  6,  not_null_float, &l_not_null_float, &i_not_null_float);
     147           8 :         do_bind(blkdesc, 9, CS_FMT_NULLTERM, CS_CHAR_TYPE,  6,  not_null_real, &l_not_null_real, &i_not_null_real);
     148           8 :         do_bind(blkdesc, 10, CS_FMT_NULLTERM, CS_CHAR_TYPE, 6,  not_null_decimal, &l_not_null_decimal, &i_not_null_decimal);
     149           8 :         do_bind(blkdesc, 11, CS_FMT_NULLTERM, CS_CHAR_TYPE, 6,  not_null_numeric, &l_not_null_numeric, &i_not_null_numeric);
     150           8 :         do_bind(blkdesc, 12, CS_FMT_UNUSED,   CS_INT_TYPE,  4,  &not_null_int, &l_not_null_int, &i_not_null_int);
     151           8 :         do_bind(blkdesc, 13, CS_FMT_UNUSED,   CS_INT_TYPE,  4,  &not_null_smallint, &l_not_null_smallint, &i_not_null_smallint);
     152           8 :         do_bind(blkdesc, 14, CS_FMT_UNUSED,   CS_INT_TYPE,  4,  &not_null_tinyint, &l_not_null_tinyint, &i_not_null_tinyint);
     153             : 
     154             :         /* nulls */
     155             : 
     156           8 :         do_bind(blkdesc, 15, CS_FMT_NULLTERM, CS_CHAR_TYPE, 7,  not_null_char, &l_null_char, &i_null_char);
     157           8 :         do_bind(blkdesc, 16, CS_FMT_NULLTERM, CS_CHAR_TYPE, 10, not_null_varchar, &l_null_varchar, &i_null_varchar);
     158           8 :         do_bind(blkdesc, 17, CS_FMT_NULLTERM, CS_CHAR_TYPE, 20, not_null_datetime, &l_null_datetime, &i_null_datetime);
     159           8 :         do_bind(blkdesc, 18, CS_FMT_NULLTERM, CS_CHAR_TYPE, 20, not_null_smalldatetime, &l_null_smalldatetime, &i_null_smalldatetime);
     160           8 :         do_bind(blkdesc, 19, CS_FMT_NULLTERM, CS_CHAR_TYPE, 6, not_null_money, &l_null_money, &i_null_money);
     161           8 :         do_bind(blkdesc, 20, CS_FMT_NULLTERM, CS_CHAR_TYPE, 6, not_null_smallmoney, &l_null_smallmoney, &i_null_smallmoney);
     162           8 :         do_bind(blkdesc, 21, CS_FMT_NULLTERM, CS_CHAR_TYPE, 6, not_null_float, &l_null_float, &i_null_float);
     163           8 :         do_bind(blkdesc, 22, CS_FMT_NULLTERM, CS_CHAR_TYPE, 6, not_null_real, &l_null_real, &i_null_real);
     164           8 :         do_bind(blkdesc, 23, CS_FMT_NULLTERM, CS_CHAR_TYPE, 6, not_null_decimal, &l_null_decimal, &i_null_decimal);
     165           8 :         do_bind(blkdesc, 24, CS_FMT_NULLTERM, CS_CHAR_TYPE, 6, not_null_numeric, &l_null_numeric, &i_null_numeric);
     166           8 :         do_bind(blkdesc, 25, CS_FMT_UNUSED,   CS_INT_TYPE,  4,  &not_null_int, &l_null_int, &i_null_int);
     167           8 :         do_bind(blkdesc, 26, CS_FMT_UNUSED,   CS_INT_TYPE,  4,  &not_null_smallint, &l_null_smallint, &i_null_smallint);
     168           8 :         do_bind(blkdesc, 27, CS_FMT_UNUSED,   CS_INT_TYPE,  4,  &not_null_tinyint, &l_null_tinyint, &i_null_tinyint);
     169             : 
     170           8 : }
     171             : 
     172             : static CS_RETCODE
     173         216 : do_bind(CS_BLKDESC * blkdesc, int colnum, CS_INT host_format, CS_INT host_type, CS_INT host_maxlen,
     174             :         void        *var_addr,
     175             :         CS_INT      *var_len_addr,
     176             :         CS_SMALLINT *var_ind_addr )
     177             : {
     178             :         CS_DATAFMT datafmt;
     179             :         CS_RETCODE ret;
     180             : 
     181         216 :         ret = blk_describe(blkdesc, colnum, &datafmt);
     182         216 :         if (ret != CS_SUCCEED) {
     183           0 :                 fprintf(stderr, "blk_describe(%d) failed", colnum);
     184           0 :                 return ret;
     185             :         }
     186             : 
     187         216 :         datafmt.format = host_format;
     188         216 :         datafmt.datatype = host_type;
     189         216 :         datafmt.maxlength = host_maxlen;
     190         216 :         datafmt.count = 1;
     191             : 
     192         216 :         ret = blk_bind(blkdesc, colnum, &datafmt, var_addr, var_len_addr, var_ind_addr );
     193         216 :         if (ret != CS_SUCCEED) {
     194           0 :                 fprintf(stderr, "blk_bind() failed\n");
     195           0 :                 return ret;
     196             :         }
     197             :         return ret;
     198             : }
     199             : 
     200             : 
     201             : int
     202           8 : main(int argc, char **argv)
     203             : {
     204             :         CS_CONTEXT *ctx;
     205             :         CS_CONNECTION *conn;
     206             :         CS_COMMAND *cmd;
     207             :         CS_BLKDESC *blkdesc;
     208           8 :         int verbose = 0;
     209           8 :         int count = 0;
     210             :         int ret;
     211             :         int i;
     212             : 
     213           8 :         const char *table_name = "all_types_bcp_unittest";
     214             : 
     215           8 :         printf("%s: Retrieve data using array binding \n", __FILE__);
     216             :         if (verbose) {
     217             :                 printf("Trying login\n");
     218             :         }
     219           8 :         ret = try_ctlogin(&ctx, &conn, &cmd, verbose);
     220           8 :         if (ret != CS_SUCCEED) {
     221           0 :                 fprintf(stderr, "Login failed\n");
     222           0 :                 return 1;
     223             :         }
     224             : 
     225           8 :         sprintf(command,"if exists (select 1 from sysobjects where type = 'U' and name = '%s') drop table %s", 
     226             :                     table_name, table_name);
     227             : 
     228           8 :         ret = run_command(cmd, command);
     229           8 :         if (ret != CS_SUCCEED)
     230             :                 return 1;
     231             : 
     232           8 :         ret = run_command(cmd, create_table_sql);
     233           8 :         if (ret != CS_SUCCEED)
     234             :                 return 1;
     235             : 
     236           8 :         ret = blk_alloc(conn, BLK_VERSION_100, &blkdesc);
     237           8 :         if (ret != CS_SUCCEED) {
     238           0 :                 fprintf(stderr, "blk_alloc() failed\n");
     239           0 :                 return 1;
     240             :         }
     241             : 
     242           8 :         ret = blk_init(blkdesc, CS_BLK_IN, (char *) table_name, CS_NULLTERM );
     243             : 
     244           8 :         if (ret != CS_SUCCEED) {
     245           0 :                 fprintf(stderr, "blk_init() failed\n");
     246           0 :                 return 1;
     247             :         }
     248             : 
     249           8 :         do_binds(blkdesc);
     250             : 
     251           8 :         printf("Sending same row 10 times... \n");
     252          88 :         for (i=0; i<10; i++) {
     253          80 :                 if((ret = blk_rowxfer(blkdesc)) != CS_SUCCEED) {
     254           0 :                         fprintf(stderr, "blk_rowxfer() failed\n");
     255           0 :                         return 1;
     256             :                 }
     257             :         }
     258             : 
     259           8 :         ret = blk_done(blkdesc, CS_BLK_ALL, &count);
     260           8 :         if (ret != CS_SUCCEED) {
     261           0 :                 fprintf(stderr, "blk_done() failed\n");
     262           0 :                 return 1;
     263             :         }
     264             : 
     265           8 :         blk_drop(blkdesc);
     266             : 
     267           8 :         printf("%d rows copied.\n", count);
     268             : 
     269           8 :         printf("done\n");
     270             : 
     271           8 :         ret = try_ctlogout(ctx, conn, cmd, verbose);
     272           8 :         if (ret != CS_SUCCEED) {
     273           0 :                 fprintf(stderr, "Logout failed\n");
     274           0 :                 return 1;
     275             :         }
     276             : 
     277             :         return 0;
     278             : }

Generated by: LCOV version 1.13