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: SQLBindParam.c,v 1.9 2009/02/18 17:59:08 lurcher Exp $
31 *
32 * $Log: SQLBindParam.c,v $
33 * Revision 1.9 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.8 2007/03/05 09:49:23 lurcher
37 * Get it to build on VMS again
38 *
39 * Revision 1.7 2006/04/11 10:22:56 lurcher
40 * Fix a data type check
41 *
42 * Revision 1.6 2006/03/08 11:22:13 lurcher
43 * Add check for valid C_TYPE
44 *
45 * Revision 1.5 2003/10/30 18:20:45 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.4 2002/12/05 17:44:30 lurcher
53 *
54 * Display unknown return values in return logging
55 *
56 * Revision 1.3 2002/08/19 09:11:49 lurcher
57 *
58 * Fix Maxor ineffiecny in Postgres Drivers, and fix a return state
59 *
60 * Revision 1.2 2001/12/13 13:00:31 lurcher
61 *
62 * Remove most if not all warnings on 64 bit platforms
63 * Add support for new MS 3.52 64 bit changes
64 * Add override to disable the stopping of tracing
65 * Add MAX_ROWS support in postgres driver
66 *
67 * Revision 1.1.1.1 2001/10/17 16:40:05 lurcher
68 *
69 * First upload to SourceForge
70 *
71 * Revision 1.2 2001/04/12 17:43:35 nick
72 *
73 * Change logging and added autotest to odbctest
74 *
75 * Revision 1.1.1.1 2000/09/04 16:42:52 nick
76 * Imported Sources
77 *
78 * Revision 1.9 1999/11/13 23:40:58 ngorham
79 *
80 * Alter the way DM logging works
81 * Upgrade the Postgres driver to 6.4.6
82 *
83 * Revision 1.8 1999/10/24 23:54:17 ngorham
84 *
85 * First part of the changes to the error reporting
86 *
87 * Revision 1.7 1999/10/09 00:56:16 ngorham
88 *
89 * Added Manush's patch to map ODBC 3-2 datetime values
90 *
91 * Revision 1.6 1999/10/09 00:15:58 ngorham
92 *
93 * Add mapping from SQL_TYPE_X to SQL_X and SQL_C_TYPE_X to SQL_C_X
94 * when the driver is a ODBC 2 one
95 *
96 * Revision 1.5 1999/09/21 22:34:24 ngorham
97 *
98 * Improve performance by removing unneeded logging calls when logging is
99 * disabled
100 *
101 * Revision 1.4 1999/07/10 21:10:15 ngorham
102 *
103 * Adjust error sqlstate from driver manager, depending on requested
104 * version (ODBC2/3)
105 *
106 * Revision 1.3 1999/07/04 21:05:06 ngorham
107 *
108 * Add LGPL Headers to code
109 *
110 * Revision 1.2 1999/06/30 23:56:54 ngorham
111 *
112 * Add initial thread safety code
113 *
114 * Revision 1.1.1.1 1999/05/29 13:41:05 sShandyb
115 * first go at it
116 *
117 * Revision 1.1.1.1 1999/05/27 18:23:17 pharvey
118 * Imported sources
119 *
120 * Revision 1.3 1999/05/09 23:27:11 nick
121 * All the API done now
122 *
123 * Revision 1.2 1999/05/03 19:50:43 nick
124 * Another check point
125 *
126 * Revision 1.1 1999/04/25 23:02:41 nick
127 * Initial revision
128 *
129 *
130 **********************************************************************/
131
132#include <config.h>
133#include "drivermanager.h"
134
135static char const rcsid[]= "$RCSfile: SQLBindParam.c,v $ $Revision: 1.9 $";
136
137SQLRETURN SQLBindParam( SQLHSTMT statement_handle,
138 SQLUSMALLINT parameter_number,
139 SQLSMALLINT value_type,
140 SQLSMALLINT parameter_type,
141 SQLULEN length_precision,
142 SQLSMALLINT parameter_scale,
143 SQLPOINTER parameter_value,
144 SQLLEN *strlen_or_ind)
145{
146 DMHSTMT statement = (DMHSTMT) statement_handle;
147 SQLRETURN ret;
148
149 /*
150 * check statement
151 */
152
153 if ( !__validate_stmt( statement ))
154 {
155 dm_log_write( __FILE__,
156 __LINE__,
157 LOG_INFO,
158 LOG_INFO,
159 "Error: SQL_INVALID_HANDLE" );
160
161 return SQL_INVALID_HANDLE;
162 }
163
164 function_entry( statement );
165
166 if ( log_info.log_flag )
167 {
168 sprintf( statement -> msg, "\n\t\tEntry:\
169\n\t\t\tStatement = %p\
170\n\t\t\tParam Number = %d\
171\n\t\t\tValue Type = %d %s\
172\n\t\t\tParameter Type = %d %s\
173\n\t\t\tLength Precision = %d\
174\n\t\t\tParameter Scale = %d\
175\n\t\t\tParameter Value = %p\
176\n\t\t\tStrLen Or Ind = %p",
177 statement,
178 parameter_number,
179 value_type,
180 __c_as_text( value_type ),
181 parameter_type,
182 __sql_as_text( parameter_type ),
183 (int)length_precision,
184 (int)parameter_scale,
185 (void*)parameter_value,
186 (void*)strlen_or_ind );
187
188 dm_log_write( __FILE__,
189 __LINE__,
190 LOG_INFO,
191 LOG_INFO,
192 statement -> msg );
193 }
194
195 thread_protect( SQL_HANDLE_STMT, statement );
196
197 if ( parameter_number < 1 )
198 {
199 dm_log_write( __FILE__,
200 __LINE__,
201 LOG_INFO,
202 LOG_INFO,
203 "Error: 07009" );
204
205 __post_internal_error_api( &statement -> error,
206 ERROR_07009, NULL,
207 statement -> connection -> environment -> requested_version,
208 SQL_API_SQLBINDPARAM );
209
210 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
211 }
212
213 if ( parameter_value == NULL &&
214 strlen_or_ind == NULL )
215 {
216 dm_log_write( __FILE__,
217 __LINE__,
218 LOG_INFO,
219 LOG_INFO,
220 "Error: HY009" );
221
222 __post_internal_error( &statement -> error,
223 ERROR_HY009, NULL,
224 statement -> connection -> environment -> requested_version );
225
226 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
227 }
228
229 /*
230 * check states
231 */
232
233 if ( statement -> state == STATE_S8 ||
234 statement -> state == STATE_S9 ||
235 statement -> state == STATE_S10 ||
236 statement -> state == STATE_S11 ||
237 statement -> state == STATE_S12 ||
238 statement -> state == STATE_S13 ||
239 statement -> state == STATE_S14 ||
240 statement -> state == STATE_S15 )
241 {
242 dm_log_write( __FILE__,
243 __LINE__,
244 LOG_INFO,
245 LOG_INFO,
246 "Error: HY010" );
247
248 __post_internal_error( &statement -> error,
249 ERROR_HY010, NULL,
250 statement -> connection -> environment -> requested_version );
251
252 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
253 }
254
255 /*
256 * check valid C_TYPE
257 */
258
259 if ( !check_target_type( value_type, statement -> connection -> environment -> requested_version ))
260 {
261 dm_log_write( __FILE__,
262 __LINE__,
263 LOG_INFO,
264 LOG_INFO,
265 "Error: HY003" );
266
267 __post_internal_error( &statement -> error,
268 ERROR_HY003, NULL,
269 statement -> connection -> environment -> requested_version );
270
271 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
272 }
273
274 if ( CHECK_SQLBINDPARAM( statement -> connection ))
275 {
276 ret = SQLBINDPARAM( statement -> connection,
277 statement -> driver_stmt,
278 parameter_number,
279 __map_type(MAP_C_DM2D,statement->connection,value_type),
280 __map_type(MAP_SQL_DM2D,statement->connection,parameter_type),
281 length_precision,
282 parameter_scale,
283 parameter_value,
284 strlen_or_ind );
285 }
286 else
287 {
288 /*
289 * map to odbc 3 operation
290 */
291
292 if ( !CHECK_SQLBINDPARAMETER( statement -> connection ))
293 {
294 dm_log_write( __FILE__,
295 __LINE__,
296 LOG_INFO,
297 LOG_INFO,
298 "Error: IM001" );
299
300 __post_internal_error( &statement -> error,
301 ERROR_IM001, NULL,
302 statement -> connection -> environment -> requested_version );
303
304 return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR );
305 }
306
307 /*
308 * this probably needs to work out the
309 * buffer length
310 */
311
312 ret = SQLBINDPARAMETER( statement -> connection,
313 statement -> driver_stmt,
314 parameter_number,
315 SQL_PARAM_INPUT,
316 __map_type(MAP_C_DM2D,statement->connection,value_type),
317 __map_type(MAP_SQL_DM2D,statement->connection,parameter_type),
318 length_precision,
319 parameter_scale,
320 parameter_value,
321 0,
322 strlen_or_ind );
323 }
324
325 if ( log_info.log_flag )
326 {
327 SQLCHAR buf[ 128 ];
328
329 sprintf( statement -> msg,
330 "\n\t\tExit:[%s]",
331 __get_return_status( ret, buf ));
332
333 dm_log_write( __FILE__,
334 __LINE__,
335 LOG_INFO,
336 LOG_INFO,
337 statement -> msg );
338 }
339
340 return function_return( SQL_HANDLE_STMT, statement, ret );
341}
342