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: SQLStatistics.c,v 1.8 2009/02/18 17:59:08 lurcher Exp $ |
31 | * |
32 | * $Log: SQLStatistics.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/02/07 14:17:07 lurcher |
37 | * Fix small typo in SQLStatistics |
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:46 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:40 lurcher |
51 | * |
52 | * Add the A functions as well as the W |
53 | * |
54 | * Revision 1.3 2002/12/05 17:44:31 lurcher |
55 | * |
56 | * Display unknown return values in return logging |
57 | * |
58 | * Revision 1.2 2002/07/24 08:49:52 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:07 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:36 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:41:01 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:19 ngorham |
87 | * |
88 | * First part of the changes to the error reporting |
89 | * |
90 | * Revision 1.5 1999/09/21 22:34:26 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:17 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:08 ngorham |
101 | * |
102 | * Add LGPL Headers to code |
103 | * |
104 | * Revision 1.2 1999/06/30 23:56:56 ngorham |
105 | * |
106 | * Add initial thread safety code |
107 | * |
108 | * Revision 1.1.1.1 1999/05/29 13:41:09 sShandyb |
109 | * first go at it |
110 | * |
111 | * Revision 1.1.1.1 1999/05/27 18:23:18 pharvey |
112 | * Imported sources |
113 | * |
114 | * Revision 1.4 1999/05/03 19:50:43 nick |
115 | * Another check point |
116 | * |
117 | * Revision 1.3 1999/04/30 16:22:47 nick |
118 | * Another checkpoint |
119 | * |
120 | * Revision 1.2 1999/04/29 20:47:37 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: SQLStatistics.c,v $ $Revision: 1.8 $" ; |
133 | |
134 | SQLRETURN SQLStatisticsA( SQLHSTMT statement_handle, |
135 | SQLCHAR *catalog_name, |
136 | SQLSMALLINT name_length1, |
137 | SQLCHAR *schema_name, |
138 | SQLSMALLINT name_length2, |
139 | SQLCHAR *table_name, |
140 | SQLSMALLINT name_length3, |
141 | SQLUSMALLINT unique, |
142 | SQLUSMALLINT reserved ) |
143 | { |
144 | return SQLStatistics( statement_handle, |
145 | catalog_name, |
146 | name_length1, |
147 | schema_name, |
148 | name_length2, |
149 | table_name, |
150 | name_length3, |
151 | unique, |
152 | reserved ); |
153 | } |
154 | |
155 | SQLRETURN SQLStatistics( SQLHSTMT statement_handle, |
156 | SQLCHAR *catalog_name, |
157 | SQLSMALLINT name_length1, |
158 | SQLCHAR *schema_name, |
159 | SQLSMALLINT name_length2, |
160 | SQLCHAR *table_name, |
161 | SQLSMALLINT name_length3, |
162 | SQLUSMALLINT unique, |
163 | SQLUSMALLINT reserved ) |
164 | { |
165 | DMHSTMT statement = (DMHSTMT) statement_handle; |
166 | SQLRETURN ret; |
167 | SQLCHAR s1[ 100 + LOG_MESSAGE_LEN ], s2[ 100 + LOG_MESSAGE_LEN ], s3[ 100 + LOG_MESSAGE_LEN ]; |
168 | |
169 | /* |
170 | * check statement |
171 | */ |
172 | |
173 | if ( !__validate_stmt( statement )) |
174 | { |
175 | dm_log_write( __FILE__, |
176 | __LINE__, |
177 | LOG_INFO, |
178 | LOG_INFO, |
179 | "Error: SQL_INVALID_HANDLE" ); |
180 | |
181 | return SQL_INVALID_HANDLE; |
182 | } |
183 | |
184 | function_entry( statement ); |
185 | |
186 | if ( log_info.log_flag ) |
187 | { |
188 | sprintf( statement -> msg, "\n\t\tEntry:\ |
189 | \n\t\t\tStatement = %p\ |
190 | \n\t\t\tCatalog Name = %s\ |
191 | \n\t\t\tSchema Name = %s\ |
192 | \n\t\t\tTable Name = %s\ |
193 | \n\t\t\tUnique = %d\ |
194 | \n\t\t\tReserved = %d" , |
195 | statement, |
196 | __string_with_length( s1, catalog_name, name_length1 ), |
197 | __string_with_length( s2, schema_name, name_length2 ), |
198 | __string_with_length( s3, table_name, name_length3 ), |
199 | unique, |
200 | reserved ); |
201 | |
202 | dm_log_write( __FILE__, |
203 | __LINE__, |
204 | LOG_INFO, |
205 | LOG_INFO, |
206 | statement -> msg ); |
207 | } |
208 | |
209 | thread_protect( SQL_HANDLE_STMT, statement ); |
210 | |
211 | if (( name_length1 < 0 && name_length1 != SQL_NTS ) || |
212 | ( name_length2 < 0 && name_length2 != SQL_NTS ) || |
213 | ( name_length3 < 0 && name_length3 != SQL_NTS )) |
214 | { |
215 | dm_log_write( __FILE__, |
216 | __LINE__, |
217 | LOG_INFO, |
218 | LOG_INFO, |
219 | "Error: HY090" ); |
220 | |
221 | __post_internal_error( &statement -> error, |
222 | ERROR_HY090, NULL, |
223 | statement -> connection -> environment -> requested_version ); |
224 | |
225 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
226 | } |
227 | |
228 | if ( reserved != SQL_ENSURE && |
229 | reserved != SQL_QUICK ) |
230 | { |
231 | dm_log_write( __FILE__, |
232 | __LINE__, |
233 | LOG_INFO, |
234 | LOG_INFO, |
235 | "Error: HY101" ); |
236 | |
237 | __post_internal_error( &statement -> error, |
238 | ERROR_HY101, NULL, |
239 | statement -> connection -> environment -> requested_version ); |
240 | |
241 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
242 | } |
243 | |
244 | if ( unique != SQL_INDEX_UNIQUE && |
245 | unique != SQL_INDEX_ALL ) |
246 | { |
247 | dm_log_write( __FILE__, |
248 | __LINE__, |
249 | LOG_INFO, |
250 | LOG_INFO, |
251 | "Error: HY100" ); |
252 | |
253 | __post_internal_error( &statement -> error, |
254 | ERROR_HY100, NULL, |
255 | statement -> connection -> environment -> requested_version ); |
256 | |
257 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
258 | } |
259 | |
260 | /* |
261 | * check states |
262 | */ |
263 | |
264 | #ifdef NR_PROBE |
265 | if ( statement -> state == STATE_S5 || |
266 | statement -> state == STATE_S6 || |
267 | statement -> state == STATE_S7 ) |
268 | #else |
269 | if ( statement -> state == STATE_S6 || |
270 | statement -> state == STATE_S7 ) |
271 | #endif |
272 | { |
273 | dm_log_write( __FILE__, |
274 | __LINE__, |
275 | LOG_INFO, |
276 | LOG_INFO, |
277 | "Error: 24000" ); |
278 | |
279 | __post_internal_error( &statement -> error, |
280 | ERROR_24000, NULL, |
281 | statement -> connection -> environment -> requested_version ); |
282 | |
283 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
284 | } |
285 | else if ( statement -> state == STATE_S8 || |
286 | statement -> state == STATE_S9 || |
287 | statement -> state == STATE_S10 || |
288 | statement -> state == STATE_S13 || |
289 | statement -> state == STATE_S14 || |
290 | statement -> state == STATE_S15 ) |
291 | { |
292 | dm_log_write( __FILE__, |
293 | __LINE__, |
294 | LOG_INFO, |
295 | LOG_INFO, |
296 | "Error: HY010" ); |
297 | |
298 | __post_internal_error( &statement -> error, |
299 | ERROR_HY010, NULL, |
300 | statement -> connection -> environment -> requested_version ); |
301 | |
302 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
303 | } |
304 | |
305 | if ( statement -> state == STATE_S11 || |
306 | statement -> state == STATE_S12 ) |
307 | { |
308 | if ( statement -> interupted_func != SQL_API_SQLSTATISTICS ) |
309 | { |
310 | dm_log_write( __FILE__, |
311 | __LINE__, |
312 | LOG_INFO, |
313 | LOG_INFO, |
314 | "Error: HY010" ); |
315 | |
316 | __post_internal_error( &statement -> error, |
317 | ERROR_HY010, NULL, |
318 | statement -> connection -> environment -> requested_version ); |
319 | |
320 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
321 | } |
322 | } |
323 | |
324 | if ( table_name == NULL ) |
325 | { |
326 | dm_log_write( __FILE__, |
327 | __LINE__, |
328 | LOG_INFO, |
329 | LOG_INFO, |
330 | "Error: HY009" ); |
331 | |
332 | __post_internal_error( &statement -> error, |
333 | ERROR_HY009, NULL, |
334 | statement -> connection -> environment -> requested_version ); |
335 | |
336 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
337 | } |
338 | |
339 | if ( statement -> metadata_id == SQL_TRUE ) { |
340 | if ( schema_name == NULL ) |
341 | { |
342 | dm_log_write( __FILE__, |
343 | __LINE__, |
344 | LOG_INFO, |
345 | LOG_INFO, |
346 | "Error: HY009" ); |
347 | |
348 | __post_internal_error( &statement -> error, |
349 | ERROR_HY009, NULL, |
350 | statement -> connection -> environment -> requested_version ); |
351 | |
352 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
353 | } |
354 | } |
355 | |
356 | if ( statement -> connection -> unicode_driver ) |
357 | { |
358 | SQLWCHAR *s1, *s2, *s3; |
359 | int wlen; |
360 | |
361 | if ( !CHECK_SQLSTATISTICSW( statement -> connection )) |
362 | { |
363 | dm_log_write( __FILE__, |
364 | __LINE__, |
365 | LOG_INFO, |
366 | LOG_INFO, |
367 | "Error: IM001" ); |
368 | |
369 | __post_internal_error( &statement -> error, |
370 | ERROR_IM001, NULL, |
371 | statement -> connection -> environment -> requested_version ); |
372 | |
373 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
374 | } |
375 | |
376 | s1 = ansi_to_unicode_alloc( catalog_name, name_length1, statement -> connection, &wlen ); |
377 | name_length1 = wlen; |
378 | s2 = ansi_to_unicode_alloc( schema_name, name_length2, statement -> connection, &wlen ); |
379 | name_length2 = wlen; |
380 | s3 = ansi_to_unicode_alloc( table_name, name_length3, statement -> connection, &wlen ); |
381 | name_length3 = wlen; |
382 | |
383 | ret = SQLSTATISTICSW( statement -> connection, |
384 | statement -> driver_stmt, |
385 | s1, |
386 | name_length1, |
387 | s2, |
388 | name_length2, |
389 | s3, |
390 | name_length3, |
391 | unique, |
392 | reserved ); |
393 | |
394 | if( s1 ) |
395 | free( s1 ); |
396 | if( s2 ) |
397 | free( s2 ); |
398 | if( s3 ) |
399 | free( s3 ); |
400 | } |
401 | else |
402 | { |
403 | if ( !CHECK_SQLSTATISTICS( statement -> connection )) |
404 | { |
405 | dm_log_write( __FILE__, |
406 | __LINE__, |
407 | LOG_INFO, |
408 | LOG_INFO, |
409 | "Error: IM001" ); |
410 | |
411 | __post_internal_error( &statement -> error, |
412 | ERROR_IM001, NULL, |
413 | statement -> connection -> environment -> requested_version ); |
414 | |
415 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
416 | } |
417 | |
418 | ret = SQLSTATISTICS( statement -> connection, |
419 | statement -> driver_stmt, |
420 | catalog_name, |
421 | name_length1, |
422 | schema_name, |
423 | name_length2, |
424 | table_name, |
425 | name_length3, |
426 | unique, |
427 | reserved ); |
428 | } |
429 | |
430 | |
431 | if ( SQL_SUCCEEDED( ret )) |
432 | { |
433 | #ifdef NR_PROBE |
434 | /******** |
435 | * Added this to get num cols from drivers which can only tell |
436 | * us after execute - PAH |
437 | */ |
438 | /* |
439 | ret = SQLNUMRESULTCOLS( statement -> connection, |
440 | statement -> driver_stmt, &statement -> numcols ); |
441 | */ |
442 | statement -> numcols = 1; |
443 | /******/ |
444 | #endif |
445 | statement -> hascols = 1; |
446 | statement -> state = STATE_S5; |
447 | statement -> prepared = 0; |
448 | } |
449 | else if ( ret == SQL_STILL_EXECUTING ) |
450 | { |
451 | statement -> interupted_func = SQL_API_SQLSTATISTICS; |
452 | if ( statement -> state != STATE_S11 && |
453 | statement -> state != STATE_S12 ) |
454 | statement -> state = STATE_S11; |
455 | } |
456 | else |
457 | { |
458 | statement -> state = STATE_S1; |
459 | } |
460 | |
461 | if ( log_info.log_flag ) |
462 | { |
463 | sprintf( statement -> msg, |
464 | "\n\t\tExit:[%s]" , |
465 | __get_return_status( ret, s1 )); |
466 | |
467 | dm_log_write( __FILE__, |
468 | __LINE__, |
469 | LOG_INFO, |
470 | LOG_INFO, |
471 | statement -> msg ); |
472 | } |
473 | |
474 | return function_return( SQL_HANDLE_STMT, statement, ret ); |
475 | } |
476 | |