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