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