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