LCOV - code coverage report
Current view: top level - clients/odbc/driver - ODBCDesc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 102 222 45.9 %
Date: 2021-10-13 02:24:04 Functions: 7 9 77.8 %

          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             : #include "ODBCGlobal.h"
      10             : #include "ODBCStmt.h"
      11             : 
      12             : #define ODBC_DESC_MAGIC_NR      21845   /* for internal sanity check only */
      13             : 
      14             : /*
      15             :  * Creates a new allocated ODBCDesc object and initializes it.
      16             :  *
      17             :  * Precondition: valid ODBCDbc object
      18             :  * Postcondition: returns a new ODBCDesc object
      19             :  */
      20             : ODBCDesc *
      21          12 : newODBCDesc(ODBCDbc *dbc)
      22             : {
      23             :         ODBCDesc *desc;
      24             : 
      25          12 :         assert(dbc);
      26             : 
      27          12 :         desc = (ODBCDesc *) malloc(sizeof(ODBCDesc));
      28          12 :         if (desc == NULL) {
      29             :                 /* Memory allocation error */
      30           0 :                 addDbcError(dbc, "HY001", NULL, 0);
      31           0 :                 return NULL;
      32             :         }
      33             : 
      34          12 :         *desc = (ODBCDesc) {
      35             :                 .Dbc = dbc,
      36             :                 .sql_desc_alloc_type = SQL_DESC_ALLOC_USER,
      37             :                 .sql_desc_array_size = 1,
      38             :                 .sql_desc_bind_type = SQL_BIND_TYPE_DEFAULT,
      39             :                 .Type = ODBC_DESC_MAGIC_NR,     /* set it valid */
      40             :         };
      41          12 :         return desc;
      42             : }
      43             : 
      44             : 
      45             : /*
      46             :  * Check if the descriptor handle is valid.
      47             :  * Note: this function is used internally by the driver to assert legal
      48             :  * and save usage of the handle and prevent crashes as much as possible.
      49             :  *
      50             :  * Precondition: none
      51             :  * Postcondition: returns 1 if it is a valid statement handle,
      52             :  *      returns 0 if is invalid and thus an unusable handle.
      53             :  */
      54             : int
      55          12 : isValidDesc(ODBCDesc *desc)
      56             : {
      57             : #ifdef ODBCDEBUG
      58          12 :         if (!(desc && desc->Type == ODBC_DESC_MAGIC_NR))
      59           0 :                 ODBCLOG("desc %pnot a valid descriptor handle\n", desc);
      60             : #endif
      61          12 :         return desc && desc->Type == ODBC_DESC_MAGIC_NR;
      62             : }
      63             : 
      64             : /*
      65             :  * Creates and adds an error msg object to the end of the error list of
      66             :  * this ODBCDesc struct.
      67             :  * When the errMsg is NULL and the SQLState is an ISO SQLState the
      68             :  * standard ISO message text for the SQLState is used as message.
      69             :  *
      70             :  * Precondition: desc must be valid. SQLState and errMsg may be NULL.
      71             :  */
      72             : void
      73           0 : addDescError(ODBCDesc *desc, const char *SQLState, const char *errMsg, int nativeErrCode)
      74             : {
      75             :         ODBCError *error = NULL;
      76             : 
      77             : #ifdef ODBCDEBUG
      78           0 :         ODBCLOG("addDescError %p %s %s %d\n", desc, SQLState, errMsg ? errMsg : getStandardSQLStateMsg(SQLState), nativeErrCode);
      79             : #endif
      80           0 :         assert(isValidDesc(desc));
      81             : 
      82           0 :         error = newODBCError(SQLState, errMsg, nativeErrCode);
      83           0 :         appendODBCError(&desc->Error, error);
      84           0 : }
      85             : 
      86             : /*
      87             :  * Extracts an error object from the error list of this ODBCDesc struct.
      88             :  * The error object itself is removed from the error list.
      89             :  * The caller is now responsible for freeing the error object memory.
      90             :  *
      91             :  * Precondition: desc and error must be valid
      92             :  * Postcondition: returns a ODBCError object or null when no error is available.
      93             :  */
      94             : ODBCError *
      95           0 : getDescError(ODBCDesc *desc)
      96             : {
      97           0 :         assert(isValidDesc(desc));
      98           0 :         return desc->Error;
      99             : }
     100             : 
     101             : static void
     102          85 : cleanODBCDescRec(ODBCDesc *desc, ODBCDescRec *rec)
     103             : {
     104          85 :         if (rec->sql_desc_base_column_name)
     105          10 :                 free(rec->sql_desc_base_column_name);
     106          85 :         if (rec->sql_desc_base_table_name)
     107          10 :                 free(rec->sql_desc_base_table_name);
     108          85 :         if (rec->sql_desc_catalog_name)
     109          33 :                 free(rec->sql_desc_catalog_name);
     110          85 :         if (rec->sql_desc_label)
     111          28 :                 free(rec->sql_desc_label);
     112          85 :         if (rec->sql_desc_literal_prefix)
     113           0 :                 free(rec->sql_desc_literal_prefix);
     114          85 :         if (rec->sql_desc_literal_suffix)
     115           0 :                 free(rec->sql_desc_literal_suffix);
     116          85 :         if (rec->sql_desc_local_type_name)
     117           0 :                 free(rec->sql_desc_local_type_name);
     118          85 :         if (rec->sql_desc_name)
     119          28 :                 free(rec->sql_desc_name);
     120          85 :         if (rec->sql_desc_schema_name)
     121          28 :                 free(rec->sql_desc_schema_name);
     122          85 :         if (rec->sql_desc_table_name)
     123          28 :                 free(rec->sql_desc_table_name);
     124          85 :         if (rec->sql_desc_type_name)
     125          33 :                 free(rec->sql_desc_type_name);
     126          85 :         *rec = (ODBCDescRec) {0};
     127          85 :         if (desc) {
     128           0 :                 if (isAD(desc)) {
     129           0 :                         rec->sql_desc_concise_type = SQL_C_DEFAULT;
     130           0 :                         rec->sql_desc_type = SQL_C_DEFAULT;
     131           0 :                 } else if (isIPD(desc)) {
     132           0 :                         rec->sql_desc_parameter_type = SQL_PARAM_INPUT;
     133           0 :                         rec->sql_desc_nullable = SQL_NULLABLE;
     134             :                 }
     135             :         }
     136          85 : }
     137             : 
     138             : void
     139        2075 : setODBCDescRecCount(ODBCDesc *desc, int count)
     140             : {
     141        2075 :         assert(count >= 0);
     142        2075 :         assert(desc->sql_desc_count >= 0);
     143             : 
     144        2075 :         if (count == desc->sql_desc_count)
     145             :                 return;
     146          41 :         if (count < desc->sql_desc_count) {
     147             :                 int i;
     148             : 
     149          97 :                 for (i = count + 1; i <= desc->sql_desc_count; i++)
     150          85 :                         cleanODBCDescRec(NULL, &desc->descRec[i]);
     151             :         }
     152          41 :         if (count == 0) {
     153          12 :                 assert(desc->descRec != NULL);
     154          12 :                 free(desc->descRec);
     155          12 :                 desc->descRec = NULL;
     156          29 :         } else if (desc->descRec == NULL) {
     157          12 :                 assert(desc->sql_desc_count == 0);
     158          12 :                 desc->descRec = malloc((count + 1) * sizeof(*desc->descRec));
     159             :         } else {
     160             :                 ODBCDescRec *p;
     161          17 :                 assert(desc->sql_desc_count > 0);
     162          17 :                 p = realloc(desc->descRec, (count + 1) * sizeof(*desc->descRec));
     163          17 :                 if (p == NULL)
     164             :                         return; /* TODO: error handling */
     165          17 :                 desc->descRec = p;
     166             :         }
     167          41 :         if (count > desc->sql_desc_count) {
     168             :                 int i;
     169             : 
     170          29 :                 memset(desc->descRec + desc->sql_desc_count + 1, 0, (count - desc->sql_desc_count) * sizeof(*desc->descRec));
     171          29 :                 if (isAD(desc)) {
     172          49 :                         for (i = desc->sql_desc_count + 1; i <= count; i++) {
     173          32 :                                 desc->descRec[i].sql_desc_concise_type = SQL_C_DEFAULT;
     174          32 :                                 desc->descRec[i].sql_desc_type = SQL_C_DEFAULT;
     175             :                         }
     176          12 :                 } else if (isIPD(desc)) {
     177          28 :                         for (i = desc->sql_desc_count + 1; i <= count; i++) {
     178          20 :                                 desc->descRec[i].sql_desc_parameter_type = SQL_PARAM_INPUT;
     179          20 :                                 desc->descRec[i].sql_desc_nullable = SQL_NULLABLE;
     180             :                         }
     181             :                 }
     182             :         }
     183          41 :         desc->sql_desc_count = count;
     184             : }
     185             : 
     186             : /*
     187             :  * Destroys the ODBCDesc object including its own managed data.
     188             :  *
     189             :  * Precondition: desc must be valid.
     190             :  * Postcondition: desc is completely destroyed, desc handle is become invalid.
     191             :  */
     192             : void
     193          12 : destroyODBCDesc(ODBCDesc *desc)
     194             : {
     195          12 :         assert(isValidDesc(desc));
     196             : 
     197          12 :         desc->Type = 0;
     198          12 :         deleteODBCErrorList(&desc->Error);
     199          12 :         setODBCDescRecCount(desc, 0);
     200          12 :         free(desc);
     201          12 : }
     202             : 
     203             : ODBCDescRec *
     204          10 : addODBCDescRec(ODBCDesc *desc, SQLSMALLINT recno)
     205             : {
     206          10 :         assert(desc);
     207          10 :         assert(recno > 0);
     208             : 
     209          10 :         if (desc->sql_desc_count < recno)
     210          10 :                 setODBCDescRecCount(desc, recno);
     211             :         else {
     212           0 :                 assert(desc->descRec != NULL);
     213           0 :                 cleanODBCDescRec(desc, &desc->descRec[recno]);
     214             :         }
     215             : 
     216          10 :         return &desc->descRec[recno];
     217             : }
     218             : 
     219             : /* Return either the column size or display size for a column or parameter. */
     220             : SQLULEN
     221         129 : ODBCLength(ODBCDescRec *rec, int lengthtype)
     222             : {
     223         129 :         switch (rec->sql_desc_concise_type) {
     224           0 :         case SQL_CHAR:
     225             :         case SQL_VARCHAR:
     226             :         case SQL_LONGVARCHAR:
     227           0 :                 if (lengthtype == SQL_DESC_OCTET_LENGTH)
     228           0 :                         return rec->sql_desc_length * 6;
     229             :                 else
     230           0 :                         return rec->sql_desc_length;
     231          39 :         case SQL_WCHAR:
     232             :         case SQL_WVARCHAR:
     233             :         case SQL_WLONGVARCHAR:
     234             :         case SQL_BINARY:
     235             :         case SQL_VARBINARY:
     236             :         case SQL_LONGVARBINARY:
     237          39 :                 return rec->sql_desc_length;
     238           0 :         case SQL_DECIMAL:
     239             :         case SQL_NUMERIC:
     240           0 :                 return rec->sql_desc_length + (lengthtype == SQL_DESC_LENGTH ? 0 : 2);
     241             :         case SQL_BIT:
     242             :                 return 1;
     243           0 :         case SQL_TINYINT:
     244             :                 switch (lengthtype) {
     245             :                 case SQL_DESC_LENGTH:
     246             :                         return 3;
     247           0 :                 case SQL_DESC_DISPLAY_SIZE:
     248           0 :                         return 3 + !rec->sql_desc_unsigned;
     249           0 :                 case SQL_DESC_OCTET_LENGTH:
     250           0 :                         return (int) sizeof(char);
     251             :                 }
     252             :                 break;
     253          15 :         case SQL_SMALLINT:
     254             :                 switch (lengthtype) {
     255             :                 case SQL_DESC_LENGTH:
     256             :                         return 5;
     257           5 :                 case SQL_DESC_DISPLAY_SIZE:
     258           5 :                         return 5 + !rec->sql_desc_unsigned;
     259           5 :                 case SQL_DESC_OCTET_LENGTH:
     260           5 :                         return (int) sizeof(short);
     261             :                 }
     262             :                 break;
     263          21 :         case SQL_INTEGER:
     264             :                 switch (lengthtype) {
     265             :                 case SQL_DESC_LENGTH:
     266             :                         return 10;
     267           7 :                 case SQL_DESC_DISPLAY_SIZE:
     268           7 :                         return 10 + !rec->sql_desc_unsigned;
     269           7 :                 case SQL_DESC_OCTET_LENGTH:
     270           7 :                         return (int) sizeof(int);
     271             :                 }
     272             :                 break;
     273           9 :         case SQL_BIGINT:
     274             :                 switch (lengthtype) {
     275           3 :                 case SQL_DESC_LENGTH:
     276           3 :                         return 19 + (rec->sql_desc_unsigned != 0);
     277             :                 case SQL_DESC_DISPLAY_SIZE:
     278             :                 case SQL_DESC_OCTET_LENGTH:
     279             :                         return 20;
     280             :                 }
     281             :                 break;
     282           0 :         case SQL_HUGEINT:
     283             :                 switch (lengthtype) {
     284           0 :                 case SQL_DESC_LENGTH:
     285           0 :                         return 39 + (rec->sql_desc_unsigned != 0);
     286             :                 case SQL_DESC_DISPLAY_SIZE:
     287             :                 case SQL_DESC_OCTET_LENGTH:
     288             :                         return 40;
     289             :                 }
     290             :                 break;
     291           0 :         case SQL_REAL:
     292             :                 switch (lengthtype) {
     293             :                 case SQL_DESC_LENGTH:
     294             :                         return 7;
     295           0 :                 case SQL_DESC_DISPLAY_SIZE:
     296             :                         /* sign, 7 digits, decimal point, E, sign, 2 digits */
     297           0 :                         return 14;
     298           0 :                 case SQL_DESC_OCTET_LENGTH:
     299           0 :                         return (int) sizeof(float);
     300             :                 }
     301             :                 break;
     302          15 :         case SQL_FLOAT:
     303             :         case SQL_DOUBLE:
     304             :                 switch (lengthtype) {
     305             :                 case SQL_DESC_LENGTH:
     306             :                         return 15;
     307           5 :                 case SQL_DESC_DISPLAY_SIZE:
     308             :                         /* sign, 15 digits, decimal point, E, sign, 3 digits */
     309           5 :                         return 24;
     310           5 :                 case SQL_DESC_OCTET_LENGTH:
     311           5 :                         return (int) sizeof(double);
     312             :                 }
     313             :                 break;
     314          15 :         case SQL_TYPE_DATE:
     315          15 :                 if (lengthtype == SQL_DESC_OCTET_LENGTH)
     316             :                         return (int) sizeof(SQL_DATE_STRUCT);
     317             :                 else {
     318             :                         /* strlen("yyyy-mm-dd") */
     319          10 :                         return 10;
     320             :                 }
     321          15 :         case SQL_TYPE_TIME:
     322          15 :                 if (lengthtype == SQL_DESC_OCTET_LENGTH)
     323             :                         return (int) sizeof(SQL_TIME_STRUCT);
     324             :                 else {
     325             :                         /* strlen("hh:mm:ss.fff") */
     326          10 :                         return 12;
     327             :                 }
     328           0 :         case SQL_TYPE_TIMESTAMP:
     329           0 :                 if (lengthtype == SQL_DESC_OCTET_LENGTH)
     330             :                         return (int) sizeof(SQL_TIMESTAMP_STRUCT);
     331             :                 else {
     332             :                         /* strlen("yyyy-mm-dd hh:mm:ss.fff") */
     333           0 :                         return 23;
     334             :                 }
     335           0 :         case SQL_INTERVAL_SECOND:
     336           0 :                 if (lengthtype == SQL_DESC_OCTET_LENGTH)
     337             :                         return (int) sizeof(SQL_INTERVAL_STRUCT);
     338             :                 /* strlen("INTERVAL -'sss[.fff]' SECOND(p,q)") */
     339           0 :                 return 11 + 13 +
     340           0 :                         (rec->sql_desc_datetime_interval_precision > 10) +
     341           0 :                         (rec->sql_desc_precision > 10) +
     342           0 :                         rec->sql_desc_datetime_interval_precision +
     343             :                         (rec->sql_desc_precision > 0 ?
     344           0 :                          rec->sql_desc_precision + 1 :
     345             :                          0);
     346           0 :         case SQL_INTERVAL_DAY_TO_SECOND:
     347           0 :                 if (lengthtype == SQL_DESC_OCTET_LENGTH)
     348             :                         return (int) sizeof(SQL_INTERVAL_STRUCT);
     349             :                 /* strlen("INTERVAL -'ddd hh:mm:ss[.fff]' DAY(p) TO SECOND(q)") */
     350           0 :                 return 11 + 21 +
     351           0 :                         (rec->sql_desc_datetime_interval_precision > 10) +
     352           0 :                         (rec->sql_desc_precision > 10) +
     353           0 :                         rec->sql_desc_datetime_interval_precision +
     354           0 :                         9 +
     355             :                         (rec->sql_desc_precision > 0 ?
     356           0 :                          rec->sql_desc_precision + 1 :
     357             :                          0);
     358           0 :         case SQL_INTERVAL_HOUR_TO_SECOND:
     359           0 :                 if (lengthtype == SQL_DESC_OCTET_LENGTH)
     360             :                         return (int) sizeof(SQL_INTERVAL_STRUCT);
     361             :                 /* strlen("INTERVAL -'hhh:mm:ss[.fff]' HOUR(p) TO SECOND(q)") */
     362           0 :                 return 11 + 22 +
     363           0 :                         (rec->sql_desc_datetime_interval_precision > 10) +
     364           0 :                         (rec->sql_desc_precision > 10) +
     365           0 :                         rec->sql_desc_datetime_interval_precision +
     366           0 :                         6 +
     367             :                         (rec->sql_desc_precision > 0 ?
     368           0 :                          rec->sql_desc_precision + 1 :
     369             :                          0);
     370           0 :         case SQL_INTERVAL_MINUTE_TO_SECOND:
     371           0 :                 if (lengthtype == SQL_DESC_OCTET_LENGTH)
     372             :                         return (int) sizeof(SQL_INTERVAL_STRUCT);
     373             :                 /* strlen("INTERVAL -'mmm:ss[.fff]' MINUTE(p) TO SECOND(q)") */
     374           0 :                 return 11 + 24 +
     375           0 :                         (rec->sql_desc_datetime_interval_precision > 10) +
     376           0 :                         (rec->sql_desc_precision > 10) +
     377           0 :                         rec->sql_desc_datetime_interval_precision +
     378           0 :                         3 +
     379             :                         (rec->sql_desc_precision > 0 ?
     380           0 :                          rec->sql_desc_precision + 1 :
     381             :                          0);
     382           0 :         case SQL_INTERVAL_YEAR:
     383           0 :                 if (lengthtype == SQL_DESC_OCTET_LENGTH)
     384             :                         return (int) sizeof(SQL_INTERVAL_STRUCT);
     385             :                 /* strlen("INTERVAL -'yyy' YEAR(p)") */
     386           0 :                 return 11 + 9 +
     387           0 :                         (rec->sql_desc_datetime_interval_precision > 10) +
     388             :                         rec->sql_desc_datetime_interval_precision;
     389           0 :         case SQL_INTERVAL_MONTH:
     390           0 :                 if (lengthtype == SQL_DESC_OCTET_LENGTH)
     391             :                         return (int) sizeof(SQL_INTERVAL_STRUCT);
     392             :                 /* strlen("INTERVAL -'yyy' MONTH(p)") */
     393           0 :                 return 11 + 10 +
     394           0 :                         (rec->sql_desc_datetime_interval_precision > 10) +
     395             :                         rec->sql_desc_datetime_interval_precision;
     396           0 :         case SQL_INTERVAL_DAY:
     397           0 :                 if (lengthtype == SQL_DESC_OCTET_LENGTH)
     398             :                         return (int) sizeof(SQL_INTERVAL_STRUCT);
     399             :                 /* strlen("INTERVAL -'yyy' DAY(p)") */
     400           0 :                 return 11 + 8 +
     401           0 :                         (rec->sql_desc_datetime_interval_precision > 10) +
     402             :                         rec->sql_desc_datetime_interval_precision;
     403           0 :         case SQL_INTERVAL_HOUR:
     404           0 :                 if (lengthtype == SQL_DESC_OCTET_LENGTH)
     405             :                         return (int) sizeof(SQL_INTERVAL_STRUCT);
     406             :                 /* strlen("INTERVAL -'yyy' HOUR(p)") */
     407           0 :                 return 11 + 9 +
     408           0 :                         (rec->sql_desc_datetime_interval_precision > 10) +
     409             :                         rec->sql_desc_datetime_interval_precision;
     410           0 :         case SQL_INTERVAL_MINUTE:
     411           0 :                 if (lengthtype == SQL_DESC_OCTET_LENGTH)
     412             :                         return (int) sizeof(SQL_INTERVAL_STRUCT);
     413             :                 /* strlen("INTERVAL -'yyy' MINUTE(p)") */
     414           0 :                 return 11 + 11 +
     415           0 :                         (rec->sql_desc_datetime_interval_precision > 10) +
     416             :                         rec->sql_desc_datetime_interval_precision;
     417           0 :         case SQL_INTERVAL_YEAR_TO_MONTH:
     418           0 :                 if (lengthtype == SQL_DESC_OCTET_LENGTH)
     419             :                         return (int) sizeof(SQL_INTERVAL_STRUCT);
     420             :                 /* strlen("INTERVAL -'yyy' YEAR(p) TO MONTH") */
     421           0 :                 return 11 + 18 +
     422           0 :                         (rec->sql_desc_datetime_interval_precision > 10) +
     423           0 :                         rec->sql_desc_datetime_interval_precision +
     424             :                         3;
     425           0 :         case SQL_INTERVAL_DAY_TO_HOUR:
     426           0 :                 if (lengthtype == SQL_DESC_OCTET_LENGTH)
     427             :                         return (int) sizeof(SQL_INTERVAL_STRUCT);
     428             :                 /* strlen("INTERVAL -'yyy' DAY(p) TO HOUR") */
     429           0 :                 return 11 + 16 +
     430           0 :                         (rec->sql_desc_datetime_interval_precision > 10) +
     431           0 :                         rec->sql_desc_datetime_interval_precision +
     432             :                         3;
     433           0 :         case SQL_INTERVAL_HOUR_TO_MINUTE:
     434           0 :                 if (lengthtype == SQL_DESC_OCTET_LENGTH)
     435             :                         return (int) sizeof(SQL_INTERVAL_STRUCT);
     436             :                 /* strlen("INTERVAL -'yyy' HOUR(p) TO MINUTE") */
     437           0 :                 return 11 + 19 +
     438           0 :                         (rec->sql_desc_datetime_interval_precision > 10) +
     439           0 :                         rec->sql_desc_datetime_interval_precision +
     440             :                         3;
     441           0 :         case SQL_INTERVAL_DAY_TO_MINUTE:
     442           0 :                 if (lengthtype == SQL_DESC_OCTET_LENGTH)
     443             :                         return (int) sizeof(SQL_INTERVAL_STRUCT);
     444             :                 /* strlen("INTERVAL -'yyy' DAY(p) TO MINUTE") */
     445           0 :                 return 11 + 18 +
     446           0 :                         (rec->sql_desc_datetime_interval_precision > 10) +
     447           0 :                         rec->sql_desc_datetime_interval_precision +
     448             :                         6;
     449           0 :         case SQL_GUID:
     450           0 :                 if (lengthtype == SQL_DESC_OCTET_LENGTH)
     451           0 :                         return 16; /* sizeof(SQLGUID) */
     452             :                 /* strlen("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee") */
     453             :                 return 36;
     454             :         default:
     455             :                 break;
     456             :         }
     457           0 :         return SQL_NO_TOTAL;
     458             : }

Generated by: LCOV version 1.14