1 /************************************************************************* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * Copyright 2008 by Sun Microsystems, Inc. 5 * 6 * OpenOffice.org - a multi-platform office productivity suite 7 * 8 * $RCSfile: mysqlc_preparedstatement.cxx,v $ 9 * 10 * $Revision: 1.1.2.5 $ 11 * 12 * This file is part of OpenOffice.org. 13 * 14 * OpenOffice.org is free software: you can redistribute it and/or modify 15 * it under the terms of the GNU Lesser General Public License version 3 16 * only, as published by the Free Software Foundation. 17 * 18 * OpenOffice.org is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 * GNU Lesser General Public License version 3 for more details 22 * (a copy is included in the LICENSE file that accompanied this code). 23 * 24 * You should have received a copy of the GNU Lesser General Public License 25 * version 3 along with OpenOffice.org. If not, see 26 * <http://www.openoffice.org/license.html> 27 * for a copy of the LGPLv3 License. 28 ************************************************************************/ 29 30 #include "mysqlc_general.hxx" 31 #include "mysqlc_preparedstatement.hxx" 32 #include "mysqlc_propertyids.hxx" 33 #include "mysqlc_resultsetmetadata.hxx" 34 35 #include <com/sun/star/lang/DisposedException.hpp> 36 #include <com/sun/star/sdbc/DataType.hpp> 37 38 #include <cppconn/connection.h> 39 #include <cppconn/exception.h> 40 #include <cppconn/parameter_metadata.h> 41 #include <cppconn/prepared_statement.h> 42 #include <cppconn/statement.h> 43 #include <cppuhelper/typeprovider.hxx> 44 #include <osl/diagnose.h> 45 46 #include <stdio.h> 47 48 using namespace connectivity::mysqlc; 49 using namespace com::sun::star::uno; 50 using namespace com::sun::star::lang; 51 using namespace com::sun::star::beans; 52 using namespace com::sun::star::sdbc; 53 using namespace com::sun::star::container; 54 using namespace com::sun::star::io; 55 using namespace com::sun::star::util; 56 using ::osl::MutexGuard; 57 using mysqlc_sdbc_driver::getStringFromAny; 58 59 60 /* {{{ my_i_to_a() -I- */ 61 static inline char * my_i_to_a(char * buf, size_t buf_size, int a) 62 { 63 snprintf(buf, buf_size, "%d", a); 64 return buf; 65 } 66 /* }}} */ 67 68 69 IMPLEMENT_SERVICE_INFO(OPreparedStatement,"com.sun.star.sdbcx.mysqlc.PreparedStatement","com.sun.star.sdbc.PreparedStatement"); 70 71 72 /* {{{ OPreparedStatement::OPreparedStatement() -I- */ 73 OPreparedStatement::OPreparedStatement(OConnection* _pConnection, sql::PreparedStatement * _cppPrepStmt) 74 :OCommonStatement(_pConnection, _cppPrepStmt) 75 { 76 OSL_TRACE("OPreparedStatement::OPreparedStatement"); 77 m_pConnection = _pConnection; 78 m_pConnection->acquire(); 79 80 try { 81 m_paramCount = ((sql::PreparedStatement *)cppStatement)->getParameterMetaData()->getParameterCount(); 82 } catch (sql::SQLException &e) { 83 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding()); 84 } 85 } 86 /* }}} */ 87 88 89 /* {{{ OPreparedStatement::~OPreparedStatement() -I- */ 90 OPreparedStatement::~OPreparedStatement() 91 { 92 OSL_TRACE("OPreparedStatement::~OPreparedStatement"); 93 } 94 /* }}} */ 95 96 97 /* {{{ OPreparedStatement::acquire() -I- */ 98 void SAL_CALL OPreparedStatement::acquire() 99 throw() 100 { 101 OSL_TRACE("OPreparedStatement::acquire"); 102 OCommonStatement::acquire(); 103 } 104 /* }}} */ 105 106 107 /* {{{ OPreparedStatement::release() -I- */ 108 void SAL_CALL OPreparedStatement::release() 109 throw() 110 { 111 OSL_TRACE("OPreparedStatement::release"); 112 OCommonStatement::release(); 113 } 114 /* }}} */ 115 116 117 /* {{{ OPreparedStatement::queryInterface() -I- */ 118 Any SAL_CALL OPreparedStatement::queryInterface(const Type & rType) 119 throw(RuntimeException) 120 { 121 OSL_TRACE("OPreparedStatement::queryInterface"); 122 Any aRet = OCommonStatement::queryInterface(rType); 123 if (!aRet.hasValue()) { 124 aRet = OPreparedStatement_BASE::queryInterface(rType); 125 } 126 return (aRet); 127 } 128 /* }}} */ 129 130 131 /* {{{ OPreparedStatement::getPropertySetInfo() -I- */ 132 Sequence< Type > SAL_CALL OPreparedStatement::getTypes() 133 throw(RuntimeException) 134 { 135 OSL_TRACE("OPreparedStatement::getTypes"); 136 return concatSequences(OPreparedStatement_BASE::getTypes(), OCommonStatement::getTypes()); 137 } 138 /* }}} */ 139 140 141 /* {{{ OPreparedStatement::getMetaData() -I- */ 142 Reference< XResultSetMetaData > SAL_CALL OPreparedStatement::getMetaData() 143 throw(SQLException, RuntimeException) 144 { 145 OSL_TRACE("OPreparedStatement::getMetaData"); 146 MutexGuard aGuard(m_aMutex); 147 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 148 149 try { 150 if (!m_xMetaData.is()) { 151 m_xMetaData = new OResultSetMetaData( 152 ((sql::PreparedStatement *)cppStatement)->getMetaData(), 153 getOwnConnection()->getConnectionEncoding() 154 ); 155 } 156 } catch (sql::MethodNotImplementedException) { 157 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::getMetaData", *this); 158 } catch (sql::SQLException &e) { 159 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding()); 160 } 161 return m_xMetaData; 162 } 163 /* }}} */ 164 165 166 /* {{{ OPreparedStatement::close() -I- */ 167 void SAL_CALL OPreparedStatement::close() 168 throw(SQLException, RuntimeException) 169 { 170 OSL_TRACE("OPreparedStatement::close"); 171 172 MutexGuard aGuard(m_aMutex); 173 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 174 175 try { 176 clearWarnings(); 177 clearParameters(); 178 OCommonStatement::close(); 179 } catch (SQLException) { 180 // If we get an error, ignore 181 } 182 183 // Remove this Statement object from the Connection object's 184 // list 185 } 186 /* }}} */ 187 188 189 /* {{{ OPreparedStatement::execute() -I- */ 190 sal_Bool SAL_CALL OPreparedStatement::execute() 191 throw(SQLException, RuntimeException) 192 { 193 OSL_TRACE("OPreparedStatement::execute"); 194 MutexGuard aGuard(m_aMutex); 195 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 196 197 sal_Bool success = sal_False; 198 try { 199 success = ((sql::PreparedStatement *)cppStatement)->execute()? sal_True:sal_False; 200 } catch (sql::SQLException &e) { 201 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding()); 202 } 203 return success; 204 } 205 /* }}} */ 206 207 208 /* {{{ OPreparedStatement::executeUpdate() -I- */ 209 sal_Int32 SAL_CALL OPreparedStatement::executeUpdate() 210 throw(SQLException, RuntimeException) 211 { 212 OSL_TRACE("OPreparedStatement::executeUpdate"); 213 MutexGuard aGuard(m_aMutex); 214 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 215 216 sal_Int32 affectedRows = sal_False; 217 try { 218 affectedRows = ((sql::PreparedStatement *)cppStatement)->executeUpdate(); 219 } catch (sql::SQLException &e) { 220 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding()); 221 } 222 return affectedRows; 223 } 224 /* }}} */ 225 226 227 /* {{{ OPreparedStatement::getPropertySetInfo() -I- */ 228 void SAL_CALL OPreparedStatement::setString(sal_Int32 parameter, const OUString& x) 229 throw(SQLException, RuntimeException) 230 { 231 OSL_TRACE("OPreparedStatement::setString"); 232 MutexGuard aGuard(m_aMutex); 233 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 234 checkParameterIndex(parameter); 235 236 try { 237 ext_std::string stringie(::rtl::OUStringToOString(x, m_pConnection->getConnectionEncoding()).getStr()); 238 ((sql::PreparedStatement *)cppStatement)->setString(parameter, stringie); 239 } catch (sql::MethodNotImplementedException) { 240 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::clearParameters", *this); 241 } catch (sql::SQLException &e) { 242 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding()); 243 } 244 } 245 /* }}} */ 246 247 248 /* {{{ OPreparedStatement::getConnection() -I- */ 249 Reference< XConnection > SAL_CALL OPreparedStatement::getConnection() 250 throw(SQLException, RuntimeException) 251 { 252 OSL_TRACE("OPreparedStatement::getConnection"); 253 MutexGuard aGuard(m_aMutex); 254 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 255 256 return (Reference< XConnection >)m_pConnection; 257 } 258 /* }}} */ 259 260 Reference< XResultSet > SAL_CALL OPreparedStatement::executeQuery(const OUString& sql) 261 throw(SQLException, RuntimeException) 262 { 263 return OCommonStatement::executeQuery( sql ); 264 } 265 266 sal_Int32 SAL_CALL OPreparedStatement::executeUpdate(const OUString& sql) 267 throw(SQLException, RuntimeException) 268 { 269 return OCommonStatement::executeUpdate( sql ); 270 } 271 272 sal_Bool SAL_CALL OPreparedStatement::execute( const OUString& sql ) 273 throw(SQLException, RuntimeException) 274 { 275 return OCommonStatement::execute( sql ); 276 } 277 278 /* {{{ OPreparedStatement::executeQuery() -I- */ 279 Reference< XResultSet > SAL_CALL OPreparedStatement::executeQuery() 280 throw(SQLException, RuntimeException) 281 { 282 OSL_TRACE("OPreparedStatement::executeQuery"); 283 MutexGuard aGuard(m_aMutex); 284 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 285 286 Reference< XResultSet > xResultSet; 287 try { 288 sql::ResultSet * res = ((sql::PreparedStatement *)cppStatement)->executeQuery(); 289 xResultSet = new OResultSet(this, res, getOwnConnection()->getConnectionEncoding()); 290 } catch (sql::SQLException &e) { 291 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding()); 292 } 293 return xResultSet; 294 } 295 /* }}} */ 296 297 298 /* {{{ OPreparedStatement::setBoolean() -I- */ 299 void SAL_CALL OPreparedStatement::setBoolean(sal_Int32 parameter, sal_Bool x) 300 throw(SQLException, RuntimeException) 301 { 302 OSL_TRACE("OPreparedStatement::setBoolean"); 303 MutexGuard aGuard(m_aMutex); 304 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 305 checkParameterIndex(parameter); 306 307 try { 308 ((sql::PreparedStatement *)cppStatement)->setBoolean(parameter, x); 309 } catch (sql::MethodNotImplementedException) { 310 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setBoolean", *this); 311 } catch (sql::SQLException &e) { 312 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding()); 313 } 314 } 315 /* }}} */ 316 317 318 /* {{{ OPreparedStatement::setByte() -I- */ 319 void SAL_CALL OPreparedStatement::setByte(sal_Int32 parameter, sal_Int8 x) 320 throw(SQLException, RuntimeException) 321 { 322 OSL_TRACE("OPreparedStatement::setByte"); 323 MutexGuard aGuard(m_aMutex); 324 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 325 checkParameterIndex(parameter); 326 327 try { 328 ((sql::PreparedStatement *)cppStatement)->setInt(parameter, x); 329 } catch (sql::MethodNotImplementedException) { 330 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setByte", *this); 331 } catch (sql::SQLException &e) { 332 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding()); 333 } 334 } 335 /* }}} */ 336 337 338 /* {{{ OPreparedStatement::setDate() -I- */ 339 void SAL_CALL OPreparedStatement::setDate(sal_Int32 parameter, const Date& aData) 340 throw(SQLException, RuntimeException) 341 { 342 OSL_TRACE("OPreparedStatement::setDate"); 343 MutexGuard aGuard(m_aMutex); 344 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 345 checkParameterIndex(parameter); 346 347 ext_std::string dateStr; 348 char buf[20]; 349 dateStr.append(my_i_to_a(buf, sizeof(buf)-1, aData.Year)); 350 dateStr.append("-", 1); 351 dateStr.append(my_i_to_a(buf, sizeof(buf)-1, aData.Month)); 352 dateStr.append("-", 1); 353 dateStr.append(my_i_to_a(buf, sizeof(buf)-1, aData.Day)); 354 355 try { 356 ((sql::PreparedStatement *)cppStatement)->setDateTime(parameter, dateStr); 357 } catch (sql::MethodNotImplementedException) { 358 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setDate", *this); 359 } catch (sql::SQLException &e) { 360 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding()); 361 } 362 } 363 /* }}} */ 364 365 366 /* {{{ OPreparedStatement::setTime() -I- */ 367 void SAL_CALL OPreparedStatement::setTime(sal_Int32 parameter, const Time& aVal) 368 throw(SQLException, RuntimeException) 369 { 370 OSL_TRACE("OPreparedStatement::setTime"); 371 MutexGuard aGuard(m_aMutex); 372 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 373 checkParameterIndex(parameter); 374 375 ext_std::string timeStr; 376 char buf[20]; 377 timeStr.append(my_i_to_a(buf, sizeof(buf)-1, aVal.Hours)); 378 timeStr.append(":", 1); 379 timeStr.append(my_i_to_a(buf, sizeof(buf)-1, aVal.Minutes)); 380 timeStr.append(":", 1); 381 timeStr.append(my_i_to_a(buf, sizeof(buf)-1, aVal.Seconds)); 382 383 try { 384 ((sql::PreparedStatement *)cppStatement)->setDateTime(parameter, timeStr); 385 } catch (sql::MethodNotImplementedException) { 386 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setTime", *this); 387 } catch (sql::SQLException &e) { 388 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding()); 389 } 390 } 391 /* }}} */ 392 393 394 /* {{{ OPreparedStatement::setTimestamp() -I- */ 395 void SAL_CALL OPreparedStatement::setTimestamp(sal_Int32 parameter, const DateTime& aVal) 396 throw(SQLException, RuntimeException) 397 { 398 OSL_TRACE("OPreparedStatement::setTimestamp"); 399 MutexGuard aGuard(m_aMutex); 400 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 401 checkParameterIndex(parameter); 402 403 ext_std::string timeStr; 404 char buf[20]; 405 timeStr.append(my_i_to_a(buf, sizeof(buf)-1, aVal.Year)); 406 timeStr.append("-", 1); 407 timeStr.append(my_i_to_a(buf, sizeof(buf)-1, aVal.Month)); 408 timeStr.append("-", 1); 409 timeStr.append(my_i_to_a(buf, sizeof(buf)-1, aVal.Day)); 410 411 timeStr.append(" ", 1); 412 413 timeStr.append(my_i_to_a(buf, sizeof(buf)-1, aVal.Hours)); 414 timeStr.append(":", 1); 415 timeStr.append(my_i_to_a(buf, sizeof(buf)-1, aVal.Minutes)); 416 timeStr.append(":", 1); 417 timeStr.append(my_i_to_a(buf, sizeof(buf)-1, aVal.Seconds)); 418 419 try { 420 ((sql::PreparedStatement *)cppStatement)->setDateTime(parameter, timeStr); 421 } catch (sql::MethodNotImplementedException) { 422 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setTimestamp", *this); 423 } catch (sql::SQLException &e) { 424 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding()); 425 } 426 } 427 /* }}} */ 428 429 430 /* {{{ OPreparedStatement::setDouble() -I- */ 431 void SAL_CALL OPreparedStatement::setDouble(sal_Int32 parameter, double x) 432 throw(SQLException, RuntimeException) 433 { 434 OSL_TRACE("OPreparedStatement::setDouble"); 435 MutexGuard aGuard(m_aMutex); 436 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 437 checkParameterIndex(parameter); 438 439 try { 440 ((sql::PreparedStatement *)cppStatement)->setDouble(parameter, x); 441 } catch (sql::MethodNotImplementedException) { 442 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setDouble", *this); 443 } catch (sql::SQLException &e) { 444 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding()); 445 } 446 } 447 /* }}} */ 448 449 450 /* {{{ OPreparedStatement::setFloat() -I- */ 451 void SAL_CALL OPreparedStatement::setFloat(sal_Int32 parameter, float x) 452 throw(SQLException, RuntimeException) 453 { 454 OSL_TRACE("OPreparedStatement::setFloat"); 455 MutexGuard aGuard(m_aMutex); 456 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 457 checkParameterIndex(parameter); 458 459 try { 460 ((sql::PreparedStatement *)cppStatement)->setDouble(parameter, x); 461 } catch (sql::MethodNotImplementedException) { 462 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setFloat", *this); 463 } catch (sql::SQLException &e) { 464 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding()); 465 } 466 } 467 /* }}} */ 468 469 470 /* {{{ OPreparedStatement::setInt() -I- */ 471 void SAL_CALL OPreparedStatement::setInt(sal_Int32 parameter, sal_Int32 x) 472 throw(SQLException, RuntimeException) 473 { 474 OSL_TRACE("OPreparedStatement::setInt"); 475 MutexGuard aGuard(m_aMutex); 476 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 477 checkParameterIndex(parameter); 478 479 try { 480 ((sql::PreparedStatement *)cppStatement)->setInt(parameter, x); 481 } catch (sql::MethodNotImplementedException) { 482 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setInt", *this); 483 } catch (sql::SQLException &e) { 484 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding()); 485 } 486 } 487 /* }}} */ 488 489 490 /* {{{ OPreparedStatement::setLong() -I- */ 491 void SAL_CALL OPreparedStatement::setLong(sal_Int32 parameter, sal_Int64 aVal) 492 throw(SQLException, RuntimeException) 493 { 494 OSL_TRACE("OPreparedStatement::setLong"); 495 MutexGuard aGuard(m_aMutex); 496 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 497 checkParameterIndex(parameter); 498 499 try { 500 ((sql::PreparedStatement *)cppStatement)->setInt64(parameter, aVal); 501 } catch (sql::MethodNotImplementedException) { 502 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setLong", *this); 503 } catch (sql::SQLException &e) { 504 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding()); 505 } 506 } 507 /* }}} */ 508 509 510 /* {{{ OPreparedStatement::setNull() -I- */ 511 void SAL_CALL OPreparedStatement::setNull(sal_Int32 parameter, sal_Int32 sqlType) 512 throw(SQLException, RuntimeException) 513 { 514 OSL_TRACE("OPreparedStatement::setNull"); 515 MutexGuard aGuard(m_aMutex); 516 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 517 checkParameterIndex(parameter); 518 519 try { 520 ((sql::PreparedStatement *)cppStatement)->setNull(parameter, sqlType); 521 } catch (sql::MethodNotImplementedException) { 522 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setNull", *this); 523 } catch (sql::SQLException &e) { 524 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding()); 525 } 526 } 527 /* }}} */ 528 529 530 /* {{{ OPreparedStatement::setClob() -U- */ 531 void SAL_CALL OPreparedStatement::setClob(sal_Int32 parameter, const Reference< XClob >& /* x */) 532 throw(SQLException, RuntimeException) 533 { 534 OSL_TRACE("OPreparedStatement::setClob"); 535 MutexGuard aGuard(m_aMutex); 536 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 537 checkParameterIndex(parameter); 538 539 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setClob", *this); 540 } 541 /* }}} */ 542 543 544 /* {{{ OPreparedStatement::setBlob() -U- */ 545 void SAL_CALL OPreparedStatement::setBlob(sal_Int32 parameter, const Reference< XBlob >& /* x */) 546 throw(SQLException, RuntimeException) 547 { 548 OSL_TRACE("OPreparedStatement::setBlob"); 549 MutexGuard aGuard(m_aMutex); 550 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 551 checkParameterIndex(parameter); 552 553 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setBlob", *this); 554 } 555 /* }}} */ 556 557 558 /* {{{ OPreparedStatement::setArray() -U- */ 559 void SAL_CALL OPreparedStatement::setArray(sal_Int32 parameter, const Reference< XArray >& /* x */) 560 throw(SQLException, RuntimeException) 561 { 562 OSL_TRACE("OPreparedStatement::setArray"); 563 MutexGuard aGuard(m_aMutex); 564 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 565 checkParameterIndex(parameter); 566 567 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setArray", *this); 568 } 569 /* }}} */ 570 571 572 /* {{{ OPreparedStatement::setRef() -U- */ 573 void SAL_CALL OPreparedStatement::setRef(sal_Int32 parameter, const Reference< XRef >& /* x */) 574 throw(SQLException, RuntimeException) 575 { 576 OSL_TRACE("OPreparedStatement::setRef"); 577 MutexGuard aGuard(m_aMutex); 578 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 579 checkParameterIndex(parameter); 580 581 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setRef", *this); 582 } 583 /* }}} */ 584 585 namespace 586 { 587 template < class COMPLEXTYPE > 588 bool impl_setObject( const Reference< XParameters >& _rxParam, sal_Int32 _parameterIndex, const Any& _value, 589 void ( SAL_CALL XParameters::*_Setter )( sal_Int32, const COMPLEXTYPE& ), bool _throwIfNotExtractable ) 590 { 591 COMPLEXTYPE aValue; 592 if ( _value >>= aValue ) 593 { 594 (_rxParam.get()->*_Setter)( _parameterIndex, aValue ); 595 return true; 596 } 597 598 if ( _throwIfNotExtractable ) 599 mysqlc_sdbc_driver::throwInvalidArgumentException( "OPreparedStatement::setObjectWithInfo", _rxParam ); 600 return false; 601 } 602 603 template < class INTTYPE > 604 void impl_setObject( const Reference< XParameters >& _rxParam, sal_Int32 _parameterIndex, const Any& _value, 605 void ( SAL_CALL XParameters::*_Setter )( sal_Int32, INTTYPE ) ) 606 { 607 sal_Int32 nValue(0); 608 if ( !( _value >>= nValue ) ) 609 mysqlc_sdbc_driver::throwInvalidArgumentException( "OPreparedStatement::setObjectWithInfo", _rxParam ); 610 (_rxParam.get()->*_Setter)( _parameterIndex, (INTTYPE)nValue ); 611 } 612 } 613 614 /* {{{ OPreparedStatement::setObjectWithInfo() -U- */ 615 void SAL_CALL OPreparedStatement::setObjectWithInfo(sal_Int32 _parameterIndex, const Any& _value, sal_Int32 _targetSqlType, sal_Int32 /* scale */) 616 throw(SQLException, RuntimeException) 617 { 618 OSL_TRACE("OPreparedStatement::setObjectWithInfo"); 619 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 620 MutexGuard aGuard(m_aMutex); 621 checkParameterIndex( _parameterIndex ); 622 623 if ( !_value.hasValue() ) 624 { 625 setNull( _parameterIndex, _targetSqlType ); 626 return; 627 } 628 629 switch ( _targetSqlType ) 630 { 631 case DataType::DECIMAL: 632 case DataType::NUMERIC: 633 { 634 double nValue(0); 635 if ( _value >>= nValue ) 636 { 637 setDouble( _parameterIndex, nValue ); 638 break; 639 } 640 } 641 // run through 642 643 case DataType::CHAR: 644 case DataType::VARCHAR: 645 case DataType::LONGVARCHAR: 646 impl_setObject( this, _parameterIndex, _value, &XParameters::setString, true ); 647 break; 648 649 case DataType::BIGINT: 650 { 651 sal_Int64 nValue = 0; 652 if ( !( _value >>= nValue ) ) 653 mysqlc_sdbc_driver::throwInvalidArgumentException( "OPreparedStatement::setObjectWithInfo", *this ); 654 setLong( _parameterIndex, nValue ); 655 } 656 break; 657 658 case DataType::FLOAT: 659 case DataType::REAL: 660 { 661 float nValue = 0; 662 if ( _value >>= nValue ) 663 { 664 setFloat(_parameterIndex,nValue); 665 break; 666 } 667 } 668 // run through if we couldn't set a float value 669 670 case DataType::DOUBLE: 671 { 672 double nValue(0); 673 if ( !( _value >>= nValue ) ) 674 mysqlc_sdbc_driver::throwInvalidArgumentException( "OPreparedStatement::setObjectWithInfo", *this ); 675 setDouble( _parameterIndex, nValue ); 676 } 677 break; 678 679 case DataType::DATE: 680 impl_setObject( this, _parameterIndex, _value, &XParameters::setDate, true ); 681 break; 682 683 case DataType::TIME: 684 impl_setObject( this, _parameterIndex, _value, &XParameters::setTime, true ); 685 break; 686 687 case DataType::TIMESTAMP: 688 impl_setObject( this, _parameterIndex, _value, &XParameters::setTimestamp, true ); 689 break; 690 691 case DataType::BINARY: 692 case DataType::VARBINARY: 693 case DataType::LONGVARBINARY: 694 { 695 if ( impl_setObject( this, _parameterIndex, _value, &XParameters::setBytes, false ) 696 || impl_setObject( this, _parameterIndex, _value, &XParameters::setBlob, false ) 697 || impl_setObject( this, _parameterIndex, _value, &XParameters::setClob, false ) 698 ) 699 break; 700 701 Reference< ::com::sun::star::io::XInputStream > xBinStream; 702 if ( _value >>= xBinStream ) 703 { 704 setBinaryStream( _parameterIndex, xBinStream, xBinStream->available() ); 705 break; 706 } 707 708 mysqlc_sdbc_driver::throwInvalidArgumentException( "OPreparedStatement::setObjectWithInfo", *this ); 709 } 710 break; 711 712 case DataType::BIT: 713 case DataType::BOOLEAN: 714 { 715 bool bValue( false ); 716 if ( _value >>= bValue ) 717 { 718 setBoolean( _parameterIndex, bValue ); 719 break; 720 } 721 sal_Int32 nValue( 0 ); 722 if ( _value >>= nValue ) 723 { 724 setBoolean( _parameterIndex, ( nValue != 0 ) ); 725 break; 726 } 727 mysqlc_sdbc_driver::throwInvalidArgumentException( "OPreparedStatement::setObjectWithInfo", *this ); 728 } 729 break; 730 731 case DataType::TINYINT: 732 impl_setObject( this, _parameterIndex, _value, &XParameters::setByte ); 733 break; 734 735 case DataType::SMALLINT: 736 impl_setObject( this, _parameterIndex, _value, &XParameters::setShort ); 737 break; 738 739 case DataType::INTEGER: 740 impl_setObject( this, _parameterIndex, _value, &XParameters::setInt ); 741 break; 742 743 default: 744 mysqlc_sdbc_driver::throwInvalidArgumentException( "OPreparedStatement::setObjectWithInfo", *this ); 745 break; 746 } 747 } 748 /* }}} */ 749 750 751 /* {{{ OPreparedStatement::setObjectNull() -U- */ 752 void SAL_CALL OPreparedStatement::setObjectNull(sal_Int32 parameter, sal_Int32 /* sqlType */, const OUString& /* typeName */) 753 throw(SQLException, RuntimeException) 754 { 755 OSL_TRACE("OPreparedStatement::setObjectNull"); 756 MutexGuard aGuard(m_aMutex); 757 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 758 checkParameterIndex(parameter); 759 760 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setObjectNull", *this); 761 } 762 /* }}} */ 763 764 765 /* {{{ OPreparedStatement::setObject() -U- */ 766 void SAL_CALL OPreparedStatement::setObject(sal_Int32 parameter, const Any& /* x */) 767 throw(SQLException, RuntimeException) 768 { 769 OSL_TRACE("OPreparedStatement::setObject"); 770 MutexGuard aGuard(m_aMutex); 771 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 772 checkParameterIndex(parameter); 773 774 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setObject", *this); 775 } 776 /* }}} */ 777 778 779 /* {{{ OPreparedStatement::setShort() -I- */ 780 void SAL_CALL OPreparedStatement::setShort(sal_Int32 parameter, sal_Int16 x) 781 throw(SQLException, RuntimeException) 782 { 783 OSL_TRACE("OPreparedStatement::setShort"); 784 MutexGuard aGuard(m_aMutex); 785 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 786 checkParameterIndex(parameter); 787 788 try { 789 ((sql::PreparedStatement *)cppStatement)->setInt(parameter, x); 790 } catch (sql::MethodNotImplementedException) { 791 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setShort", *this); 792 } catch (sql::SQLException &e) { 793 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding()); 794 } 795 } 796 /* }}} */ 797 798 799 /* {{{ OPreparedStatement::setBytes() -I- */ 800 void SAL_CALL OPreparedStatement::setBytes(sal_Int32 parameter, const Sequence< sal_Int8 >& x) 801 throw(SQLException, RuntimeException) 802 { 803 OSL_TRACE("OPreparedStatement::setBytes"); 804 MutexGuard aGuard(m_aMutex); 805 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 806 checkParameterIndex(parameter); 807 808 ext_std::string blobby((char *)x.getConstArray(), x.getLength()); 809 try { 810 ((sql::PreparedStatement *)cppStatement)->setString(parameter, blobby); 811 } catch (sql::MethodNotImplementedException) { 812 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setBytes", *this); 813 } catch (sql::SQLException &e) { 814 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding()); 815 } 816 } 817 /* }}} */ 818 819 820 /* {{{ OPreparedStatement::setCharacterStream() -U- */ 821 void SAL_CALL OPreparedStatement::setCharacterStream(sal_Int32 parameter, 822 const Reference< XInputStream >& /* x */, 823 sal_Int32 /* length */) 824 throw(SQLException, RuntimeException) 825 { 826 OSL_TRACE("OPreparedStatement::setCharacterStream"); 827 MutexGuard aGuard(m_aMutex); 828 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 829 checkParameterIndex(parameter); 830 831 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setCharacterStream", *this); 832 } 833 /* }}} */ 834 835 836 /* {{{ OPreparedStatement::setBinaryStream() -U- */ 837 void SAL_CALL OPreparedStatement::setBinaryStream(sal_Int32 parameter, 838 const Reference< XInputStream >& /* x */, 839 sal_Int32 /* length */) 840 throw(SQLException, RuntimeException) 841 { 842 OSL_TRACE("OPreparedStatement::setBinaryStream"); 843 MutexGuard aGuard(m_aMutex); 844 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 845 checkParameterIndex(parameter); 846 847 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::setBinaryStream", *this); 848 } 849 /* }}} */ 850 851 852 /* {{{ OPreparedStatement::clearParameters() -I- */ 853 void SAL_CALL OPreparedStatement::clearParameters() 854 throw(SQLException, RuntimeException) 855 { 856 OSL_TRACE("OPreparedStatement::clearParameters"); 857 MutexGuard aGuard(m_aMutex); 858 checkDisposed(OPreparedStatement::rBHelper.bDisposed); 859 860 try { 861 ((sql::PreparedStatement *)cppStatement)->clearParameters(); 862 } catch (sql::MethodNotImplementedException) { 863 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::clearParameters", *this); 864 } catch (sql::SQLException &e) { 865 mysqlc_sdbc_driver::translateAndThrow(e, *this, m_pConnection->getConnectionEncoding()); 866 } 867 } 868 /* }}} */ 869 870 871 /* {{{ OPreparedStatement::clearBatch() -U- */ 872 void SAL_CALL OPreparedStatement::clearBatch() 873 throw(SQLException, RuntimeException) 874 { 875 OSL_TRACE("OPreparedStatement::clearBatch"); 876 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::clearBatch", *this); 877 } 878 /* }}} */ 879 880 881 /* {{{ OPreparedStatement::addBatch() -U- */ 882 void SAL_CALL OPreparedStatement::addBatch() 883 throw(SQLException, RuntimeException) 884 { 885 OSL_TRACE("OPreparedStatement::addBatch"); 886 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OPreparedStatement::addBatch", *this); 887 } 888 /* }}} */ 889 890 891 /* {{{ OPreparedStatement::executeBatch() -I- */ 892 Sequence< sal_Int32 > SAL_CALL OPreparedStatement::executeBatch() 893 throw(SQLException, RuntimeException) 894 { 895 OSL_TRACE("OPreparedStatement::executeBatch"); 896 Sequence< sal_Int32 > aRet= Sequence< sal_Int32 > (); 897 return aRet; 898 } 899 /* }}} */ 900 901 902 /* {{{ OPreparedStatement::setFastPropertyValue_NoBroadcast() -I- */ 903 void OPreparedStatement::setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const Any& rValue) 904 throw(Exception) 905 { 906 OSL_TRACE("OPreparedStatement::setFastPropertyValue_NoBroadcast"); 907 switch(nHandle) 908 { 909 case PROPERTY_ID_RESULTSETCONCURRENCY: 910 break; 911 case PROPERTY_ID_RESULTSETTYPE: 912 break; 913 case PROPERTY_ID_FETCHDIRECTION: 914 break; 915 case PROPERTY_ID_USEBOOKMARKS: 916 break; 917 default: 918 /* XXX: Recursion ?? */ 919 OPreparedStatement::setFastPropertyValue_NoBroadcast(nHandle,rValue); 920 } 921 } 922 /* }}} */ 923 924 925 /* {{{ OPreparedStatement::checkParameterIndex() -I- */ 926 void OPreparedStatement::checkParameterIndex(sal_Int32 column) 927 { 928 OSL_TRACE("OPreparedStatement::checkColumnIndex"); 929 if (column < 1 || column > (sal_Int32) m_paramCount) { 930 OUString buf( RTL_CONSTASCII_USTRINGPARAM( "Parameter index out of range" ) ); 931 throw SQLException(buf, *this, OUString(), 1, Any ()); 932 } 933 } 934 /* }}} */ 935 936 937 /* 938 * Local variables: 939 * tab-width: 4 940 * c-basic-offset: 4 941 * End: 942 * vim600: noet sw=4 ts=4 fdm=marker 943 * vim<600: noet sw=4 ts=4 944 */ 945