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