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: SQLPrepare.c,v 1.7 2009/02/18 17:59:08 lurcher Exp $
31 *
32 * $Log: SQLPrepare.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 2005/03/04 14:38:08 lurcher
37 * Bump version number
38 *
39 * Revision 1.5 2003/10/30 18:20:46 lurcher
40 *
41 * Fix broken thread protection
42 * Remove SQLNumResultCols after execute, lease S4/S% to driver
43 * Fix string overrun in SQLDriverConnect
44 * Add initial support for Interix
45 *
46 * Revision 1.4 2002/12/05 17:44:31 lurcher
47 *
48 * Display unknown return values in return logging
49 *
50 * Revision 1.3 2002/07/24 08:49:52 lurcher
51 *
52 * Alter UNICODE support to use iconv for UNICODE-ANSI conversion
53 *
54 * Revision 1.2 2001/12/13 13:00:32 lurcher
55 *
56 * Remove most if not all warnings on 64 bit platforms
57 * Add support for new MS 3.52 64 bit changes
58 * Add override to disable the stopping of tracing
59 * Add MAX_ROWS support in postgres driver
60 *
61 * Revision 1.1.1.1 2001/10/17 16:40:06 lurcher
62 *
63 * First upload to SourceForge
64 *
65 * Revision 1.4 2001/04/12 17:43:36 nick
66 *
67 * Change logging and added autotest to odbctest
68 *
69 * Revision 1.3 2000/12/31 20:30:54 nick
70 *
71 * Add UNICODE support
72 *
73 * Revision 1.2 2000/10/25 09:23:53 nick
74 *
75 * Fixed incorrect error return when NULL string passed
76 *
77 * Revision 1.1.1.1 2000/09/04 16:42:52 nick
78 * Imported Sources
79 *
80 * Revision 1.7 1999/11/13 23:41:00 ngorham
81 *
82 * Alter the way DM logging works
83 * Upgrade the Postgres driver to 6.4.6
84 *
85 * Revision 1.6 1999/10/24 23:54:18 ngorham
86 *
87 * First part of the changes to the error reporting
88 *
89 * Revision 1.5 1999/09/21 22:34:25 ngorham
90 *
91 * Improve performance by removing unneeded logging calls when logging is
92 * disabled
93 *
94 * Revision 1.4 1999/07/10 21:10:16 ngorham
95 *
96 * Adjust error sqlstate from driver manager, depending on requested
97 * version (ODBC2/3)
98 *
99 * Revision 1.3 1999/07/04 21:05:08 ngorham
100 *
101 * Add LGPL Headers to code
102 *
103 * Revision 1.2 1999/06/30 23:56:55 ngorham
104 *
105 * Add initial thread safety code
106 *
107 * Revision 1.1.1.1 1999/05/29 13:41:08 sShandyb
108 * first go at it
109 *
110 * Revision 1.1.1.1 1999/05/27 18:23:18 pharvey
111 * Imported sources
112 *
113 * Revision 1.4 1999/05/03 19:50:43 nick
114 * Another check point
115 *
116 * Revision 1.3 1999/04/30 16:22:47 nick
117 * Another checkpoint
118 *
119 * Revision 1.2 1999/04/29 20:47:37 nick
120 * Another checkpoint
121 *
122 * Revision 1.1 1999/04/25 23:06:11 nick
123 * Initial revision
124 *
125 *
126 **********************************************************************/
127
128#include <config.h>
129#include "drivermanager.h"
130
131static char const rcsid[]= "$RCSfile: SQLPrepare.c,v $ $Revision: 1.7 $";
132
133SQLRETURN SQLPrepareA( SQLHSTMT statement_handle,
134 SQLCHAR *statement_text,
135 SQLINTEGER text_length )
136{
137 return SQLPrepare( statement_handle,
138 statement_text,
139 text_length );
140}
141
142SQLRETURN SQLPrepare( SQLHSTMT statement_handle,
143 SQLCHAR *statement_text,
144 SQLINTEGER text_length )
145{
146 DMHSTMT statement = (DMHSTMT) statement_handle;
147 SQLRETURN ret;
148 SQLCHAR *s1;
149 SQLCHAR s2[ 100 + LOG_MESSAGE_LEN ];
150
151 /*
152 * check statement
153 */
154
155 if ( !__validate_stmt( statement ))
156 {
157 dm_log_write( __FILE__,
158 __LINE__,
159 LOG_INFO,
160 LOG_INFO,
161 "Error: SQL_INVALID_HANDLE" );
162
163 return SQL_INVALID_HANDLE;
164 }
165
166 function_entry( statement );
167
168 if ( log_info.log_flag )
169 {
170 /*
171 * allocate some space for the buffer
172 */
173
174 if ( statement_text && text_length == SQL_NTS )
175 {
176 s1 = malloc( strlen((char*) statement_text ) + 100 );
177 }
178 else if ( statement_text )
179 {
180 s1 = malloc( text_length + 100 );
181 }
182 else
183 {
184 s1 = malloc( 101 );
185 }
186
187 sprintf( statement -> msg, "\n\t\tEntry:\
188\n\t\t\tStatement = %p\
189\n\t\t\tSQL = %s",
190 statement,
191 __string_with_length( s1, statement_text, text_length ));
192
193 free( s1 );
194
195 dm_log_write( __FILE__,
196 __LINE__,
197 LOG_INFO,
198 LOG_INFO,
199 statement -> msg );
200 }
201
202 thread_protect( SQL_HANDLE_STMT, statement );
203
204 if ( !statement_text )
205 {
206 dm_log_write( __FILE__,
207 __LINE__,
208 LOG_INFO,
209 LOG_INFO,
210 "Error: HY009" );
211
212 __post_internal_error( &statement -> error,
213 ERROR_HY009, NULL,
214 statement -> connection -> environment -> requested_version );
215
216 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
217 }
218
219 if ( text_length <= 0 && text_length != SQL_NTS )
220 {
221 dm_log_write( __FILE__,
222 __LINE__,
223 LOG_INFO,
224 LOG_INFO,
225 "Error: HY090" );
226
227 __post_internal_error( &statement -> error,
228 ERROR_HY090, NULL,
229 statement -> connection -> environment -> requested_version );
230
231 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
232 }
233
234 /*
235 * check states
236 */
237#ifdef NR_PROBE
238 if ( statement -> state == STATE_S5 ||
239 statement -> state == STATE_S6 ||
240 statement -> state == STATE_S7 )
241#else
242 if ( statement -> state == STATE_S6 ||
243 statement -> state == STATE_S7 )
244#endif
245 {
246 dm_log_write( __FILE__,
247 __LINE__,
248 LOG_INFO,
249 LOG_INFO,
250 "Error: 24000" );
251
252 __post_internal_error( &statement -> error,
253 ERROR_24000, NULL,
254 statement -> connection -> environment -> requested_version );
255
256 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
257 }
258 else if ( statement -> state == STATE_S8 ||
259 statement -> state == STATE_S9 ||
260 statement -> state == STATE_S10 ||
261 statement -> state == STATE_S13 ||
262 statement -> state == STATE_S14 ||
263 statement -> state == STATE_S15 )
264 {
265 dm_log_write( __FILE__,
266 __LINE__,
267 LOG_INFO,
268 LOG_INFO,
269 "Error: HY010" );
270
271 __post_internal_error( &statement -> error,
272 ERROR_HY010, NULL,
273 statement -> connection -> environment -> requested_version );
274
275 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
276 }
277
278 if ( statement -> state == STATE_S11 ||
279 statement -> state == STATE_S12 )
280 {
281 if ( statement -> interupted_func != SQL_API_SQLPREPARE )
282 {
283 dm_log_write( __FILE__,
284 __LINE__,
285 LOG_INFO,
286 LOG_INFO,
287 "Error: HY010" );
288
289 __post_internal_error( &statement -> error,
290 ERROR_HY010, NULL,
291 statement -> connection -> environment -> requested_version );
292
293 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
294 }
295 }
296
297 if ( statement -> connection -> unicode_driver )
298 {
299 int wlen;
300 SQLWCHAR *s1;
301
302 if ( !CHECK_SQLPREPAREW( statement -> connection ))
303 {
304 dm_log_write( __FILE__,
305 __LINE__,
306 LOG_INFO,
307 LOG_INFO,
308 "Error: IM001" );
309
310 __post_internal_error( &statement -> error,
311 ERROR_IM001, NULL,
312 statement -> connection -> environment -> requested_version );
313
314 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
315 }
316
317 s1 = ansi_to_unicode_alloc( statement_text, text_length, statement -> connection, &wlen );
318
319 text_length = wlen;
320
321 ret = SQLPREPAREW( statement -> connection ,
322 statement -> driver_stmt,
323 s1,
324 text_length );
325
326 if ( s1 )
327 free( s1 );
328 }
329 else
330 {
331 if ( !CHECK_SQLPREPARE( statement -> connection ))
332 {
333 dm_log_write( __FILE__,
334 __LINE__,
335 LOG_INFO,
336 LOG_INFO,
337 "Error: IM001" );
338
339 __post_internal_error( &statement -> error,
340 ERROR_IM001, NULL,
341 statement -> connection -> environment -> requested_version );
342
343 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
344 }
345
346 ret = SQLPREPARE( statement -> connection ,
347 statement -> driver_stmt,
348 statement_text,
349 text_length );
350 }
351
352 if ( SQL_SUCCEEDED( ret ))
353 {
354 statement -> hascols = 0;
355 statement -> state = STATE_S3;
356 statement -> prepared = 1;
357 }
358 else if ( ret == SQL_STILL_EXECUTING )
359 {
360 statement -> interupted_func = SQL_API_SQLPREPARE;
361 if ( statement -> state != STATE_S11 &&
362 statement -> state != STATE_S12 )
363 statement -> state = STATE_S11;
364 }
365 else
366 {
367 statement -> state = STATE_S1;
368 }
369
370 if ( log_info.log_flag )
371 {
372 sprintf( statement -> msg,
373 "\n\t\tExit:[%s]",
374 __get_return_status( ret, s2 ));
375
376 dm_log_write( __FILE__,
377 __LINE__,
378 LOG_INFO,
379 LOG_INFO,
380 statement -> msg );
381 }
382
383 return function_return( SQL_HANDLE_STMT, statement, ret );
384}
385