LCOV - code coverage report
Current view: top level - src/ctlib/unittests - long_binary.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 54 58 93.1 %
Date: 2025-01-18 11:50:39 Functions: 3 4 75.0 %

          Line data    Source code
       1             : /* Test we can insert long binary into database.
       2             :  */
       3             : #include "common.h"
       4             : 
       5             : #include <freetds/macros.h>
       6             : 
       7             : static const CS_INT unused = CS_UNUSED, nullterm = CS_NULLTERM;
       8             : static CS_INT result_len = -1;
       9             : 
      10             : static CS_RETCODE
      11           0 : csmsg_callback(CS_CONTEXT *ctx TDS_UNUSED, CS_CLIENTMSG * emsgp)
      12             : {
      13           0 :         printf("message from csmsg_callback(): %s\n", emsgp->msgstring);
      14           0 :         return CS_SUCCEED;
      15             : }
      16             : 
      17             : static int
      18          40 : fetch_results(CS_COMMAND * command)
      19             : {
      20             :         CS_INT result_type, int_result, copied, rows_read;
      21          40 :         CS_SMALLINT ind = 0;
      22             :         CS_DATAFMT datafmt;
      23          40 :         int result = 0;
      24             : 
      25          40 :         memset(&datafmt, 0, sizeof(datafmt));
      26          40 :         datafmt.datatype = CS_INT_TYPE;
      27          40 :         datafmt.count = 1;
      28             : 
      29          40 :         check_call(ct_results, (command, &result_type));
      30             :         do {
      31         108 :                 if (result_type == CS_ROW_RESULT) {
      32           4 :                         check_call(ct_bind, (command, 1, &datafmt, &int_result, &copied, &ind));
      33           4 :                         check_call(ct_fetch, (command, CS_UNUSED, CS_UNUSED, CS_UNUSED, &rows_read));
      34           4 :                         printf("received %d bytes\n", (int) int_result);
      35           4 :                         result_len = int_result;
      36             :                 }
      37         108 :                 if (result_type == CS_CMD_FAIL) {
      38           4 :                         result = 1;
      39             :                 }
      40         108 :         } while (ct_results(command, &result_type) == CS_SUCCEED);
      41          40 :         return result;
      42             : }
      43             : 
      44             : static int
      45          36 : execute_sql(CS_COMMAND * command, const char *sql)
      46             : {
      47          36 :         printf("executing sql: %s\n", sql);
      48          36 :         check_call(ct_command, (command, CS_LANG_CMD, sql, nullterm, unused));
      49          36 :         check_call(ct_send, (command));
      50          36 :         return fetch_results(command);
      51             : }
      52             : 
      53             : int
      54           8 : main(int argc, char **argv)
      55             : {
      56           8 :         int verbose = 0;
      57             :         CS_COMMAND *command;
      58             :         CS_CONNECTION *connection;
      59             :         CS_CONTEXT *context;
      60             :         unsigned char buffer[65536];
      61           8 :         CS_INT buffer_len = 8192;
      62           8 :         CS_SMALLINT ind = 0;
      63             :         CS_DATAFMT datafmt;
      64             :         CS_INT ret;
      65             :         int i;
      66             : 
      67           8 :         printf("-- begin --\n");
      68             : 
      69           8 :         check_call(try_ctlogin, (&context, &connection, &command, verbose));
      70           8 :         check_call(cs_config, (context, CS_SET, CS_MESSAGE_CB, (CS_VOID *) csmsg_callback, unused, 0));
      71             : 
      72           8 :         execute_sql(command, "if object_id('mps_table') is not null drop table mps_table");
      73           8 :         execute_sql(command, "if object_id('mps_rpc') is not null drop procedure mps_rpc");
      74             :         /* if this query fails probably we are using wrong database vendor or version */
      75           8 :         ret = execute_sql(command, "create procedure mps_rpc (@varbinary_param varbinary(max)) as "
      76             :                     "insert mps_table values (@varbinary_param) " "select len(varbinary_data) from mps_table");
      77           8 :         if (ret != 0) {
      78           4 :                 try_ctlogout(context, connection, command, verbose);
      79           4 :                 return 0;
      80             :         }
      81           4 :         execute_sql(command, "create table mps_table (varbinary_data varbinary(max))");
      82             : 
      83           4 :         if (argc > 1)
      84           0 :                 buffer_len = atoi(argv[1]);
      85           4 :         if (buffer_len < 0 || buffer_len > sizeof(buffer))
      86             :                 return 1;
      87             : 
      88           4 :         printf("sending %d bytes\n", buffer_len);
      89             : 
      90       32772 :         for (i = 0; i < buffer_len; i++)
      91       32768 :                 buffer[i] = (rand() % 16);
      92             : 
      93           4 :         memset(&datafmt, 0, sizeof(datafmt));
      94           4 :         strcpy(datafmt.name, "@varbinary_param");
      95           4 :         datafmt.namelen = nullterm;
      96           4 :         datafmt.datatype = CS_IMAGE_TYPE;
      97           4 :         datafmt.status = CS_INPUTVALUE;
      98             : 
      99           4 :         check_call(ct_command, (command, CS_RPC_CMD, "mps_rpc", nullterm, unused));
     100           4 :         check_call(ct_setparam, (command, &datafmt, buffer, &buffer_len, &ind));
     101           4 :         check_call(ct_send, (command));
     102             : 
     103           4 :         fetch_results(command);
     104             : 
     105           4 :         execute_sql(command, "drop table mps_table");
     106           4 :         execute_sql(command, "drop procedure mps_rpc");
     107           4 :         try_ctlogout(context, connection, command, verbose);
     108             : 
     109           4 :         printf("-- end --\n");
     110           4 :         return (result_len == buffer_len) ? 0 : 1;
     111             : }

Generated by: LCOV version 1.13