1/*********************************************************************
2 *
3 * (pharvey@codebydesign.com).
4 *
5 * Modified and extended by Nick Gorham
6 * (nick@lurcher.org).
7 *
8 * copyright (c) 1999 Nick Gorham
9 *
10 * Any bugs or problems should be considered the fault of Nick and not
11 * Peter.
12 *
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2 of the License, or (at your option) any later version.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 **********************************************************************
28 *
29 * $Id: SQLExecDirectW.c,v 1.10 2009/02/18 17:59:08 lurcher Exp $
30 *
31 * $Log: SQLExecDirectW.c,v $
32 * Revision 1.10 2009/02/18 17:59:08 lurcher
33 * Shift to using config.h, the compile lines were making it hard to spot warnings
34 *
35 * Revision 1.9 2008/08/29 08:01:38 lurcher
36 * Alter the way W functions are passed to the driver
37 *
38 * Revision 1.8 2007/02/28 15:37:48 lurcher
39 * deal with drivers that call internal W functions and end up in the driver manager. controlled by the --enable-handlemap configure arg
40 *
41 * Revision 1.7 2006/04/11 10:22:56 lurcher
42 * Fix a data type check
43 *
44 * Revision 1.6 2003/10/30 18:20:45 lurcher
45 *
46 * Fix broken thread protection
47 * Remove SQLNumResultCols after execute, lease S4/S% to driver
48 * Fix string overrun in SQLDriverConnect
49 * Add initial support for Interix
50 *
51 * Revision 1.5 2002/12/05 17:44:30 lurcher
52 *
53 * Display unknown return values in return logging
54 *
55 * Revision 1.4 2002/08/23 09:42:37 lurcher
56 *
57 * Fix some build warnings with casts, and a AIX linker mod, to include
58 * deplib's on the link line, but not the libtool generated ones
59 *
60 * Revision 1.3 2002/07/24 08:49:52 lurcher
61 *
62 * Alter UNICODE support to use iconv for UNICODE-ANSI conversion
63 *
64 * Revision 1.2 2002/01/15 14:47:44 lurcher
65 *
66 * Reset stmt->prepared flag after entering a SQLParamData state from
67 * SQLExecDirect
68 *
69 * Revision 1.1.1.1 2001/10/17 16:40:05 lurcher
70 *
71 * First upload to SourceForge
72 *
73 * Revision 1.3 2001/04/12 17:43:36 nick
74 *
75 * Change logging and added autotest to odbctest
76 *
77 * Revision 1.2 2001/01/04 13:16:25 nick
78 *
79 * Add support for GNU portable threads and tidy up some UNICODE compile
80 * warnings
81 *
82 * Revision 1.1 2000/12/31 20:30:54 nick
83 *
84 * Add UNICODE support
85 *
86 *
87 **********************************************************************/
88
89#include <config.h>
90#include "drivermanager.h"
91
92static char const rcsid[]= "$RCSfile: SQLExecDirectW.c,v $";
93
94SQLRETURN SQLExecDirectW( SQLHSTMT statement_handle,
95 SQLWCHAR *statement_text,
96 SQLINTEGER text_length )
97{
98 DMHSTMT statement = (DMHSTMT) statement_handle;
99 SQLRETURN ret;
100 SQLCHAR *s1;
101 SQLCHAR s2[ 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_SQLEXECDIRECTW( 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 SQLEXECDIRECTW( parent_statement -> connection,
137 statement,
138 statement_text,
139 text_length );
140 }
141 }
142 }
143#endif
144 return SQL_INVALID_HANDLE;
145 }
146
147 function_entry( statement );
148
149 if ( log_info.log_flag )
150 {
151 /*
152 * allocate some space for the buffer
153 */
154
155 if ( statement_text && text_length == SQL_NTS )
156 {
157 s1 = malloc( wide_strlen( statement_text ) * 2 + LOG_MESSAGE_LEN * 2 );
158 }
159 else if ( statement_text )
160 {
161 s1 = malloc( text_length + LOG_MESSAGE_LEN * 2 );
162 }
163 else
164 {
165 s1 = malloc( LOG_MESSAGE_LEN * 2 );
166 }
167
168 sprintf( statement -> msg, "\n\t\tEntry:\
169\n\t\t\tStatement = %p\
170\n\t\t\tSQL = %s",
171 statement,
172 __wstring_with_length( s1, statement_text, text_length ));
173
174 free( s1 );
175
176 dm_log_write( __FILE__,
177 __LINE__,
178 LOG_INFO,
179 LOG_INFO,
180 statement -> msg );
181 }
182
183 thread_protect( SQL_HANDLE_STMT, statement );
184
185 if ( !statement_text )
186 {
187 dm_log_write( __FILE__,
188 __LINE__,
189 LOG_INFO,
190 LOG_INFO,
191 "Error: HY009" );
192
193 __post_internal_error( &statement -> error,
194 ERROR_HY009, NULL,
195 statement -> connection -> environment -> requested_version );
196
197 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
198 }
199
200 if ( text_length <= 0 && text_length != SQL_NTS )
201 {
202 dm_log_write( __FILE__,
203 __LINE__,
204 LOG_INFO,
205 LOG_INFO,
206 "Error: HY090" );
207
208 __post_internal_error( &statement -> error,
209 ERROR_HY090, NULL,
210 statement -> connection -> environment -> requested_version );
211
212 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
213 }
214
215 /*
216 * check states
217 */
218
219#ifdef NR_PROBE
220 if ( statement -> state == STATE_S5 ||
221 statement -> state == STATE_S6 ||
222 statement -> state == STATE_S7 )
223#else
224 if (( statement -> state == STATE_S6 && statement -> eod == 0 ) ||
225 statement -> state == STATE_S7 )
226#endif
227 {
228 dm_log_write( __FILE__,
229 __LINE__,
230 LOG_INFO,
231 LOG_INFO,
232 "Error: 24000" );
233
234 __post_internal_error( &statement -> error,
235 ERROR_24000, NULL,
236 statement -> connection -> environment -> requested_version );
237
238 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
239 }
240 else if ( statement -> state == STATE_S8 ||
241 statement -> state == STATE_S9 ||
242 statement -> state == STATE_S10 )
243 {
244 dm_log_write( __FILE__,
245 __LINE__,
246 LOG_INFO,
247 LOG_INFO,
248 "Error: HY010" );
249
250 __post_internal_error( &statement -> error,
251 ERROR_HY010, NULL,
252 statement -> connection -> environment -> requested_version );
253
254 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
255 }
256
257 if ( statement -> state == STATE_S11 ||
258 statement -> state == STATE_S12 )
259 {
260 if ( statement -> interupted_func != SQL_API_SQLEXECDIRECT )
261 {
262 dm_log_write( __FILE__,
263 __LINE__,
264 LOG_INFO,
265 LOG_INFO,
266 "Error: HY010" );
267
268 __post_internal_error( &statement -> error,
269 ERROR_HY010, NULL,
270 statement -> connection -> environment -> requested_version );
271
272 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
273 }
274 }
275
276 if ( statement -> connection -> unicode_driver ||
277 CHECK_SQLEXECDIRECTW( statement -> connection ))
278 {
279#ifdef NR_PROBE
280 if ( !CHECK_SQLEXECDIRECTW( statement -> connection ) ||
281 !CHECK_SQLNUMRESULTCOLS( statement -> connection ))
282 {
283 dm_log_write( __FILE__,
284 __LINE__,
285 LOG_INFO,
286 LOG_INFO,
287 "Error: IM001" );
288
289 __post_internal_error( &statement -> error,
290 ERROR_IM001, NULL,
291 statement -> connection -> environment -> requested_version );
292
293 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
294 }
295#else
296 if ( !CHECK_SQLEXECDIRECTW( statement -> connection ))
297 {
298 dm_log_write( __FILE__,
299 __LINE__,
300 LOG_INFO,
301 LOG_INFO,
302 "Error: IM001" );
303
304 __post_internal_error( &statement -> error,
305 ERROR_IM001, NULL,
306 statement -> connection -> environment -> requested_version );
307
308 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
309 }
310#endif
311
312 ret = SQLEXECDIRECTW( statement -> connection,
313 statement -> driver_stmt,
314 statement_text,
315 text_length );
316 }
317 else
318 {
319 SQLCHAR *as1 = NULL;
320 int clen;
321
322#ifdef NR_PROBE
323 if ( !CHECK_SQLEXECDIRECT( statement -> connection ) ||
324 !CHECK_SQLNUMRESULTCOLS( statement -> connection ))
325 {
326 dm_log_write( __FILE__,
327 __LINE__,
328 LOG_INFO,
329 LOG_INFO,
330 "Error: IM001" );
331
332 __post_internal_error( &statement -> error,
333 ERROR_IM001, NULL,
334 statement -> connection -> environment -> requested_version );
335
336 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
337 }
338#else
339 if ( !CHECK_SQLEXECDIRECT( statement -> connection ))
340 {
341 dm_log_write( __FILE__,
342 __LINE__,
343 LOG_INFO,
344 LOG_INFO,
345 "Error: IM001" );
346
347 __post_internal_error( &statement -> error,
348 ERROR_IM001, NULL,
349 statement -> connection -> environment -> requested_version );
350
351 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
352 }
353#endif
354
355 as1 = (SQLCHAR*) unicode_to_ansi_alloc( statement_text, text_length, statement -> connection, &clen );
356
357 text_length = clen;
358
359 ret = SQLEXECDIRECT( statement -> connection,
360 statement -> driver_stmt,
361 as1,
362 text_length );
363
364 if ( as1 )
365 free( as1 );
366 }
367
368 if ( SQL_SUCCEEDED( ret ))
369 {
370#ifdef NR_PROBE
371 SQLRETURN local_ret;
372
373 /*
374 * grab any errors
375 */
376
377 if ( ret == SQL_SUCCESS_WITH_INFO )
378 {
379 function_return_ex( IGNORE_THREAD, statement, ret, TRUE );
380 }
381
382 local_ret = SQLNUMRESULTCOLS( statement -> connection,
383 statement -> driver_stmt, &statement -> numcols );
384
385 if ( statement -> numcols > 0 )
386 {
387 statement -> state = STATE_S5;
388 }
389 else
390 {
391 statement -> state = STATE_S4;
392 }
393#else
394 /*
395 * We don't know for sure
396 */
397 statement -> hascols = 1;
398 statement -> state = STATE_S5;
399#endif
400
401 statement -> prepared = 0;
402
403 /*
404 * there is a issue here with transactions, but for the
405 * moment
406 *
407 statement -> connection -> state = STATE_C6;
408 */
409 }
410 else if ( ret == SQL_NO_DATA )
411 {
412 statement -> state = STATE_S4;
413 statement -> prepared = 0;
414 }
415 else if ( ret == SQL_NEED_DATA )
416 {
417 statement -> interupted_func = SQL_API_SQLEXECDIRECT;
418 statement -> interupted_state = statement -> state;
419 statement -> state = STATE_S8;
420
421 statement -> prepared = 0;
422 }
423 else if ( ret == SQL_PARAM_DATA_AVAILABLE )
424 {
425 statement -> interupted_func = SQL_API_SQLEXECDIRECT;
426 statement -> interupted_state = statement -> state;
427 statement -> state = STATE_S13;
428 }
429 else if ( ret == SQL_STILL_EXECUTING )
430 {
431 statement -> interupted_func = SQL_API_SQLEXECDIRECT;
432 if ( statement -> state != STATE_S11 &&
433 statement -> state != STATE_S12 )
434 statement -> state = STATE_S11;
435
436 statement -> prepared = 0;
437 }
438 else if ( statement -> state >= STATE_S2 && statement -> state <= STATE_S4 ||
439 statement -> state >= STATE_S11 && statement -> state <= STATE_S12 &&
440 statement -> interupted_state >= STATE_S2 && statement -> interupted_state <= STATE_S4)
441 {
442 statement -> state = STATE_S1;
443 }
444 else if ( statement -> state >= STATE_S11 && statement -> state <= STATE_S12 )
445 {
446 statement -> state = statement -> interupted_state;
447 }
448
449 if ( log_info.log_flag )
450 {
451 sprintf( statement -> msg,
452 "\n\t\tExit:[%s]",
453 __get_return_status( ret, s2 ));
454
455 dm_log_write( __FILE__,
456 __LINE__,
457 LOG_INFO,
458 LOG_INFO,
459 statement -> msg );
460 }
461
462 return function_return( SQL_HANDLE_STMT, statement, ret );
463}
464