| 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: SQLTablesW.c,v 1.9 2009/02/18 17:59:08 lurcher Exp $ | 
|---|
| 31 | * | 
|---|
| 32 | * $Log: SQLTablesW.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:39  lurcher | 
|---|
| 37 | * Alter the way W functions are passed to the driver | 
|---|
| 38 | * | 
|---|
| 39 | * Revision 1.7  2007/02/28 15:37:49  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:46  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:31  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:52  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:07  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:36  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: SQLTablesW.c,v $"; | 
|---|
| 89 |  | 
|---|
| 90 | SQLRETURN SQLTablesW( 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 *table_type, | 
|---|
| 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_SQLTABLESW( 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  SQLTABLESW( 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 | table_type, | 
|---|
| 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\tTable 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, table_type, 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 | /* | 
|---|
| 179 | * this is a fix for old version of EXCEL | 
|---|
| 180 | */ | 
|---|
| 181 |  | 
|---|
| 182 | if ( !catalog_name ) | 
|---|
| 183 | name_length1 = 0; | 
|---|
| 184 | if ( !schema_name ) | 
|---|
| 185 | name_length2 = 0; | 
|---|
| 186 | if ( !table_name ) | 
|---|
| 187 | name_length3 = 0; | 
|---|
| 188 | if ( !table_type ) | 
|---|
| 189 | name_length4 = 0; | 
|---|
| 190 |  | 
|---|
| 191 | if (( name_length1 < 0 && name_length1 != SQL_NTS ) || | 
|---|
| 192 | ( name_length2 < 0 && name_length2 != SQL_NTS ) || | 
|---|
| 193 | ( name_length3 < 0 && name_length3 != SQL_NTS ) || | 
|---|
| 194 | ( name_length4 < 0 && name_length4 != SQL_NTS )) | 
|---|
| 195 | { | 
|---|
| 196 | dm_log_write( __FILE__, | 
|---|
| 197 | __LINE__, | 
|---|
| 198 | LOG_INFO, | 
|---|
| 199 | LOG_INFO, | 
|---|
| 200 | "Error: HY090"); | 
|---|
| 201 |  | 
|---|
| 202 | __post_internal_error( &statement -> error, | 
|---|
| 203 | ERROR_HY090, NULL, | 
|---|
| 204 | statement -> connection -> environment -> requested_version ); | 
|---|
| 205 |  | 
|---|
| 206 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 209 | /* | 
|---|
| 210 | * check states | 
|---|
| 211 | */ | 
|---|
| 212 |  | 
|---|
| 213 | #ifdef NR_PROBE | 
|---|
| 214 | if ( statement -> state == STATE_S5 || | 
|---|
| 215 | statement -> state == STATE_S6 || | 
|---|
| 216 | statement -> state == STATE_S7 ) | 
|---|
| 217 | #else | 
|---|
| 218 | if (( statement -> state == STATE_S6 && statement -> eod == 0 ) || | 
|---|
| 219 | statement -> state == STATE_S7 ) | 
|---|
| 220 | #endif | 
|---|
| 221 | { | 
|---|
| 222 | dm_log_write( __FILE__, | 
|---|
| 223 | __LINE__, | 
|---|
| 224 | LOG_INFO, | 
|---|
| 225 | LOG_INFO, | 
|---|
| 226 | "Error: 24000"); | 
|---|
| 227 |  | 
|---|
| 228 | __post_internal_error( &statement -> error, | 
|---|
| 229 | ERROR_24000, NULL, | 
|---|
| 230 | statement -> connection -> environment -> requested_version ); | 
|---|
| 231 |  | 
|---|
| 232 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); | 
|---|
| 233 | } | 
|---|
| 234 | else if ( statement -> state == STATE_S8 || | 
|---|
| 235 | statement -> state == STATE_S9 || | 
|---|
| 236 | statement -> state == STATE_S10 || | 
|---|
| 237 | statement -> state == STATE_S13 || | 
|---|
| 238 | statement -> state == STATE_S14 || | 
|---|
| 239 | statement -> state == STATE_S15 ) | 
|---|
| 240 | { | 
|---|
| 241 | dm_log_write( __FILE__, | 
|---|
| 242 | __LINE__, | 
|---|
| 243 | LOG_INFO, | 
|---|
| 244 | LOG_INFO, | 
|---|
| 245 | "Error: HY010"); | 
|---|
| 246 |  | 
|---|
| 247 | __post_internal_error( &statement -> error, | 
|---|
| 248 | ERROR_HY010, NULL, | 
|---|
| 249 | statement -> connection -> environment -> requested_version ); | 
|---|
| 250 |  | 
|---|
| 251 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); | 
|---|
| 252 | } | 
|---|
| 253 |  | 
|---|
| 254 | if ( statement -> state == STATE_S11 || | 
|---|
| 255 | statement -> state == STATE_S12 ) | 
|---|
| 256 | { | 
|---|
| 257 | if ( statement -> interupted_func != SQL_API_SQLTABLES ) | 
|---|
| 258 | { | 
|---|
| 259 | dm_log_write( __FILE__, | 
|---|
| 260 | __LINE__, | 
|---|
| 261 | LOG_INFO, | 
|---|
| 262 | LOG_INFO, | 
|---|
| 263 | "Error: HY010"); | 
|---|
| 264 |  | 
|---|
| 265 | __post_internal_error( &statement -> error, | 
|---|
| 266 | ERROR_HY010, NULL, | 
|---|
| 267 | statement -> connection -> environment -> requested_version ); | 
|---|
| 268 |  | 
|---|
| 269 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); | 
|---|
| 270 | } | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | /* | 
|---|
| 274 | * TO_DO Check the SQL_ATTR_METADATA_ID settings | 
|---|
| 275 | */ | 
|---|
| 276 |  | 
|---|
| 277 | if ( statement -> connection -> unicode_driver || | 
|---|
| 278 | CHECK_SQLTABLESW( statement -> connection )) | 
|---|
| 279 | { | 
|---|
| 280 | if ( !CHECK_SQLTABLESW( statement -> connection )) | 
|---|
| 281 | { | 
|---|
| 282 | dm_log_write( __FILE__, | 
|---|
| 283 | __LINE__, | 
|---|
| 284 | LOG_INFO, | 
|---|
| 285 | LOG_INFO, | 
|---|
| 286 | "Error: IM001"); | 
|---|
| 287 |  | 
|---|
| 288 | __post_internal_error( &statement -> error, | 
|---|
| 289 | ERROR_IM001, NULL, | 
|---|
| 290 | statement -> connection -> environment -> requested_version ); | 
|---|
| 291 |  | 
|---|
| 292 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); | 
|---|
| 293 | } | 
|---|
| 294 |  | 
|---|
| 295 | ret = SQLTABLESW( statement -> connection , | 
|---|
| 296 | statement -> driver_stmt, | 
|---|
| 297 | catalog_name, | 
|---|
| 298 | name_length1, | 
|---|
| 299 | schema_name, | 
|---|
| 300 | name_length2, | 
|---|
| 301 | table_name, | 
|---|
| 302 | name_length3, | 
|---|
| 303 | table_type, | 
|---|
| 304 | name_length4 ); | 
|---|
| 305 | } | 
|---|
| 306 | else | 
|---|
| 307 | { | 
|---|
| 308 | SQLCHAR *as1, *as2, *as3, *as4; | 
|---|
| 309 | int clen; | 
|---|
| 310 |  | 
|---|
| 311 | if ( !CHECK_SQLTABLES( statement -> connection )) | 
|---|
| 312 | { | 
|---|
| 313 | dm_log_write( __FILE__, | 
|---|
| 314 | __LINE__, | 
|---|
| 315 | LOG_INFO, | 
|---|
| 316 | LOG_INFO, | 
|---|
| 317 | "Error: IM001"); | 
|---|
| 318 |  | 
|---|
| 319 | __post_internal_error( &statement -> error, | 
|---|
| 320 | ERROR_IM001, NULL, | 
|---|
| 321 | statement -> connection -> environment -> requested_version ); | 
|---|
| 322 |  | 
|---|
| 323 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); | 
|---|
| 324 | } | 
|---|
| 325 |  | 
|---|
| 326 | as1 = (SQLCHAR*) unicode_to_ansi_alloc( catalog_name, name_length1, statement -> connection, &clen ); | 
|---|
| 327 | name_length1 = clen; | 
|---|
| 328 | as2 = (SQLCHAR*) unicode_to_ansi_alloc( schema_name, name_length2, statement -> connection, &clen ); | 
|---|
| 329 | name_length2 = clen; | 
|---|
| 330 | as3 = (SQLCHAR*) unicode_to_ansi_alloc( table_name, name_length3, statement -> connection, &clen ); | 
|---|
| 331 | name_length3 = clen; | 
|---|
| 332 | as4 = (SQLCHAR*) unicode_to_ansi_alloc( table_type, name_length4, statement -> connection, &clen ); | 
|---|
| 333 | name_length4 = clen; | 
|---|
| 334 |  | 
|---|
| 335 | ret = SQLTABLES( statement -> connection , | 
|---|
| 336 | statement -> driver_stmt, | 
|---|
| 337 | as1, | 
|---|
| 338 | name_length1, | 
|---|
| 339 | as2, | 
|---|
| 340 | name_length2, | 
|---|
| 341 | as3, | 
|---|
| 342 | name_length3, | 
|---|
| 343 | as4, | 
|---|
| 344 | name_length4 ); | 
|---|
| 345 |  | 
|---|
| 346 | if ( as1 ) free( as1 ); | 
|---|
| 347 | if ( as2 ) free( as2 ); | 
|---|
| 348 | if ( as3 ) free( as3 ); | 
|---|
| 349 | if ( as4 ) free( as4 ); | 
|---|
| 350 | } | 
|---|
| 351 |  | 
|---|
| 352 | if ( SQL_SUCCEEDED( ret )) | 
|---|
| 353 | { | 
|---|
| 354 | #ifdef NR_PROBE | 
|---|
| 355 | /******** | 
|---|
| 356 | * Added this to get num cols from drivers which can only tell | 
|---|
| 357 | * us after execute - PAH | 
|---|
| 358 | */ | 
|---|
| 359 | /* | 
|---|
| 360 | * There is no point in doing this as we can't trust the value | 
|---|
| 361 | * from SQLPrepare, so we can't perform checks on the column number | 
|---|
| 362 | * | 
|---|
| 363 | ret = SQLNUMRESULTCOLS( statement -> connection, | 
|---|
| 364 | statement -> driver_stmt, &statement -> numcols ); | 
|---|
| 365 | */ | 
|---|
| 366 | statement -> numcols = 1; | 
|---|
| 367 | /******/ | 
|---|
| 368 | #endif | 
|---|
| 369 | statement -> hascols = 1; | 
|---|
| 370 | statement -> state = STATE_S5; | 
|---|
| 371 | statement -> prepared = 0; | 
|---|
| 372 | } | 
|---|
| 373 | else if ( ret == SQL_STILL_EXECUTING ) | 
|---|
| 374 | { | 
|---|
| 375 | statement -> interupted_func = SQL_API_SQLTABLES; | 
|---|
| 376 | if ( statement -> state != STATE_S11 && | 
|---|
| 377 | statement -> state != STATE_S12 ) | 
|---|
| 378 | statement -> state = STATE_S11; | 
|---|
| 379 | } | 
|---|
| 380 | else | 
|---|
| 381 | { | 
|---|
| 382 | statement -> state = STATE_S1; | 
|---|
| 383 | } | 
|---|
| 384 |  | 
|---|
| 385 | if ( log_info.log_flag ) | 
|---|
| 386 | { | 
|---|
| 387 | sprintf( statement -> msg, | 
|---|
| 388 | "\n\t\tExit:[%s]", | 
|---|
| 389 | __get_return_status( ret, s1 )); | 
|---|
| 390 |  | 
|---|
| 391 | dm_log_write( __FILE__, | 
|---|
| 392 | __LINE__, | 
|---|
| 393 | LOG_INFO, | 
|---|
| 394 | LOG_INFO, | 
|---|
| 395 | statement -> msg ); | 
|---|
| 396 | } | 
|---|
| 397 |  | 
|---|
| 398 | return function_return( SQL_HANDLE_STMT, statement, ret ); | 
|---|
| 399 | } | 
|---|
| 400 |  | 
|---|