LCOV - code coverage report
Current view: top level - src/odbc/unittests - params.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 35 39 89.7 %
Date: 2024-03-23 08:24:27 Functions: 2 2 100.0 %

          Line data    Source code
       1             : #include "common.h"
       2             : 
       3             : /* Test for store procedure and params */
       4             : /* Test from Tom Rogers */
       5             : 
       6             : /* SP definition */
       7             : 
       8             : static const char sp_define[] = "CREATE PROCEDURE spTestProc\n"
       9             :         "    @InParam int,\n"
      10             :         "    @OutParam int OUTPUT,\n"
      11             :         "    @OutString varchar(20) OUTPUT\n"
      12             :         "AS\n"
      13             :         "     SELECT @OutParam = @InParam\n"
      14             :         "     SELECT @OutString = 'This is cool!'\n"
      15             :         "     IF @InParam < 10\n" "          RETURN (0)\n" "     ELSE\n" "          RETURN (1)\n";
      16             : 
      17             : #define SP_TEXT "{?=call spTestProc(?,?,?)}"
      18             : #define OUTSTRING_LEN 20
      19             : 
      20             : static int
      21          16 : Test(int bind_before)
      22             : {
      23          16 :         SQLSMALLINT ReturnCode = 0;
      24          16 :         SQLSMALLINT InParam = 5;
      25          16 :         SQLSMALLINT OutParam = 1;
      26             :         char OutString[OUTSTRING_LEN];
      27          16 :         SQLLEN cbReturnCode = 0, cbInParam = 0, cbOutParam = 0;
      28          16 :         SQLLEN cbOutString = SQL_NTS;
      29             : 
      30          16 :         odbc_connect();
      31             : 
      32             :         /* drop proc */
      33          16 :         odbc_command("IF OBJECT_ID('spTestProc') IS NOT NULL DROP PROC spTestProc");
      34             : 
      35             :         /* create proc */
      36          16 :         odbc_command(sp_define);
      37             : 
      38          16 :         if (!bind_before)
      39           8 :                 CHKPrepare(T(SP_TEXT), strlen(SP_TEXT), "S");
      40             : 
      41          16 :         CHKBindParameter(1, SQL_PARAM_OUTPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &ReturnCode, 0, &cbReturnCode, "S");
      42          16 :         CHKBindParameter(2, SQL_PARAM_INPUT,  SQL_C_SSHORT, SQL_INTEGER, 0, 0, &InParam,    0, &cbInParam,    "S");
      43          16 :         CHKBindParameter(3, SQL_PARAM_OUTPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &OutParam,   0, &cbOutParam,   "S");
      44             : 
      45             :         OutString[0] = '\0';
      46          16 :         strcpy(OutString, "Test");    /* Comment this line and we get an error!  Why? */
      47          16 :         CHKBindParameter(4, SQL_PARAM_OUTPUT, SQL_C_CHAR, SQL_VARCHAR, OUTSTRING_LEN, 0, OutString, 
      48             :             OUTSTRING_LEN, &cbOutString, "S");
      49             : 
      50          16 :         if (bind_before)
      51           8 :                 CHKPrepare(T(SP_TEXT), strlen(SP_TEXT), "S");
      52             : 
      53          16 :         CHKExecute("S");
      54             : 
      55          16 :         odbc_command("DROP PROC spTestProc");
      56             : 
      57          16 :         printf("Output:\n");
      58          16 :         printf("   Return Code = %d\n", (int) ReturnCode);
      59          16 :         printf("   InParam = %d\n", (int) InParam);
      60          16 :         printf("   OutParam = %d\n", (int) OutParam);
      61          16 :         printf("   OutString = %s\n", OutString);
      62             : 
      63          16 :         if (InParam != OutParam) {
      64           0 :                 fprintf(stderr, "InParam != OutParam\n");
      65           0 :                 return 1;
      66             :         }
      67             : 
      68          16 :         if (strcmp(OutString, "This is cool!") != 0) {
      69           0 :                 fprintf(stderr, "Bad string returned\n");
      70           0 :                 return 1;
      71             :         }
      72             : 
      73          16 :         odbc_disconnect();
      74          16 :         ODBC_FREE();
      75          16 :         return 0;
      76             : }
      77             : 
      78             : int
      79           8 : main(void)
      80             : {
      81           8 :         if (Test(0))
      82             :                 return 1;
      83           8 :         if (Test(1))
      84             :                 return 1;
      85             : 
      86           8 :         printf("Done successfully!\n");
      87           8 :         return 0;
      88             : }

Generated by: LCOV version 1.13