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: SQLGetStmtAttrW.c,v 1.7 2009/02/18 17:59:08 lurcher Exp $
31 *
32 * $Log: SQLGetStmtAttrW.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 2009/02/04 09:30:02 lurcher
37 * Fix some SQLINTEGER/SQLLEN conflicts
38 *
39 * Revision 1.5 2008/08/29 08:01:39 lurcher
40 * Alter the way W functions are passed to the driver
41 *
42 * Revision 1.4 2007/02/28 15:37:48 lurcher
43 * deal with drivers that call internal W functions and end up in the driver manager. controlled by the --enable-handlemap configure arg
44 *
45 * Revision 1.3 2003/10/30 18:20:46 lurcher
46 *
47 * Fix broken thread protection
48 * Remove SQLNumResultCols after execute, lease S4/S% to driver
49 * Fix string overrun in SQLDriverConnect
50 * Add initial support for Interix
51 *
52 * Revision 1.2 2002/12/05 17:44:31 lurcher
53 *
54 * Display unknown return values in return logging
55 *
56 * Revision 1.1.1.1 2001/10/17 16:40:06 lurcher
57 *
58 * First upload to SourceForge
59 *
60 * Revision 1.3 2001/07/03 09:30:41 nick
61 *
62 * Add ability to alter size of displayed message in the log
63 *
64 * Revision 1.2 2001/04/12 17:43:36 nick
65 *
66 * Change logging and added autotest to odbctest
67 *
68 * Revision 1.1 2000/12/31 20:30:54 nick
69 *
70 * Add UNICODE support
71 *
72 *
73 **********************************************************************/
74
75#include <config.h>
76#include "drivermanager.h"
77
78static char const rcsid[]= "$RCSfile: SQLGetStmtAttrW.c,v $";
79
80SQLRETURN SQLGetStmtAttrW( SQLHSTMT statement_handle,
81 SQLINTEGER attribute,
82 SQLPOINTER value,
83 SQLINTEGER buffer_length,
84 SQLINTEGER *string_length )
85{
86 DMHSTMT statement = (DMHSTMT) statement_handle;
87 SQLRETURN ret;
88 SQLCHAR s1[ 100 + LOG_MESSAGE_LEN ];
89
90 /*
91 * check statement
92 */
93
94 if ( !__validate_stmt( statement ))
95 {
96 dm_log_write( __FILE__,
97 __LINE__,
98 LOG_INFO,
99 LOG_INFO,
100 "Error: SQL_INVALID_HANDLE" );
101
102#ifdef WITH_HANDLE_REDIRECT
103 {
104 DMHSTMT parent_statement;
105
106 parent_statement = find_parent_handle( statement, SQL_HANDLE_STMT );
107
108 if ( parent_statement ) {
109 dm_log_write( __FILE__,
110 __LINE__,
111 LOG_INFO,
112 LOG_INFO,
113 "Info: found parent handle" );
114
115 if ( CHECK_SQLGETSTMTATTRW( parent_statement -> connection ))
116 {
117 dm_log_write( __FILE__,
118 __LINE__,
119 LOG_INFO,
120 LOG_INFO,
121 "Info: calling redirected driver function" );
122
123 return SQLGETSTMTATTRW( parent_statement -> connection,
124 statement_handle,
125 attribute,
126 value,
127 buffer_length,
128 string_length );
129 }
130 }
131 }
132#endif
133 return SQL_INVALID_HANDLE;
134 }
135
136 function_entry( statement );
137
138 if ( log_info.log_flag )
139 {
140 sprintf( statement -> msg, "\n\t\tEntry:\
141\n\t\t\tStatement = %p\
142\n\t\t\tAttribute = %s\
143\n\t\t\tValue = %p\
144\n\t\t\tBuffer Length = %d\
145\n\t\t\tStrLen = %p",
146 statement,
147 __stmt_attr_as_string( s1, attribute ),
148 value,
149 (int)buffer_length,
150 (void*)string_length );
151
152 dm_log_write( __FILE__,
153 __LINE__,
154 LOG_INFO,
155 LOG_INFO,
156 statement -> msg );
157 }
158
159 thread_protect( SQL_HANDLE_STMT, statement );
160
161 /*
162 * check states
163 */
164
165 if ( attribute == SQL_ATTR_ROW_NUMBER || attribute == SQL_GET_BOOKMARK )
166 {
167 if ( statement -> state == STATE_S1 ||
168 statement -> state == STATE_S2 ||
169 statement -> state == STATE_S3 ||
170 statement -> state == STATE_S4 ||
171 statement -> state == STATE_S5 ||
172 ( statement -> state == STATE_S6 ||
173 statement -> state == STATE_S7 ) && statement -> eod )
174 {
175 dm_log_write( __FILE__,
176 __LINE__,
177 LOG_INFO,
178 LOG_INFO,
179 "Error: 24000" );
180
181 __post_internal_error( &statement -> error,
182 ERROR_24000, NULL,
183 statement -> connection -> environment -> requested_version );
184
185 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
186 }
187 }
188
189 if ( statement -> state == STATE_S8 ||
190 statement -> state == STATE_S9 ||
191 statement -> state == STATE_S10 ||
192 statement -> state == STATE_S11 ||
193 statement -> state == STATE_S12 ||
194 statement -> state == STATE_S13 ||
195 statement -> state == STATE_S14 ||
196 statement -> state == STATE_S15 )
197 {
198 dm_log_write( __FILE__,
199 __LINE__,
200 LOG_INFO,
201 LOG_INFO,
202 "Error: HY010" );
203
204 __post_internal_error( &statement -> error,
205 ERROR_HY010, NULL,
206 statement -> connection -> environment -> requested_version );
207
208 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
209 }
210
211 /*
212 * states S5 - S7 are handled by the driver
213 */
214
215 if ( statement -> connection -> unicode_driver ||
216 CHECK_SQLGETSTMTATTRW( statement -> connection ))
217 {
218 if ( !CHECK_SQLGETSTMTATTRW( statement -> connection ))
219 {
220 dm_log_write( __FILE__,
221 __LINE__,
222 LOG_INFO,
223 LOG_INFO,
224 "Error: IM001" );
225
226 __post_internal_error( &statement -> error,
227 ERROR_IM001, NULL,
228 statement -> connection -> environment -> requested_version );
229
230 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
231 }
232 }
233 else
234 {
235 if ( !CHECK_SQLGETSTMTATTR( statement -> connection ))
236 {
237 dm_log_write( __FILE__,
238 __LINE__,
239 LOG_INFO,
240 LOG_INFO,
241 "Error: IM001" );
242
243 __post_internal_error( &statement -> error,
244 ERROR_IM001, NULL,
245 statement -> connection -> environment -> requested_version );
246
247 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
248 }
249 }
250
251 /*
252 * map descriptors to our copies
253 */
254
255 if ( attribute == SQL_ATTR_APP_ROW_DESC )
256 {
257 if ( value )
258 memcpy( value, &statement -> ard, sizeof( statement -> ard ));
259
260 ret = SQL_SUCCESS;
261 }
262 else if ( attribute == SQL_ATTR_APP_PARAM_DESC )
263 {
264 if ( value )
265 memcpy( value, &statement -> apd, sizeof( SQLHANDLE ));
266
267 ret = SQL_SUCCESS;
268 }
269 else if ( attribute == SQL_ATTR_IMP_ROW_DESC )
270 {
271 if ( value )
272 memcpy( value, &statement -> ird, sizeof( SQLHANDLE ));
273
274 ret = SQL_SUCCESS;
275 }
276 else if ( attribute == SQL_ATTR_IMP_PARAM_DESC )
277 {
278 if ( value )
279 memcpy( value, &statement -> ipd, sizeof( SQLHANDLE ));
280
281 ret = SQL_SUCCESS;
282 }
283
284 /*
285 * does the call need mapping from 3 to 2
286 */
287
288 else if ( attribute == SQL_ATTR_FETCH_BOOKMARK_PTR &&
289 statement -> connection -> driver_act_ver == SQL_OV_ODBC2 &&
290 CHECK_SQLEXTENDEDFETCH( statement -> connection ))
291 {
292 if ( value )
293 memcpy( value, &statement -> fetch_bm_ptr, sizeof( SQLLEN * ));
294
295 ret = SQL_SUCCESS;
296 }
297 else if ( attribute == SQL_ATTR_ROW_STATUS_PTR &&
298 statement -> connection -> driver_act_ver == SQL_OV_ODBC2 &&
299 CHECK_SQLEXTENDEDFETCH( statement -> connection ))
300 {
301 if ( value )
302 memcpy( value, &statement -> row_st_arr, sizeof( SQLLEN * ));
303
304 ret = SQL_SUCCESS;
305 }
306 else if ( attribute == SQL_ATTR_ROWS_FETCHED_PTR &&
307 statement -> connection -> driver_act_ver == SQL_OV_ODBC2 &&
308 CHECK_SQLEXTENDEDFETCH( statement -> connection ))
309 {
310 if ( value )
311 memcpy( value, &statement -> row_ct_ptr, sizeof( SQLULEN * ));
312
313 ret = SQL_SUCCESS;
314 }
315 else
316 {
317 if ( statement -> connection -> unicode_driver )
318 {
319 ret = SQLGETSTMTATTRW( statement -> connection,
320 statement -> driver_stmt,
321 attribute,
322 value,
323 buffer_length,
324 string_length );
325 }
326 else
327 {
328 /*
329 * don't know if any string attributes to convert...
330 */
331
332 ret = SQLGETSTMTATTR( statement -> connection,
333 statement -> driver_stmt,
334 attribute,
335 value,
336 buffer_length,
337 string_length );
338 }
339 }
340
341 if ( log_info.log_flag )
342 {
343 sprintf( statement -> msg,
344 "\n\t\tExit:[%s]",
345 __get_return_status( ret, s1 ));
346
347 dm_log_write( __FILE__,
348 __LINE__,
349 LOG_INFO,
350 LOG_INFO,
351 statement -> msg );
352 }
353
354 return function_return( SQL_HANDLE_STMT, statement, ret );
355}
356