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: SQLDataSources.c,v 1.9 2009/02/18 17:59:08 lurcher Exp $
31 *
32 * $Log: SQLDataSources.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 2008/06/30 08:40:48 lurcher
37 * Few more tweeks towards a release
38 *
39 * Revision 1.7 2003/10/30 18:20:45 lurcher
40 *
41 * Fix broken thread protection
42 * Remove SQLNumResultCols after execute, lease S4/S% to driver
43 * Fix string overrun in SQLDriverConnect
44 * Add initial support for Interix
45 *
46 * Revision 1.6 2003/04/10 13:45:51 lurcher
47 *
48 * Alter the way that SQLDataSources returns the description field (again)
49 *
50 * Revision 1.5 2003/02/27 12:19:39 lurcher
51 *
52 * Add the A functions as well as the W
53 *
54 * Revision 1.4 2002/12/05 17:44:30 lurcher
55 *
56 * Display unknown return values in return logging
57 *
58 * Revision 1.3 2002/07/08 11:40:35 lurcher
59 *
60 * Merge two config tests
61 *
62 * Revision 1.2 2001/12/13 13:00:32 lurcher
63 *
64 * Remove most if not all warnings on 64 bit platforms
65 * Add support for new MS 3.52 64 bit changes
66 * Add override to disable the stopping of tracing
67 * Add MAX_ROWS support in postgres driver
68 *
69 * Revision 1.1.1.1 2001/10/17 16:40:05 lurcher
70 *
71 * First upload to SourceForge
72 *
73 * Revision 1.2 2001/04/12 17:43:36 nick
74 *
75 * Change logging and added autotest to odbctest
76 *
77 * Revision 1.1.1.1 2000/09/04 16:42:52 nick
78 * Imported Sources
79 *
80 * Revision 1.10 2000/08/10 15:12:17 ngorham
81 *
82 * Fix incorrect return from SQLDataSources
83 *
84 * Revision 1.9 2000/05/04 15:08:29 ngorham
85 *
86 * Update SQLDataSource.c
87 *
88 * Revision 1.8 2000/05/04 12:57:03 ngorham
89 *
90 * Fix problem in SQLDataSource, the description is from the Driver not the
91 * DSN
92 *
93 * Revision 1.7 1999/11/13 23:40:59 ngorham
94 *
95 * Alter the way DM logging works
96 * Upgrade the Postgres driver to 6.4.6
97 *
98 * Revision 1.6 1999/10/24 23:54:17 ngorham
99 *
100 * First part of the changes to the error reporting
101 *
102 * Revision 1.5 1999/09/21 22:34:24 ngorham
103 *
104 * Improve performance by removing unneeded logging calls when logging is
105 * disabled
106 *
107 * Revision 1.4 1999/07/10 21:10:16 ngorham
108 *
109 * Adjust error sqlstate from driver manager, depending on requested
110 * version (ODBC2/3)
111 *
112 * Revision 1.3 1999/07/04 21:05:07 ngorham
113 *
114 * Add LGPL Headers to code
115 *
116 * Revision 1.2 1999/06/30 23:56:54 ngorham
117 *
118 * Add initial thread safety code
119 *
120 * Revision 1.1.1.1 1999/05/29 13:41:05 sShandyb
121 * first go at it
122 *
123 * Revision 1.1.1.1 1999/05/27 18:23:17 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
138static char const rcsid[]= "$RCSfile: SQLDataSources.c,v $ $Revision: 1.9 $";
139
140#define BUFFERSIZE 1024*4
141
142SQLRETURN SQLDataSourcesA( SQLHENV environment_handle,
143 SQLUSMALLINT direction,
144 SQLCHAR *server_name,
145 SQLSMALLINT buffer_length1,
146 SQLSMALLINT *name_length1,
147 SQLCHAR *description,
148 SQLSMALLINT buffer_length2,
149 SQLSMALLINT *name_length2 )
150{
151 return SQLDataSources( environment_handle,
152 direction,
153 server_name,
154 buffer_length1,
155 name_length1,
156 description,
157 buffer_length2,
158 name_length2 );
159}
160
161SQLRETURN SQLDataSources( SQLHENV environment_handle,
162 SQLUSMALLINT direction,
163 SQLCHAR *server_name,
164 SQLSMALLINT buffer_length1,
165 SQLSMALLINT *name_length1,
166 SQLCHAR *description,
167 SQLSMALLINT buffer_length2,
168 SQLSMALLINT *name_length2 )
169{
170 DMHENV environment = (DMHENV) environment_handle;
171 SQLRETURN ret;
172 char buffer[ BUFFERSIZE + 1 ];
173 char object[ INI_MAX_OBJECT_NAME + 1 ];
174 char property[ INI_MAX_PROPERTY_VALUE + 1 ];
175 char driver[ INI_MAX_PROPERTY_VALUE + 1 ];
176 SQLCHAR s1[ 100 + LOG_MESSAGE_LEN ];
177
178 if ( !__validate_env( environment ))
179 {
180 dm_log_write( __FILE__,
181 __LINE__,
182 LOG_INFO,
183 LOG_INFO,
184 "Error: SQL_INVALID_HANDLE" );
185
186 return SQL_INVALID_HANDLE;
187 }
188
189 function_entry( environment );
190
191 if ( log_info.log_flag )
192 {
193 sprintf( environment -> msg, "\n\t\tEntry:\
194\n\t\t\tEnvironment = %p",
195 environment );
196
197 dm_log_write( __FILE__,
198 __LINE__,
199 LOG_INFO,
200 LOG_INFO,
201 environment -> msg );
202 }
203
204 thread_protect( SQL_HANDLE_ENV, environment );
205
206 /*
207 * check that a version has been requested
208 */
209
210 if ( environment -> requested_version == 0 )
211 {
212 dm_log_write( __FILE__,
213 __LINE__,
214 LOG_INFO,
215 LOG_INFO,
216 "Error: HY010" );
217
218 __post_internal_error( &environment -> error,
219 ERROR_HY010, NULL,
220 environment -> requested_version );
221
222 return function_return_nodrv( SQL_HANDLE_ENV, environment, SQL_ERROR );
223 }
224
225 if ( buffer_length1 < 0 || buffer_length2 < 0 )
226 {
227 dm_log_write( __FILE__,
228 __LINE__,
229 LOG_INFO,
230 LOG_INFO,
231 "Error: HY090" );
232
233 __post_internal_error( &environment -> error,
234 ERROR_HY090, NULL,
235 environment -> requested_version );
236
237 return function_return_nodrv( SQL_HANDLE_ENV, environment, SQL_ERROR );
238 }
239
240 if ( direction != SQL_FETCH_FIRST &&
241 direction != SQL_FETCH_FIRST_USER &&
242 direction != SQL_FETCH_FIRST_SYSTEM &&
243 direction != SQL_FETCH_NEXT )
244 {
245 dm_log_write( __FILE__,
246 __LINE__,
247 LOG_INFO,
248 LOG_INFO,
249 "Error: HY103" );
250
251 __post_internal_error( &environment -> error,
252 ERROR_HY103, NULL,
253 environment -> requested_version );
254
255 return function_return_nodrv( SQL_HANDLE_ENV, environment, SQL_ERROR );
256 }
257
258 /*
259 * for this function USER = "~/.odbc.ini" and
260 * SYSTEM = "/usr/odbc.ini
261 */
262
263 if ( direction == SQL_FETCH_FIRST )
264 {
265 environment -> fetch_mode = ODBC_BOTH_DSN;
266 environment -> entry = 0;
267 }
268 else if ( direction == SQL_FETCH_FIRST_USER )
269 {
270 environment -> fetch_mode = ODBC_USER_DSN;
271 environment -> entry = 0;
272 }
273 else if ( direction == SQL_FETCH_FIRST_SYSTEM )
274 {
275 environment -> fetch_mode = ODBC_SYSTEM_DSN;
276 environment -> entry = 0;
277 }
278
279 /*
280 * this is lifted from Peters code
281 */
282
283 memset( buffer, 0, sizeof( buffer ));
284 memset( object, 0, sizeof( object ));
285 SQLSetConfigMode( environment -> fetch_mode );
286
287 SQLGetPrivateProfileString( NULL, NULL, NULL,
288 buffer, sizeof( buffer ),
289 "ODBC.INI" );
290
291 if ( iniElement( buffer, '\0', '\0',
292 environment -> entry,
293 object, sizeof( object )) != INI_SUCCESS )
294 {
295 environment -> entry = 0;
296 ret = SQL_NO_DATA;
297 }
298 else
299 {
300 memset( buffer, 0, sizeof( buffer ));
301 memset( property, 0, sizeof( property ));
302 memset( driver, 0, sizeof( driver ));
303
304 SQLGetPrivateProfileString( object, "Driver", "",
305 driver, sizeof( driver ), "ODBC.INI" );
306
307 if ( strlen( driver ) > 0 )
308 {
309 /*
310 * Make this return the description from the driver setup, this is
311 * the way its done in Windows
312
313 SQLGetPrivateProfileString( driver, "Description", driver,
314 property, sizeof( property ), "ODBCINST.INI" );
315 */
316
317 /*
318 * even though the string is called description, it should
319 * actually be the driver name entry from odbcinst.ini on windows
320 * there is no separate Description line
321 */
322 strcpy( property, driver );
323 }
324 else
325 {
326 /*
327 * May as well try and get something
328 */
329
330 SQLGetPrivateProfileString( object, "Description", "",
331 property, sizeof( property ), "ODBC.INI" );
332 }
333
334 environment -> entry++;
335
336 if (( server_name && buffer_length1 <= strlen( object )) ||
337 ( description && buffer_length2 <= strlen( property )))
338 {
339 __post_internal_error( &environment -> error,
340 ERROR_01004, NULL,
341 environment -> requested_version );
342 ret = SQL_SUCCESS_WITH_INFO;
343 }
344 else
345 {
346 ret = SQL_SUCCESS;
347 }
348
349 if ( server_name )
350 {
351 if ( buffer_length1 <= strlen( object ))
352 {
353 memcpy( server_name, object, buffer_length1 );
354 server_name[ buffer_length1 - 1 ] = '\0';
355 }
356 else
357 {
358 strcpy((char*) server_name, object );
359 }
360 }
361
362 if ( description )
363 {
364 if ( buffer_length2 <= strlen( property ))
365 {
366 memcpy( description, property, buffer_length2 );
367 description[ buffer_length1 - 1 ] = '\0';
368 }
369 else
370 {
371 strcpy((char*) description, property );
372 }
373 }
374
375 if ( name_length1 )
376 {
377 *name_length1 = strlen( object );
378 }
379 if ( name_length2 )
380 {
381 *name_length2 = strlen( property );
382 }
383 }
384
385 /* NEVER FORGET TO RESET THIS TO ODBC_BOTH_DSN */
386 SQLSetConfigMode( ODBC_BOTH_DSN );
387
388 if ( log_info.log_flag )
389 {
390 sprintf( environment -> msg,
391 "\n\t\tExit:[%s]",
392 __get_return_status( SQL_SUCCESS, s1 ));
393
394 dm_log_write( __FILE__,
395 __LINE__,
396 LOG_INFO,
397 LOG_INFO,
398 environment -> msg );
399 }
400
401 return function_return_nodrv( SQL_HANDLE_ENV, environment, ret );
402}
403