| 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: SQLColumnPrivilegesW.c,v 1.9 2009/02/18 17:59:08 lurcher Exp $ | 
|---|
| 31 | * | 
|---|
| 32 | * $Log: SQLColumnPrivilegesW.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: SQLColumnPrivilegesW.c,v $"; | 
|---|
| 89 |  | 
|---|
| 90 | SQLRETURN SQLColumnPrivilegesW( | 
|---|
| 91 | SQLHSTMT            statement_handle, | 
|---|
| 92 | SQLWCHAR             *catalog_name, | 
|---|
| 93 | SQLSMALLINT         name_length1, | 
|---|
| 94 | SQLWCHAR             *schema_name, | 
|---|
| 95 | SQLSMALLINT         name_length2, | 
|---|
| 96 | SQLWCHAR             *table_name, | 
|---|
| 97 | SQLSMALLINT         name_length3, | 
|---|
| 98 | SQLWCHAR             *column_name, | 
|---|
| 99 | SQLSMALLINT         name_length4 ) | 
|---|
| 100 | { | 
|---|
| 101 | DMHSTMT statement = (DMHSTMT) statement_handle; | 
|---|
| 102 | SQLRETURN ret; | 
|---|
| 103 | SQLCHAR s1[ 100 + LOG_MESSAGE_LEN ], s2[ 100 + LOG_MESSAGE_LEN ], s3[ 100 + LOG_MESSAGE_LEN ], s4[ 100 + LOG_MESSAGE_LEN ]; | 
|---|
| 104 |  | 
|---|
| 105 | /* | 
|---|
| 106 | * check statement | 
|---|
| 107 | */ | 
|---|
| 108 |  | 
|---|
| 109 | if ( !__validate_stmt( statement )) | 
|---|
| 110 | { | 
|---|
| 111 | dm_log_write( __FILE__, | 
|---|
| 112 | __LINE__, | 
|---|
| 113 | LOG_INFO, | 
|---|
| 114 | LOG_INFO, | 
|---|
| 115 | "Error: SQL_INVALID_HANDLE"); | 
|---|
| 116 |  | 
|---|
| 117 | #ifdef WITH_HANDLE_REDIRECT | 
|---|
| 118 | { | 
|---|
| 119 | DMHSTMT parent_statement; | 
|---|
| 120 |  | 
|---|
| 121 | parent_statement = find_parent_handle( statement, SQL_HANDLE_STMT ); | 
|---|
| 122 |  | 
|---|
| 123 | if ( parent_statement ) { | 
|---|
| 124 | dm_log_write( __FILE__, | 
|---|
| 125 | __LINE__, | 
|---|
| 126 | LOG_INFO, | 
|---|
| 127 | LOG_INFO, | 
|---|
| 128 | "Info: found parent handle"); | 
|---|
| 129 |  | 
|---|
| 130 | if ( CHECK_SQLCOLUMNPRIVILEGESW( parent_statement -> connection )) | 
|---|
| 131 | { | 
|---|
| 132 | dm_log_write( __FILE__, | 
|---|
| 133 | __LINE__, | 
|---|
| 134 | LOG_INFO, | 
|---|
| 135 | LOG_INFO, | 
|---|
| 136 | "Info: calling redirected driver function"); | 
|---|
| 137 |  | 
|---|
| 138 | return  SQLCOLUMNPRIVILEGESW( parent_statement -> connection, | 
|---|
| 139 | statement_handle, | 
|---|
| 140 | catalog_name, | 
|---|
| 141 | name_length1, | 
|---|
| 142 | schema_name, | 
|---|
| 143 | name_length2, | 
|---|
| 144 | table_name, | 
|---|
| 145 | name_length3, | 
|---|
| 146 | column_name, | 
|---|
| 147 | name_length4 ); | 
|---|
| 148 | } | 
|---|
| 149 | } | 
|---|
| 150 | } | 
|---|
| 151 | #endif | 
|---|
| 152 | return SQL_INVALID_HANDLE; | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | function_entry( statement ); | 
|---|
| 156 |  | 
|---|
| 157 | if ( log_info.log_flag ) | 
|---|
| 158 | { | 
|---|
| 159 | sprintf( statement -> msg, "\n\t\tEntry:\ | 
|---|
| 160 | \n\t\t\tStatement = %p\ | 
|---|
| 161 | \n\t\t\tCatalog Name = %s\ | 
|---|
| 162 | \n\t\t\tSchema Name = %s\ | 
|---|
| 163 | \n\t\t\tTable Name = %s\ | 
|---|
| 164 | \n\t\t\tColumn Name = %s", | 
|---|
| 165 | statement, | 
|---|
| 166 | __wstring_with_length( s1, catalog_name, name_length1 ), | 
|---|
| 167 | __wstring_with_length( s2, schema_name, name_length2 ), | 
|---|
| 168 | __wstring_with_length( s3, table_name, name_length3 ), | 
|---|
| 169 | __wstring_with_length( s4, column_name, name_length4 )); | 
|---|
| 170 |  | 
|---|
| 171 | dm_log_write( __FILE__, | 
|---|
| 172 | __LINE__, | 
|---|
| 173 | LOG_INFO, | 
|---|
| 174 | LOG_INFO, | 
|---|
| 175 | statement -> msg ); | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | thread_protect( SQL_HANDLE_STMT, statement ); | 
|---|
| 179 |  | 
|---|
| 180 | if ( table_name == NULL ) | 
|---|
| 181 | { | 
|---|
| 182 | dm_log_write( __FILE__, | 
|---|
| 183 | __LINE__, | 
|---|
| 184 | LOG_INFO, | 
|---|
| 185 | LOG_INFO, | 
|---|
| 186 | "Error: HY009"); | 
|---|
| 187 |  | 
|---|
| 188 | __post_internal_error( &statement -> error, | 
|---|
| 189 | ERROR_HY009, NULL, | 
|---|
| 190 | statement -> connection -> environment -> requested_version ); | 
|---|
| 191 |  | 
|---|
| 192 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); | 
|---|
| 193 | } | 
|---|
| 194 |  | 
|---|
| 195 | if (( name_length1 < 0 && name_length1 != SQL_NTS ) || | 
|---|
| 196 | ( name_length2 < 0 && name_length2 != SQL_NTS ) || | 
|---|
| 197 | ( name_length3 < 0 && name_length3 != SQL_NTS ) || | 
|---|
| 198 | ( name_length4 < 0 && name_length4 != SQL_NTS )) | 
|---|
| 199 | { | 
|---|
| 200 | dm_log_write( __FILE__, | 
|---|
| 201 | __LINE__, | 
|---|
| 202 | LOG_INFO, | 
|---|
| 203 | LOG_INFO, | 
|---|
| 204 | "Error: HY090"); | 
|---|
| 205 |  | 
|---|
| 206 | __post_internal_error( &statement -> error, | 
|---|
| 207 | ERROR_HY090, NULL, | 
|---|
| 208 | statement -> connection -> environment -> requested_version ); | 
|---|
| 209 |  | 
|---|
| 210 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 | /* | 
|---|
| 214 | * check states | 
|---|
| 215 | */ | 
|---|
| 216 |  | 
|---|
| 217 | #ifdef NR_PROBE | 
|---|
| 218 | if ( statement -> state == STATE_S5 || | 
|---|
| 219 | statement -> state == STATE_S6 || | 
|---|
| 220 | statement -> state == STATE_S7 ) | 
|---|
| 221 | #else | 
|---|
| 222 | if (( statement -> state == STATE_S6 && statement -> eod == 0 ) || | 
|---|
| 223 | statement -> state == STATE_S7 ) | 
|---|
| 224 | #endif | 
|---|
| 225 | { | 
|---|
| 226 | dm_log_write( __FILE__, | 
|---|
| 227 | __LINE__, | 
|---|
| 228 | LOG_INFO, | 
|---|
| 229 | LOG_INFO, | 
|---|
| 230 | "Error: 24000"); | 
|---|
| 231 |  | 
|---|
| 232 | __post_internal_error( &statement -> error, | 
|---|
| 233 | ERROR_24000, NULL, | 
|---|
| 234 | statement -> connection -> environment -> requested_version ); | 
|---|
| 235 |  | 
|---|
| 236 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); | 
|---|
| 237 | } | 
|---|
| 238 | else if ( statement -> state == STATE_S8 || | 
|---|
| 239 | statement -> state == STATE_S9 || | 
|---|
| 240 | statement -> state == STATE_S10 || | 
|---|
| 241 | statement -> state == STATE_S13 || | 
|---|
| 242 | statement -> state == STATE_S14 || | 
|---|
| 243 | statement -> state == STATE_S15 ) | 
|---|
| 244 | { | 
|---|
| 245 | dm_log_write( __FILE__, | 
|---|
| 246 | __LINE__, | 
|---|
| 247 | LOG_INFO, | 
|---|
| 248 | LOG_INFO, | 
|---|
| 249 | "Error: HY010"); | 
|---|
| 250 |  | 
|---|
| 251 | __post_internal_error( &statement -> error, | 
|---|
| 252 | ERROR_HY010, NULL, | 
|---|
| 253 | statement -> connection -> environment -> requested_version ); | 
|---|
| 254 |  | 
|---|
| 255 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); | 
|---|
| 256 | } | 
|---|
| 257 |  | 
|---|
| 258 | if ( statement -> state == STATE_S11 || | 
|---|
| 259 | statement -> state == STATE_S12 ) | 
|---|
| 260 | { | 
|---|
| 261 | if ( statement -> interupted_func != SQL_API_SQLCOLUMNPRIVILEGES ) | 
|---|
| 262 | { | 
|---|
| 263 | dm_log_write( __FILE__, | 
|---|
| 264 | __LINE__, | 
|---|
| 265 | LOG_INFO, | 
|---|
| 266 | LOG_INFO, | 
|---|
| 267 | "Error: HY010"); | 
|---|
| 268 |  | 
|---|
| 269 | __post_internal_error( &statement -> error, | 
|---|
| 270 | ERROR_HY010, NULL, | 
|---|
| 271 | statement -> connection -> environment -> requested_version ); | 
|---|
| 272 |  | 
|---|
| 273 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); | 
|---|
| 274 | } | 
|---|
| 275 | } | 
|---|
| 276 |  | 
|---|
| 277 | /* | 
|---|
| 278 | * TO_DO Check the SQL_ATTR_METADATA_ID settings | 
|---|
| 279 | */ | 
|---|
| 280 |  | 
|---|
| 281 | if ( statement -> connection -> unicode_driver || | 
|---|
| 282 | CHECK_SQLCOLUMNPRIVILEGESW( statement -> connection )) | 
|---|
| 283 | { | 
|---|
| 284 | if ( !CHECK_SQLCOLUMNPRIVILEGESW( statement -> connection )) | 
|---|
| 285 | { | 
|---|
| 286 | dm_log_write( __FILE__, | 
|---|
| 287 | __LINE__, | 
|---|
| 288 | LOG_INFO, | 
|---|
| 289 | LOG_INFO, | 
|---|
| 290 | "Error: IM001"); | 
|---|
| 291 |  | 
|---|
| 292 | __post_internal_error( &statement -> error, | 
|---|
| 293 | ERROR_IM001, NULL, | 
|---|
| 294 | statement -> connection -> environment -> requested_version ); | 
|---|
| 295 |  | 
|---|
| 296 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); | 
|---|
| 297 | } | 
|---|
| 298 |  | 
|---|
| 299 | ret = SQLCOLUMNPRIVILEGESW( statement -> connection , | 
|---|
| 300 | statement -> driver_stmt, | 
|---|
| 301 | catalog_name, | 
|---|
| 302 | name_length1, | 
|---|
| 303 | schema_name, | 
|---|
| 304 | name_length2, | 
|---|
| 305 | table_name, | 
|---|
| 306 | name_length3, | 
|---|
| 307 | column_name, | 
|---|
| 308 | name_length4 ); | 
|---|
| 309 | } | 
|---|
| 310 | else | 
|---|
| 311 | { | 
|---|
| 312 | SQLCHAR *as1, *as2, *as3, *as4; | 
|---|
| 313 | int clen; | 
|---|
| 314 |  | 
|---|
| 315 | if ( !CHECK_SQLCOLUMNPRIVILEGES( statement -> connection )) | 
|---|
| 316 | { | 
|---|
| 317 | dm_log_write( __FILE__, | 
|---|
| 318 | __LINE__, | 
|---|
| 319 | LOG_INFO, | 
|---|
| 320 | LOG_INFO, | 
|---|
| 321 | "Error: IM001"); | 
|---|
| 322 |  | 
|---|
| 323 | __post_internal_error( &statement -> error, | 
|---|
| 324 | ERROR_IM001, NULL, | 
|---|
| 325 | statement -> connection -> environment -> requested_version ); | 
|---|
| 326 |  | 
|---|
| 327 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); | 
|---|
| 328 | } | 
|---|
| 329 |  | 
|---|
| 330 | /* | 
|---|
| 331 | * The MS book says do this in place, but that causes more problems | 
|---|
| 332 | * than we need | 
|---|
| 333 | */ | 
|---|
| 334 |  | 
|---|
| 335 | as1 = (SQLCHAR*) unicode_to_ansi_alloc( catalog_name, name_length1, statement -> connection, &clen ); | 
|---|
| 336 | name_length1 = clen; | 
|---|
| 337 | as2 = (SQLCHAR*) unicode_to_ansi_alloc( schema_name, name_length2, statement -> connection, &clen ); | 
|---|
| 338 | name_length2 = clen; | 
|---|
| 339 | as3 = (SQLCHAR*) unicode_to_ansi_alloc( table_name, name_length3, statement -> connection, &clen ); | 
|---|
| 340 | name_length3 = clen; | 
|---|
| 341 | as4 = (SQLCHAR*) unicode_to_ansi_alloc( column_name, name_length4, statement -> connection, &clen ); | 
|---|
| 342 | name_length4 = clen; | 
|---|
| 343 |  | 
|---|
| 344 | ret = SQLCOLUMNPRIVILEGES( statement -> connection , | 
|---|
| 345 | statement -> driver_stmt, | 
|---|
| 346 | as1, | 
|---|
| 347 | name_length1, | 
|---|
| 348 | as2, | 
|---|
| 349 | name_length2, | 
|---|
| 350 | as3, | 
|---|
| 351 | name_length3, | 
|---|
| 352 | as4, | 
|---|
| 353 | name_length4 ); | 
|---|
| 354 |  | 
|---|
| 355 | if ( as1 ) free( as1 ); | 
|---|
| 356 | if ( as2 ) free( as2 ); | 
|---|
| 357 | if ( as3 ) free( as3 ); | 
|---|
| 358 | if ( as4 ) free( as4 ); | 
|---|
| 359 | } | 
|---|
| 360 |  | 
|---|
| 361 | if ( SQL_SUCCEEDED( ret )) | 
|---|
| 362 | { | 
|---|
| 363 | statement -> state = STATE_S5; | 
|---|
| 364 | statement -> prepared = 0; | 
|---|
| 365 | } | 
|---|
| 366 | else if ( ret == SQL_STILL_EXECUTING ) | 
|---|
| 367 | { | 
|---|
| 368 | statement -> interupted_func = SQL_API_SQLCOLUMNPRIVILEGES; | 
|---|
| 369 | if ( statement -> state != STATE_S11 && | 
|---|
| 370 | statement -> state != STATE_S12 ) | 
|---|
| 371 | statement -> state = STATE_S11; | 
|---|
| 372 | } | 
|---|
| 373 | else | 
|---|
| 374 | { | 
|---|
| 375 | statement -> state = STATE_S1; | 
|---|
| 376 | } | 
|---|
| 377 |  | 
|---|
| 378 | if ( log_info.log_flag ) | 
|---|
| 379 | { | 
|---|
| 380 | sprintf( statement -> msg, | 
|---|
| 381 | "\n\t\tExit:[%s]", | 
|---|
| 382 | __get_return_status( ret, s1 )); | 
|---|
| 383 |  | 
|---|
| 384 | dm_log_write( __FILE__, | 
|---|
| 385 | __LINE__, | 
|---|
| 386 | LOG_INFO, | 
|---|
| 387 | LOG_INFO, | 
|---|
| 388 | statement -> msg ); | 
|---|
| 389 | } | 
|---|
| 390 |  | 
|---|
| 391 | return function_return( SQL_HANDLE_STMT, statement, ret ); | 
|---|
| 392 | } | 
|---|
| 393 |  | 
|---|