LCOV - code coverage report
Current view: top level - clients/odbc/driver - SQLSetDescField.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 24 184 13.0 %
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             :  * SQLSetDescField()
      21             :  * CLI Compliance: ISO 92
      22             :  **********************************************************************/
      23             : 
      24             : #include "ODBCGlobal.h"
      25             : #include "ODBCStmt.h"
      26             : #include "ODBCUtil.h"
      27             : 
      28             : SQLRETURN
      29          22 : MNDBSetDescField(ODBCDesc *desc,
      30             :                  SQLSMALLINT RecNumber,
      31             :                  SQLSMALLINT FieldIdentifier,
      32             :                  SQLPOINTER ValuePtr,
      33             :                  SQLINTEGER BufferLength)
      34             : {
      35             :         ODBCDescRec *rec;
      36             :         struct sql_types *tp;
      37             : 
      38          22 :         if (isAD(desc))
      39             :                 tp = ODBC_c_types;
      40             :         else
      41             :                 tp = ODBC_sql_types;
      42             : 
      43          22 :         switch (FieldIdentifier) {
      44           0 :         case SQL_DESC_ALLOC_TYPE:               /* SQLSMALLINT */
      45             :                 /* Invalid descriptor field identifier */
      46           0 :                 addDescError(desc, "HY091", NULL, 0);
      47           0 :                 return SQL_ERROR;
      48           0 :         case SQL_DESC_ARRAY_SIZE:               /* SQLULEN */
      49           0 :                 if ((SQLULEN) (uintptr_t) ValuePtr == 0) {
      50             :                         /* Invalid attribute/option identifier */
      51           0 :                         addDescError(desc, "HY092", NULL, 0);
      52           0 :                         return SQL_ERROR;
      53             :                 }
      54           0 :                 if (isAD(desc)) {
      55             :                         /* limit size to protect against bugs */
      56           0 :                         if ((SQLULEN) (uintptr_t) ValuePtr > 10000) {
      57             :                                 /* Driver does not support this function */
      58           0 :                                 addDescError(desc, "IM001", NULL, 0);
      59           0 :                                 return SQL_ERROR;
      60             :                         }
      61           0 :                         desc->sql_desc_array_size = (SQLULEN) (uintptr_t) ValuePtr;
      62             :                 }
      63             :                 return SQL_SUCCESS;
      64           0 :         case SQL_DESC_ARRAY_STATUS_PTR:         /* SQLUSMALLINT * */
      65           0 :                 desc->sql_desc_array_status_ptr = (SQLUSMALLINT *) ValuePtr;
      66           0 :                 return SQL_SUCCESS;
      67           0 :         case SQL_DESC_BIND_OFFSET_PTR:          /* SQLLEN * */
      68           0 :                 if (isAD(desc))
      69           0 :                         desc->sql_desc_bind_offset_ptr = (SQLLEN *) ValuePtr;
      70             :                 return SQL_SUCCESS;
      71           0 :         case SQL_DESC_BIND_TYPE:                /* SQLINTEGER */
      72           0 :                 if (isAD(desc))
      73           0 :                         desc->sql_desc_bind_type = (SQLINTEGER) (intptr_t) ValuePtr;
      74             :                 return SQL_SUCCESS;
      75           0 :         case SQL_DESC_COUNT:                    /* SQLSMALLINT */
      76           0 :                 if (isIRD(desc)) {
      77             :                         /* Invalid descriptor field identifier */
      78           0 :                         addDescError(desc, "HY091", NULL, 0);
      79           0 :                         return SQL_ERROR;
      80             :                 }
      81           0 :                 setODBCDescRecCount(desc, (int) (SQLSMALLINT) (intptr_t) ValuePtr);
      82           0 :                 return SQL_SUCCESS;
      83           0 :         case SQL_DESC_ROWS_PROCESSED_PTR:       /* SQLULEN * */
      84           0 :                 if (desc->Stmt)
      85           0 :                         desc->sql_desc_rows_processed_ptr = (SQLULEN *) ValuePtr;
      86             :                 return SQL_SUCCESS;
      87             :         }
      88             : 
      89          22 :         if (RecNumber <= 0) {
      90             :                 /* Invalid descriptor index */
      91           0 :                 addDescError(desc, "07009", NULL, 0);
      92           0 :                 return SQL_ERROR;
      93             :         }
      94          22 :         if (RecNumber > desc->sql_desc_count)
      95             :                 return SQL_NO_DATA;
      96             : 
      97          22 :         if (isIRD(desc)) {
      98             :                 /* the Implementation Row Descriptor is read-only */
      99             :                 /* Invalid descriptor field identifier */
     100           0 :                 addDescError(desc, "HY091", NULL, 0);
     101           0 :                 return SQL_ERROR;
     102             :         }
     103             : 
     104          22 :         rec = &desc->descRec[RecNumber];
     105             : 
     106             :         /* break for read-only fields since the error is the same as
     107             :            unknown FieldIdentifier */
     108          22 :         switch (FieldIdentifier) {
     109             :         case SQL_DESC_AUTO_UNIQUE_VALUE:        /* SQLINTEGER */
     110             :         case SQL_DESC_BASE_COLUMN_NAME:         /* SQLCHAR * */
     111             :         case SQL_DESC_BASE_TABLE_NAME:          /* SQLCHAR * */
     112             :         case SQL_DESC_CASE_SENSITIVE:           /* SQLINTEGER */
     113             :         case SQL_DESC_CATALOG_NAME:             /* SQLCHAR * */
     114             :         case SQL_DESC_DISPLAY_SIZE:             /* SQLLEN */
     115             :         case SQL_DESC_FIXED_PREC_SCALE:         /* SQLSMALLINT */
     116             :         case SQL_DESC_LABEL:                    /* SQLCHAR * */
     117             :         case SQL_DESC_LITERAL_PREFIX:           /* SQLCHAR * */
     118             :         case SQL_DESC_LITERAL_SUFFIX:           /* SQLCHAR * */
     119             :         case SQL_DESC_LOCAL_TYPE_NAME:          /* SQLCHAR * */
     120             :         case SQL_DESC_NULLABLE:                 /* SQLSMALLINT */
     121             :         case SQL_DESC_ROWVER:                   /* SQLSMALLINT */
     122             :         case SQL_DESC_SCHEMA_NAME:              /* SQLCHAR * */
     123             :         case SQL_DESC_SEARCHABLE:               /* SQLSMALLINT */
     124             :         case SQL_DESC_TABLE_NAME:               /* SQLCHAR * */
     125             :         case SQL_DESC_TYPE_NAME:                /* SQLCHAR * */
     126             :         case SQL_DESC_UNSIGNED:                 /* SQLSMALLINT */
     127             :         case SQL_DESC_UPDATABLE:                /* SQLSMALLINT */
     128             :                 break;          /* read-only or unused */
     129             :         case SQL_DESC_CONCISE_TYPE:             /* SQLSMALLINT */
     130         300 :                 while (tp->concise_type != 0) {
     131         300 :                         if ((intptr_t) tp->concise_type == (intptr_t) ValuePtr) {
     132          22 :                                 if (tp->concise_type == SQL_HUGEINT)
     133           0 :                                         desc->Dbc->allow_hugeint = true;
     134          22 :                                 rec->sql_desc_concise_type = tp->concise_type;
     135          22 :                                 rec->sql_desc_type = tp->type;
     136          22 :                                 rec->sql_desc_datetime_interval_code = tp->code;
     137          22 :                                 if (tp->precision != UNAFFECTED)
     138          15 :                                         rec->sql_desc_precision = tp->precision;
     139          22 :                                 if (tp->datetime_interval_precision != UNAFFECTED)
     140           0 :                                         rec->sql_desc_datetime_interval_precision = tp->datetime_interval_precision;
     141          22 :                                 if (tp->length != UNAFFECTED)
     142           3 :                                         rec->sql_desc_length = tp->length;
     143          22 :                                 if (tp->scale != UNAFFECTED)
     144           0 :                                         rec->sql_desc_scale = tp->scale;
     145          22 :                                 rec->sql_desc_fixed_prec_scale = tp->fixed;
     146          22 :                                 rec->sql_desc_num_prec_radix = tp->radix;
     147          22 :                                 return SQL_SUCCESS;
     148             :                         }
     149         278 :                         tp++;
     150             :                 }
     151             :                 /* Invalid attribute/option identifier */
     152           0 :                 addDescError(desc, "HY092", NULL, 0);
     153           0 :                 return SQL_ERROR;
     154           0 :         case SQL_DESC_DATA_PTR:                 /* SQLPOINTER */
     155             :                 /* TODO: consistency check */
     156           0 :                 rec->sql_desc_data_ptr = ValuePtr;
     157           0 :                 return SQL_SUCCESS;
     158             :         case SQL_DESC_DATETIME_INTERVAL_CODE:   /* SQLSMALLINT */
     159           0 :                 while (tp->concise_type != 0) {
     160           0 :                         if ((intptr_t) tp->code == (intptr_t) ValuePtr &&
     161           0 :                             tp->type == rec->sql_desc_type) {
     162           0 :                                 rec->sql_desc_concise_type = tp->concise_type;
     163           0 :                                 rec->sql_desc_type = tp->type;
     164           0 :                                 rec->sql_desc_datetime_interval_code = tp->code;
     165           0 :                                 if (tp->precision != UNAFFECTED)
     166           0 :                                         rec->sql_desc_precision = tp->precision;
     167           0 :                                 if (tp->datetime_interval_precision != UNAFFECTED)
     168           0 :                                         rec->sql_desc_datetime_interval_precision = tp->datetime_interval_precision;
     169           0 :                                 if (tp->length != UNAFFECTED)
     170           0 :                                         rec->sql_desc_length = tp->length;
     171           0 :                                 if (tp->scale != UNAFFECTED)
     172           0 :                                         rec->sql_desc_scale = tp->scale;
     173           0 :                                 rec->sql_desc_fixed_prec_scale = tp->fixed;
     174           0 :                                 rec->sql_desc_num_prec_radix = tp->radix;
     175           0 :                                 return SQL_SUCCESS;
     176             :                         }
     177           0 :                         tp++;
     178             :                 }
     179             :                 /* Inconsistent descriptor information */
     180           0 :                 addDescError(desc, "HY021", NULL, 0);
     181           0 :                 return SQL_ERROR;
     182           0 :         case SQL_DESC_DATETIME_INTERVAL_PRECISION: /* SQLINTEGER */
     183           0 :                 rec->sql_desc_datetime_interval_precision = (SQLINTEGER) (intptr_t) ValuePtr;
     184           0 :                 return SQL_SUCCESS;
     185           0 :         case SQL_DESC_INDICATOR_PTR:            /* SQLLEN * */
     186           0 :                 if (isAD(desc))
     187           0 :                         rec->sql_desc_indicator_ptr = (SQLLEN *) ValuePtr;
     188             :                 return SQL_SUCCESS;
     189           0 :         case SQL_DESC_LENGTH:                   /* SQLULEN */
     190           0 :                 rec->sql_desc_length = (SQLULEN) (uintptr_t) ValuePtr;
     191           0 :                 return SQL_SUCCESS;
     192           0 :         case SQL_DESC_NAME:                     /* SQLCHAR * */
     193           0 :                 if (isID(desc)) {
     194           0 :                         fixODBCstring(ValuePtr, BufferLength, SQLINTEGER,
     195             :                                       addDescError, desc, return SQL_ERROR);
     196           0 :                         if (rec->sql_desc_name != NULL)
     197           0 :                                 free(rec->sql_desc_name);
     198           0 :                         rec->sql_desc_name = (SQLCHAR *) dupODBCstring((SQLCHAR *) ValuePtr, (size_t) BufferLength);
     199           0 :                         if (rec->sql_desc_name == NULL) {
     200             :                                 /* Memory allocation error */
     201           0 :                                 addDescError(desc, "HY001", NULL, 0);
     202           0 :                                 return SQL_ERROR;
     203             :                         }
     204           0 :                         rec->sql_desc_unnamed = *rec->sql_desc_name ? SQL_NAMED : SQL_UNNAMED;
     205             :                 }
     206             :                 return SQL_SUCCESS;
     207           0 :         case SQL_DESC_NUM_PREC_RADIX:
     208           0 :                 rec->sql_desc_num_prec_radix = (SQLINTEGER) (intptr_t) ValuePtr;
     209           0 :                 return SQL_SUCCESS;
     210           0 :         case SQL_DESC_OCTET_LENGTH:             /* SQLLEN */
     211           0 :                 rec->sql_desc_octet_length = (SQLLEN) (intptr_t) ValuePtr;
     212           0 :                 return SQL_SUCCESS;
     213           0 :         case SQL_DESC_OCTET_LENGTH_PTR:         /* SQLLEN * */
     214           0 :                 if (isAD(desc))
     215           0 :                         rec->sql_desc_octet_length_ptr = (SQLLEN *) ValuePtr;
     216             :                 return SQL_SUCCESS;
     217           0 :         case SQL_DESC_PARAMETER_TYPE:           /* SQLSMALLINT */
     218           0 :                 switch ((SQLSMALLINT) (intptr_t) ValuePtr) {
     219             :                 case SQL_PARAM_INPUT:
     220             :                         break;
     221           0 :                 case SQL_PARAM_INPUT_OUTPUT:
     222             :                 case SQL_PARAM_OUTPUT:
     223             :                         /* Driver does not support this function */
     224           0 :                         addDescError(desc, "IM001", NULL, 0);
     225           0 :                         return SQL_ERROR;
     226           0 :                 default:
     227             :                         /* Invalid attribute/option identifier */
     228           0 :                         addDescError(desc, "HY092", NULL, 0);
     229           0 :                         return SQL_ERROR;
     230             :                 }
     231           0 :                 if (isIPD(desc))
     232           0 :                         rec->sql_desc_parameter_type = (SQLSMALLINT) (intptr_t) ValuePtr;
     233             :                 return SQL_SUCCESS;
     234           0 :         case SQL_DESC_PRECISION:                /* SQLSMALLINT */
     235           0 :                 rec->sql_desc_precision = (SQLSMALLINT) (intptr_t) ValuePtr;
     236           0 :                 return SQL_SUCCESS;
     237           0 :         case SQL_DESC_SCALE:                    /* SQLSMALLINT */
     238           0 :                 rec->sql_desc_scale = (SQLSMALLINT) (intptr_t) ValuePtr;
     239           0 :                 return SQL_SUCCESS;
     240             :         case SQL_DESC_TYPE:                     /* SQLSMALLINT */
     241           0 :                 while (tp->concise_type != 0) {
     242           0 :                         if ((SQLSMALLINT) (intptr_t) ValuePtr == tp->type &&
     243           0 :                             (((SQLSMALLINT) (intptr_t) ValuePtr != SQL_DATETIME &&
     244           0 :                               (SQLSMALLINT) (intptr_t) ValuePtr != SQL_INTERVAL) ||
     245           0 :                              tp->code == rec->sql_desc_datetime_interval_code)) {
     246           0 :                                 if (tp->concise_type == SQL_HUGEINT)
     247           0 :                                         desc->Dbc->allow_hugeint = true;
     248           0 :                                 rec->sql_desc_concise_type = tp->concise_type;
     249           0 :                                 rec->sql_desc_type = tp->type;
     250           0 :                                 rec->sql_desc_datetime_interval_code = tp->code;
     251           0 :                                 if (tp->precision != UNAFFECTED)
     252           0 :                                         rec->sql_desc_precision = tp->precision;
     253           0 :                                 if (tp->datetime_interval_precision != UNAFFECTED)
     254           0 :                                         rec->sql_desc_datetime_interval_precision = tp->datetime_interval_precision;
     255           0 :                                 if (tp->length != UNAFFECTED)
     256           0 :                                         rec->sql_desc_length = tp->length;
     257           0 :                                 if (tp->scale != UNAFFECTED)
     258           0 :                                         rec->sql_desc_scale = tp->scale;
     259           0 :                                 rec->sql_desc_fixed_prec_scale = tp->fixed;
     260           0 :                                 rec->sql_desc_num_prec_radix = tp->radix;
     261           0 :                                 return SQL_SUCCESS;
     262             :                         }
     263           0 :                         tp++;
     264             :                 }
     265             :                 /* Inconsistent descriptor information */
     266           0 :                 addDescError(desc, "HY021", NULL, 0);
     267           0 :                 return SQL_ERROR;
     268           0 :         case SQL_DESC_UNNAMED:                  /* SQLSMALLINT */
     269           0 :                 if ((SQLSMALLINT) (intptr_t) ValuePtr == SQL_NAMED) {
     270             :                         /* Invalid descriptor field identifier */
     271           0 :                         addDescError(desc, "HY091", NULL, 0);
     272           0 :                         return SQL_ERROR;
     273           0 :                 } else if ((SQLSMALLINT) (intptr_t) ValuePtr == SQL_UNNAMED && isIPD(desc)) {
     274           0 :                         rec->sql_desc_unnamed = SQL_UNNAMED;
     275           0 :                         if (rec->sql_desc_name)
     276           0 :                                 free(rec->sql_desc_name);
     277           0 :                         rec->sql_desc_name = NULL;
     278           0 :                         return SQL_SUCCESS;
     279             :                 }
     280             :                 /* Inconsistent descriptor information */
     281           0 :                 addDescError(desc, "HY021", NULL, 0);
     282           0 :                 return SQL_ERROR;
     283             :         }
     284             : 
     285             :         /* Invalid descriptor field identifier */
     286           0 :         addDescError(desc, "HY091", NULL, 0);
     287           0 :         return SQL_ERROR;
     288             : }
     289             : 
     290             : SQLRETURN SQL_API
     291             : SQLSetDescField(SQLHDESC DescriptorHandle,
     292             :                 SQLSMALLINT RecNumber,
     293             :                 SQLSMALLINT FieldIdentifier,
     294             :                 SQLPOINTER ValuePtr,
     295             :                 SQLINTEGER BufferLength)
     296             : {
     297             : #ifdef ODBCDEBUG
     298           0 :         ODBCLOG("SQLSetDescField %p %d %s %p %d\n",
     299             :                 DescriptorHandle, (int) RecNumber,
     300             :                 translateFieldIdentifier(FieldIdentifier),
     301             :                 ValuePtr, (int) BufferLength);
     302             : #endif
     303             : 
     304           0 :         if (!isValidDesc((ODBCDesc *) DescriptorHandle))
     305             :                 return SQL_INVALID_HANDLE;
     306             : 
     307           0 :         clearDescErrors((ODBCDesc *) DescriptorHandle);
     308             : 
     309           0 :         return MNDBSetDescField((ODBCDesc *) DescriptorHandle, RecNumber, FieldIdentifier, ValuePtr, BufferLength);
     310             : }
     311             : 
     312             : SQLRETURN SQL_API
     313             : SQLSetDescFieldW(SQLHDESC DescriptorHandle,
     314             :                  SQLSMALLINT RecNumber,
     315             :                  SQLSMALLINT FieldIdentifier,
     316             :                  SQLPOINTER ValuePtr,
     317             :                  SQLINTEGER BufferLength)
     318             : {
     319             :         ODBCDesc *desc = (ODBCDesc *) DescriptorHandle;
     320             :         SQLRETURN rc;
     321             :         SQLPOINTER ptr;
     322             :         SQLINTEGER n;
     323             : 
     324             : #ifdef ODBCDEBUG
     325           0 :         ODBCLOG("SQLSetDescFieldW %p %d %s %p %d\n",
     326             :                 DescriptorHandle, (int) RecNumber,
     327             :                 translateFieldIdentifier(FieldIdentifier),
     328             :                 ValuePtr, (int) BufferLength);
     329             : #endif
     330             : 
     331           0 :         if (!isValidDesc(desc))
     332             :                 return SQL_INVALID_HANDLE;
     333             : 
     334           0 :         clearDescErrors(desc);
     335             : 
     336           0 :         switch (FieldIdentifier) {
     337           0 :         case SQL_DESC_NAME:
     338           0 :                 if (BufferLength > 0)        /* convert from bytes to characters */
     339           0 :                         BufferLength /= 2;
     340           0 :                 fixWcharIn(ValuePtr, BufferLength, SQLCHAR, ptr,
     341             :                            addDescError, desc, return SQL_ERROR);
     342             :                 n = SQL_NTS;
     343           0 :                 break;
     344             :         default:
     345             :                 ptr = ValuePtr;
     346             :                 n = BufferLength;
     347             :                 break;
     348             :         }
     349             : 
     350           0 :         rc = MNDBSetDescField(desc, RecNumber, FieldIdentifier, ptr, n);
     351             : 
     352           0 :         if (ptr && ptr != ValuePtr)
     353           0 :                 free(ptr);
     354             : 
     355             :         return rc;
     356             : }

Generated by: LCOV version 1.14