| 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: SQLBulkOperations.c,v 1.4 2009/02/18 17:59:08 lurcher Exp $ |
| 31 | * |
| 32 | * $Log: SQLBulkOperations.c,v $ |
| 33 | * Revision 1.4 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.3 2003/10/30 18:20:45 lurcher |
| 37 | * |
| 38 | * Fix broken thread protection |
| 39 | * Remove SQLNumResultCols after execute, lease S4/S% to driver |
| 40 | * Fix string overrun in SQLDriverConnect |
| 41 | * Add initial support for Interix |
| 42 | * |
| 43 | * Revision 1.2 2002/12/05 17:44:30 lurcher |
| 44 | * |
| 45 | * Display unknown return values in return logging |
| 46 | * |
| 47 | * Revision 1.1.1.1 2001/10/17 16:40:05 lurcher |
| 48 | * |
| 49 | * First upload to SourceForge |
| 50 | * |
| 51 | * Revision 1.2 2001/04/12 17:43:35 nick |
| 52 | * |
| 53 | * Change logging and added autotest to odbctest |
| 54 | * |
| 55 | * Revision 1.1.1.1 2000/09/04 16:42:52 nick |
| 56 | * Imported Sources |
| 57 | * |
| 58 | * Revision 1.7 1999/11/13 23:40:58 ngorham |
| 59 | * |
| 60 | * Alter the way DM logging works |
| 61 | * Upgrade the Postgres driver to 6.4.6 |
| 62 | * |
| 63 | * Revision 1.6 1999/10/24 23:54:17 ngorham |
| 64 | * |
| 65 | * First part of the changes to the error reporting |
| 66 | * |
| 67 | * Revision 1.5 1999/09/21 22:34:24 ngorham |
| 68 | * |
| 69 | * Improve performance by removing unneeded logging calls when logging is |
| 70 | * disabled |
| 71 | * |
| 72 | * Revision 1.4 1999/07/10 21:10:15 ngorham |
| 73 | * |
| 74 | * Adjust error sqlstate from driver manager, depending on requested |
| 75 | * version (ODBC2/3) |
| 76 | * |
| 77 | * Revision 1.3 1999/07/04 21:05:06 ngorham |
| 78 | * |
| 79 | * Add LGPL Headers to code |
| 80 | * |
| 81 | * Revision 1.2 1999/06/30 23:56:54 ngorham |
| 82 | * |
| 83 | * Add initial thread safety code |
| 84 | * |
| 85 | * Revision 1.1.1.1 1999/05/29 13:41:05 sShandyb |
| 86 | * first go at it |
| 87 | * |
| 88 | * Revision 1.2 1999/06/02 23:48:45 ngorham |
| 89 | * |
| 90 | * Added more 3-2 mapping |
| 91 | * |
| 92 | * Revision 1.1.1.1 1999/05/27 18:23:17 pharvey |
| 93 | * Imported sources |
| 94 | * |
| 95 | * Revision 1.2 1999/05/09 23:27:11 nick |
| 96 | * All the API done now |
| 97 | * |
| 98 | * Revision 1.1 1999/04/25 23:02:41 nick |
| 99 | * Initial revision |
| 100 | * |
| 101 | * |
| 102 | **********************************************************************/ |
| 103 | |
| 104 | #include <config.h> |
| 105 | #include "drivermanager.h" |
| 106 | |
| 107 | static char const rcsid[]= "$RCSfile: SQLBulkOperations.c,v $ $Revision: 1.4 $" ; |
| 108 | |
| 109 | SQLRETURN SQLBulkOperations( |
| 110 | SQLHSTMT statement_handle, |
| 111 | SQLSMALLINT operation ) |
| 112 | { |
| 113 | DMHSTMT statement = (DMHSTMT)statement_handle; |
| 114 | SQLRETURN ret; |
| 115 | SQLCHAR s1[ 100 + LOG_MESSAGE_LEN ]; |
| 116 | |
| 117 | /* |
| 118 | * check statement |
| 119 | */ |
| 120 | |
| 121 | if ( !__validate_stmt( statement )) |
| 122 | { |
| 123 | dm_log_write( __FILE__, |
| 124 | __LINE__, |
| 125 | LOG_INFO, |
| 126 | LOG_INFO, |
| 127 | "Error: SQL_INVALID_HANDLE" ); |
| 128 | |
| 129 | return SQL_INVALID_HANDLE; |
| 130 | } |
| 131 | |
| 132 | function_entry( statement ); |
| 133 | |
| 134 | if ( log_info.log_flag ) |
| 135 | { |
| 136 | sprintf( statement -> msg, "\n\t\tEntry:\ |
| 137 | \n\t\t\tStatement = %p\ |
| 138 | \n\t\t\tOption = %d" , |
| 139 | statement, |
| 140 | operation ); |
| 141 | |
| 142 | dm_log_write( __FILE__, |
| 143 | __LINE__, |
| 144 | LOG_INFO, |
| 145 | LOG_INFO, |
| 146 | statement -> msg ); |
| 147 | } |
| 148 | |
| 149 | thread_protect( SQL_HANDLE_STMT, statement ); |
| 150 | |
| 151 | /* |
| 152 | * check states |
| 153 | */ |
| 154 | |
| 155 | if ( statement -> state == STATE_S1 || |
| 156 | statement -> state == STATE_S2 || |
| 157 | statement -> state == STATE_S3 ) |
| 158 | { |
| 159 | dm_log_write( __FILE__, |
| 160 | __LINE__, |
| 161 | LOG_INFO, |
| 162 | LOG_INFO, |
| 163 | "Error: HY010" ); |
| 164 | |
| 165 | __post_internal_error( &statement -> error, |
| 166 | ERROR_HY010, NULL, |
| 167 | statement -> connection -> environment -> requested_version ); |
| 168 | |
| 169 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
| 170 | } |
| 171 | |
| 172 | if ( statement -> state == STATE_S4 ) |
| 173 | { |
| 174 | dm_log_write( __FILE__, |
| 175 | __LINE__, |
| 176 | LOG_INFO, |
| 177 | LOG_INFO, |
| 178 | "Error: 24000" ); |
| 179 | |
| 180 | __post_internal_error( &statement -> error, |
| 181 | ERROR_24000, NULL, |
| 182 | statement -> connection -> environment -> requested_version ); |
| 183 | |
| 184 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
| 185 | } |
| 186 | |
| 187 | if ( statement -> state == STATE_S7 ) |
| 188 | { |
| 189 | dm_log_write( __FILE__, |
| 190 | __LINE__, |
| 191 | LOG_INFO, |
| 192 | LOG_INFO, |
| 193 | "Error: HY010" ); |
| 194 | |
| 195 | __post_internal_error( &statement -> error, |
| 196 | ERROR_HY010, NULL, |
| 197 | statement -> connection -> environment -> requested_version ); |
| 198 | |
| 199 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
| 200 | } |
| 201 | |
| 202 | if ( statement -> state == STATE_S8 || |
| 203 | statement -> state == STATE_S9 || |
| 204 | statement -> state == STATE_S10 || |
| 205 | statement -> state == STATE_S13 || |
| 206 | statement -> state == STATE_S14 || |
| 207 | statement -> state == STATE_S15 ) |
| 208 | { |
| 209 | dm_log_write( __FILE__, |
| 210 | __LINE__, |
| 211 | LOG_INFO, |
| 212 | LOG_INFO, |
| 213 | "Error: HY010" ); |
| 214 | |
| 215 | __post_internal_error( &statement -> error, |
| 216 | ERROR_HY010, NULL, |
| 217 | statement -> connection -> environment -> requested_version ); |
| 218 | |
| 219 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
| 220 | } |
| 221 | |
| 222 | if ( statement -> state == STATE_S11 || |
| 223 | statement -> state == STATE_S12 ) |
| 224 | { |
| 225 | if ( statement -> interupted_func != SQL_API_SQLBULKOPERATIONS ) |
| 226 | { |
| 227 | dm_log_write( __FILE__, |
| 228 | __LINE__, |
| 229 | LOG_INFO, |
| 230 | LOG_INFO, |
| 231 | "Error: HY010" ); |
| 232 | |
| 233 | __post_internal_error( &statement -> error, |
| 234 | ERROR_HY010, NULL, |
| 235 | statement -> connection -> environment -> requested_version ); |
| 236 | |
| 237 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | if ( statement -> state != STATE_S11 && statement -> state != STATE_S12 ) |
| 242 | { |
| 243 | statement -> interupted_state = statement -> state; |
| 244 | } |
| 245 | /* |
| 246 | * there are a lot of conditions that should be tested here |
| 247 | */ |
| 248 | |
| 249 | if ( CHECK_SQLBULKOPERATIONS( statement -> connection )) |
| 250 | { |
| 251 | ret = SQLBULKOPERATIONS( statement -> connection, |
| 252 | statement -> driver_stmt, |
| 253 | operation ); |
| 254 | } |
| 255 | else if ( CHECK_SQLSETPOS( statement -> connection ) && |
| 256 | statement -> connection -> driver_act_ver == SQL_OV_ODBC2 && |
| 257 | operation == SQL_ADD ) |
| 258 | { |
| 259 | ret = SQLSETPOS( statement -> connection, |
| 260 | statement -> driver_stmt, |
| 261 | 0, |
| 262 | SQL_ADD, |
| 263 | SQL_LOCK_NO_CHANGE ); |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | dm_log_write( __FILE__, |
| 268 | __LINE__, |
| 269 | LOG_INFO, |
| 270 | LOG_INFO, |
| 271 | "Error: IM001" ); |
| 272 | |
| 273 | __post_internal_error( &statement -> error, |
| 274 | ERROR_IM001, NULL, |
| 275 | statement -> connection -> environment -> requested_version ); |
| 276 | |
| 277 | return function_return_nodrv( SQL_HANDLE_STMT, statement, SQL_ERROR ); |
| 278 | } |
| 279 | |
| 280 | if ( ret == SQL_STILL_EXECUTING ) |
| 281 | { |
| 282 | statement -> interupted_func = SQL_API_SQLBULKOPERATIONS; |
| 283 | if ( statement -> state != STATE_S11 && |
| 284 | statement -> state != STATE_S12 ) |
| 285 | statement -> state = STATE_S11; |
| 286 | } |
| 287 | else if ( ret == SQL_NEED_DATA ) |
| 288 | { |
| 289 | statement -> interupted_func = SQL_API_SQLBULKOPERATIONS; |
| 290 | statement -> interupted_state = statement -> state; |
| 291 | statement -> state = STATE_S8; |
| 292 | } |
| 293 | else |
| 294 | { |
| 295 | statement -> state = statement -> interupted_state; |
| 296 | } |
| 297 | |
| 298 | if ( log_info.log_flag ) |
| 299 | { |
| 300 | sprintf( statement -> msg, |
| 301 | "\n\t\tExit:[%s]" , |
| 302 | __get_return_status( ret, s1 )); |
| 303 | |
| 304 | dm_log_write( __FILE__, |
| 305 | __LINE__, |
| 306 | LOG_INFO, |
| 307 | LOG_INFO, |
| 308 | statement -> msg ); |
| 309 | } |
| 310 | |
| 311 | return function_return( SQL_HANDLE_STMT, statement, ret ); |
| 312 | } |
| 313 | |