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: SQLSetDescFieldW.c,v 1.8 2009/02/18 17:59:08 lurcher Exp $ |
31 | * |
32 | * $Log: SQLSetDescFieldW.c,v $ |
33 | * Revision 1.8 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.7 2008/08/29 08:01:39 lurcher |
37 | * Alter the way W functions are passed to the driver |
38 | * |
39 | * Revision 1.6 2007/03/05 09:49:24 lurcher |
40 | * Get it to build on VMS again |
41 | * |
42 | * Revision 1.5 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.4 2006/04/18 10:24:47 lurcher |
46 | * Add a couple of changes from Mark Vanderwiel |
47 | * |
48 | * Revision 1.3 2003/10/30 18:20:46 lurcher |
49 | * |
50 | * Fix broken thread protection |
51 | * Remove SQLNumResultCols after execute, lease S4/S% to driver |
52 | * Fix string overrun in SQLDriverConnect |
53 | * Add initial support for Interix |
54 | * |
55 | * Revision 1.2 2002/12/05 17:44:31 lurcher |
56 | * |
57 | * Display unknown return values in return logging |
58 | * |
59 | * Revision 1.1.1.1 2001/10/17 16:40:07 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/17 16:29:39 nick |
68 | * |
69 | * More checks and autotest fixes |
70 | * |
71 | * Revision 1.2 2001/04/12 17:43:36 nick |
72 | * |
73 | * Change logging and added autotest to odbctest |
74 | * |
75 | * Revision 1.1 2000/12/31 20:30:54 nick |
76 | * |
77 | * Add UNICODE support |
78 | * |
79 | * |
80 | * |
81 | **********************************************************************/ |
82 | |
83 | #include <config.h> |
84 | #include "drivermanager.h" |
85 | |
86 | static char const rcsid[]= "$RCSfile: SQLSetDescFieldW.c,v $" ; |
87 | |
88 | SQLRETURN SQLSetDescFieldW( SQLHDESC descriptor_handle, |
89 | SQLSMALLINT rec_number, |
90 | SQLSMALLINT field_identifier, |
91 | SQLPOINTER value, |
92 | SQLINTEGER buffer_length ) |
93 | { |
94 | /* |
95 | * not quite sure how the descriptor can be |
96 | * allocated to a statement, all the documentation talks |
97 | * about state transitions on statement states, but the |
98 | * descriptor may be allocated with more than one statement |
99 | * at one time. Which one should I check ? |
100 | */ |
101 | DMHDESC descriptor = (DMHDESC) descriptor_handle; |
102 | SQLRETURN ret; |
103 | SQLCHAR s1[ 100 + LOG_MESSAGE_LEN ]; |
104 | int isStrField = 0; |
105 | |
106 | /* |
107 | * check descriptor |
108 | */ |
109 | |
110 | if ( !__validate_desc( descriptor )) |
111 | { |
112 | dm_log_write( __FILE__, |
113 | __LINE__, |
114 | LOG_INFO, |
115 | LOG_INFO, |
116 | "Error: SQL_INVALID_HANDLE" ); |
117 | |
118 | #ifdef WITH_HANDLE_REDIRECT |
119 | { |
120 | DMHDESC parent_desc; |
121 | |
122 | parent_desc = find_parent_handle( descriptor, SQL_HANDLE_DESC ); |
123 | |
124 | if ( parent_desc ) { |
125 | dm_log_write( __FILE__, |
126 | __LINE__, |
127 | LOG_INFO, |
128 | LOG_INFO, |
129 | "Info: found parent handle" ); |
130 | |
131 | if ( CHECK_SQLSETDESCFIELDW( parent_desc -> connection )) |
132 | { |
133 | dm_log_write( __FILE__, |
134 | __LINE__, |
135 | LOG_INFO, |
136 | LOG_INFO, |
137 | "Info: calling redirected driver function" ); |
138 | |
139 | return SQLSETDESCFIELDW( parent_desc -> connection, |
140 | descriptor, |
141 | rec_number, |
142 | field_identifier, |
143 | value, |
144 | buffer_length ); |
145 | } |
146 | } |
147 | } |
148 | #endif |
149 | return SQL_INVALID_HANDLE; |
150 | } |
151 | |
152 | function_entry( descriptor ); |
153 | |
154 | if ( log_info.log_flag ) |
155 | { |
156 | sprintf( descriptor -> msg, "\n\t\tEntry:\ |
157 | \n\t\t\tDescriptor = %p\ |
158 | \n\t\t\tRec Number = %d\ |
159 | \n\t\t\tField Ident = %s\ |
160 | \n\t\t\tValue = %p\ |
161 | \n\t\t\tBuffer Length = %d" , |
162 | descriptor, |
163 | rec_number, |
164 | __desc_attr_as_string( s1, field_identifier ), |
165 | value, |
166 | (int)buffer_length ); |
167 | |
168 | dm_log_write( __FILE__, |
169 | __LINE__, |
170 | LOG_INFO, |
171 | LOG_INFO, |
172 | descriptor -> msg ); |
173 | } |
174 | |
175 | thread_protect( SQL_HANDLE_DESC, descriptor ); |
176 | |
177 | if ( descriptor -> connection -> state < STATE_C4 ) |
178 | { |
179 | dm_log_write( __FILE__, |
180 | __LINE__, |
181 | LOG_INFO, |
182 | LOG_INFO, |
183 | "Error: HY010" ); |
184 | |
185 | __post_internal_error( &descriptor -> error, |
186 | ERROR_HY010, NULL, |
187 | descriptor -> connection -> environment -> requested_version ); |
188 | |
189 | return function_return_nodrv( SQL_HANDLE_DESC, descriptor, SQL_ERROR ); |
190 | } |
191 | |
192 | /* |
193 | * check status of statements associated with this descriptor |
194 | */ |
195 | |
196 | if( __check_stmt_from_desc( descriptor, STATE_S8 ) || |
197 | __check_stmt_from_desc( descriptor, STATE_S9 ) || |
198 | __check_stmt_from_desc( descriptor, STATE_S10 ) || |
199 | __check_stmt_from_desc( descriptor, STATE_S11 ) || |
200 | __check_stmt_from_desc( descriptor, STATE_S12 ) || |
201 | __check_stmt_from_desc( descriptor, STATE_S13 ) || |
202 | __check_stmt_from_desc( descriptor, STATE_S14 ) || |
203 | __check_stmt_from_desc( descriptor, STATE_S15 )) { |
204 | |
205 | dm_log_write( __FILE__, |
206 | __LINE__, |
207 | LOG_INFO, |
208 | LOG_INFO, |
209 | "Error: HY010" ); |
210 | |
211 | __post_internal_error( &descriptor -> error, |
212 | ERROR_HY010, NULL, |
213 | descriptor -> connection -> environment -> requested_version ); |
214 | |
215 | return function_return_nodrv( SQL_HANDLE_DESC, descriptor, SQL_ERROR ); |
216 | } |
217 | |
218 | if ( rec_number < 0 ) |
219 | { |
220 | __post_internal_error( &descriptor -> error, |
221 | ERROR_07009, NULL, |
222 | descriptor -> connection -> environment -> requested_version ); |
223 | |
224 | return function_return_nodrv( SQL_HANDLE_DESC, descriptor, SQL_ERROR ); |
225 | } |
226 | |
227 | switch ( field_identifier ) |
228 | { |
229 | /* Fixed-length fields: buffer_length is ignored */ |
230 | case SQL_DESC_ALLOC_TYPE: |
231 | case SQL_DESC_ARRAY_SIZE: |
232 | case SQL_DESC_ARRAY_STATUS_PTR: |
233 | case SQL_DESC_BIND_OFFSET_PTR: |
234 | case SQL_DESC_BIND_TYPE: |
235 | case SQL_DESC_COUNT: |
236 | case SQL_DESC_ROWS_PROCESSED_PTR: |
237 | case SQL_DESC_AUTO_UNIQUE_VALUE: |
238 | case SQL_DESC_CASE_SENSITIVE: |
239 | case SQL_DESC_CONCISE_TYPE: |
240 | case SQL_DESC_DATA_PTR: |
241 | case SQL_DESC_DATETIME_INTERVAL_CODE: |
242 | case SQL_DESC_DATETIME_INTERVAL_PRECISION: |
243 | case SQL_DESC_DISPLAY_SIZE: |
244 | case SQL_DESC_FIXED_PREC_SCALE: |
245 | case SQL_DESC_INDICATOR_PTR: |
246 | case SQL_DESC_LENGTH: |
247 | case SQL_DESC_NULLABLE: |
248 | case SQL_DESC_NUM_PREC_RADIX: |
249 | case SQL_DESC_OCTET_LENGTH: |
250 | case SQL_DESC_OCTET_LENGTH_PTR: |
251 | case SQL_DESC_PARAMETER_TYPE: |
252 | case SQL_DESC_PRECISION: |
253 | case SQL_DESC_ROWVER: |
254 | case SQL_DESC_SCALE: |
255 | case SQL_DESC_SEARCHABLE: |
256 | case SQL_DESC_TYPE: |
257 | case SQL_DESC_UNNAMED: |
258 | case SQL_DESC_UNSIGNED: |
259 | case SQL_DESC_UPDATABLE: |
260 | isStrField = 0; |
261 | break; |
262 | /* Pointer to data: buffer_length must be valid */ |
263 | case SQL_DESC_BASE_COLUMN_NAME: |
264 | case SQL_DESC_BASE_TABLE_NAME: |
265 | case SQL_DESC_CATALOG_NAME: |
266 | case SQL_DESC_LABEL: |
267 | case SQL_DESC_LITERAL_PREFIX: |
268 | case SQL_DESC_LITERAL_SUFFIX: |
269 | case SQL_DESC_LOCAL_TYPE_NAME: |
270 | case SQL_DESC_NAME: |
271 | case SQL_DESC_SCHEMA_NAME: |
272 | case SQL_DESC_TABLE_NAME: |
273 | case SQL_DESC_TYPE_NAME: |
274 | isStrField = 1; |
275 | break; |
276 | default: |
277 | isStrField = buffer_length != SQL_IS_POINTER && buffer_length != SQL_IS_INTEGER |
278 | && buffer_length != SQL_IS_UINTEGER && buffer_length != SQL_IS_SMALLINT && |
279 | buffer_length != SQL_IS_USMALLINT; |
280 | } |
281 | |
282 | if ( isStrField && buffer_length < 0 && buffer_length != SQL_NTS) |
283 | { |
284 | __post_internal_error( &descriptor -> error, |
285 | ERROR_HY090, NULL, |
286 | descriptor -> connection -> environment -> requested_version ); |
287 | |
288 | return function_return_nodrv( SQL_HANDLE_DESC, descriptor, SQL_ERROR ); |
289 | } |
290 | |
291 | if ( field_identifier == SQL_DESC_COUNT && (intptr_t)value < 0 ) |
292 | { |
293 | __post_internal_error( &descriptor -> error, |
294 | ERROR_07009, NULL, |
295 | descriptor -> connection -> environment -> requested_version ); |
296 | |
297 | return function_return_nodrv( SQL_HANDLE_DESC, descriptor, SQL_ERROR ); |
298 | } |
299 | |
300 | if ( field_identifier == SQL_DESC_PARAMETER_TYPE && (intptr_t)value != SQL_PARAM_INPUT |
301 | && (intptr_t)value != SQL_PARAM_OUTPUT && (intptr_t)value != SQL_PARAM_INPUT_OUTPUT && |
302 | (intptr_t)value != SQL_PARAM_INPUT_OUTPUT_STREAM && (intptr_t)value != SQL_PARAM_OUTPUT_STREAM ) |
303 | { |
304 | __post_internal_error( &descriptor -> error, |
305 | ERROR_HY105, NULL, |
306 | descriptor -> connection -> environment -> requested_version ); |
307 | |
308 | return function_return_nodrv( SQL_HANDLE_DESC, descriptor, SQL_ERROR ); |
309 | } |
310 | |
311 | if ( descriptor -> connection -> unicode_driver || |
312 | CHECK_SQLSETDESCFIELDW( descriptor -> connection )) |
313 | { |
314 | if ( !CHECK_SQLSETDESCFIELDW( descriptor -> connection )) |
315 | { |
316 | dm_log_write( __FILE__, |
317 | __LINE__, |
318 | LOG_INFO, |
319 | LOG_INFO, |
320 | "Error: IM001" ); |
321 | |
322 | __post_internal_error( &descriptor -> error, |
323 | ERROR_IM001, NULL, |
324 | descriptor -> connection -> environment -> requested_version ); |
325 | |
326 | return function_return_nodrv( SQL_HANDLE_DESC, descriptor, SQL_ERROR ); |
327 | } |
328 | |
329 | ret = SQLSETDESCFIELDW( descriptor -> connection, |
330 | descriptor -> driver_desc, |
331 | rec_number, |
332 | field_identifier, |
333 | value, |
334 | buffer_length ); |
335 | |
336 | if ( log_info.log_flag ) |
337 | { |
338 | sprintf( descriptor -> msg, |
339 | "\n\t\tExit:[%s]" , |
340 | __get_return_status( ret, s1 )); |
341 | |
342 | dm_log_write( __FILE__, |
343 | __LINE__, |
344 | LOG_INFO, |
345 | LOG_INFO, |
346 | descriptor -> msg ); |
347 | } |
348 | } |
349 | else |
350 | { |
351 | SQLCHAR *ascii_str = NULL; |
352 | |
353 | if ( !CHECK_SQLSETDESCFIELD( descriptor -> connection )) |
354 | { |
355 | dm_log_write( __FILE__, |
356 | __LINE__, |
357 | LOG_INFO, |
358 | LOG_INFO, |
359 | "Error: IM001" ); |
360 | |
361 | __post_internal_error( &descriptor -> error, |
362 | ERROR_IM001, NULL, |
363 | descriptor -> connection -> environment -> requested_version ); |
364 | |
365 | return function_return_nodrv( SQL_HANDLE_DESC, descriptor, SQL_ERROR ); |
366 | } |
367 | |
368 | /* |
369 | * is it a char arg... |
370 | */ |
371 | |
372 | switch ( field_identifier ) |
373 | { |
374 | case SQL_DESC_NAME: /* This is the only R/W SQLCHAR* type */ |
375 | ascii_str = (SQLCHAR*) unicode_to_ansi_alloc( value, buffer_length, descriptor -> connection, NULL ); |
376 | value = ascii_str; |
377 | buffer_length = strlen((char*) ascii_str ); |
378 | break; |
379 | |
380 | default: |
381 | break; |
382 | } |
383 | |
384 | ret = SQLSETDESCFIELD( descriptor -> connection, |
385 | descriptor -> driver_desc, |
386 | rec_number, |
387 | field_identifier, |
388 | value, |
389 | buffer_length ); |
390 | |
391 | if ( log_info.log_flag ) |
392 | { |
393 | sprintf( descriptor -> msg, |
394 | "\n\t\tExit:[%s]" , |
395 | __get_return_status( ret, s1 )); |
396 | |
397 | dm_log_write( __FILE__, |
398 | __LINE__, |
399 | LOG_INFO, |
400 | LOG_INFO, |
401 | descriptor -> msg ); |
402 | } |
403 | |
404 | if ( ascii_str ) |
405 | { |
406 | free( ascii_str ); |
407 | } |
408 | } |
409 | |
410 | return function_return( SQL_HANDLE_DESC, descriptor, ret ); |
411 | } |
412 | |