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: SQLProcedures.c,v 1.7 2009/02/18 17:59:08 lurcher Exp $
31 *
32 * $Log: SQLProcedures.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.7 1999/11/13 23:41:00 ngorham
79 *
80 * Alter the way DM logging works
81 * Upgrade the Postgres driver to 6.4.6
82 *
83 * Revision 1.6 1999/10/24 23:54:18 ngorham
84 *
85 * First part of the changes to the error reporting
86 *
87 * Revision 1.5 1999/09/21 22:34:25 ngorham
88 *
89 * Improve performance by removing unneeded logging calls when logging is
90 * disabled
91 *
92 * Revision 1.4 1999/07/10 21:10:17 ngorham
93 *
94 * Adjust error sqlstate from driver manager, depending on requested
95 * version (ODBC2/3)
96 *
97 * Revision 1.3 1999/07/04 21:05:08 ngorham
98 *
99 * Add LGPL Headers to code
100 *
101 * Revision 1.2 1999/06/30 23:56:55 ngorham
102 *
103 * Add initial thread safety code
104 *
105 * Revision 1.1.1.1 1999/05/29 13:41:08 sShandyb
106 * first go at it
107 *
108 * Revision 1.1.1.1 1999/05/27 18:23:18 pharvey
109 * Imported sources
110 *
111 * Revision 1.3 1999/05/03 19:50:43 nick
112 * Another check point
113 *
114 * Revision 1.2 1999/04/30 16:22:47 nick
115 * Another checkpoint
116 *
117 * Revision 1.1 1999/04/25 23:06:11 nick
118 * Initial revision
119 *
120 *
121 **********************************************************************/
122
123#include <config.h>
124#include "drivermanager.h"
125
126static char const rcsid[]= "$RCSfile: SQLProcedures.c,v $ $Revision: 1.7 $";
127
128SQLRETURN SQLProceduresA(
129 SQLHSTMT statement_handle,
130 SQLCHAR *sz_catalog_name,
131 SQLSMALLINT cb_catalog_name,
132 SQLCHAR *sz_schema_name,
133 SQLSMALLINT cb_schema_name,
134 SQLCHAR *sz_proc_name,
135 SQLSMALLINT cb_proc_name )
136{
137 return SQLProcedures( statement_handle,
138 sz_catalog_name,
139 cb_catalog_name,
140 sz_schema_name,
141 cb_schema_name,
142 sz_proc_name,
143 cb_proc_name );
144}
145
146SQLRETURN SQLProcedures(
147 SQLHSTMT statement_handle,
148 SQLCHAR *sz_catalog_name,
149 SQLSMALLINT cb_catalog_name,
150 SQLCHAR *sz_schema_name,
151 SQLSMALLINT cb_schema_name,
152 SQLCHAR *sz_proc_name,
153 SQLSMALLINT cb_proc_name )
154{
155 DMHSTMT statement = (DMHSTMT) statement_handle;
156 SQLRETURN ret;
157 SQLCHAR s1[ 100 + LOG_MESSAGE_LEN ], s2[ 100 + LOG_MESSAGE_LEN ], s3[ 100 + LOG_MESSAGE_LEN ];
158
159 /*
160 * check statement
161 */
162
163 if ( !__validate_stmt( statement ))
164 {
165 dm_log_write( __FILE__,
166 __LINE__,
167 LOG_INFO,
168 LOG_INFO,
169 "Error: SQL_INVALID_HANDLE" );
170
171 return SQL_INVALID_HANDLE;
172 }
173
174 function_entry( statement );
175
176 if ( log_info.log_flag )
177 {
178 sprintf( statement -> msg, "\n\t\tEntry:\
179\n\t\t\tStatement = %p\
180\n\t\t\tCatalog Name = %s\
181\n\t\t\tSchema Name = %s\
182\n\t\t\tProc Name = %s",
183 statement,
184 __string_with_length( s1, sz_catalog_name, cb_catalog_name ),
185 __string_with_length( s2, sz_schema_name, cb_schema_name ),
186 __string_with_length( s3, sz_proc_name, cb_proc_name ));
187
188 dm_log_write( __FILE__,
189 __LINE__,
190 LOG_INFO,
191 LOG_INFO,
192 statement -> msg );
193 }
194
195 thread_protect( SQL_HANDLE_STMT, statement );
196
197 if (( cb_catalog_name < 0 && cb_catalog_name != SQL_NTS ) ||
198 ( cb_schema_name < 0 && cb_schema_name != SQL_NTS ) ||
199 ( cb_proc_name < 0 && cb_proc_name != SQL_NTS ))
200 {
201 dm_log_write( __FILE__,
202 __LINE__,
203 LOG_INFO,
204 LOG_INFO,
205 "Error: HY090" );
206
207 __post_internal_error( &statement -> error,
208 ERROR_HY090, NULL,
209 statement -> connection -> environment -> requested_version );
210
211 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
212 }
213
214 /*
215 * check states
216 */
217
218#ifdef NR_PROBE
219 if ( statement -> state == STATE_S5 ||
220 statement -> state == STATE_S6 ||
221 statement -> state == STATE_S7 )
222#else
223 if (( statement -> state == STATE_S6 && statement -> eod == 0 ) ||
224 statement -> state == STATE_S7 )
225#endif
226 {
227 dm_log_write( __FILE__,
228 __LINE__,
229 LOG_INFO,
230 LOG_INFO,
231 "Error: 24000" );
232
233 __post_internal_error( &statement -> error,
234 ERROR_24000, NULL,
235 statement -> connection -> environment -> requested_version );
236
237 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
238 }
239 else if ( statement -> state == STATE_S8 ||
240 statement -> state == STATE_S9 ||
241 statement -> state == STATE_S10 ||
242 statement -> state == STATE_S13 ||
243 statement -> state == STATE_S14 ||
244 statement -> state == STATE_S15 )
245 {
246 dm_log_write( __FILE__,
247 __LINE__,
248 LOG_INFO,
249 LOG_INFO,
250 "Error: HY010" );
251
252 __post_internal_error( &statement -> error,
253 ERROR_HY010, NULL,
254 statement -> connection -> environment -> requested_version );
255
256 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
257 }
258
259 if ( statement -> state == STATE_S11 ||
260 statement -> state == STATE_S12 )
261 {
262 if ( statement -> interupted_func != SQL_API_SQLPROCEDURES )
263 {
264 dm_log_write( __FILE__,
265 __LINE__,
266 LOG_INFO,
267 LOG_INFO,
268 "Error: HY010" );
269
270 __post_internal_error( &statement -> error,
271 ERROR_HY010, NULL,
272 statement -> connection -> environment -> requested_version );
273
274 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
275 }
276 }
277
278 /*
279 * TO_DO Check the SQL_ATTR_METADATA_ID settings
280 */
281
282 if ( statement -> connection -> unicode_driver )
283 {
284 SQLWCHAR *s1, *s2, *s3;
285 int wlen;
286
287 if ( !CHECK_SQLPROCEDURESW( statement -> connection ))
288 {
289 dm_log_write( __FILE__,
290 __LINE__,
291 LOG_INFO,
292 LOG_INFO,
293 "Error: IM001" );
294
295 __post_internal_error( &statement -> error,
296 ERROR_IM001, NULL,
297 statement -> connection -> environment -> requested_version );
298
299 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
300 }
301
302 s1 = ansi_to_unicode_alloc( sz_catalog_name, cb_catalog_name, statement -> connection, &wlen );
303 cb_catalog_name = wlen;
304 s2 = ansi_to_unicode_alloc( sz_schema_name, cb_schema_name, statement -> connection, &wlen );
305 cb_schema_name = wlen;
306 s3 = ansi_to_unicode_alloc( sz_proc_name, cb_proc_name, statement -> connection, &wlen );
307 cb_proc_name = wlen;
308
309 ret = SQLPROCEDURESW( statement -> connection ,
310 statement -> driver_stmt,
311 s1,
312 cb_catalog_name,
313 s2,
314 cb_schema_name,
315 s3,
316 cb_proc_name );
317
318 if ( s1 )
319 free( s1 );
320 if ( s2 )
321 free( s2 );
322 if ( s3 )
323 free( s3 );
324 }
325 else
326 {
327 if ( !CHECK_SQLPROCEDURES( statement -> connection ))
328 {
329 dm_log_write( __FILE__,
330 __LINE__,
331 LOG_INFO,
332 LOG_INFO,
333 "Error: IM001" );
334
335 __post_internal_error( &statement -> error,
336 ERROR_IM001, NULL,
337 statement -> connection -> environment -> requested_version );
338
339 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
340 }
341
342 ret = SQLPROCEDURES( statement -> connection ,
343 statement -> driver_stmt,
344 sz_catalog_name,
345 cb_catalog_name,
346 sz_schema_name,
347 cb_schema_name,
348 sz_proc_name,
349 cb_proc_name );
350 }
351
352 if ( SQL_SUCCEEDED( ret ))
353 {
354 statement -> state = STATE_S5;
355 statement -> prepared = 0;
356 }
357 else if ( ret == SQL_STILL_EXECUTING )
358 {
359 statement -> interupted_func = SQL_API_SQLPROCEDURES;
360 if ( statement -> state != STATE_S11 &&
361 statement -> state != STATE_S12 )
362 statement -> state = STATE_S11;
363 }
364 else
365 {
366 statement -> state = STATE_S1;
367 }
368
369 if ( log_info.log_flag )
370 {
371 sprintf( statement -> msg,
372 "\n\t\tExit:[%s]",
373 __get_return_status( ret, s1 ));
374
375 dm_log_write( __FILE__,
376 __LINE__,
377 LOG_INFO,
378 LOG_INFO,
379 statement -> msg );
380 }
381
382 return function_return( SQL_HANDLE_STMT, statement, ret );
383}
384