LCOV - code coverage report
Current view: top level - src/ctlib/unittests - lang_ct_param.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 107 149 71.8 %
Date: 2025-01-18 11:50:39 Functions: 2 2 100.0 %

          Line data    Source code
       1             : #include "common.h"
       2             : 
       3             : #include <stdarg.h>
       4             : 
       5             : static const char *query =
       6             :         "insert into #ctparam_lang (name,name2,age,cost,bdate,fval) values (@in1, @in2, @in3, @moneyval, @dateval, @floatval)";
       7             : 
       8             : static int insert_test(CS_CONNECTION *conn, CS_COMMAND *cmd, int useNames);
       9             : 
      10             : /* Testing: binding of data via ct_param */
      11             : int
      12           8 : main(int argc, char *argv[])
      13             : {
      14           8 :         int errCode = 0;
      15             :         
      16             :         CS_CONTEXT *ctx;
      17             :         CS_CONNECTION *conn;
      18             :         CS_COMMAND *cmd;
      19           8 :         int verbose = 0;
      20             : 
      21             :         CS_CHAR cmdbuf[4096];
      22             : 
      23           8 :         if (argc > 1 && (0 == strcmp(argv[1], "-v")))
      24           0 :                 verbose = 1;
      25             : 
      26           8 :         printf("%s: submit language query with variables using ct_param\n", __FILE__);
      27           8 :         if (verbose) {
      28           0 :                 printf("Trying login\n");
      29             :         }
      30           8 :         check_call(try_ctlogin, (&ctx, &conn, &cmd, verbose));
      31           8 :         error_to_stdout = true;
      32             : 
      33           8 :         strcpy(cmdbuf, "create table #ctparam_lang (id numeric identity not null, \
      34             :                 name varchar(30), name2 varchar(20), age int, cost money, bdate datetime, fval float) ");
      35             : 
      36           8 :         check_call(run_command, (cmd, cmdbuf));
      37             : 
      38             :         /* test by name */
      39           8 :         errCode = insert_test(conn, cmd, 1);
      40             :         /* if worked, test by position */
      41           8 :         if (0 == errCode)
      42           8 :                 errCode = insert_test(conn, cmd, 0);
      43           8 :         query = "insert into #ctparam_lang (name,name2,age,cost,bdate,fval) values (?, ?, ?, ?, ?, ?)";
      44           8 :         if (0 == errCode)
      45           8 :                 errCode = insert_test(conn, cmd, 0);
      46             : 
      47           8 :         if (verbose && (0 == errCode))
      48           0 :                 printf("lang_ct_param tests successful\n");
      49             : 
      50           8 :         if (verbose) {
      51           0 :                 printf("Trying logout\n");
      52             :         }
      53           8 :         check_call(try_ctlogout, (ctx, conn, cmd, verbose));
      54             : 
      55             :         return errCode;
      56             : }
      57             : 
      58             : static int 
      59          24 : insert_test(CS_CONNECTION *conn, CS_COMMAND *cmd, int useNames)
      60             : {
      61             :         CS_CONTEXT *ctx;
      62             : 
      63             :         CS_RETCODE ret;
      64             :         CS_INT res_type;
      65             : 
      66             :         CS_DATAFMT datafmt;
      67             :         CS_DATAFMT srcfmt;
      68             :         CS_DATAFMT destfmt;
      69             :         CS_INT intvar;
      70             :         CS_FLOAT floatvar;
      71             :         CS_MONEY moneyvar;
      72             :         CS_DATEREC datevar;
      73             :         char moneystring[10];
      74             :         char dummy_name[30];
      75             :         char dummy_name2[20];
      76             :         CS_INT destlen;
      77          24 :         CS_INT rowsAffected = -1;
      78          24 :         int rows_found = 0;
      79             : 
      80             :         /* clear table */
      81          24 :         run_command(cmd, "delete #ctparam_lang");
      82             : 
      83             :         /*
      84             :          * Assign values to the variables used for parameter passing.
      85             :          */
      86             : 
      87          24 :         intvar = 2;
      88          24 :         floatvar = 0.12;
      89          24 :         strcpy(dummy_name, "joe blow");
      90          24 :         strcpy(dummy_name2, "");
      91          24 :         strcpy(moneystring, "300.90");
      92             : 
      93             :         /*
      94             :          * Clear and setup the CS_DATAFMT structures used to convert datatypes.
      95             :          */
      96             : 
      97          24 :         memset(&srcfmt, 0, sizeof(CS_DATAFMT));
      98             :         srcfmt.datatype = CS_CHAR_TYPE;
      99          24 :         srcfmt.maxlength = strlen(moneystring);
     100          24 :         srcfmt.precision = 5;
     101          24 :         srcfmt.scale = 2;
     102             :         srcfmt.locale = NULL;
     103             : 
     104          24 :         memset(&destfmt, 0, sizeof(CS_DATAFMT));
     105          24 :         destfmt.datatype = CS_MONEY_TYPE;
     106          24 :         destfmt.maxlength = sizeof(CS_MONEY);
     107          24 :         destfmt.precision = 5;
     108          24 :         destfmt.scale = 2;
     109             :         destfmt.locale = NULL;
     110             : 
     111             :         /*
     112             :          * Convert the string representing the money value
     113             :          * to a CS_MONEY variable. Since this routine does not have the
     114             :          * context handle, we use the property functions to get it.
     115             :          */
     116          24 :         if ((ret = ct_cmd_props(cmd, CS_GET, CS_PARENT_HANDLE, &conn, CS_UNUSED, NULL)) != CS_SUCCEED) {
     117           0 :                 fprintf(stderr, "ct_cmd_props() failed\n");
     118           0 :                 return 1;
     119             :         }
     120          24 :         if ((ret = ct_con_props(conn, CS_GET, CS_PARENT_HANDLE, &ctx, CS_UNUSED, NULL)) != CS_SUCCEED) {
     121           0 :                 fprintf(stderr, "ct_con_props() failed\n");
     122           0 :                 return 1;
     123             :         }
     124          24 :         ret = cs_convert(ctx, &srcfmt, (CS_VOID *) moneystring, &destfmt, &moneyvar, &destlen);
     125          24 :         if (ret != CS_SUCCEED) {
     126           0 :                 fprintf(stderr, "cs_convert() failed\n");
     127           0 :                 return 1;
     128             :         }
     129             : 
     130             :         /*
     131             :          * create the command
     132             :          */
     133          24 :         if ((ret = ct_command(cmd, CS_LANG_CMD, query, strlen(query), 
     134             :                 CS_UNUSED)) != CS_SUCCEED) 
     135             :         {
     136           0 :                 fprintf(stderr, "ct_command(CS_LANG_CMD) failed\n");
     137           0 :                 return 1;
     138             :         }
     139             : 
     140             :         /*
     141             :          * Clear and setup the CS_DATAFMT structure, then pass
     142             :          * each of the parameters for the query.
     143             :          */
     144          24 :         memset(&datafmt, 0, sizeof(datafmt));
     145          24 :         if (useNames)
     146           8 :                 strcpy(datafmt.name, "@in1");
     147             :         else
     148             :                 datafmt.name[0] = 0;
     149          24 :         datafmt.maxlength = 255;
     150          24 :         datafmt.namelen = CS_NULLTERM;
     151             :         datafmt.datatype = CS_CHAR_TYPE;
     152          24 :         datafmt.status = CS_INPUTVALUE;
     153             : 
     154             :         /*
     155             :          * The character string variable is filled in by the RPC so pass NULL
     156             :          * for the data 0 for data length, and -1 for the indicator arguments.
     157             :          */
     158          24 :         ret = ct_param(cmd, &datafmt, dummy_name, strlen(dummy_name), 0);
     159          24 :         if (CS_SUCCEED != ret) {
     160           0 :                 fprintf(stderr, "ct_param(char) failed\n");
     161           0 :                 return 1;
     162             :         }
     163             : 
     164          24 :         if (useNames)
     165           8 :                 strcpy(datafmt.name, "@in2");
     166             :         else
     167          16 :                 datafmt.name[0] = 0;
     168          24 :         datafmt.maxlength = 255;
     169          24 :         datafmt.namelen = CS_NULLTERM;
     170          24 :         datafmt.datatype = CS_CHAR_TYPE;
     171          24 :         datafmt.status = CS_INPUTVALUE;
     172             : 
     173          24 :         ret = ct_param(cmd, &datafmt, dummy_name2, strlen(dummy_name2), 0);
     174          24 :         if (CS_SUCCEED != ret) {
     175           0 :                 fprintf(stderr, "ct_param(char) failed\n");
     176           0 :                 return 1;
     177             :         }
     178             : 
     179          24 :         if (useNames)
     180           8 :                 strcpy(datafmt.name, "@in3");
     181             :         else
     182          16 :                 datafmt.name[0] = 0;
     183          24 :         datafmt.namelen = CS_NULLTERM;
     184          24 :         datafmt.datatype = CS_INT_TYPE;
     185          24 :         datafmt.status = CS_INPUTVALUE;
     186             : 
     187          24 :         ret = ct_param(cmd, &datafmt, (CS_VOID *) & intvar, CS_SIZEOF(CS_INT), 0);
     188          24 :         if (CS_SUCCEED != ret) {
     189           0 :                 fprintf(stderr, "ct_param(int) failed\n");
     190           0 :                 return 1;
     191             :         }
     192             : 
     193          24 :         if (useNames)
     194           8 :                 strcpy(datafmt.name, "@moneyval");
     195             :         else
     196          16 :                 datafmt.name[0] = 0;
     197          24 :         datafmt.namelen = CS_NULLTERM;
     198          24 :         datafmt.datatype = CS_MONEY_TYPE;
     199          24 :         datafmt.status = CS_INPUTVALUE;
     200             : 
     201          24 :         ret = ct_param(cmd, &datafmt, (CS_VOID *) & moneyvar, CS_SIZEOF(CS_MONEY), 0);
     202          24 :         if (CS_SUCCEED != ret) {
     203           0 :                 fprintf(stderr, "ct_param(money) failed\n");
     204           0 :                 return 1;
     205             :         }
     206             : 
     207          24 :         if (useNames)
     208           8 :                 strcpy(datafmt.name, "@dateval");
     209             :         else
     210          16 :                 datafmt.name[0] = 0;
     211          24 :         datafmt.namelen = CS_NULLTERM;
     212          24 :         datafmt.datatype = CS_DATETIME_TYPE;
     213          24 :         datafmt.status = CS_INPUTVALUE;
     214          24 :         memset(&datevar, 0, sizeof(CS_DATEREC));
     215          24 :         datevar.dateyear = 2003;
     216          24 :         datevar.datemonth = 2;
     217          24 :         datevar.datedmonth = 1;
     218             : 
     219          24 :         ret = ct_param(cmd, &datafmt, &datevar, 0, 0);
     220          24 :         if (CS_SUCCEED != ret) {
     221           0 :                 fprintf(stderr, "ct_param(datetime) failed");
     222           0 :                 return 1;
     223             :         }
     224             : 
     225          24 :         if (useNames)
     226           8 :                 strcpy(datafmt.name, "@floatval");
     227             :         else
     228          16 :                 datafmt.name[0] = 0;
     229          24 :         datafmt.namelen = CS_NULLTERM;
     230          24 :         datafmt.datatype = CS_FLOAT_TYPE;
     231          24 :         datafmt.status = CS_INPUTVALUE;
     232             : 
     233          24 :         ret = ct_param(cmd, &datafmt, &floatvar, 0, 0);
     234          24 :         if (CS_SUCCEED != ret) {
     235           0 :                 fprintf(stderr, "ct_param(float) failed");
     236           0 :                 return 1;
     237             :         }
     238             : 
     239             :         /*
     240             :          * Send the command to the server
     241             :          */
     242          24 :         if (ct_send(cmd) != CS_SUCCEED) {
     243           0 :                 fprintf(stderr, "ct_send(CS_LANG_CMD) failed\n");
     244           0 :                 return 1;
     245             :         }
     246             : 
     247             :         /*
     248             :          * Process the results.
     249             :          */
     250          72 :         while ((ret = ct_results(cmd, &res_type)) == CS_SUCCEED) {
     251          48 :                 switch ((int) res_type) {
     252             : 
     253          48 :                 case CS_CMD_SUCCEED:
     254             :                 case CS_CMD_DONE:
     255             :                         {
     256          48 :                                 CS_INT rowsAffected=0;
     257          48 :                                 ct_res_info(cmd, CS_ROW_COUNT, &rowsAffected, CS_UNUSED, NULL);
     258          48 :                                 if (1 != rowsAffected) 
     259           0 :                                         fprintf(stderr, "insert touched %d rows instead of 1\n", rowsAffected);
     260             :                                 else
     261             :                                         rows_found = 1;
     262             :                         }
     263          48 :                         break;
     264             : 
     265           0 :                 case CS_CMD_FAIL:
     266             :                         /*
     267             :                          * The server encountered an error while
     268             :                          * processing our command.
     269             :                          */
     270           0 :                         fprintf(stderr, "ct_results returned CS_CMD_FAIL.\n");
     271           0 :                         break;
     272             : 
     273           0 :                 case CS_STATUS_RESULT:
     274             :                         /*
     275             :                          * The server encountered an error while
     276             :                          * processing our command.
     277             :                          */
     278           0 :                         fprintf(stderr, "ct_results returned CS_STATUS_RESULT.\n");
     279           0 :                         break;
     280             : 
     281           0 :                 default:
     282             :                         /*
     283             :                          * We got something unexpected.
     284             :                          */
     285           0 :                         fprintf(stderr, "ct_results returned unexpected result type %d\n", res_type);
     286           0 :                         return 1;
     287             :                 }
     288             :         }
     289          24 :         if (ret != CS_END_RESULTS) {
     290           0 :                 fprintf(stderr, "ct_results returned unexpected result %d.\n", (int) ret);
     291           0 :                 exit(1);
     292             :         }
     293             : 
     294          24 :         if (!rows_found) {
     295           0 :                 fprintf(stderr, "expected 1 rows inserted, inserted %d.\n", (int) rowsAffected);
     296           0 :                 exit(1);
     297             :         }
     298             : 
     299             :         /* test row inserted */
     300          24 :         ret = run_command(cmd, "if not exists(select * from #ctparam_lang where name = 'joe blow' and name2 is not null and age = 2) select 1");
     301          24 :         if (ret != CS_SUCCEED) {
     302           0 :                 fprintf(stderr, "check row inserted failed\n");
     303           0 :                 exit(1);
     304             :         }
     305             : 
     306             :         return 0;
     307             : }

Generated by: LCOV version 1.13