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: SQLStatisticsW.c,v 1.9 2009/02/18 17:59:08 lurcher Exp $
31 *
32 * $Log: SQLStatisticsW.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
88static char const rcsid[]= "$RCSfile: SQLStatisticsW.c,v $";
89
90SQLRETURN SQLStatisticsW( 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 SQLUSMALLINT unique,
98 SQLUSMALLINT reserved )
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 ];
103
104 /*
105 * check statement
106 */
107
108 if ( !__validate_stmt( statement ))
109 {
110 dm_log_write( __FILE__,
111 __LINE__,
112 LOG_INFO,
113 LOG_INFO,
114 "Error: SQL_INVALID_HANDLE" );
115
116#ifdef WITH_HANDLE_REDIRECT
117 {
118 DMHSTMT parent_statement;
119
120 parent_statement = find_parent_handle( statement, SQL_HANDLE_STMT );
121
122 if ( parent_statement ) {
123 dm_log_write( __FILE__,
124 __LINE__,
125 LOG_INFO,
126 LOG_INFO,
127 "Info: found parent handle" );
128
129 if ( CHECK_SQLSTATISTICSW( parent_statement -> connection ))
130 {
131 dm_log_write( __FILE__,
132 __LINE__,
133 LOG_INFO,
134 LOG_INFO,
135 "Info: calling redirected driver function" );
136
137 return SQLSTATISTICSW( parent_statement -> connection,
138 statement_handle,
139 catalog_name,
140 name_length1,
141 schema_name,
142 name_length2,
143 table_name,
144 name_length3,
145 unique,
146 reserved );
147 }
148 }
149 }
150#endif
151 return SQL_INVALID_HANDLE;
152 }
153
154 function_entry( statement );
155
156 if ( log_info.log_flag )
157 {
158 sprintf( statement -> msg, "\n\t\tEntry:\
159\n\t\t\tStatement = %p\
160\n\t\t\tCatalog Name = %s\
161\n\t\t\tSchema Name = %s\
162\n\t\t\tTable Name = %s\
163\n\t\t\tUnique = %d\
164\n\t\t\tReserved = %d",
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 unique,
170 reserved );
171
172 dm_log_write( __FILE__,
173 __LINE__,
174 LOG_INFO,
175 LOG_INFO,
176 statement -> msg );
177 }
178
179 thread_protect( SQL_HANDLE_STMT, statement );
180
181 if (( name_length1 < 0 && name_length1 != SQL_NTS ) ||
182 ( name_length2 < 0 && name_length2 != SQL_NTS ) ||
183 ( name_length3 < 0 && name_length3 != SQL_NTS ))
184 {
185 dm_log_write( __FILE__,
186 __LINE__,
187 LOG_INFO,
188 LOG_INFO,
189 "Error: HY090" );
190
191 __post_internal_error( &statement -> error,
192 ERROR_HY090, NULL,
193 statement -> connection -> environment -> requested_version );
194
195 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
196 }
197
198 if ( reserved != SQL_ENSURE &&
199 reserved != SQL_QUICK )
200 {
201 dm_log_write( __FILE__,
202 __LINE__,
203 LOG_INFO,
204 LOG_INFO,
205 "Error: HY101" );
206
207 __post_internal_error( &statement -> error,
208 ERROR_HY101, NULL,
209 statement -> connection -> environment -> requested_version );
210
211 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
212 }
213
214 if ( unique != SQL_INDEX_UNIQUE &&
215 unique != SQL_INDEX_ALL )
216 {
217 dm_log_write( __FILE__,
218 __LINE__,
219 LOG_INFO,
220 LOG_INFO,
221 "Error: HY100" );
222
223 __post_internal_error( &statement -> error,
224 ERROR_HY100, NULL,
225 statement -> connection -> environment -> requested_version );
226
227 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
228 }
229
230 /*
231 * check states
232 */
233
234#ifdef NR_PROBE
235 if ( statement -> state == STATE_S5 ||
236 statement -> state == STATE_S6 ||
237 statement -> state == STATE_S7 )
238#else
239 if (( statement -> state == STATE_S6 && statement -> eod == 0 ) ||
240 statement -> state == STATE_S7 )
241#endif
242 {
243 dm_log_write( __FILE__,
244 __LINE__,
245 LOG_INFO,
246 LOG_INFO,
247 "Error: 24000" );
248
249 __post_internal_error( &statement -> error,
250 ERROR_24000, NULL,
251 statement -> connection -> environment -> requested_version );
252
253 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
254 }
255 else if ( statement -> state == STATE_S8 ||
256 statement -> state == STATE_S9 ||
257 statement -> state == STATE_S10 ||
258 statement -> state == STATE_S13 ||
259 statement -> state == STATE_S14 ||
260 statement -> state == STATE_S15 )
261 {
262 dm_log_write( __FILE__,
263 __LINE__,
264 LOG_INFO,
265 LOG_INFO,
266 "Error: HY010" );
267
268 __post_internal_error( &statement -> error,
269 ERROR_HY010, NULL,
270 statement -> connection -> environment -> requested_version );
271
272 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
273 }
274
275 if ( statement -> state == STATE_S11 ||
276 statement -> state == STATE_S12 )
277 {
278 if ( statement -> interupted_func != SQL_API_SQLSTATISTICS )
279 {
280 dm_log_write( __FILE__,
281 __LINE__,
282 LOG_INFO,
283 LOG_INFO,
284 "Error: HY010" );
285
286 __post_internal_error( &statement -> error,
287 ERROR_HY010, NULL,
288 statement -> connection -> environment -> requested_version );
289
290 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
291 }
292 }
293
294 if ( table_name == NULL )
295 {
296 dm_log_write( __FILE__,
297 __LINE__,
298 LOG_INFO,
299 LOG_INFO,
300 "Error: HY009" );
301
302 __post_internal_error( &statement -> error,
303 ERROR_HY009, NULL,
304 statement -> connection -> environment -> requested_version );
305
306 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
307 }
308
309 if ( statement -> metadata_id == SQL_TRUE ) {
310 if ( schema_name == NULL )
311 {
312 dm_log_write( __FILE__,
313 __LINE__,
314 LOG_INFO,
315 LOG_INFO,
316 "Error: HY009" );
317
318 __post_internal_error( &statement -> error,
319 ERROR_HY009, NULL,
320 statement -> connection -> environment -> requested_version );
321
322 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
323 }
324 }
325
326 if ( statement -> connection -> unicode_driver ||
327 CHECK_SQLSTATISTICSW( statement -> connection ))
328 {
329 if ( !CHECK_SQLSTATISTICSW( statement -> connection ))
330 {
331 dm_log_write( __FILE__,
332 __LINE__,
333 LOG_INFO,
334 LOG_INFO,
335 "Error: IM001" );
336
337 __post_internal_error( &statement -> error,
338 ERROR_IM001, NULL,
339 statement -> connection -> environment -> requested_version );
340
341 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
342 }
343
344 ret = SQLSTATISTICSW( statement -> connection,
345 statement -> driver_stmt,
346 catalog_name,
347 name_length1,
348 schema_name,
349 name_length2,
350 table_name,
351 name_length3,
352 unique,
353 reserved );
354 }
355 else
356 {
357 SQLCHAR *as1, *as2, *as3;
358 int clen;
359
360 if ( !CHECK_SQLSTATISTICS( 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 as1 = (SQLCHAR*) unicode_to_ansi_alloc( catalog_name, name_length1, statement -> connection, &clen );
376 name_length1 = clen;
377 as2 = (SQLCHAR*) unicode_to_ansi_alloc( schema_name, name_length2, statement -> connection, &clen );
378 name_length2 = clen;
379 as3 = (SQLCHAR*) unicode_to_ansi_alloc( table_name, name_length3, statement -> connection, &clen );
380 name_length3 = clen;
381
382 ret = SQLSTATISTICS( statement -> connection,
383 statement -> driver_stmt,
384 as1,
385 name_length1,
386 as2,
387 name_length2,
388 as3,
389 name_length3,
390 unique,
391 reserved );
392
393 if ( as1 ) free( as1 );
394 if ( as2 ) free( as2 );
395 if ( as3 ) free( as3 );
396 }
397
398 if ( SQL_SUCCEEDED( ret ))
399 {
400#ifdef NR_PROBE
401 /********
402 * Added this to get num cols from drivers which can only tell
403 * us after execute - PAH
404 */
405 /*
406 ret = SQLNUMRESULTCOLS( statement -> connection,
407 statement -> driver_stmt, &statement -> numcols );
408 */
409 statement -> numcols = 1;
410 /******/
411#endif
412 statement -> hascols = 1;
413 statement -> state = STATE_S5;
414 statement -> prepared = 0;
415 }
416 else if ( ret == SQL_STILL_EXECUTING )
417 {
418 statement -> interupted_func = SQL_API_SQLSTATISTICS;
419 if ( statement -> state != STATE_S11 &&
420 statement -> state != STATE_S12 )
421 statement -> state = STATE_S11;
422 }
423 else
424 {
425 statement -> state = STATE_S1;
426 }
427
428 if ( log_info.log_flag )
429 {
430 sprintf( statement -> msg,
431 "\n\t\tExit:[%s]",
432 __get_return_status( ret, s1 ));
433
434 dm_log_write( __FILE__,
435 __LINE__,
436 LOG_INFO,
437 LOG_INFO,
438 statement -> msg );
439 }
440
441 return function_return( SQL_HANDLE_STMT, statement, ret );
442}
443