| 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: SQLColumnsW.c,v 1.9 2009/02/18 17:59:08 lurcher Exp $ |
| 31 | * |
| 32 | * $Log: SQLColumnsW.c,v $ |
| 33 | * Revision 1.9 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.8 2008/08/29 08:01:38 lurcher |
| 37 | * Alter the way W functions are passed to the driver |
| 38 | * |
| 39 | * Revision 1.7 2007/02/28 15:37:47 lurcher |
| 40 | * deal with drivers that call internal W functions and end up in the driver manager. controlled by the --enable-handlemap configure arg |
| 41 | * |
| 42 | * Revision 1.6 2004/01/12 09:54:39 lurcher |
| 43 | * |
| 44 | * Fix problem where STATE_S5 stops metadata calls |
| 45 | * |
| 46 | * Revision 1.5 2003/10/30 18:20:45 lurcher |
| 47 | * |
| 48 | * Fix broken thread protection |
| 49 | * Remove SQLNumResultCols after execute, lease S4/S% to driver |
| 50 | * Fix string overrun in SQLDriverConnect |
| 51 | * Add initial support for Interix |
| 52 | * |
| 53 | * Revision 1.4 2002/12/05 17:44:30 lurcher |
| 54 | * |
| 55 | * Display unknown return values in return logging |
| 56 | * |
| 57 | * Revision 1.3 2002/08/23 09:42:37 lurcher |
| 58 | * |
| 59 | * Fix some build warnings with casts, and a AIX linker mod, to include |
| 60 | * deplib's on the link line, but not the libtool generated ones |
| 61 | * |
| 62 | * Revision 1.2 2002/07/24 08:49:51 lurcher |
| 63 | * |
| 64 | * Alter UNICODE support to use iconv for UNICODE-ANSI conversion |
| 65 | * |
| 66 | * Revision 1.1.1.1 2001/10/17 16:40:05 lurcher |
| 67 | * |
| 68 | * First upload to SourceForge |
| 69 | * |
| 70 | * Revision 1.3 2001/07/03 09:30:41 nick |
| 71 | * |
| 72 | * Add ability to alter size of displayed message in the log |
| 73 | * |
| 74 | * Revision 1.2 2001/04/12 17:43:35 nick |
| 75 | * |
| 76 | * Change logging and added autotest to odbctest |
| 77 | * |
| 78 | * Revision 1.1 2000/12/31 20:30:54 nick |
| 79 | * |
| 80 | * Add UNICODE support |
| 81 | * |
| 82 | * |
| 83 | **********************************************************************/ |
| 84 | |
| 85 | #include <config.h> |
| 86 | #include "drivermanager.h" |
| 87 | |
| 88 | static char const rcsid[]= "$RCSfile: SQLColumnsW.c,v $" ; |
| 89 | |
| 90 | SQLRETURN SQLColumnsW( SQLHSTMT statement_handle, |
| 91 | SQLWCHAR *catalog_name, |
| 92 | SQLSMALLINT name_length1, |
| 93 | SQLWCHAR *schema_name, |
| 94 | SQLSMALLINT name_length2, |
| 95 | SQLWCHAR *table_name, |
| 96 | SQLSMALLINT name_length3, |
| 97 | SQLWCHAR *column_name, |
| 98 | SQLSMALLINT name_length4 ) |
| 99 | { |
| 100 | DMHSTMT statement = (DMHSTMT) statement_handle; |
| 101 | SQLRETURN ret; |
| 102 | SQLCHAR s1[ 100 + LOG_MESSAGE_LEN ], s2[ 100 + LOG_MESSAGE_LEN ], s3[ 100 + LOG_MESSAGE_LEN ], s4[ 100 + LOG_MESSAGE_LEN ]; |
| 103 | |
| 104 | /* |
| 105 | * check statement |
| 106 | */ |
| 107 | if ( !__validate_stmt( statement )) |
| 108 | { |
| 109 | dm_log_write( __FILE__, |
| 110 | __LINE__, |
| 111 | LOG_INFO, |
| 112 | LOG_INFO, |
| 113 | "Error: SQL_INVALID_HANDLE" ); |
| 114 | |
| 115 | #ifdef WITH_HANDLE_REDIRECT |
| 116 | { |
| 117 | DMHSTMT parent_statement; |
| 118 | |
| 119 | parent_statement = find_parent_handle( statement, SQL_HANDLE_STMT ); |
| 120 | |
| 121 | if ( parent_statement ) { |
| 122 | dm_log_write( __FILE__, |
| 123 | __LINE__, |
| 124 | LOG_INFO, |
| 125 | LOG_INFO, |
| 126 | "Info: found parent handle" ); |
| 127 | |
| 128 | if ( CHECK_SQLCOLUMNSW( parent_statement -> connection )) |
| 129 | { |
| 130 | dm_log_write( __FILE__, |
| 131 | __LINE__, |
| 132 | LOG_INFO, |
| 133 | LOG_INFO, |
| 134 | "Info: calling redirected driver function" ); |
| 135 | |
| 136 | return SQLCOLUMNSW( parent_statement -> connection, |
| 137 | statement_handle, |
| 138 | catalog_name, |
| 139 | name_length1, |
| 140 | schema_name, |
| 141 | name_length2, |
| 142 | table_name, |
| 143 | name_length3, |
| 144 | column_name, |
| 145 | name_length4 ); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | #endif |
| 150 | return SQL_INVALID_HANDLE; |
| 151 | } |
| 152 | |
| 153 | function_entry( statement ); |
| 154 | |
| 155 | if ( log_info.log_flag ) |
| 156 | { |
| 157 | sprintf( statement -> msg, "\n\t\tEntry:\ |
| 158 | \n\t\t\tStatement = %p\ |
| 159 | \n\t\t\tCatalog Name = %s\ |
| 160 | \n\t\t\tSchema Name = %s\ |
| 161 | \n\t\t\tTable Name = %s\ |
| 162 | \n\t\t\tColumn Type = %s" , |
| 163 | statement, |
| 164 | __wstring_with_length( s1, catalog_name, name_length1 ), |
| 165 | __wstring_with_length( s2, schema_name, name_length2 ), |
| 166 | __wstring_with_length( s3, table_name, name_length3 ), |
| 167 | __wstring_with_length( s4, column_name, name_length4 )); |
| 168 | |
| 169 | dm_log_write( __FILE__, |
| 170 | __LINE__, |
| 171 | LOG_INFO, |
| 172 | LOG_INFO, |
| 173 | statement -> msg ); |
| 174 | } |
| 175 | |
| 176 | thread_protect( SQL_HANDLE_STMT, statement ); |
| 177 | |
| 178 | if (( catalog_name && name_length1 < 0 && name_length1 != SQL_NTS ) || |
| 179 | ( schema_name && name_length2 < 0 && name_length2 != SQL_NTS ) || |
| 180 | ( table_name && name_length3 < 0 && name_length3 != SQL_NTS ) || |
| 181 | ( column_name && name_length4 < 0 && name_length4 != SQL_NTS )) |
| 182 | { |
| 183 | __post_internal_error( &statement -> error, |
| 184 | ERROR_HY090, NULL, |
| 185 | statement -> connection -> environment -> requested_version ); |
| 186 | |
| 187 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * check states |
| 192 | */ |
| 193 | |
| 194 | #ifdef NR_PROBE |
| 195 | if ( statement -> state == STATE_S5 || |
| 196 | statement -> state == STATE_S6 || |
| 197 | statement -> state == STATE_S7 ) |
| 198 | #else |
| 199 | if (( statement -> state == STATE_S6 && statement -> eod == 0 ) || |
| 200 | statement -> state == STATE_S7 ) |
| 201 | #endif |
| 202 | { |
| 203 | dm_log_write( __FILE__, |
| 204 | __LINE__, |
| 205 | LOG_INFO, |
| 206 | LOG_INFO, |
| 207 | "Error: 24000" ); |
| 208 | |
| 209 | __post_internal_error( &statement -> error, |
| 210 | ERROR_24000, NULL, |
| 211 | statement -> connection -> environment -> requested_version ); |
| 212 | |
| 213 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
| 214 | } |
| 215 | else if ( statement -> state == STATE_S8 || |
| 216 | statement -> state == STATE_S9 || |
| 217 | statement -> state == STATE_S10 || |
| 218 | statement -> state == STATE_S13 || |
| 219 | statement -> state == STATE_S14 || |
| 220 | statement -> state == STATE_S15 ) |
| 221 | { |
| 222 | dm_log_write( __FILE__, |
| 223 | __LINE__, |
| 224 | LOG_INFO, |
| 225 | LOG_INFO, |
| 226 | "Error: HY010" ); |
| 227 | |
| 228 | __post_internal_error( &statement -> error, |
| 229 | ERROR_HY010, NULL, |
| 230 | statement -> connection -> environment -> requested_version ); |
| 231 | |
| 232 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
| 233 | } |
| 234 | |
| 235 | if ( statement -> state == STATE_S11 || |
| 236 | statement -> state == STATE_S12 ) |
| 237 | { |
| 238 | if ( statement -> interupted_func != SQL_API_SQLCOLUMNS ) |
| 239 | { |
| 240 | __post_internal_error( &statement -> error, |
| 241 | ERROR_HY010, NULL, |
| 242 | statement -> connection -> environment -> requested_version ); |
| 243 | |
| 244 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * TO_DO Check the SQL_ATTR_METADATA_ID settings |
| 250 | */ |
| 251 | |
| 252 | if ( statement -> connection -> unicode_driver || |
| 253 | CHECK_SQLCOLUMNSW( statement -> connection )) |
| 254 | { |
| 255 | if ( !CHECK_SQLCOLUMNSW( statement -> connection )) |
| 256 | { |
| 257 | dm_log_write( __FILE__, |
| 258 | __LINE__, |
| 259 | LOG_INFO, |
| 260 | LOG_INFO, |
| 261 | "Error: IM001" ); |
| 262 | |
| 263 | __post_internal_error( &statement -> error, |
| 264 | ERROR_IM001, NULL, |
| 265 | statement -> connection -> environment -> requested_version ); |
| 266 | |
| 267 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
| 268 | } |
| 269 | |
| 270 | ret = SQLCOLUMNSW( statement -> connection , |
| 271 | statement -> driver_stmt, |
| 272 | catalog_name, |
| 273 | name_length1, |
| 274 | schema_name, |
| 275 | name_length2, |
| 276 | table_name, |
| 277 | name_length3, |
| 278 | column_name, |
| 279 | name_length4 ); |
| 280 | } |
| 281 | else |
| 282 | { |
| 283 | SQLCHAR *as1, *as2, *as3, *as4; |
| 284 | int clen; |
| 285 | |
| 286 | if ( !CHECK_SQLCOLUMNS( statement -> connection )) |
| 287 | { |
| 288 | dm_log_write( __FILE__, |
| 289 | __LINE__, |
| 290 | LOG_INFO, |
| 291 | LOG_INFO, |
| 292 | "Error: IM001" ); |
| 293 | |
| 294 | __post_internal_error( &statement -> error, |
| 295 | ERROR_IM001, NULL, |
| 296 | statement -> connection -> environment -> requested_version ); |
| 297 | |
| 298 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
| 299 | } |
| 300 | |
| 301 | as1 = (SQLCHAR*) unicode_to_ansi_alloc( catalog_name, name_length1, statement -> connection, &clen ); |
| 302 | name_length1 = clen; |
| 303 | as2 = (SQLCHAR*) unicode_to_ansi_alloc( schema_name, name_length2, statement -> connection, &clen ); |
| 304 | name_length2 = clen; |
| 305 | as3 = (SQLCHAR*) unicode_to_ansi_alloc( table_name, name_length3, statement -> connection, &clen ); |
| 306 | name_length3 = clen; |
| 307 | as4 = (SQLCHAR*) unicode_to_ansi_alloc( column_name, name_length4, statement -> connection, &clen ); |
| 308 | name_length4 = clen; |
| 309 | |
| 310 | ret = SQLCOLUMNS( statement -> connection , |
| 311 | statement -> driver_stmt, |
| 312 | as1, |
| 313 | name_length1, |
| 314 | as2, |
| 315 | name_length2, |
| 316 | as3, |
| 317 | name_length3, |
| 318 | as4, |
| 319 | name_length4 ); |
| 320 | |
| 321 | if ( as1 ) free( as1 ); |
| 322 | if ( as2 ) free( as2 ); |
| 323 | if ( as3 ) free( as3 ); |
| 324 | if ( as4 ) free( as4 ); |
| 325 | } |
| 326 | |
| 327 | if ( SQL_SUCCEEDED( ret )) |
| 328 | { |
| 329 | #ifdef NR_PROBE |
| 330 | /******** |
| 331 | * Added this to get num cols from drivers which can only tell |
| 332 | * us after execute - PAH |
| 333 | */ |
| 334 | |
| 335 | /* |
| 336 | * grab any errors |
| 337 | */ |
| 338 | |
| 339 | if ( ret == SQL_SUCCESS_WITH_INFO ) |
| 340 | { |
| 341 | function_return_ex( IGNORE_THREAD, statement, ret, TRUE ); |
| 342 | } |
| 343 | |
| 344 | SQLNUMRESULTCOLS( statement -> connection, |
| 345 | statement -> driver_stmt, &statement -> numcols ); |
| 346 | /******/ |
| 347 | #endif |
| 348 | statement -> hascols = 1; |
| 349 | statement -> state = STATE_S5; |
| 350 | statement -> prepared = 0; |
| 351 | } |
| 352 | else if ( ret == SQL_STILL_EXECUTING ) |
| 353 | { |
| 354 | statement -> interupted_func = SQL_API_SQLCOLUMNS; |
| 355 | if ( statement -> state != STATE_S11 && |
| 356 | statement -> state != STATE_S12 ) |
| 357 | statement -> state = STATE_S11; |
| 358 | } |
| 359 | else |
| 360 | { |
| 361 | statement -> state = STATE_S1; |
| 362 | } |
| 363 | |
| 364 | if ( log_info.log_flag ) |
| 365 | { |
| 366 | sprintf( statement -> msg, |
| 367 | "\n\t\tExit:[%s]" , |
| 368 | __get_return_status( ret, s1 )); |
| 369 | |
| 370 | dm_log_write( __FILE__, |
| 371 | __LINE__, |
| 372 | LOG_INFO, |
| 373 | LOG_INFO, |
| 374 | statement -> msg ); |
| 375 | } |
| 376 | |
| 377 | return function_return( SQL_HANDLE_STMT, statement, ret ); |
| 378 | } |
| 379 | |