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