| 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: SQLColumnPrivileges.c,v 1.8 2009/02/18 17:59:08 lurcher Exp $ | 
|---|
| 31 | * | 
|---|
| 32 | * $Log: SQLColumnPrivileges.c,v $ | 
|---|
| 33 | * Revision 1.8  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.7  2005/11/21 17:25:43  lurcher | 
|---|
| 37 | * A few DM fixes for Oracle's ODBC driver | 
|---|
| 38 | * | 
|---|
| 39 | * Revision 1.6  2004/01/12 09:54:39  lurcher | 
|---|
| 40 | * | 
|---|
| 41 | * Fix problem where STATE_S5 stops metadata calls | 
|---|
| 42 | * | 
|---|
| 43 | * Revision 1.5  2003/10/30 18:20:45  lurcher | 
|---|
| 44 | * | 
|---|
| 45 | * Fix broken thread protection | 
|---|
| 46 | * Remove SQLNumResultCols after execute, lease S4/S% to driver | 
|---|
| 47 | * Fix string overrun in SQLDriverConnect | 
|---|
| 48 | * Add initial support for Interix | 
|---|
| 49 | * | 
|---|
| 50 | * Revision 1.4  2003/02/27 12:19:39  lurcher | 
|---|
| 51 | * | 
|---|
| 52 | * Add the A functions as well as the W | 
|---|
| 53 | * | 
|---|
| 54 | * Revision 1.3  2002/12/05 17:44:30  lurcher | 
|---|
| 55 | * | 
|---|
| 56 | * Display unknown return values in return logging | 
|---|
| 57 | * | 
|---|
| 58 | * Revision 1.2  2002/07/24 08:49:51  lurcher | 
|---|
| 59 | * | 
|---|
| 60 | * Alter UNICODE support to use iconv for UNICODE-ANSI conversion | 
|---|
| 61 | * | 
|---|
| 62 | * Revision 1.1.1.1  2001/10/17 16:40:05  lurcher | 
|---|
| 63 | * | 
|---|
| 64 | * First upload to SourceForge | 
|---|
| 65 | * | 
|---|
| 66 | * Revision 1.4  2001/07/03 09:30:41  nick | 
|---|
| 67 | * | 
|---|
| 68 | * Add ability to alter size of displayed message in the log | 
|---|
| 69 | * | 
|---|
| 70 | * Revision 1.3  2001/04/12 17:43:35  nick | 
|---|
| 71 | * | 
|---|
| 72 | * Change logging and added autotest to odbctest | 
|---|
| 73 | * | 
|---|
| 74 | * Revision 1.2  2000/12/31 20:30:54  nick | 
|---|
| 75 | * | 
|---|
| 76 | * Add UNICODE support | 
|---|
| 77 | * | 
|---|
| 78 | * Revision 1.1.1.1  2000/09/04 16:42:52  nick | 
|---|
| 79 | * Imported Sources | 
|---|
| 80 | * | 
|---|
| 81 | * Revision 1.7  1999/11/13 23:40:58  ngorham | 
|---|
| 82 | * | 
|---|
| 83 | * Alter the way DM logging works | 
|---|
| 84 | * Upgrade the Postgres driver to 6.4.6 | 
|---|
| 85 | * | 
|---|
| 86 | * Revision 1.6  1999/10/24 23:54:17  ngorham | 
|---|
| 87 | * | 
|---|
| 88 | * First part of the changes to the error reporting | 
|---|
| 89 | * | 
|---|
| 90 | * Revision 1.5  1999/09/21 22:34:24  ngorham | 
|---|
| 91 | * | 
|---|
| 92 | * Improve performance by removing unneeded logging calls when logging is | 
|---|
| 93 | * disabled | 
|---|
| 94 | * | 
|---|
| 95 | * Revision 1.4  1999/07/10 21:10:15  ngorham | 
|---|
| 96 | * | 
|---|
| 97 | * Adjust error sqlstate from driver manager, depending on requested | 
|---|
| 98 | * version (ODBC2/3) | 
|---|
| 99 | * | 
|---|
| 100 | * Revision 1.3  1999/07/04 21:05:07  ngorham | 
|---|
| 101 | * | 
|---|
| 102 | * Add LGPL Headers to code | 
|---|
| 103 | * | 
|---|
| 104 | * Revision 1.2  1999/06/30 23:56:54  ngorham | 
|---|
| 105 | * | 
|---|
| 106 | * Add initial thread safety code | 
|---|
| 107 | * | 
|---|
| 108 | * Revision 1.1.1.1  1999/05/29 13:41:05  sShandyb | 
|---|
| 109 | * first go at it | 
|---|
| 110 | * | 
|---|
| 111 | * Revision 1.1.1.1  1999/05/27 18:23:17  pharvey | 
|---|
| 112 | * Imported sources | 
|---|
| 113 | * | 
|---|
| 114 | * Revision 1.3  1999/05/03 19:50:43  nick | 
|---|
| 115 | * Another check point | 
|---|
| 116 | * | 
|---|
| 117 | * Revision 1.2  1999/04/30 16:22:47  nick | 
|---|
| 118 | * Another checkpoint | 
|---|
| 119 | * | 
|---|
| 120 | * Revision 1.1  1999/04/25 23:06:11  nick | 
|---|
| 121 | * Initial revision | 
|---|
| 122 | * | 
|---|
| 123 | * | 
|---|
| 124 | **********************************************************************/ | 
|---|
| 125 |  | 
|---|
| 126 | #include <config.h> | 
|---|
| 127 | #include "drivermanager.h" | 
|---|
| 128 |  | 
|---|
| 129 | static char const rcsid[]= "$RCSfile: SQLColumnPrivileges.c,v $ $Revision: 1.8 $"; | 
|---|
| 130 |  | 
|---|
| 131 | SQLRETURN SQLColumnPrivilegesA( | 
|---|
| 132 | SQLHSTMT            statement_handle, | 
|---|
| 133 | SQLCHAR             *catalog_name, | 
|---|
| 134 | SQLSMALLINT         name_length1, | 
|---|
| 135 | SQLCHAR             *schema_name, | 
|---|
| 136 | SQLSMALLINT         name_length2, | 
|---|
| 137 | SQLCHAR             *table_name, | 
|---|
| 138 | SQLSMALLINT         name_length3, | 
|---|
| 139 | SQLCHAR             *column_name, | 
|---|
| 140 | SQLSMALLINT         name_length4 ) | 
|---|
| 141 | { | 
|---|
| 142 | return SQLColumnPrivileges( statement_handle, | 
|---|
| 143 | catalog_name, | 
|---|
| 144 | name_length1, | 
|---|
| 145 | schema_name, | 
|---|
| 146 | name_length2, | 
|---|
| 147 | table_name, | 
|---|
| 148 | name_length3, | 
|---|
| 149 | column_name, | 
|---|
| 150 | name_length4 ); | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | SQLRETURN SQLColumnPrivileges( | 
|---|
| 154 | SQLHSTMT            statement_handle, | 
|---|
| 155 | SQLCHAR             *catalog_name, | 
|---|
| 156 | SQLSMALLINT         name_length1, | 
|---|
| 157 | SQLCHAR             *schema_name, | 
|---|
| 158 | SQLSMALLINT         name_length2, | 
|---|
| 159 | SQLCHAR             *table_name, | 
|---|
| 160 | SQLSMALLINT         name_length3, | 
|---|
| 161 | SQLCHAR             *column_name, | 
|---|
| 162 | SQLSMALLINT         name_length4 ) | 
|---|
| 163 | { | 
|---|
| 164 | DMHSTMT statement = (DMHSTMT) statement_handle; | 
|---|
| 165 | SQLRETURN ret; | 
|---|
| 166 | SQLCHAR s1[ 100 + LOG_MESSAGE_LEN ], s2[ 100 + LOG_MESSAGE_LEN ], s3[ 100 + LOG_MESSAGE_LEN ], s4[ 100 + LOG_MESSAGE_LEN ]; | 
|---|
| 167 |  | 
|---|
| 168 | /* | 
|---|
| 169 | * check statement | 
|---|
| 170 | */ | 
|---|
| 171 |  | 
|---|
| 172 | if ( !__validate_stmt( statement )) | 
|---|
| 173 | { | 
|---|
| 174 | dm_log_write( __FILE__, | 
|---|
| 175 | __LINE__, | 
|---|
| 176 | LOG_INFO, | 
|---|
| 177 | LOG_INFO, | 
|---|
| 178 | "Error: SQL_INVALID_HANDLE"); | 
|---|
| 179 |  | 
|---|
| 180 | return SQL_INVALID_HANDLE; | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | function_entry( statement ); | 
|---|
| 184 |  | 
|---|
| 185 | if ( log_info.log_flag ) | 
|---|
| 186 | { | 
|---|
| 187 | sprintf( statement -> msg, "\n\t\tEntry:\ | 
|---|
| 188 | \n\t\t\tStatement = %p\ | 
|---|
| 189 | \n\t\t\tCatalog Name = %s\ | 
|---|
| 190 | \n\t\t\tSchema Name = %s\ | 
|---|
| 191 | \n\t\t\tTable Name = %s\ | 
|---|
| 192 | \n\t\t\tColumn Name = %s", | 
|---|
| 193 | statement, | 
|---|
| 194 | __string_with_length( s1, catalog_name, name_length1 ), | 
|---|
| 195 | __string_with_length( s2, schema_name, name_length2 ), | 
|---|
| 196 | __string_with_length( s3, table_name, name_length3 ), | 
|---|
| 197 | __string_with_length( s4, column_name, name_length4 )); | 
|---|
| 198 |  | 
|---|
| 199 | dm_log_write( __FILE__, | 
|---|
| 200 | __LINE__, | 
|---|
| 201 | LOG_INFO, | 
|---|
| 202 | LOG_INFO, | 
|---|
| 203 | statement -> msg ); | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | thread_protect( SQL_HANDLE_STMT, statement ); | 
|---|
| 207 |  | 
|---|
| 208 | if ( table_name == NULL ) | 
|---|
| 209 | { | 
|---|
| 210 | dm_log_write( __FILE__, | 
|---|
| 211 | __LINE__, | 
|---|
| 212 | LOG_INFO, | 
|---|
| 213 | LOG_INFO, | 
|---|
| 214 | "Error: HY009"); | 
|---|
| 215 |  | 
|---|
| 216 | __post_internal_error( &statement -> error, | 
|---|
| 217 | ERROR_HY009, NULL, | 
|---|
| 218 | statement -> connection -> environment -> requested_version ); | 
|---|
| 219 |  | 
|---|
| 220 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); | 
|---|
| 221 | } | 
|---|
| 222 |  | 
|---|
| 223 | if (( name_length1 < 0 && name_length1 != SQL_NTS ) || | 
|---|
| 224 | ( name_length2 < 0 && name_length2 != SQL_NTS ) || | 
|---|
| 225 | ( name_length3 < 0 && name_length3 != SQL_NTS ) || | 
|---|
| 226 | ( name_length4 < 0 && name_length4 != SQL_NTS )) | 
|---|
| 227 | { | 
|---|
| 228 | dm_log_write( __FILE__, | 
|---|
| 229 | __LINE__, | 
|---|
| 230 | LOG_INFO, | 
|---|
| 231 | LOG_INFO, | 
|---|
| 232 | "Error: HY090"); | 
|---|
| 233 |  | 
|---|
| 234 | __post_internal_error( &statement -> error, | 
|---|
| 235 | ERROR_HY090, NULL, | 
|---|
| 236 | statement -> connection -> environment -> requested_version ); | 
|---|
| 237 |  | 
|---|
| 238 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); | 
|---|
| 239 | } | 
|---|
| 240 |  | 
|---|
| 241 | /* | 
|---|
| 242 | * check states | 
|---|
| 243 | */ | 
|---|
| 244 |  | 
|---|
| 245 | #ifdef NR_PROBE | 
|---|
| 246 | if ( statement -> state == STATE_S5 || | 
|---|
| 247 | statement -> state == STATE_S6 || | 
|---|
| 248 | statement -> state == STATE_S7 ) | 
|---|
| 249 | #else | 
|---|
| 250 | if (( statement -> state == STATE_S6 && statement -> eod == 0 ) || | 
|---|
| 251 | statement -> state == STATE_S7 ) | 
|---|
| 252 | #endif | 
|---|
| 253 | { | 
|---|
| 254 | dm_log_write( __FILE__, | 
|---|
| 255 | __LINE__, | 
|---|
| 256 | LOG_INFO, | 
|---|
| 257 | LOG_INFO, | 
|---|
| 258 | "Error: 24000"); | 
|---|
| 259 |  | 
|---|
| 260 | __post_internal_error( &statement -> error, | 
|---|
| 261 | ERROR_24000, NULL, | 
|---|
| 262 | statement -> connection -> environment -> requested_version ); | 
|---|
| 263 |  | 
|---|
| 264 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); | 
|---|
| 265 | } | 
|---|
| 266 | else if ( statement -> state == STATE_S8 || | 
|---|
| 267 | statement -> state == STATE_S9 || | 
|---|
| 268 | statement -> state == STATE_S10 || | 
|---|
| 269 | statement -> state == STATE_S13 || | 
|---|
| 270 | statement -> state == STATE_S14 || | 
|---|
| 271 | statement -> state == STATE_S15 ) | 
|---|
| 272 | { | 
|---|
| 273 | dm_log_write( __FILE__, | 
|---|
| 274 | __LINE__, | 
|---|
| 275 | LOG_INFO, | 
|---|
| 276 | LOG_INFO, | 
|---|
| 277 | "Error: HY010"); | 
|---|
| 278 |  | 
|---|
| 279 | __post_internal_error( &statement -> error, | 
|---|
| 280 | ERROR_HY010, NULL, | 
|---|
| 281 | statement -> connection -> environment -> requested_version ); | 
|---|
| 282 |  | 
|---|
| 283 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | if ( statement -> state == STATE_S11 || | 
|---|
| 287 | statement -> state == STATE_S12 ) | 
|---|
| 288 | { | 
|---|
| 289 | if ( statement -> interupted_func != SQL_API_SQLCOLUMNPRIVILEGES ) | 
|---|
| 290 | { | 
|---|
| 291 | dm_log_write( __FILE__, | 
|---|
| 292 | __LINE__, | 
|---|
| 293 | LOG_INFO, | 
|---|
| 294 | LOG_INFO, | 
|---|
| 295 | "Error: HY010"); | 
|---|
| 296 |  | 
|---|
| 297 | __post_internal_error( &statement -> error, | 
|---|
| 298 | ERROR_HY010, NULL, | 
|---|
| 299 | statement -> connection -> environment -> requested_version ); | 
|---|
| 300 |  | 
|---|
| 301 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); | 
|---|
| 302 | } | 
|---|
| 303 | } | 
|---|
| 304 |  | 
|---|
| 305 | /* | 
|---|
| 306 | * TO_DO Check the SQL_ATTR_METADATA_ID settings | 
|---|
| 307 | */ | 
|---|
| 308 |  | 
|---|
| 309 | if ( statement -> connection -> unicode_driver ) | 
|---|
| 310 | { | 
|---|
| 311 | SQLWCHAR *s1, *s2, *s3, *s4; | 
|---|
| 312 | int wlen; | 
|---|
| 313 |  | 
|---|
| 314 | if ( !CHECK_SQLCOLUMNPRIVILEGESW( statement -> connection )) | 
|---|
| 315 | { | 
|---|
| 316 | dm_log_write( __FILE__, | 
|---|
| 317 | __LINE__, | 
|---|
| 318 | LOG_INFO, | 
|---|
| 319 | LOG_INFO, | 
|---|
| 320 | "Error: IM001"); | 
|---|
| 321 |  | 
|---|
| 322 | __post_internal_error( &statement -> error, | 
|---|
| 323 | ERROR_IM001, NULL, | 
|---|
| 324 | statement -> connection -> environment -> requested_version ); | 
|---|
| 325 |  | 
|---|
| 326 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); | 
|---|
| 327 | } | 
|---|
| 328 |  | 
|---|
| 329 | s1 = ansi_to_unicode_alloc( catalog_name, name_length1, statement -> connection, &wlen ); | 
|---|
| 330 | name_length1 = wlen; | 
|---|
| 331 | s2 = ansi_to_unicode_alloc( schema_name, name_length2, statement -> connection, &wlen ); | 
|---|
| 332 | name_length2 = wlen; | 
|---|
| 333 | s3 = ansi_to_unicode_alloc( table_name, name_length3, statement -> connection, &wlen ); | 
|---|
| 334 | name_length3 = wlen; | 
|---|
| 335 | s4 = ansi_to_unicode_alloc( column_name, name_length4, statement -> connection, &wlen ); | 
|---|
| 336 | name_length4 = wlen; | 
|---|
| 337 |  | 
|---|
| 338 | ret = SQLCOLUMNPRIVILEGESW( statement -> connection , | 
|---|
| 339 | statement -> driver_stmt, | 
|---|
| 340 | s1, | 
|---|
| 341 | name_length1, | 
|---|
| 342 | s2, | 
|---|
| 343 | name_length2, | 
|---|
| 344 | s3, | 
|---|
| 345 | name_length3, | 
|---|
| 346 | s4, | 
|---|
| 347 | name_length4 ); | 
|---|
| 348 |  | 
|---|
| 349 | if( s1 ) | 
|---|
| 350 | free( s1 ); | 
|---|
| 351 | if( s2 ) | 
|---|
| 352 | free( s2 ); | 
|---|
| 353 | if( s3 ) | 
|---|
| 354 | free( s3 ); | 
|---|
| 355 | if( s4 ) | 
|---|
| 356 | free( s4 ); | 
|---|
| 357 | } | 
|---|
| 358 | else | 
|---|
| 359 | { | 
|---|
| 360 | if ( !CHECK_SQLCOLUMNPRIVILEGES( statement -> connection )) | 
|---|
| 361 | { | 
|---|
| 362 | dm_log_write( __FILE__, | 
|---|
| 363 | __LINE__, | 
|---|
| 364 | LOG_INFO, | 
|---|
| 365 | LOG_INFO, | 
|---|
| 366 | "Error: IM001"); | 
|---|
| 367 |  | 
|---|
| 368 | __post_internal_error( &statement -> error, | 
|---|
| 369 | ERROR_IM001, NULL, | 
|---|
| 370 | statement -> connection -> environment -> requested_version ); | 
|---|
| 371 |  | 
|---|
| 372 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); | 
|---|
| 373 | } | 
|---|
| 374 |  | 
|---|
| 375 | ret = SQLCOLUMNPRIVILEGES( statement -> connection , | 
|---|
| 376 | statement -> driver_stmt, | 
|---|
| 377 | catalog_name, | 
|---|
| 378 | name_length1, | 
|---|
| 379 | schema_name, | 
|---|
| 380 | name_length2, | 
|---|
| 381 | table_name, | 
|---|
| 382 | name_length3, | 
|---|
| 383 | column_name, | 
|---|
| 384 | name_length4 ); | 
|---|
| 385 | } | 
|---|
| 386 |  | 
|---|
| 387 | if ( SQL_SUCCEEDED( ret )) | 
|---|
| 388 | { | 
|---|
| 389 | statement -> state = STATE_S5; | 
|---|
| 390 | statement -> prepared = 0; | 
|---|
| 391 | } | 
|---|
| 392 | else if ( ret == SQL_STILL_EXECUTING ) | 
|---|
| 393 | { | 
|---|
| 394 | statement -> interupted_func = SQL_API_SQLCOLUMNPRIVILEGES; | 
|---|
| 395 | if ( statement -> state != STATE_S11 && | 
|---|
| 396 | statement -> state != STATE_S12 ) | 
|---|
| 397 | statement -> state = STATE_S11; | 
|---|
| 398 | } | 
|---|
| 399 | else | 
|---|
| 400 | { | 
|---|
| 401 | statement -> state = STATE_S1; | 
|---|
| 402 | } | 
|---|
| 403 |  | 
|---|
| 404 | if ( log_info.log_flag ) | 
|---|
| 405 | { | 
|---|
| 406 | sprintf( statement -> msg, | 
|---|
| 407 | "\n\t\tExit:[%s]", | 
|---|
| 408 | __get_return_status( ret, s1 )); | 
|---|
| 409 |  | 
|---|
| 410 | dm_log_write( __FILE__, | 
|---|
| 411 | __LINE__, | 
|---|
| 412 | LOG_INFO, | 
|---|
| 413 | LOG_INFO, | 
|---|
| 414 | statement -> msg ); | 
|---|
| 415 | } | 
|---|
| 416 |  | 
|---|
| 417 | return function_return( SQL_HANDLE_STMT, statement, ret ); | 
|---|
| 418 | } | 
|---|
| 419 |  | 
|---|