| 1 | /********************************************************************* |
| 2 | * |
| 3 | * This is based on code created by Peter Harvey, |
| 4 | * (pharvey@codebydesign.com). |
| 5 | * |
| 6 | * Modified and extended by Nick Gorham |
| 7 | * (nick@lurcher.org). |
| 8 | * |
| 9 | * Any bugs or problems should be considered the fault of Nick and not |
| 10 | * Peter. |
| 11 | * |
| 12 | * copyright (c) 1999 Nick Gorham |
| 13 | * |
| 14 | * This library is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU Lesser General Public |
| 16 | * License as published by the Free Software Foundation; either |
| 17 | * version 2 of the License, or (at your option) any later version. |
| 18 | * |
| 19 | * This library is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | * Lesser General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU Lesser General Public |
| 25 | * License along with this library; if not, write to the Free Software |
| 26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 27 | * |
| 28 | ********************************************************************** |
| 29 | * |
| 30 | * $Id: SQLGetDescRec.c,v 1.13 2009/02/18 17:59:08 lurcher Exp $ |
| 31 | * |
| 32 | * $Log: SQLGetDescRec.c,v $ |
| 33 | * Revision 1.13 2009/02/18 17:59:08 lurcher |
| 34 | * Shift to using config.h, the compile lines were making it hard to spot warnings |
| 35 | * |
| 36 | * Revision 1.12 2008/09/29 14:02:45 lurcher |
| 37 | * Fix missing dlfcn group option |
| 38 | * |
| 39 | * Revision 1.11 2007/04/02 10:50:19 lurcher |
| 40 | * Fix some 64bit problems (only when sizeof(SQLLEN) == 8 ) |
| 41 | * |
| 42 | * Revision 1.10 2007/03/05 09:49:23 lurcher |
| 43 | * Get it to build on VMS again |
| 44 | * |
| 45 | * Revision 1.9 2006/01/06 18:44:35 lurcher |
| 46 | * Couple of unicode fixes |
| 47 | * |
| 48 | * Revision 1.8 2004/11/22 17:02:49 lurcher |
| 49 | * Fix unicode/ansi conversion in the SQLGet functions |
| 50 | * |
| 51 | * Revision 1.7 2003/10/30 18:20:46 lurcher |
| 52 | * |
| 53 | * Fix broken thread protection |
| 54 | * Remove SQLNumResultCols after execute, lease S4/S% to driver |
| 55 | * Fix string overrun in SQLDriverConnect |
| 56 | * Add initial support for Interix |
| 57 | * |
| 58 | * Revision 1.6 2003/02/27 12:19:39 lurcher |
| 59 | * |
| 60 | * Add the A functions as well as the W |
| 61 | * |
| 62 | * Revision 1.5 2002/12/05 17:44:30 lurcher |
| 63 | * |
| 64 | * Display unknown return values in return logging |
| 65 | * |
| 66 | * Revision 1.4 2002/08/23 09:42:37 lurcher |
| 67 | * |
| 68 | * Fix some build warnings with casts, and a AIX linker mod, to include |
| 69 | * deplib's on the link line, but not the libtool generated ones |
| 70 | * |
| 71 | * Revision 1.3 2002/07/24 08:49:52 lurcher |
| 72 | * |
| 73 | * Alter UNICODE support to use iconv for UNICODE-ANSI conversion |
| 74 | * |
| 75 | * Revision 1.2 2001/12/13 13:00:32 lurcher |
| 76 | * |
| 77 | * Remove most if not all warnings on 64 bit platforms |
| 78 | * Add support for new MS 3.52 64 bit changes |
| 79 | * Add override to disable the stopping of tracing |
| 80 | * Add MAX_ROWS support in postgres driver |
| 81 | * |
| 82 | * Revision 1.1.1.1 2001/10/17 16:40:05 lurcher |
| 83 | * |
| 84 | * First upload to SourceForge |
| 85 | * |
| 86 | * Revision 1.5 2001/07/03 09:30:41 nick |
| 87 | * |
| 88 | * Add ability to alter size of displayed message in the log |
| 89 | * |
| 90 | * Revision 1.4 2001/04/17 16:29:39 nick |
| 91 | * |
| 92 | * More checks and autotest fixes |
| 93 | * |
| 94 | * Revision 1.3 2001/04/12 17:43:36 nick |
| 95 | * |
| 96 | * Change logging and added autotest to odbctest |
| 97 | * |
| 98 | * Revision 1.2 2000/12/31 20:30:54 nick |
| 99 | * |
| 100 | * Add UNICODE support |
| 101 | * |
| 102 | * Revision 1.1.1.1 2000/09/04 16:42:52 nick |
| 103 | * Imported Sources |
| 104 | * |
| 105 | * Revision 1.8 2001/04/27 01:29:35 ngorham |
| 106 | * |
| 107 | * Added a couple of fixes from Tim Roepken |
| 108 | * |
| 109 | * Revision 1.7 1999/11/13 23:40:59 ngorham |
| 110 | * |
| 111 | * Alter the way DM logging works |
| 112 | * Upgrade the Postgres driver to 6.4.6 |
| 113 | * |
| 114 | * Revision 1.6 1999/10/24 23:54:18 ngorham |
| 115 | * |
| 116 | * First part of the changes to the error reporting |
| 117 | * |
| 118 | * Revision 1.5 1999/09/21 22:34:25 ngorham |
| 119 | * |
| 120 | * Improve performance by removing unneeded logging calls when logging is |
| 121 | * disabled |
| 122 | * |
| 123 | * Revision 1.4 1999/07/10 21:10:16 ngorham |
| 124 | * |
| 125 | * Adjust error sqlstate from driver manager, depending on requested |
| 126 | * version (ODBC2/3) |
| 127 | * |
| 128 | * Revision 1.3 1999/07/04 21:05:07 ngorham |
| 129 | * |
| 130 | * Add LGPL Headers to code |
| 131 | * |
| 132 | * Revision 1.2 1999/06/30 23:56:55 ngorham |
| 133 | * |
| 134 | * Add initial thread safety code |
| 135 | * |
| 136 | * Revision 1.1.1.1 1999/05/29 13:41:07 sShandyb |
| 137 | * first go at it |
| 138 | * |
| 139 | * Revision 1.1.1.1 1999/05/27 18:23:17 pharvey |
| 140 | * Imported sources |
| 141 | * |
| 142 | * Revision 1.3 1999/05/04 22:41:12 nick |
| 143 | * and another night ends |
| 144 | * |
| 145 | * Revision 1.2 1999/05/03 19:50:43 nick |
| 146 | * Another check point |
| 147 | * |
| 148 | * Revision 1.1 1999/04/25 23:06:11 nick |
| 149 | * Initial revision |
| 150 | * |
| 151 | * |
| 152 | **********************************************************************/ |
| 153 | |
| 154 | #include <config.h> |
| 155 | #include "drivermanager.h" |
| 156 | |
| 157 | static char const rcsid[]= "$RCSfile: SQLGetDescRec.c,v $ $Revision: 1.13 $" ; |
| 158 | |
| 159 | SQLRETURN SQLGetDescRecA( SQLHDESC descriptor_handle, |
| 160 | SQLSMALLINT rec_number, |
| 161 | SQLCHAR *name, |
| 162 | SQLSMALLINT buffer_length, |
| 163 | SQLSMALLINT *string_length, |
| 164 | SQLSMALLINT *type, |
| 165 | SQLSMALLINT *sub_type, |
| 166 | SQLLEN *length, |
| 167 | SQLSMALLINT *precision, |
| 168 | SQLSMALLINT *scale, |
| 169 | SQLSMALLINT *nullable ) |
| 170 | { |
| 171 | return SQLGetDescRec( descriptor_handle, |
| 172 | rec_number, |
| 173 | name, |
| 174 | buffer_length, |
| 175 | string_length, |
| 176 | type, |
| 177 | sub_type, |
| 178 | length, |
| 179 | precision, |
| 180 | scale, |
| 181 | nullable ); |
| 182 | } |
| 183 | |
| 184 | SQLRETURN SQLGetDescRec( SQLHDESC descriptor_handle, |
| 185 | SQLSMALLINT rec_number, |
| 186 | SQLCHAR *name, |
| 187 | SQLSMALLINT buffer_length, |
| 188 | SQLSMALLINT *string_length, |
| 189 | SQLSMALLINT *type, |
| 190 | SQLSMALLINT *sub_type, |
| 191 | SQLLEN *length, |
| 192 | SQLSMALLINT *precision, |
| 193 | SQLSMALLINT *scale, |
| 194 | SQLSMALLINT *nullable ) |
| 195 | { |
| 196 | /* |
| 197 | * not quite sure how the descriptor can be |
| 198 | * allocated to a statement, all the documentation talks |
| 199 | * about state transitions on statement states, but the |
| 200 | * descriptor may be allocated with more than one statement |
| 201 | * at one time. Which one should I check ? |
| 202 | */ |
| 203 | DMHDESC descriptor = (DMHDESC) descriptor_handle; |
| 204 | SQLRETURN ret; |
| 205 | SQLCHAR s1[ 100 + LOG_MESSAGE_LEN ], s2[ 100 + LOG_MESSAGE_LEN ], s3[ 100 + LOG_MESSAGE_LEN ], s4[ 100 + LOG_MESSAGE_LEN ]; |
| 206 | SQLCHAR s5[ 100 + LOG_MESSAGE_LEN ], s6[ 100 + LOG_MESSAGE_LEN ], s7[ 100 + LOG_MESSAGE_LEN ]; |
| 207 | SQLCHAR s8[ 100 + LOG_MESSAGE_LEN ]; |
| 208 | |
| 209 | /* |
| 210 | * check descriptor |
| 211 | */ |
| 212 | |
| 213 | if ( !__validate_desc( descriptor )) |
| 214 | { |
| 215 | dm_log_write( __FILE__, |
| 216 | __LINE__, |
| 217 | LOG_INFO, |
| 218 | LOG_INFO, |
| 219 | "Error: SQL_INVALID_HANDLE" ); |
| 220 | |
| 221 | return SQL_INVALID_HANDLE; |
| 222 | } |
| 223 | |
| 224 | function_entry( descriptor ); |
| 225 | |
| 226 | if ( log_info.log_flag ) |
| 227 | { |
| 228 | sprintf( descriptor -> msg, "\n\t\tEntry:\ |
| 229 | \n\t\t\tDescriptor = %p\ |
| 230 | \n\t\t\tRec Number = %d\ |
| 231 | \n\t\t\tName = %p\ |
| 232 | \n\t\t\tBuffer length = %d\ |
| 233 | \n\t\t\tString Length = %p\ |
| 234 | \n\t\t\tType = %p\ |
| 235 | \n\t\t\tSub Type = %p\ |
| 236 | \n\t\t\tLength = %p\ |
| 237 | \n\t\t\tPrecision = %p\ |
| 238 | \n\t\t\tScale = %p\ |
| 239 | \n\t\t\tNullable = %p" , |
| 240 | descriptor, |
| 241 | rec_number, |
| 242 | name, |
| 243 | buffer_length, |
| 244 | string_length, |
| 245 | type, |
| 246 | sub_type, |
| 247 | length, |
| 248 | precision, |
| 249 | scale, |
| 250 | nullable ); |
| 251 | |
| 252 | dm_log_write( __FILE__, |
| 253 | __LINE__, |
| 254 | LOG_INFO, |
| 255 | LOG_INFO, |
| 256 | descriptor -> msg ); |
| 257 | } |
| 258 | |
| 259 | thread_protect( SQL_HANDLE_DESC, descriptor ); |
| 260 | |
| 261 | if ( descriptor -> connection -> state < STATE_C4 ) |
| 262 | { |
| 263 | dm_log_write( __FILE__, |
| 264 | __LINE__, |
| 265 | LOG_INFO, |
| 266 | LOG_INFO, |
| 267 | "Error: HY010" ); |
| 268 | |
| 269 | __post_internal_error( &descriptor -> error, |
| 270 | ERROR_HY010, NULL, |
| 271 | descriptor -> connection -> environment -> requested_version ); |
| 272 | |
| 273 | return function_return_nodrv( SQL_HANDLE_DESC, descriptor, SQL_ERROR ); |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * check status of statements associated with this descriptor |
| 278 | */ |
| 279 | |
| 280 | if( __check_stmt_from_desc( descriptor, STATE_S8 ) || |
| 281 | __check_stmt_from_desc( descriptor, STATE_S9 ) || |
| 282 | __check_stmt_from_desc( descriptor, STATE_S10 ) || |
| 283 | __check_stmt_from_desc( descriptor, STATE_S11 ) || |
| 284 | __check_stmt_from_desc( descriptor, STATE_S12 ) || |
| 285 | __check_stmt_from_desc( descriptor, STATE_S13 ) || |
| 286 | __check_stmt_from_desc( descriptor, STATE_S14 ) || |
| 287 | __check_stmt_from_desc( descriptor, STATE_S15 )) { |
| 288 | |
| 289 | dm_log_write( __FILE__, |
| 290 | __LINE__, |
| 291 | LOG_INFO, |
| 292 | LOG_INFO, |
| 293 | "Error: HY010" ); |
| 294 | |
| 295 | __post_internal_error( &descriptor -> error, |
| 296 | ERROR_HY010, NULL, |
| 297 | descriptor -> connection -> environment -> requested_version ); |
| 298 | |
| 299 | return function_return_nodrv( SQL_HANDLE_DESC, descriptor, SQL_ERROR ); |
| 300 | } |
| 301 | |
| 302 | if( __check_stmt_from_desc_ird( descriptor, STATE_S1 )) { |
| 303 | |
| 304 | dm_log_write( __FILE__, |
| 305 | __LINE__, |
| 306 | LOG_INFO, |
| 307 | LOG_INFO, |
| 308 | "Error: HY007" ); |
| 309 | |
| 310 | __post_internal_error( &descriptor -> error, |
| 311 | ERROR_HY007, NULL, |
| 312 | descriptor -> connection -> environment -> requested_version ); |
| 313 | |
| 314 | return function_return_nodrv( SQL_HANDLE_DESC, descriptor, SQL_ERROR ); |
| 315 | } |
| 316 | |
| 317 | if ( descriptor -> connection -> unicode_driver ) |
| 318 | { |
| 319 | SQLWCHAR *s1 = NULL; |
| 320 | |
| 321 | if ( !CHECK_SQLGETDESCRECW( descriptor -> connection )) |
| 322 | { |
| 323 | dm_log_write( __FILE__, |
| 324 | __LINE__, |
| 325 | LOG_INFO, |
| 326 | LOG_INFO, |
| 327 | "Error: IM001" ); |
| 328 | |
| 329 | __post_internal_error( &descriptor -> error, |
| 330 | ERROR_IM001, NULL, |
| 331 | descriptor -> connection -> environment -> requested_version ); |
| 332 | |
| 333 | return function_return_nodrv( SQL_HANDLE_DESC, descriptor, SQL_ERROR ); |
| 334 | } |
| 335 | |
| 336 | if ( name && buffer_length > 0 ) |
| 337 | { |
| 338 | s1 = malloc( sizeof( SQLWCHAR ) * ( buffer_length + 1 )); |
| 339 | } |
| 340 | |
| 341 | ret = SQLGETDESCRECW( descriptor -> connection, |
| 342 | descriptor -> driver_desc, |
| 343 | rec_number, |
| 344 | s1 ? s1 : (SQLWCHAR*) name, |
| 345 | buffer_length, |
| 346 | string_length, |
| 347 | type, |
| 348 | sub_type, |
| 349 | length, |
| 350 | precision, |
| 351 | scale, |
| 352 | nullable ); |
| 353 | |
| 354 | if ( SQL_SUCCEEDED( ret ) && name && s1 ) |
| 355 | { |
| 356 | unicode_to_ansi_copy((char*) name, buffer_length, s1, SQL_NTS, descriptor -> connection, NULL ); |
| 357 | } |
| 358 | if ( s1 ) |
| 359 | { |
| 360 | free( s1 ); |
| 361 | } |
| 362 | if ( SQL_SUCCEEDED( ret ) && string_length && name ) |
| 363 | { |
| 364 | *string_length = strlen((char*)name); |
| 365 | } |
| 366 | } |
| 367 | else |
| 368 | { |
| 369 | if ( !CHECK_SQLGETDESCREC( descriptor -> connection )) |
| 370 | { |
| 371 | dm_log_write( __FILE__, |
| 372 | __LINE__, |
| 373 | LOG_INFO, |
| 374 | LOG_INFO, |
| 375 | "Error: IM001" ); |
| 376 | |
| 377 | __post_internal_error( &descriptor -> error, |
| 378 | ERROR_IM001, NULL, |
| 379 | descriptor -> connection -> environment -> requested_version ); |
| 380 | |
| 381 | return function_return_nodrv( SQL_HANDLE_DESC, descriptor, SQL_ERROR ); |
| 382 | } |
| 383 | |
| 384 | ret = SQLGETDESCREC( descriptor -> connection, |
| 385 | descriptor -> driver_desc, |
| 386 | rec_number, |
| 387 | name, |
| 388 | buffer_length, |
| 389 | string_length, |
| 390 | type, |
| 391 | sub_type, |
| 392 | length, |
| 393 | precision, |
| 394 | scale, |
| 395 | nullable ); |
| 396 | } |
| 397 | |
| 398 | if ( log_info.log_flag ) |
| 399 | { |
| 400 | sprintf( descriptor -> msg, |
| 401 | "\n\t\tExit:[%s]\ |
| 402 | \n\t\t\tName = %s\ |
| 403 | \n\t\t\tType = %s\ |
| 404 | \n\t\t\tSub Type = %s\ |
| 405 | \n\t\t\tLength = %s\ |
| 406 | \n\t\t\tPrecision = %s\ |
| 407 | \n\t\t\tScale = %s\ |
| 408 | \n\t\t\tNullable = %s" , |
| 409 | __get_return_status( ret, s8 ), |
| 410 | __sdata_as_string( s1, SQL_CHAR, |
| 411 | string_length, name ), |
| 412 | __sptr_as_string( s2, type ), |
| 413 | __sptr_as_string( s3, sub_type ), |
| 414 | __ptr_as_string( s4, length ), |
| 415 | __sptr_as_string( s5, precision ), |
| 416 | __sptr_as_string( s6, scale ), |
| 417 | __sptr_as_string( s7, nullable )); |
| 418 | |
| 419 | dm_log_write( __FILE__, |
| 420 | __LINE__, |
| 421 | LOG_INFO, |
| 422 | LOG_INFO, |
| 423 | descriptor -> msg ); |
| 424 | } |
| 425 | |
| 426 | return function_return( SQL_HANDLE_DESC, descriptor, ret ); |
| 427 | } |
| 428 | |