LCOV - code coverage report
Current view: top level - clients/odbc/driver - SQLBindParameter.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 36 63 57.1 %
Date: 2021-10-13 02:24:04 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : /*
      10             :  * This code was created by Peter Harvey (mostly during Christmas 98/99).
      11             :  * This code is LGPL. Please ensure that this message remains in future
      12             :  * distributions and uses of this code (thats about all I get out of it).
      13             :  * - Peter Harvey pharvey@codebydesign.com
      14             :  *
      15             :  * This file has been modified for the MonetDB project.  See the file
      16             :  * Copyright in this directory for more information.
      17             :  */
      18             : 
      19             : /**********************************************************************
      20             :  * SQLBindParameter()
      21             :  * CLI Compliance: ODBC (Microsoft)
      22             :  *
      23             :  * Note: this function does not yet support Output parameters, only SQL_PARAM_INPUT.
      24             :  *
      25             :  * Author: Martin van Dinther, Sjoerd Mullender
      26             :  * Date  : 30 Aug 2002
      27             :  *
      28             :  **********************************************************************/
      29             : 
      30             : #include "ODBCGlobal.h"
      31             : #include "ODBCStmt.h"
      32             : 
      33             : SQLRETURN
      34           5 : MNDBBindParameter(ODBCStmt *stmt,
      35             :                   SQLUSMALLINT ParameterNumber,
      36             :                   SQLSMALLINT InputOutputType,
      37             :                   SQLSMALLINT ValueType,
      38             :                   SQLSMALLINT ParameterType,
      39             :                   SQLULEN ColumnSize,
      40             :                   SQLSMALLINT DecimalDigits,
      41             :                   SQLPOINTER ParameterValuePtr,
      42             :                   SQLLEN BufferLength,
      43             :                   SQLLEN *StrLen_or_IndPtr)
      44             : {
      45             :         ODBCDesc *apd, *ipd;
      46             :         ODBCDescRec *apdrec, *ipdrec;
      47             :         SQLRETURN rc;
      48             : 
      49           5 :         if (!isValidStmt(stmt))
      50             :                  return SQL_INVALID_HANDLE;
      51             : 
      52           5 :         clearStmtErrors(stmt);
      53             : 
      54             :         /* check input parameters */
      55           5 :         if (ParameterNumber <= 0) {
      56             :                 /* Invalid descriptor index */
      57           0 :                 addStmtError(stmt, "07009", NULL, 0);
      58           0 :                 return SQL_ERROR;
      59             :         }
      60             :         /* For safety: limit the maximum number of columns to bind */
      61           5 :         if (ParameterNumber > MONETDB_MAX_BIND_COLS) {
      62             :                 /* General error */
      63           0 :                 addStmtError(stmt, "HY000", "Maximum number of bind columns (8192) exceeded", 0);
      64           0 :                 return SQL_ERROR;
      65             :         }
      66             : 
      67           5 :         switch (InputOutputType) {
      68             :         case SQL_PARAM_INPUT:
      69             :                 break;
      70           0 :         case SQL_PARAM_INPUT_OUTPUT:
      71             :         case SQL_PARAM_OUTPUT:
      72             :                 /* Optional feature not implemented */
      73           0 :                 addStmtError(stmt, "HYC00", "Output parameters are not supported", 0);
      74           0 :                 return SQL_ERROR;
      75           0 :         default:
      76             :                 /* Invalid parameter type */
      77           0 :                 addStmtError(stmt, "HY105", NULL, 0);
      78           0 :                 return SQL_ERROR;
      79             :         }
      80             : 
      81           5 :         if (ParameterValuePtr == NULL && StrLen_or_IndPtr == NULL
      82             :             /* && InputOutputType != SQL_PARAM_OUTPUT */ ) {
      83             :                 /* Invalid use of null pointer */
      84           0 :                 addStmtError(stmt, "HY009", NULL, 0);
      85           0 :                 return SQL_ERROR;
      86             :         }
      87             : 
      88           5 :         if (BufferLength < 0) {
      89             :                 /* Invalid string or buffer length */
      90           0 :                 addStmtError(stmt, "HY090", NULL, 0);
      91           0 :                 return SQL_ERROR;
      92             :         }
      93             : 
      94             :         /* can't let SQLSetDescField below do this check since it
      95             :            returns the wrong error code if the type is incorrect */
      96           5 :         switch (ValueType) {
      97             :         case SQL_C_CHAR:
      98             :         case SQL_C_WCHAR:
      99             :         case SQL_C_BINARY:
     100             :         case SQL_C_BIT:
     101             :         case SQL_C_STINYINT:
     102             :         case SQL_C_UTINYINT:
     103             :         case SQL_C_TINYINT:
     104             :         case SQL_C_SSHORT:
     105             :         case SQL_C_USHORT:
     106             :         case SQL_C_SHORT:
     107             :         case SQL_C_SLONG:
     108             :         case SQL_C_ULONG:
     109             :         case SQL_C_LONG:
     110             :         case SQL_C_SBIGINT:
     111             :         case SQL_C_UBIGINT:
     112             :         case SQL_C_NUMERIC:
     113             :         case SQL_C_FLOAT:
     114             :         case SQL_C_DOUBLE:
     115             :         case SQL_C_TYPE_DATE:
     116             :         case SQL_C_TYPE_TIME:
     117             :         case SQL_C_TYPE_TIMESTAMP:
     118             :         case SQL_C_INTERVAL_YEAR:
     119             :         case SQL_C_INTERVAL_MONTH:
     120             :         case SQL_C_INTERVAL_YEAR_TO_MONTH:
     121             :         case SQL_C_INTERVAL_DAY:
     122             :         case SQL_C_INTERVAL_HOUR:
     123             :         case SQL_C_INTERVAL_MINUTE:
     124             :         case SQL_C_INTERVAL_SECOND:
     125             :         case SQL_C_INTERVAL_DAY_TO_HOUR:
     126             :         case SQL_C_INTERVAL_DAY_TO_MINUTE:
     127             :         case SQL_C_INTERVAL_DAY_TO_SECOND:
     128             :         case SQL_C_INTERVAL_HOUR_TO_MINUTE:
     129             :         case SQL_C_INTERVAL_HOUR_TO_SECOND:
     130             :         case SQL_C_INTERVAL_MINUTE_TO_SECOND:
     131             :         case SQL_C_GUID:
     132             :         case SQL_C_DEFAULT:
     133             :                 break;
     134           0 :         default:
     135             :                 /* Invalid application buffer type */
     136           0 :                 addStmtError(stmt, "HY003", NULL, 0);
     137           0 :                 return SQL_ERROR;
     138             :         }
     139             : 
     140           5 :         apd = stmt->ApplParamDescr;
     141           5 :         ipd = stmt->ImplParamDescr;
     142             : 
     143           5 :         apdrec = addODBCDescRec(apd, ParameterNumber);
     144           5 :         ipdrec = addODBCDescRec(ipd, ParameterNumber);
     145             : 
     146             :         /* we disallow types not supported by the server */
     147           5 :         switch (ParameterType) {
     148           1 :         case SQL_CHAR:
     149             :         case SQL_VARCHAR:
     150             :         case SQL_LONGVARCHAR:
     151             : /*      case SQL_BINARY: */
     152             :         case SQL_VARBINARY:
     153             :         case SQL_LONGVARBINARY:
     154             :         case SQL_TYPE_DATE:
     155             :         case SQL_INTERVAL_MONTH:
     156             : /*      case SQL_INTERVAL_YEAR: */
     157             : /*      case SQL_INTERVAL_YEAR_TO_MONTH: */
     158             : /*      case SQL_INTERVAL_DAY: */
     159             : /*      case SQL_INTERVAL_HOUR: */
     160             : /*      case SQL_INTERVAL_MINUTE: */
     161             : /*      case SQL_INTERVAL_DAY_TO_HOUR: */
     162             : /*      case SQL_INTERVAL_DAY_TO_MINUTE: */
     163             : /*      case SQL_INTERVAL_HOUR_TO_MINUTE: */
     164           1 :                 ipdrec->sql_desc_length = ColumnSize;
     165           1 :                 break;
     166           1 :         case SQL_TYPE_TIME:
     167             :         case SQL_TYPE_TIMESTAMP:
     168             :         case SQL_INTERVAL_SECOND:
     169             : /*      case SQL_INTERVAL_DAY_TO_SECOND: */
     170             : /*      case SQL_INTERVAL_HOUR_TO_SECOND: */
     171             : /*      case SQL_INTERVAL_MINUTE_TO_SECOND: */
     172           1 :                 ipdrec->sql_desc_precision = DecimalDigits;
     173           1 :                 ipdrec->sql_desc_length = ColumnSize;
     174           1 :                 break;
     175           0 :         case SQL_DECIMAL:
     176             :         case SQL_NUMERIC:
     177           0 :                 ipdrec->sql_desc_precision = (SQLSMALLINT) ColumnSize;
     178           0 :                 ipdrec->sql_desc_scale = DecimalDigits;
     179           0 :                 break;
     180           1 :         case SQL_FLOAT:
     181             :         case SQL_REAL:
     182             :         case SQL_DOUBLE:
     183           1 :                 ipdrec->sql_desc_precision = (SQLSMALLINT) ColumnSize;
     184           1 :                 break;
     185             :         case SQL_WCHAR:
     186             :         case SQL_WVARCHAR:
     187             :         case SQL_WLONGVARCHAR:
     188             :         case SQL_BIT:
     189             :         case SQL_TINYINT:
     190             :         case SQL_SMALLINT:
     191             :         case SQL_INTEGER:
     192             :         case SQL_BIGINT:
     193             :         case SQL_HUGEINT:
     194             :         case SQL_GUID:
     195             :                 break;
     196           0 :         default:
     197             :                 /* Invalid SQL data type */
     198           0 :                 addStmtError(stmt, "HY004", NULL, 0);
     199           0 :                 return SQL_ERROR;
     200             : 
     201             :         /* these types are not allowed by the server */
     202           0 :         case SQL_BINARY:
     203             :         case SQL_INTERVAL_YEAR:
     204             :         case SQL_INTERVAL_YEAR_TO_MONTH:
     205             :         case SQL_INTERVAL_DAY:
     206             :         case SQL_INTERVAL_HOUR:
     207             :         case SQL_INTERVAL_MINUTE:
     208             :         case SQL_INTERVAL_DAY_TO_HOUR:
     209             :         case SQL_INTERVAL_DAY_TO_MINUTE:
     210             :         case SQL_INTERVAL_HOUR_TO_MINUTE:
     211             :         case SQL_INTERVAL_DAY_TO_SECOND:
     212             :         case SQL_INTERVAL_HOUR_TO_SECOND:
     213             :         case SQL_INTERVAL_MINUTE_TO_SECOND:
     214             :                 /* Optional feature not implemented */
     215           0 :                 addStmtError(stmt, "HYC00", NULL, 0);
     216           0 :                 return SQL_ERROR;
     217             :         }
     218             : 
     219           5 :         rc = MNDBSetDescField(apd, ParameterNumber, SQL_DESC_CONCISE_TYPE, (SQLPOINTER) (intptr_t) ValueType, 0);
     220           5 :         if (!SQL_SUCCEEDED(rc))
     221             :                 return rc;
     222           5 :         rc = MNDBSetDescField(ipd, ParameterNumber, SQL_DESC_CONCISE_TYPE, (SQLPOINTER) (intptr_t) ParameterType, 0);
     223           5 :         if (!SQL_SUCCEEDED(rc))
     224             :                 return rc;
     225           5 :         ipdrec->sql_desc_parameter_type = InputOutputType;
     226           5 :         apdrec->sql_desc_data_ptr = ParameterValuePtr;
     227           5 :         apdrec->sql_desc_octet_length = BufferLength;
     228           5 :         apdrec->sql_desc_indicator_ptr = StrLen_or_IndPtr;
     229           5 :         apdrec->sql_desc_octet_length_ptr = StrLen_or_IndPtr;
     230             : 
     231           5 :         return SQL_SUCCESS;
     232             : }
     233             : 
     234             : SQLRETURN SQL_API
     235             : SQLBindParameter(SQLHSTMT StatementHandle,
     236             :                  SQLUSMALLINT ParameterNumber,
     237             :                  SQLSMALLINT InputOutputType,
     238             :                  SQLSMALLINT ValueType,
     239             :                  SQLSMALLINT ParameterType,
     240             :                  SQLULEN ColumnSize,
     241             :                  SQLSMALLINT DecimalDigits,
     242             :                  SQLPOINTER ParameterValuePtr,
     243             :                  SQLLEN BufferLength,
     244             :                  SQLLEN *StrLen_or_IndPtr)
     245             : {
     246             : #ifdef ODBCDEBUG
     247           5 :         ODBCLOG("SQLBindParameter %p %u %d %s %s " ULENFMT " %d %p " LENFMT " %p\n",
     248             :                 StatementHandle, (unsigned int) ParameterNumber,
     249             :                 (int) InputOutputType, translateCType(ValueType),
     250             :                 translateSQLType(ParameterType),
     251             :                 ULENCAST ColumnSize, (int) DecimalDigits,
     252             :                 ParameterValuePtr, LENCAST BufferLength,
     253             :                 StrLen_or_IndPtr);
     254             : #endif
     255             : 
     256           5 :         return MNDBBindParameter((ODBCStmt *) StatementHandle, ParameterNumber,
     257             :                                  InputOutputType, ValueType, ParameterType,
     258             :                                  ColumnSize, DecimalDigits, ParameterValuePtr,
     259             :                                  BufferLength, StrLen_or_IndPtr);
     260             : }

Generated by: LCOV version 1.14