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: SQLSetParam.c,v 1.7 2009/02/18 17:59:08 lurcher Exp $ |
31 | * |
32 | * $Log: SQLSetParam.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/04/26 08:40:35 lurcher |
37 | * |
38 | * Add data type mapping for SQLSetPos. |
39 | * Remove out of date macro in sqlext.h |
40 | * |
41 | * Revision 1.5 2003/10/30 18:20:46 lurcher |
42 | * |
43 | * Fix broken thread protection |
44 | * Remove SQLNumResultCols after execute, lease S4/S% to driver |
45 | * Fix string overrun in SQLDriverConnect |
46 | * Add initial support for Interix |
47 | * |
48 | * Revision 1.4 2002/12/05 17:44:31 lurcher |
49 | * |
50 | * Display unknown return values in return logging |
51 | * |
52 | * Revision 1.3 2002/08/19 09:11:49 lurcher |
53 | * |
54 | * Fix Maxor ineffiecny in Postgres Drivers, and fix a return state |
55 | * |
56 | * Revision 1.2 2001/12/13 13:00:32 lurcher |
57 | * |
58 | * Remove most if not all warnings on 64 bit platforms |
59 | * Add support for new MS 3.52 64 bit changes |
60 | * Add override to disable the stopping of tracing |
61 | * Add MAX_ROWS support in postgres driver |
62 | * |
63 | * Revision 1.1.1.1 2001/10/17 16:40:07 lurcher |
64 | * |
65 | * First upload to SourceForge |
66 | * |
67 | * Revision 1.2 2001/04/12 17:43:36 nick |
68 | * |
69 | * Change logging and added autotest to odbctest |
70 | * |
71 | * Revision 1.1.1.1 2000/09/04 16:42:52 nick |
72 | * Imported Sources |
73 | * |
74 | * Revision 1.8 1999/11/13 23:41:01 ngorham |
75 | * |
76 | * Alter the way DM logging works |
77 | * Upgrade the Postgres driver to 6.4.6 |
78 | * |
79 | * Revision 1.7 1999/10/29 21:07:40 ngorham |
80 | * |
81 | * Fix some stupid bugs in the DM |
82 | * Make the postgres driver work via unix sockets |
83 | * |
84 | * Revision 1.6 1999/10/24 23:54:19 ngorham |
85 | * |
86 | * First part of the changes to the error reporting |
87 | * |
88 | * Revision 1.5 1999/09/21 22:34:25 ngorham |
89 | * |
90 | * Improve performance by removing unneeded logging calls when logging is |
91 | * disabled |
92 | * |
93 | * Revision 1.4 1999/07/10 21:10:17 ngorham |
94 | * |
95 | * Adjust error sqlstate from driver manager, depending on requested |
96 | * version (ODBC2/3) |
97 | * |
98 | * Revision 1.3 1999/07/04 21:05:08 ngorham |
99 | * |
100 | * Add LGPL Headers to code |
101 | * |
102 | * Revision 1.2 1999/06/30 23:56:55 ngorham |
103 | * |
104 | * Add initial thread safety code |
105 | * |
106 | * Revision 1.1.1.1 1999/05/29 13:41:08 sShandyb |
107 | * first go at it |
108 | * |
109 | * Revision 1.4 1999/06/03 22:20:25 ngorham |
110 | * |
111 | * Finished off the ODBC3-2 mapping |
112 | * |
113 | * Revision 1.3 1999/06/02 20:12:10 ngorham |
114 | * |
115 | * Fixed botched log entry, and removed the dos \r from the sql header files. |
116 | * |
117 | * Revision 1.2 1999/06/02 19:57:21 ngorham |
118 | * |
119 | * Added code to check if a attempt is being made to compile with a C++ |
120 | * Compiler, and issue a message. |
121 | * Start work on the ODBC2-3 conversions. |
122 | * |
123 | * Revision 1.1.1.1 1999/05/27 18:23:18 pharvey |
124 | * Imported sources |
125 | * |
126 | * Revision 1.2 1999/05/09 23:27:11 nick |
127 | * All the API done now |
128 | * |
129 | * Revision 1.1 1999/04/25 23:06:11 nick |
130 | * Initial revision |
131 | * |
132 | * |
133 | **********************************************************************/ |
134 | |
135 | #include <config.h> |
136 | #include "drivermanager.h" |
137 | |
138 | static char const rcsid[]= "$RCSfile: SQLSetParam.c,v $ $Revision: 1.7 $" ; |
139 | |
140 | int check_value_type( int c_type, int connection_mode) |
141 | { |
142 | /* |
143 | * * driver defined types |
144 | * */ |
145 | if ( connection_mode >= SQL_OV_ODBC3_80 && c_type >= 0x4000 && c_type <= 0x7FFF ) { |
146 | return 1; |
147 | } |
148 | |
149 | switch( c_type ) { |
150 | case SQL_C_CHAR: |
151 | case SQL_C_LONG: |
152 | case SQL_C_SHORT: |
153 | case SQL_C_FLOAT: |
154 | case SQL_C_NUMERIC: |
155 | case SQL_C_DEFAULT: |
156 | case SQL_C_DATE: |
157 | case SQL_C_TIME: |
158 | case SQL_C_TIMESTAMP: |
159 | case SQL_C_TYPE_DATE: |
160 | case SQL_C_TYPE_TIME: |
161 | case SQL_C_TYPE_TIMESTAMP: |
162 | case SQL_C_INTERVAL_YEAR: |
163 | case SQL_C_INTERVAL_MONTH: |
164 | case SQL_C_INTERVAL_DAY: |
165 | case SQL_C_INTERVAL_HOUR: |
166 | case SQL_C_INTERVAL_MINUTE: |
167 | case SQL_C_INTERVAL_SECOND: |
168 | case SQL_C_INTERVAL_YEAR_TO_MONTH: |
169 | case SQL_C_INTERVAL_DAY_TO_HOUR: |
170 | case SQL_C_INTERVAL_DAY_TO_MINUTE: |
171 | case SQL_C_INTERVAL_DAY_TO_SECOND: |
172 | case SQL_C_INTERVAL_HOUR_TO_MINUTE: |
173 | case SQL_C_INTERVAL_HOUR_TO_SECOND: |
174 | case SQL_C_INTERVAL_MINUTE_TO_SECOND: |
175 | case SQL_C_BINARY: |
176 | case SQL_C_BIT: |
177 | case SQL_C_SBIGINT: |
178 | case SQL_C_UBIGINT: |
179 | case SQL_C_TINYINT: |
180 | case SQL_C_SLONG: |
181 | case SQL_C_SSHORT: |
182 | case SQL_C_STINYINT: |
183 | case SQL_C_ULONG: |
184 | case SQL_C_USHORT: |
185 | case SQL_C_UTINYINT: |
186 | case SQL_C_GUID: |
187 | case SQL_C_WCHAR: |
188 | case SQL_ARD_TYPE: |
189 | case SQL_C_DOUBLE: |
190 | /* case SQL_C_XML: still trying to find what value this is */ |
191 | return 1; |
192 | |
193 | default: |
194 | return 0; |
195 | } |
196 | } |
197 | |
198 | SQLRETURN SQLSetParam( SQLHSTMT statement_handle, |
199 | SQLUSMALLINT parameter_number, |
200 | SQLSMALLINT value_type, |
201 | SQLSMALLINT parameter_type, |
202 | SQLULEN length_precision, |
203 | SQLSMALLINT parameter_scale, |
204 | SQLPOINTER parameter_value, |
205 | SQLLEN *strlen_or_ind ) |
206 | { |
207 | DMHSTMT statement = (DMHSTMT) statement_handle; |
208 | SQLRETURN ret; |
209 | SQLCHAR s1[ 100 + LOG_MESSAGE_LEN ]; |
210 | |
211 | /* |
212 | * check statement |
213 | */ |
214 | |
215 | if ( !__validate_stmt( statement )) |
216 | { |
217 | dm_log_write( __FILE__, |
218 | __LINE__, |
219 | LOG_INFO, |
220 | LOG_INFO, |
221 | "Error: SQL_INVALID_HANDLE" ); |
222 | |
223 | return SQL_INVALID_HANDLE; |
224 | } |
225 | |
226 | function_entry( statement ); |
227 | |
228 | if ( log_info.log_flag ) |
229 | { |
230 | sprintf( statement -> msg, "\n\t\tEntry:\ |
231 | \n\t\t\tStatement = %p\ |
232 | \n\t\t\tParam Number = %d\ |
233 | \n\t\t\tValue Type = %d %s\ |
234 | \n\t\t\tParameter Type = %d %s\ |
235 | \n\t\t\tLength Precision = %d\ |
236 | \n\t\t\tParameter Scale = %d\ |
237 | \n\t\t\tParameter Value = %p\ |
238 | \n\t\t\tStrLen Or Ind = %p" , |
239 | statement, |
240 | parameter_number, |
241 | value_type, |
242 | __c_as_text( value_type ), |
243 | parameter_type, |
244 | __sql_as_text( parameter_type ), |
245 | (int)length_precision, |
246 | (int)parameter_scale, |
247 | (void*)parameter_value, |
248 | (void*)strlen_or_ind ); |
249 | |
250 | dm_log_write( __FILE__, |
251 | __LINE__, |
252 | LOG_INFO, |
253 | LOG_INFO, |
254 | statement -> msg ); |
255 | } |
256 | |
257 | thread_protect( SQL_HANDLE_STMT, statement ); |
258 | |
259 | if ( parameter_number < 1 ) |
260 | { |
261 | dm_log_write( __FILE__, |
262 | __LINE__, |
263 | LOG_INFO, |
264 | LOG_INFO, |
265 | "Error: 07009" ); |
266 | |
267 | __post_internal_error_api( &statement -> error, |
268 | ERROR_07009, NULL, |
269 | statement -> connection -> environment -> requested_version, |
270 | SQL_API_SQLSETPARAM ); |
271 | |
272 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
273 | } |
274 | |
275 | if (!check_value_type( value_type, statement -> connection -> environment -> requested_version )) |
276 | { |
277 | dm_log_write( __FILE__, |
278 | __LINE__, |
279 | LOG_INFO, |
280 | LOG_INFO, |
281 | "Error: HY003" ); |
282 | |
283 | __post_internal_error_api( &statement -> error, |
284 | ERROR_HY003, NULL, |
285 | statement -> connection -> environment -> requested_version, |
286 | SQL_API_SQLSETPARAM ); |
287 | |
288 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
289 | } |
290 | |
291 | if ( parameter_value == NULL && strlen_or_ind == NULL && value_type != SQL_PARAM_OUTPUT ) |
292 | { |
293 | dm_log_write( __FILE__, |
294 | __LINE__, |
295 | LOG_INFO, |
296 | LOG_INFO, |
297 | "Error: HY009" ); |
298 | |
299 | __post_internal_error_api( &statement -> error, |
300 | ERROR_HY009, NULL, |
301 | statement -> connection -> environment -> requested_version, |
302 | SQL_API_SQLSETPARAM ); |
303 | |
304 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
305 | } |
306 | |
307 | /* |
308 | * check states |
309 | */ |
310 | |
311 | if ( statement -> state == STATE_S8 || |
312 | statement -> state == STATE_S9 || |
313 | statement -> state == STATE_S10 || |
314 | statement -> state == STATE_S11 || |
315 | statement -> state == STATE_S12 || |
316 | statement -> state == STATE_S13 || |
317 | statement -> state == STATE_S14 || |
318 | statement -> state == STATE_S15 ) |
319 | { |
320 | dm_log_write( __FILE__, |
321 | __LINE__, |
322 | LOG_INFO, |
323 | LOG_INFO, |
324 | "Error: HY010" ); |
325 | |
326 | __post_internal_error( &statement -> error, |
327 | ERROR_HY010, NULL, |
328 | statement -> connection -> environment -> requested_version ); |
329 | |
330 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
331 | } |
332 | |
333 | if ( CHECK_SQLSETPARAM( statement -> connection )) |
334 | { |
335 | ret = SQLSETPARAM( statement -> connection, |
336 | statement -> driver_stmt, |
337 | parameter_number, |
338 | __map_type(MAP_C_DM2D,statement->connection,value_type), |
339 | __map_type(MAP_SQL_DM2D,statement->connection,parameter_type), |
340 | length_precision, |
341 | parameter_scale, |
342 | parameter_value, |
343 | strlen_or_ind ); |
344 | } |
345 | else if ( CHECK_SQLBINDPARAMETER( statement -> connection )) |
346 | { |
347 | ret = SQLBINDPARAMETER( statement -> connection, |
348 | statement -> driver_stmt, |
349 | parameter_number, |
350 | SQL_PARAM_INPUT_OUTPUT, |
351 | __map_type(MAP_C_DM2D,statement->connection,value_type), |
352 | __map_type(MAP_SQL_DM2D,statement->connection,parameter_type), |
353 | length_precision, |
354 | parameter_scale, |
355 | parameter_value, |
356 | SQL_SETPARAM_VALUE_MAX, |
357 | strlen_or_ind ); |
358 | } |
359 | else if ( CHECK_SQLBINDPARAM( statement -> connection )) |
360 | { |
361 | ret = SQLBINDPARAM( statement -> connection, |
362 | statement -> driver_stmt, |
363 | parameter_number, |
364 | __map_type(MAP_C_DM2D,statement->connection,value_type), |
365 | __map_type(MAP_SQL_DM2D,statement->connection,parameter_type), |
366 | length_precision, |
367 | parameter_scale, |
368 | parameter_value, |
369 | strlen_or_ind ); |
370 | } |
371 | else |
372 | { |
373 | dm_log_write( __FILE__, |
374 | __LINE__, |
375 | LOG_INFO, |
376 | LOG_INFO, |
377 | "Error: IM001" ); |
378 | |
379 | __post_internal_error( &statement -> error, |
380 | ERROR_IM001, NULL, |
381 | statement -> connection -> environment -> requested_version ); |
382 | |
383 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
384 | } |
385 | |
386 | if ( log_info.log_flag ) |
387 | { |
388 | sprintf( statement -> msg, |
389 | "\n\t\tExit:[%s]" , |
390 | __get_return_status( ret, s1 )); |
391 | |
392 | dm_log_write( __FILE__, |
393 | __LINE__, |
394 | LOG_INFO, |
395 | LOG_INFO, |
396 | statement -> msg ); |
397 | } |
398 | |
399 | return function_return( SQL_HANDLE_STMT, statement, ret ); |
400 | } |
401 | |