Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / mysqlc / source / mysqlc_connection.cxx
blobe23d73541fab2a2addd5933989be4ca8d6c2e599
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "mysqlc_connection.hxx"
21 #include "mysqlc_databasemetadata.hxx"
24 #include "mysqlc_driver.hxx"
25 #include "mysqlc_statement.hxx"
26 #include "mysqlc_preparedstatement.hxx"
27 #include "mysqlc_general.hxx"
29 #include <cppconn/driver.h>
30 #include <cppconn/connection.h>
31 #include <cppconn/statement.h>
32 #include <cppconn/metadata.h>
33 #include <cppconn/exception.h>
35 #include <com/sun/star/sdbc/ColumnValue.hpp>
36 #include <com/sun/star/sdbc/XRow.hpp>
37 #include <com/sun/star/sdbc/TransactionIsolation.hpp>
38 #include <com/sun/star/lang/DisposedException.hpp>
39 #include <com/sun/star/beans/NamedValue.hpp>
41 #include <osl/module.hxx>
42 #include <osl/thread.h>
43 #include <osl/file.h>
44 #include <rtl/uri.hxx>
45 #include <rtl/ustrbuf.hxx>
47 using namespace connectivity::mysqlc;
49 #include <stdio.h>
51 //------------------------------------------------------------------------------
52 using namespace com::sun::star::uno;
53 using namespace com::sun::star::container;
54 using namespace com::sun::star::lang;
55 using namespace com::sun::star::beans;
56 using namespace com::sun::star::sdbc;
57 using ::osl::MutexGuard;
58 using ::rtl::OUString;
61 #define MYSQLC_URI_PREFIX "sdbc:mysqlc:"
64 /* {{{ OConnection::OConnection() -I- */
65 OConnection::OConnection(MysqlCDriver& _rDriver, sql::Driver * _cppDriver)
66 :OMetaConnection_BASE(m_aMutex)
67 ,OSubComponent<OConnection, OConnection_BASE>((::cppu::OWeakObject*)&_rDriver, this)
68 ,m_xMetaData(NULL)
69 ,m_rDriver(_rDriver)
70 ,cppDriver(_cppDriver)
71 ,m_bClosed(sal_False)
72 ,m_bUseCatalog(sal_False)
73 ,m_bUseOldDateFormat(sal_False)
75 OSL_TRACE("OConnection::OConnection");
76 m_rDriver.acquire();
78 /* }}} */
81 /* {{{ OConnection::OConnection() -I- */
82 OConnection::~OConnection()
84 OSL_TRACE("OConnection::~OConnection");
85 if (!isClosed()) {
86 close();
88 m_rDriver.release();
90 /* }}} */
93 /* {{{ OConnection::release() -I- */
94 void SAL_CALL OConnection::release()
95 throw()
97 OSL_TRACE("OConnection::release");
98 relase_ChildImpl();
100 /* }}} */
102 #ifndef SYSTEM_MYSQL
103 extern "C" { void SAL_CALL thisModule() {} }
104 #endif
106 /* {{{ OConnection::construct() -I- */
107 void OConnection::construct(const OUString& url, const Sequence< PropertyValue >& info)
108 throw(SQLException)
110 OSL_TRACE("OConnection::construct");
111 MutexGuard aGuard(m_aMutex);
113 sal_Int32 nIndex;
114 sal_Bool bEmbedded = sal_False;
115 OUString token;
116 OUString aHostName("localhost");
117 sal_Int32 nPort = 3306;
118 OUString aDbName;
120 m_settings.encoding = m_rDriver.getDefaultEncoding();
121 m_settings.quoteIdentifier = OUString();
123 // parse url. Url has the following format:
124 // external server: sdbc:mysqlc:[hostname]:[port]/[dbname]
126 if (!url.compareTo(OUString(MYSQLC_URI_PREFIX), sizeof(MYSQLC_URI_PREFIX)-1)) {
127 nIndex = 12;
128 } else {
129 bEmbedded = sal_True;
130 nIndex = 20;
131 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OConnection::construct (embedded MySQL)", *this);
134 token = url.getToken(0, '/', nIndex);
135 if (!token.isEmpty()) {
136 sal_Int32 nIndex1 = 0;
137 OUString hostandport = token.getToken(0,':', nIndex1);
138 if (!hostandport.isEmpty()) {
139 aHostName = hostandport;
140 hostandport = token.getToken(0, ':', nIndex1);
141 if (!hostandport.isEmpty() && nIndex1) {
142 nPort = hostandport.toInt32();
144 token = url.getToken(0, '/', nIndex);
145 if (!token.isEmpty() && nIndex) {
146 aDbName = token;
151 // get user and password for mysql connection
152 const PropertyValue *pIter = info.getConstArray();
153 const PropertyValue *pEnd = pIter + info.getLength();
154 OUString aUser, aPass, sUnixSocket, sNamedPipe;
155 bool unixSocketPassed = false;
156 bool namedPipePassed = false;
158 m_settings.connectionURL = url;
159 for (;pIter != pEnd;++pIter) {
160 if (!pIter->Name.compareToAscii("user")) {
161 OSL_VERIFY( pIter->Value >>= aUser );
162 } else if (!pIter->Name.compareToAscii("password")) {
163 OSL_VERIFY( pIter->Value >>= aPass );
164 } else if (!pIter->Name.compareToAscii("LocalSocket")) {
165 OSL_VERIFY( pIter->Value >>= sUnixSocket );
166 unixSocketPassed = true;
167 } else if (!pIter->Name.compareToAscii("NamedPipe")) {
168 OSL_VERIFY( pIter->Value >>= sNamedPipe );
169 namedPipePassed = true;
170 } else if ( !pIter->Name.compareToAscii("PublicConnectionURL")) {
171 OSL_VERIFY( pIter->Value >>= m_settings.connectionURL );
172 } else if ( !pIter->Name.compareToAscii("NewURL")) { // legacy name for "PublicConnectionURL"
173 OSL_VERIFY( pIter->Value >>= m_settings.connectionURL );
177 if (bEmbedded == sal_False) {
178 try {
179 sql::ConnectOptionsMap connProps;
180 std::string host_str = OUStringToOString(aHostName, m_settings.encoding).getStr();
181 std::string user_str = OUStringToOString(aUser, m_settings.encoding).getStr();
182 std::string pass_str = OUStringToOString(aPass, m_settings.encoding).getStr();
183 std::string schema_str = OUStringToOString(aDbName, m_settings.encoding).getStr();
184 connProps["hostName"] = sql::ConnectPropertyVal(host_str);
185 connProps["userName"] = sql::ConnectPropertyVal(user_str);
186 connProps["password"] = sql::ConnectPropertyVal(pass_str);
187 connProps["schema"] = sql::ConnectPropertyVal(schema_str);
188 connProps["port"] = sql::ConnectPropertyVal((int)(nPort));
189 if (unixSocketPassed) {
190 sql::SQLString socket_str = OUStringToOString(sUnixSocket, m_settings.encoding).getStr();
191 connProps["socket"] = socket_str;
192 } else if (namedPipePassed) {
193 sql::SQLString pipe_str = OUStringToOString(sNamedPipe, m_settings.encoding).getStr();
194 connProps["socket"] = pipe_str;
197 #ifndef SYSTEM_MYSQL
198 ::rtl::OUString sMySQLClientLib( MYSQL_LIB );
200 ::rtl::OUString moduleBase;
201 OSL_VERIFY( ::osl::Module::getUrlFromAddress( &thisModule, moduleBase ) );
202 ::rtl::OUString sMySQLClientLibURL;
205 sMySQLClientLibURL = ::rtl::Uri::convertRelToAbs( moduleBase, sMySQLClientLib.pData );
207 catch ( const ::rtl::MalformedUriException& e )
209 (void)e; // silence compiler
210 #if OSL_DEBUG_LEVEL > 0
211 ::rtl::OString sMessage( "OConnection::construct: malformed URI: " );
212 sMessage += ::rtl::OUStringToOString( e.getMessage(), osl_getThreadTextEncoding() );
213 OSL_FAIL( sMessage.getStr() );
214 #endif
217 ::rtl::OUString sMySQLClientLibPath;
218 osl_getSystemPathFromFileURL( sMySQLClientLibURL.pData, &sMySQLClientLibPath.pData );
220 sql::SQLString mysqlLib = ::rtl::OUStringToOString( sMySQLClientLibPath, osl_getThreadTextEncoding() ).getStr();
221 connProps["clientlib"] = mysqlLib;
223 OSL_TRACE("clientlib=%s", mysqlLib.c_str());
224 #endif
226 OSL_TRACE("hostName=%s", host_str.c_str());
227 OSL_TRACE("port=%i", int(nPort));
228 OSL_TRACE("userName=%s", user_str.c_str());
229 OSL_TRACE("password=%s", pass_str.c_str());
230 OSL_TRACE("schema=%s", schema_str.c_str());
232 m_settings.cppConnection.reset(cppDriver->connect(connProps));
233 } catch (const sql::SQLException &e) {
234 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
236 } else {
237 // TODO: support for embedded server
240 m_settings.schema = aDbName;
241 OSL_TRACE("%s", OUStringToOString(m_settings.schema, getConnectionEncoding()).getStr());
243 // Check if the server is 4.1 or above
244 if (this->getMysqlVersion() < 40100) {
245 throw SQLException(
246 ::rtl::OUString( "MySQL Connector/OO.org requires MySQL Server 4.1 or above" ),
247 *this,
248 ::rtl::OUString(),
250 Any());
252 std::auto_ptr<sql::Statement> stmt(m_settings.cppConnection->createStatement());
253 stmt->executeUpdate("SET session sql_mode='ANSI_QUOTES'");
254 stmt->executeUpdate("SET NAMES utf8");
256 /* }}} */
259 // XServiceInfo
260 IMPLEMENT_SERVICE_INFO(OConnection, "com.sun.star.sdbc.drivers.mysqlc.OConnection", "com.sun.star.sdbc.Connection")
263 /* {{{ OConnection::createStatement() -I- */
264 Reference< XStatement > SAL_CALL OConnection::createStatement()
265 throw(SQLException, RuntimeException)
267 OSL_TRACE("OConnection::createStatement");
268 MutexGuard aGuard(m_aMutex);
269 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
271 // create a statement
272 Reference< XStatement > xReturn;
273 // the statement can only be executed once
274 try {
275 xReturn = new OStatement(this, m_settings.cppConnection->createStatement());
276 m_aStatements.push_back(WeakReferenceHelper(xReturn));
277 return xReturn;
278 } catch (const sql::SQLException & e) {
279 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
281 return xReturn;
283 /* }}} */
286 /* {{{ OConnection::createStatement() -I- */
287 Reference< XPreparedStatement > SAL_CALL OConnection::prepareStatement(const OUString& _sSql)
288 throw(SQLException, RuntimeException)
290 OSL_TRACE("OConnection::prepareStatement");
291 MutexGuard aGuard(m_aMutex);
292 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
293 const ::rtl::OUString sSqlStatement = transFormPreparedStatement( _sSql );
295 Reference< XPreparedStatement > xStatement;
296 try {
297 // create a statement
298 // the statement can only be executed more than once
299 xStatement = new OPreparedStatement(this,
300 m_settings.cppConnection->prepareStatement(OUStringToOString(sSqlStatement, getConnectionEncoding()).getStr()));
301 m_aStatements.push_back( WeakReferenceHelper( xStatement ) );
302 } catch (const sql::SQLException & e) {
303 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
305 return xStatement;
307 /* }}} */
310 /* {{{ OConnection::prepareCall() -U- */
311 Reference< XPreparedStatement > SAL_CALL OConnection::prepareCall(const OUString& /*_sSql*/ )
312 throw(SQLException, RuntimeException)
314 OSL_TRACE("OConnection::prepareCall");
315 MutexGuard aGuard(m_aMutex);
316 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
318 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OConnection::prepareCall", *this);
319 return Reference< XPreparedStatement >();
321 /* }}} */
324 /* {{{ OConnection::nativeSQL() -I- */
325 OUString SAL_CALL OConnection::nativeSQL(const OUString& _sSql)
326 throw(SQLException, RuntimeException)
328 OSL_TRACE("OConnection::nativeSQL");
329 MutexGuard aGuard(m_aMutex);
331 const ::rtl::OUString sSqlStatement = transFormPreparedStatement( _sSql );
332 ::rtl::OUString sNativeSQL;
333 try {
334 sNativeSQL = mysqlc_sdbc_driver::convert(m_settings.cppConnection->nativeSQL(mysqlc_sdbc_driver::convert(sSqlStatement, getConnectionEncoding())),
335 getConnectionEncoding());
336 } catch (const sql::SQLException & e) {
337 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
339 return sNativeSQL;
341 /* }}} */
344 /* {{{ OConnection::setAutoCommit() -I- */
345 void SAL_CALL OConnection::setAutoCommit(sal_Bool autoCommit)
346 throw(SQLException, RuntimeException)
348 OSL_TRACE("OConnection::setAutoCommit");
349 MutexGuard aGuard(m_aMutex);
350 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
351 try {
352 m_settings.cppConnection->setAutoCommit(autoCommit == sal_True? true:false);
353 } catch (const sql::SQLException & e) {
354 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
357 /* }}} */
360 /* {{{ OConnection::getAutoCommit() -I- */
361 sal_Bool SAL_CALL OConnection::getAutoCommit()
362 throw(SQLException, RuntimeException)
364 OSL_TRACE("OConnection::getAutoCommit");
365 // you have to distinguish which if you are in autocommit mode or not
366 // at normal case true should be fine here
368 MutexGuard aGuard(m_aMutex);
369 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
371 sal_Bool autoCommit = sal_False;
372 try {
373 autoCommit = m_settings.cppConnection->getAutoCommit() == true ? sal_True : sal_False;
374 } catch (const sql::SQLException & e) {
375 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
377 return autoCommit;
379 /* }}} */
382 /* {{{ OConnection::commit() -I- */
383 void SAL_CALL OConnection::commit()
384 throw(SQLException, RuntimeException)
386 OSL_TRACE("OConnection::commit");
387 MutexGuard aGuard(m_aMutex);
388 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
389 try {
390 m_settings.cppConnection->commit();
391 } catch (const sql::SQLException & e) {
392 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
395 /* }}} */
398 /* {{{ OConnection::rollback() -I- */
399 void SAL_CALL OConnection::rollback()
400 throw(SQLException, RuntimeException)
402 OSL_TRACE("OConnection::rollback");
403 MutexGuard aGuard(m_aMutex);
404 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
405 try {
406 m_settings.cppConnection->rollback();
407 } catch (const sql::SQLException & e) {
408 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
411 /* }}} */
414 /* {{{ OConnection::isClosed() -I- */
415 sal_Bool SAL_CALL OConnection::isClosed()
416 throw(SQLException, RuntimeException)
418 OSL_TRACE("OConnection::isClosed");
419 MutexGuard aGuard(m_aMutex);
421 // just simple -> we are close when we are disposed taht means someone called dispose(); (XComponent)
422 return (OConnection_BASE::rBHelper.bDisposed);
424 /* }}} */
427 /* {{{ OConnection::createStatement() -I- */
428 Reference< XDatabaseMetaData > SAL_CALL OConnection::getMetaData()
429 throw(SQLException, RuntimeException)
431 OSL_TRACE("OConnection::getMetaData");
432 MutexGuard aGuard(m_aMutex);
433 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
435 Reference< XDatabaseMetaData > xMetaData = m_xMetaData;
436 if (!xMetaData.is()) {
437 try {
438 xMetaData = new ODatabaseMetaData(*this); // need the connection because it can return it
439 } catch (const sql::SQLException & e) {
440 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
442 m_xMetaData = xMetaData;
445 return xMetaData;
447 /* }}} */
450 /* {{{ OConnection::createStatement() -I- */
451 void SAL_CALL OConnection::setReadOnly(sal_Bool readOnly)
452 throw(SQLException, RuntimeException)
454 OSL_TRACE("OConnection::setReadOnly");
455 MutexGuard aGuard(m_aMutex);
456 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
458 m_settings.readOnly = readOnly;
460 /* }}} */
463 /* {{{ OConnection::createStatement() -I- */
464 sal_Bool SAL_CALL OConnection::isReadOnly()
465 throw(SQLException, RuntimeException)
467 OSL_TRACE("OConnection::isReadOnly");
468 MutexGuard aGuard(m_aMutex);
469 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
471 // return if your connection to readonly
472 return (m_settings.readOnly);
474 /* }}} */
477 /* {{{ OConnection::createStatement() -I- */
478 void SAL_CALL OConnection::setCatalog(const OUString& catalog)
479 throw(SQLException, RuntimeException)
481 OSL_TRACE("OConnection::setCatalog");
482 MutexGuard aGuard(m_aMutex);
483 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
485 try {
486 // m_settings.cppConnection->setCatalog(OUStringToOString(catalog, m_settings.encoding).getStr());
487 m_settings.cppConnection->setSchema(OUStringToOString(catalog, getConnectionEncoding()).getStr());
488 } catch (sql::SQLException & e) {
489 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
492 /* }}} */
495 /* {{{ OConnection::createStatement() -I- */
496 OUString SAL_CALL OConnection::getCatalog()
497 throw(SQLException, RuntimeException)
499 OSL_TRACE("OConnection::getCatalog");
500 MutexGuard aGuard(m_aMutex);
501 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
503 OUString catalog;
504 try {
505 catalog = mysqlc_sdbc_driver::convert(m_settings.cppConnection->getSchema(), getConnectionEncoding());
506 } catch (const sql::SQLException & e) {
507 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
509 return catalog;
511 /* }}} */
514 /* {{{ OConnection::createStatement() -I- */
515 void SAL_CALL OConnection::setTransactionIsolation(sal_Int32 level)
516 throw(SQLException, RuntimeException)
518 OSL_TRACE("OConnection::setTransactionIsolation");
519 MutexGuard aGuard(m_aMutex);
520 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
522 sql::enum_transaction_isolation cpplevel = sql::TRANSACTION_SERIALIZABLE;
524 switch (level) {
525 case TransactionIsolation::READ_UNCOMMITTED:
526 cpplevel = sql::TRANSACTION_READ_UNCOMMITTED;
527 break;
528 case TransactionIsolation::READ_COMMITTED:
529 cpplevel = sql::TRANSACTION_READ_COMMITTED;
530 break;
531 case TransactionIsolation::REPEATABLE_READ:
532 cpplevel = sql::TRANSACTION_REPEATABLE_READ;
533 break;
534 case TransactionIsolation::SERIALIZABLE:
535 cpplevel = sql::TRANSACTION_SERIALIZABLE;
536 break;
537 case TransactionIsolation::NONE:
538 cpplevel = sql::TRANSACTION_SERIALIZABLE;
539 break;
540 default:;
541 /* XXX: Exception ?? */
543 try {
544 m_settings.cppConnection->setTransactionIsolation(cpplevel);
545 } catch (const sql::SQLException & e) {
546 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
549 /* }}} */
552 /* {{{ OConnection::createStatement() -I- */
553 sal_Int32 SAL_CALL OConnection::getTransactionIsolation()
554 throw(SQLException, RuntimeException)
556 OSL_TRACE("OConnection::getTransactionIsolation");
557 MutexGuard aGuard(m_aMutex);
558 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
560 try {
561 switch (m_settings.cppConnection->getTransactionIsolation()) {
562 case sql::TRANSACTION_SERIALIZABLE: return TransactionIsolation::SERIALIZABLE;
563 case sql::TRANSACTION_REPEATABLE_READ: return TransactionIsolation::REPEATABLE_READ;
564 case sql::TRANSACTION_READ_COMMITTED: return TransactionIsolation::READ_COMMITTED;
565 case sql::TRANSACTION_READ_UNCOMMITTED: return TransactionIsolation::READ_UNCOMMITTED;
566 default:
569 } catch (const sql::SQLException & e) {
570 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
572 return TransactionIsolation::NONE;
574 /* }}} */
577 /* {{{ OConnection::getTypeMap() -I- */
578 Reference<XNameAccess> SAL_CALL OConnection::getTypeMap()
579 throw(SQLException, RuntimeException)
581 OSL_TRACE("OConnection::getTypeMap");
582 MutexGuard aGuard(m_aMutex);
583 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
585 Reference<XNameAccess > t;
587 t = m_typeMap;
589 return (t);
591 /* }}} */
594 /* {{{ OConnection::setTypeMap() -I- */
595 void SAL_CALL OConnection::setTypeMap(const Reference<XNameAccess >& typeMap)
596 throw(SQLException, RuntimeException)
598 OSL_TRACE("OConnection::setTypeMap");
599 MutexGuard aGuard(m_aMutex);
600 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
602 m_typeMap = typeMap;
604 /* }}} */
607 // XCloseable
608 /* {{{ OConnection::close() -I- */
609 void SAL_CALL OConnection::close()
610 throw(SQLException, RuntimeException)
612 OSL_TRACE("OConnection::close");
614 we need block, because the mutex is a local variable,
615 which will guard the block
618 // we just dispose us
619 MutexGuard aGuard(m_aMutex);
620 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
622 dispose();
624 /* }}} */
627 // XWarningsSupplier
628 /* {{{ OConnection::getWarnings() -I- */
629 Any SAL_CALL OConnection::getWarnings()
630 throw(SQLException, RuntimeException)
632 Any x = Any();
633 OSL_TRACE("OConnection::getWarnings");
634 // when you collected some warnings -> return it
635 return x;
637 /* }}} */
640 /* {{{ OConnection::clearWarnings() -I- */
641 void SAL_CALL OConnection::clearWarnings()
642 throw(SQLException, RuntimeException)
644 OSL_TRACE("OConnection::clearWarnings");
645 // you should clear your collected warnings here#
647 /* }}} */
650 /* {{{ OConnection::buildTypeInfo() -I- */
651 void OConnection::buildTypeInfo()
652 throw(SQLException)
654 OSL_TRACE("OConnection::buildTypeInfo");
656 /* }}} */
659 /* {{{ OConnection::disposing() -I- */
660 void OConnection::disposing()
662 OSL_TRACE("OConnection::disposing");
663 // we noticed that we should be destroied in near future so we have to dispose our statements
664 MutexGuard aGuard(m_aMutex);
666 for (OWeakRefArray::iterator i = m_aStatements.begin(); i != m_aStatements.end() ; ++i) {
667 Reference< XComponent > xComp(i->get(), UNO_QUERY);
668 if (xComp.is()) {
669 xComp->dispose();
672 m_aStatements.clear();
674 m_bClosed = sal_True;
675 m_xMetaData = WeakReference< XDatabaseMetaData >();
677 dispose_ChildImpl();
678 OConnection_BASE::disposing();
680 /* }}} */
683 /* ToDo - upcast the connection to MySQL_Connection and use ::getSessionVariable() */
685 /* {{{ OConnection::getMysqlVariable() -I- */
686 OUString OConnection::getMysqlVariable(const char *varname)
687 throw(SQLException, RuntimeException)
689 OSL_TRACE("OConnection::getMysqlVariable");
690 MutexGuard aGuard(m_aMutex);
691 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
693 OUString ret;
694 ::rtl::OUStringBuffer aStatement;
695 aStatement.appendAscii( "SHOW SESSION VARIABLES LIKE '" );
696 aStatement.appendAscii( varname );
697 aStatement.append( sal_Unicode( '\'' ) );
699 try {
700 XStatement * stmt = new OStatement(this, m_settings.cppConnection->createStatement());
701 Reference< XResultSet > rs = stmt->executeQuery( aStatement.makeStringAndClear() );
702 if (rs.is() && rs->next()) {
703 Reference< XRow > xRow(rs, UNO_QUERY);
704 ret = xRow->getString(2);
706 } catch (const sql::SQLException & e) {
707 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
710 return ret;
712 /* }}} */
715 /* {{{ OConnection::getMysqlVersion() -I- */
716 sal_Int32 OConnection::getMysqlVersion()
717 throw(SQLException, RuntimeException)
719 OSL_TRACE("OConnection::getMysqlVersion");
720 MutexGuard aGuard(m_aMutex);
721 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
723 sal_Int32 version(0);
724 try {
725 version = 10000 * m_settings.cppConnection->getMetaData()->getDatabaseMajorVersion();
726 version += 100 * m_settings.cppConnection->getMetaData()->getDatabaseMinorVersion();
727 version += m_settings.cppConnection->getMetaData()->getDatabasePatchVersion();
728 } catch (const sql::SQLException & e) {
729 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
731 return version;
733 /* }}} */
736 /* {{{ OConnection::sdbcColumnType() -I- */
737 // TODO: Not used
738 //sal_Int32 OConnection::sdbcColumnType(OUString typeName)
740 // OSL_TRACE("OConnection::sdbcColumnType");
741 // int i = 0;
742 // while (mysqlc_types[i].typeName) {
743 // if (OUString::createFromAscii(mysqlc_types[i].typeName).equals(
744 // typeName.toAsciiUpperCase()))
745 // {
746 // return mysqlc_types[i].dataType;
747 // }
748 // i++;
749 // }
750 // return 0;
752 // -----------------------------------------------------------------------------
753 ::rtl::OUString OConnection::transFormPreparedStatement(const ::rtl::OUString& _sSQL)
755 ::rtl::OUString sSqlStatement = _sSQL;
756 if ( !m_xParameterSubstitution.is() ) {
757 try {
758 Sequence< Any > aArgs(1);
759 Reference< XConnection> xCon = this;
760 aArgs[0] <<= NamedValue(::rtl::OUString("ActiveConnection"), makeAny(xCon));
762 m_xParameterSubstitution.set(m_rDriver.getFactory()->createInstanceWithArguments(::rtl::OUString("org.openoffice.comp.helper.ParameterSubstitution"),aArgs),UNO_QUERY);
763 } catch(const Exception&) {}
765 if ( m_xParameterSubstitution.is() ) {
766 try {
767 sSqlStatement = m_xParameterSubstitution->substituteVariables(sSqlStatement,sal_True);
768 } catch(const Exception&) { }
770 return sSqlStatement;
773 /* }}} */
776 * Local variables:
777 * tab-width: 4
778 * c-basic-offset: 4
779 * End:
780 * vim600: noet sw=4 ts=4 fdm=marker
781 * vim<600: noet sw=4 ts=4
784 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */