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: SQLProceduresW.c,v 1.9 2009/02/18 17:59:08 lurcher Exp $
31 *
32 * $Log: SQLProceduresW.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: SQLProceduresW.c,v $ $Revision: 1.9 $";
89
90SQLRETURN SQLProceduresW(
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_proc_name,
97 SQLSMALLINT cb_proc_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_SQLPROCEDURESW( 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 SQLPROCEDURESW( parent_statement -> connection,
137 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 }
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\tProc Name = %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_proc_name, cb_proc_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_proc_name < 0 && cb_proc_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_SQLPROCEDURES )
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 /*
256 * TO_DO Check the SQL_ATTR_METADATA_ID settings
257 */
258
259 if ( statement -> connection -> unicode_driver ||
260 CHECK_SQLPROCEDURESW( statement -> connection ))
261 {
262 if ( !CHECK_SQLPROCEDURESW( statement -> connection ))
263 {
264 dm_log_write( __FILE__,
265 __LINE__,
266 LOG_INFO,
267 LOG_INFO,
268 "Error: IM001" );
269
270 __post_internal_error( &statement -> error,
271 ERROR_IM001, NULL,
272 statement -> connection -> environment -> requested_version );
273
274 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
275 }
276
277 ret = SQLPROCEDURESW( statement -> connection ,
278 statement -> driver_stmt,
279 sz_catalog_name,
280 cb_catalog_name,
281 sz_schema_name,
282 cb_schema_name,
283 sz_proc_name,
284 cb_proc_name );
285 }
286 else
287 {
288 SQLCHAR *as1, *as2, *as3;
289 int clen;
290
291 if ( !CHECK_SQLPROCEDURES( statement -> connection ))
292 {
293 dm_log_write( __FILE__,
294 __LINE__,
295 LOG_INFO,
296 LOG_INFO,
297 "Error: IM001" );
298
299 __post_internal_error( &statement -> error,
300 ERROR_IM001, NULL,
301 statement -> connection -> environment -> requested_version );
302
303 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
304 }
305
306 as1 = (SQLCHAR*) unicode_to_ansi_alloc( sz_catalog_name, cb_catalog_name, statement -> connection, &clen );
307 cb_catalog_name = clen;
308 as2 = (SQLCHAR*) unicode_to_ansi_alloc( sz_schema_name, cb_schema_name, statement -> connection, &clen );
309 cb_schema_name = clen;
310 as3 = (SQLCHAR*) unicode_to_ansi_alloc( sz_proc_name, cb_proc_name, statement -> connection, &clen );
311 cb_proc_name = clen;
312
313 ret = SQLPROCEDURES( statement -> connection ,
314 statement -> driver_stmt,
315 as1,
316 cb_catalog_name,
317 as2,
318 cb_schema_name,
319 as3,
320 cb_proc_name );
321
322 if ( as1 ) free( as1 );
323 if ( as2 ) free( as2 );
324 if ( as3 ) free( as3 );
325 }
326
327 if ( SQL_SUCCEEDED( ret ))
328 {
329 statement -> state = STATE_S5;
330 statement -> prepared = 0;
331 }
332 else if ( ret == SQL_STILL_EXECUTING )
333 {
334 statement -> interupted_func = SQL_API_SQLPROCEDURES;
335 if ( statement -> state != STATE_S11 &&
336 statement -> state != STATE_S12 )
337 statement -> state = STATE_S11;
338 }
339 else
340 {
341 statement -> state = STATE_S1;
342 }
343
344 if ( log_info.log_flag )
345 {
346 sprintf( statement -> msg,
347 "\n\t\tExit:[%s]",
348 __get_return_status( ret, s1 ));
349
350 dm_log_write( __FILE__,
351 __LINE__,
352 LOG_INFO,
353 LOG_INFO,
354 statement -> msg );
355 }
356
357 return function_return( SQL_HANDLE_STMT, statement, ret );
358}
359