| 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: __connection.c,v 1.6 2009/02/18 17:59:08 lurcher Exp $ |
| 31 | * |
| 32 | * $Log: __connection.c,v $ |
| 33 | * Revision 1.6 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.5 2008/09/29 14:02:45 lurcher |
| 37 | * Fix missing dlfcn group option |
| 38 | * |
| 39 | * Revision 1.4 2004/09/08 16:38:54 lurcher |
| 40 | * |
| 41 | * Get ready for a 2.2.10 release |
| 42 | * |
| 43 | * Revision 1.3 2003/04/10 13:45:52 lurcher |
| 44 | * |
| 45 | * Alter the way that SQLDataSources returns the description field (again) |
| 46 | * |
| 47 | * Revision 1.2 2003/04/09 08:42:18 lurcher |
| 48 | * |
| 49 | * Allow setting of odbcinstQ lib from odbcinst.ini and Environment |
| 50 | * |
| 51 | * Revision 1.1.1.1 2001/10/17 16:40:07 lurcher |
| 52 | * |
| 53 | * First upload to SourceForge |
| 54 | * |
| 55 | * Revision 1.2 2001/05/15 10:57:44 nick |
| 56 | * |
| 57 | * Add initial support for VMS |
| 58 | * |
| 59 | * Revision 1.1.1.1 2000/09/04 16:42:52 nick |
| 60 | * Imported Sources |
| 61 | * |
| 62 | * Revision 1.7 1999/11/28 18:35:50 ngorham |
| 63 | * |
| 64 | * Add extra ODBC3/2 Date/Time mapping |
| 65 | * |
| 66 | * Revision 1.6 1999/11/13 23:41:01 ngorham |
| 67 | * |
| 68 | * Alter the way DM logging works |
| 69 | * Upgrade the Postgres driver to 6.4.6 |
| 70 | * |
| 71 | * Revision 1.5 1999/10/09 00:56:16 ngorham |
| 72 | * |
| 73 | * Added Manush's patch to map ODBC 3-2 datetime values |
| 74 | * |
| 75 | * Revision 1.4 1999/08/03 21:47:39 shandyb |
| 76 | * Moving to automake: changed files in DriverManager |
| 77 | * |
| 78 | * Revision 1.3 1999/07/04 21:05:08 ngorham |
| 79 | * |
| 80 | * Add LGPL Headers to code |
| 81 | * |
| 82 | * Revision 1.2 1999/06/19 17:51:41 ngorham |
| 83 | * |
| 84 | * Applied assorted minor bug fixes |
| 85 | * |
| 86 | * Revision 1.1.1.1 1999/05/29 13:41:09 sShandyb |
| 87 | * first go at it |
| 88 | * |
| 89 | * Revision 1.1.1.1 1999/05/27 18:23:18 pharvey |
| 90 | * Imported sources |
| 91 | * |
| 92 | * Revision 1.1 1999/04/25 23:06:11 nick |
| 93 | * Initial revision |
| 94 | * |
| 95 | * |
| 96 | **********************************************************************/ |
| 97 | |
| 98 | #include <config.h> |
| 99 | #include "drivermanager.h" |
| 100 | |
| 101 | /* |
| 102 | * list of places to look, a $ at the start indicates |
| 103 | * then it following text should be looked in as a env |
| 104 | * variable |
| 105 | */ |
| 106 | |
| 107 | static char const rcsid[]= "$RCSfile: __connection.c,v $ $Revision: 1.6 $" ; |
| 108 | |
| 109 | /* |
| 110 | * search for the library (.so) that the DSN points to |
| 111 | */ |
| 112 | |
| 113 | char *__find_lib_name( char *dsn, char *lib_name, char *driver_name ) |
| 114 | { |
| 115 | char driver[ INI_MAX_PROPERTY_VALUE + 1 ]; |
| 116 | char driver_lib[ INI_MAX_PROPERTY_VALUE + 1 ]; |
| 117 | |
| 118 | SQLSetConfigMode( ODBC_USER_DSN ); |
| 119 | |
| 120 | /* |
| 121 | * GET DRIVER FROM ODBC.INI |
| 122 | */ |
| 123 | |
| 124 | SQLGetPrivateProfileString( dsn, "Driver" , "" , |
| 125 | driver_lib, sizeof( driver_lib ), "ODBC.INI" ); |
| 126 | |
| 127 | if ( driver_lib[ 0 ] == 0 ) |
| 128 | { |
| 129 | /* |
| 130 | * if not found look in system DSN |
| 131 | */ |
| 132 | |
| 133 | SQLSetConfigMode( ODBC_SYSTEM_DSN ); |
| 134 | |
| 135 | SQLGetPrivateProfileString( dsn, "Driver" , "" , |
| 136 | driver_lib, sizeof( driver_lib ), "ODBC.INI" ); |
| 137 | |
| 138 | SQLSetConfigMode( ODBC_BOTH_DSN ); |
| 139 | if ( driver_lib[ 0 ] == 0 ) |
| 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * GET DRIVER FROM ODBCINST.INI IF ODBC.INI HAD USER FRIENDLY NAME |
| 145 | */ |
| 146 | |
| 147 | strcpy( driver_name, "" ); |
| 148 | |
| 149 | if ( driver_lib[ 0 ] != '/' ) |
| 150 | { |
| 151 | strcpy( driver, driver_lib ); |
| 152 | |
| 153 | /* |
| 154 | * allow the use of "User odbcinst files |
| 155 | */ |
| 156 | |
| 157 | #ifdef PLATFORM64 |
| 158 | SQLGetPrivateProfileString( driver, "Driver64" , "" , |
| 159 | driver_lib, sizeof( driver_lib ), "ODBCINST.INI" ); |
| 160 | |
| 161 | if ( driver_lib[ 0 ] == '\0' ) |
| 162 | { |
| 163 | SQLGetPrivateProfileString( driver, "Driver" , "" , |
| 164 | driver_lib, sizeof( driver_lib ), "ODBCINST.INI" ); |
| 165 | } |
| 166 | #else |
| 167 | SQLGetPrivateProfileString( driver, "Driver" , "" , |
| 168 | driver_lib, sizeof( driver_lib ), "ODBCINST.INI" ); |
| 169 | #endif |
| 170 | |
| 171 | strcpy( driver_name, driver ); |
| 172 | |
| 173 | if ( driver_lib[ 0 ] == 0 ) { |
| 174 | return NULL; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | strcpy( lib_name, driver_lib ); |
| 179 | |
| 180 | return lib_name; |
| 181 | } |
| 182 | |
| 183 | static SQLSMALLINT sql_old_to_new(SQLSMALLINT type) |
| 184 | { |
| 185 | switch(type) { |
| 186 | case SQL_TIME: |
| 187 | type=SQL_TYPE_TIME; |
| 188 | break; |
| 189 | |
| 190 | case SQL_DATE: |
| 191 | type=SQL_TYPE_DATE; |
| 192 | break; |
| 193 | |
| 194 | case SQL_TIMESTAMP: |
| 195 | type=SQL_TYPE_TIMESTAMP; |
| 196 | break; |
| 197 | } |
| 198 | return type; |
| 199 | } |
| 200 | |
| 201 | static SQLSMALLINT sql_new_to_old(SQLSMALLINT type) |
| 202 | { |
| 203 | switch(type) { |
| 204 | case SQL_TYPE_TIME: |
| 205 | type=SQL_TIME; |
| 206 | break; |
| 207 | |
| 208 | case SQL_TYPE_DATE: |
| 209 | type=SQL_DATE; |
| 210 | break; |
| 211 | |
| 212 | case SQL_TYPE_TIMESTAMP: |
| 213 | type=SQL_TIMESTAMP; |
| 214 | break; |
| 215 | } |
| 216 | return type; |
| 217 | } |
| 218 | |
| 219 | static SQLSMALLINT c_old_to_new(SQLSMALLINT type) |
| 220 | { |
| 221 | switch(type) { |
| 222 | case SQL_C_TIME: |
| 223 | type=SQL_C_TYPE_TIME; |
| 224 | break; |
| 225 | |
| 226 | case SQL_C_DATE: |
| 227 | type=SQL_C_TYPE_DATE; |
| 228 | break; |
| 229 | |
| 230 | case SQL_C_TIMESTAMP: |
| 231 | type=SQL_C_TYPE_TIMESTAMP; |
| 232 | break; |
| 233 | } |
| 234 | return type; |
| 235 | } |
| 236 | |
| 237 | static SQLSMALLINT c_new_to_old(SQLSMALLINT type) |
| 238 | { |
| 239 | switch(type) { |
| 240 | case SQL_C_TYPE_TIME: |
| 241 | type=SQL_C_TIME; |
| 242 | break; |
| 243 | |
| 244 | case SQL_C_TYPE_DATE: |
| 245 | type=SQL_C_DATE; |
| 246 | break; |
| 247 | |
| 248 | case SQL_C_TYPE_TIMESTAMP: |
| 249 | type=SQL_C_TIMESTAMP; |
| 250 | break; |
| 251 | } |
| 252 | return type; |
| 253 | } |
| 254 | |
| 255 | SQLSMALLINT __map_type(int map, DMHDBC connection, SQLSMALLINT type) |
| 256 | { |
| 257 | int driver_ver=connection->driver_act_ver; |
| 258 | int wanted_ver=connection->environment->requested_version; |
| 259 | |
| 260 | if(driver_ver==SQL_OV_ODBC2 && wanted_ver>=SQL_OV_ODBC3) { |
| 261 | switch(map) { |
| 262 | case MAP_SQL_DM2D: |
| 263 | type=sql_new_to_old(type); |
| 264 | break; |
| 265 | |
| 266 | case MAP_SQL_D2DM: |
| 267 | type=sql_old_to_new(type); |
| 268 | break; |
| 269 | |
| 270 | case MAP_C_DM2D: |
| 271 | type=c_new_to_old(type); |
| 272 | break; |
| 273 | |
| 274 | case MAP_C_D2DM: |
| 275 | type=c_old_to_new(type); |
| 276 | break; |
| 277 | } |
| 278 | } else if(driver_ver>=SQL_OV_ODBC3 && wanted_ver==SQL_OV_ODBC2) { |
| 279 | switch(map) { |
| 280 | case MAP_SQL_DM2D: |
| 281 | type=sql_old_to_new(type); |
| 282 | break; |
| 283 | |
| 284 | case MAP_SQL_D2DM: |
| 285 | type=sql_new_to_old(type); |
| 286 | break; |
| 287 | |
| 288 | case MAP_C_DM2D: |
| 289 | type=c_old_to_new(type); |
| 290 | break; |
| 291 | |
| 292 | case MAP_C_D2DM: |
| 293 | type=c_new_to_old(type); |
| 294 | break; |
| 295 | } |
| 296 | } else if(driver_ver>=SQL_OV_ODBC3 && wanted_ver>=SQL_OV_ODBC3) { |
| 297 | switch(map) { |
| 298 | case MAP_SQL_DM2D: |
| 299 | case MAP_SQL_D2DM: |
| 300 | type=sql_old_to_new(type); |
| 301 | break; |
| 302 | |
| 303 | case MAP_C_DM2D: |
| 304 | case MAP_C_D2DM: |
| 305 | type=c_old_to_new(type); |
| 306 | break; |
| 307 | } |
| 308 | } else if(driver_ver==SQL_OV_ODBC2 && wanted_ver==SQL_OV_ODBC2) { |
| 309 | switch(map) { |
| 310 | case MAP_SQL_DM2D: |
| 311 | case MAP_SQL_D2DM: |
| 312 | type=sql_new_to_old(type); |
| 313 | break; |
| 314 | |
| 315 | case MAP_C_DM2D: |
| 316 | case MAP_C_D2DM: |
| 317 | type=c_new_to_old(type); |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | return type; |
| 323 | } |
| 324 | |
| 325 | |