update credits
[LibreOffice.git] / mysqlc / source / mysqlc_connection.cxx
blobbd9b1591209c35d6e96f2a7ac65c0300b4596759
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;
60 #define MYSQLC_URI_PREFIX "sdbc:mysqlc:"
63 /* {{{ OConnection::OConnection() -I- */
64 OConnection::OConnection(MysqlCDriver& _rDriver, sql::Driver * _cppDriver)
65 :OMetaConnection_BASE(m_aMutex)
66 ,OSubComponent<OConnection, OConnection_BASE>((::cppu::OWeakObject*)&_rDriver, this)
67 ,m_xMetaData(NULL)
68 ,m_rDriver(_rDriver)
69 ,cppDriver(_cppDriver)
70 ,m_bClosed(sal_False)
71 ,m_bUseCatalog(sal_False)
72 ,m_bUseOldDateFormat(sal_False)
74 OSL_TRACE("OConnection::OConnection");
75 m_rDriver.acquire();
77 /* }}} */
80 /* {{{ OConnection::OConnection() -I- */
81 OConnection::~OConnection()
83 OSL_TRACE("OConnection::~OConnection");
84 if (!isClosed()) {
85 close();
87 m_rDriver.release();
89 /* }}} */
92 /* {{{ OConnection::release() -I- */
93 void SAL_CALL OConnection::release()
94 throw()
96 OSL_TRACE("OConnection::release");
97 relase_ChildImpl();
99 /* }}} */
101 /* {{{ OConnection::construct() -I- */
102 void OConnection::construct(const OUString& url, const Sequence< PropertyValue >& info)
103 throw(SQLException)
105 OSL_TRACE("OConnection::construct");
106 MutexGuard aGuard(m_aMutex);
108 sal_Int32 nIndex;
109 sal_Bool bEmbedded = sal_False;
110 OUString token;
111 OUString aHostName("localhost");
112 sal_Int32 nPort = 3306;
113 OUString aDbName;
115 m_settings.encoding = m_rDriver.getDefaultEncoding();
116 m_settings.quoteIdentifier = OUString();
118 // parse url. Url has the following format:
119 // external server: sdbc:mysqlc:[hostname]:[port]/[dbname]
121 if (!url.compareTo(OUString(MYSQLC_URI_PREFIX), sizeof(MYSQLC_URI_PREFIX)-1)) {
122 nIndex = 12;
123 } else {
124 bEmbedded = sal_True;
125 nIndex = 20;
126 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OConnection::construct (embedded MySQL)", *this);
129 token = url.getToken(0, '/', nIndex);
130 if (!token.isEmpty()) {
131 sal_Int32 nIndex1 = 0;
132 OUString hostandport = token.getToken(0,':', nIndex1);
133 if (!hostandport.isEmpty()) {
134 aHostName = hostandport;
135 hostandport = token.getToken(0, ':', nIndex1);
136 if (!hostandport.isEmpty() && nIndex1) {
137 nPort = hostandport.toInt32();
139 token = url.getToken(0, '/', nIndex);
140 if (!token.isEmpty() && nIndex) {
141 aDbName = token;
146 // get user and password for mysql connection
147 const PropertyValue *pIter = info.getConstArray();
148 const PropertyValue *pEnd = pIter + info.getLength();
149 OUString aUser, aPass, sUnixSocket, sNamedPipe;
150 bool unixSocketPassed = false;
151 bool namedPipePassed = false;
153 m_settings.connectionURL = url;
154 for (;pIter != pEnd;++pIter) {
155 if (!pIter->Name.compareToAscii("user")) {
156 OSL_VERIFY( pIter->Value >>= aUser );
157 } else if (!pIter->Name.compareToAscii("password")) {
158 OSL_VERIFY( pIter->Value >>= aPass );
159 } else if (!pIter->Name.compareToAscii("LocalSocket")) {
160 OSL_VERIFY( pIter->Value >>= sUnixSocket );
161 unixSocketPassed = true;
162 } else if (!pIter->Name.compareToAscii("NamedPipe")) {
163 OSL_VERIFY( pIter->Value >>= sNamedPipe );
164 namedPipePassed = true;
165 } else if ( !pIter->Name.compareToAscii("PublicConnectionURL")) {
166 OSL_VERIFY( pIter->Value >>= m_settings.connectionURL );
167 } else if ( !pIter->Name.compareToAscii("NewURL")) { // legacy name for "PublicConnectionURL"
168 OSL_VERIFY( pIter->Value >>= m_settings.connectionURL );
172 if (bEmbedded == sal_False) {
173 try {
174 sql::ConnectOptionsMap connProps;
175 std::string host_str = OUStringToOString(aHostName, m_settings.encoding).getStr();
176 std::string user_str = OUStringToOString(aUser, m_settings.encoding).getStr();
177 std::string pass_str = OUStringToOString(aPass, m_settings.encoding).getStr();
178 std::string schema_str = OUStringToOString(aDbName, m_settings.encoding).getStr();
179 connProps["hostName"] = sql::ConnectPropertyVal(host_str);
180 connProps["userName"] = sql::ConnectPropertyVal(user_str);
181 connProps["password"] = sql::ConnectPropertyVal(pass_str);
182 connProps["schema"] = sql::ConnectPropertyVal(schema_str);
183 connProps["port"] = sql::ConnectPropertyVal((int)(nPort));
184 if (unixSocketPassed) {
185 sql::SQLString socket_str = OUStringToOString(sUnixSocket, m_settings.encoding).getStr();
186 connProps["socket"] = socket_str;
187 } else if (namedPipePassed) {
188 sql::SQLString pipe_str = OUStringToOString(sNamedPipe, m_settings.encoding).getStr();
189 connProps["socket"] = pipe_str;
192 OSL_TRACE("hostName=%s", host_str.c_str());
193 OSL_TRACE("port=%i", int(nPort));
194 OSL_TRACE("userName=%s", user_str.c_str());
195 OSL_TRACE("password=%s", pass_str.c_str());
196 OSL_TRACE("schema=%s", schema_str.c_str());
198 m_settings.cppConnection.reset(cppDriver->connect(connProps));
199 } catch (const sql::SQLException &e) {
200 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
202 } else {
203 // TODO: support for embedded server
206 m_settings.schema = aDbName;
207 OSL_TRACE("%s", OUStringToOString(m_settings.schema, getConnectionEncoding()).getStr());
209 // Check if the server is 4.1 or above
210 if (this->getMysqlVersion() < 40100) {
211 throw SQLException(
212 OUString( "MariaDB LibreOffice Connector requires MySQL Server 4.1 or above" ),
213 *this,
214 OUString(),
216 Any());
218 std::auto_ptr<sql::Statement> stmt(m_settings.cppConnection->createStatement());
219 stmt->executeUpdate("SET session sql_mode='ANSI_QUOTES'");
220 stmt->executeUpdate("SET NAMES utf8");
222 /* }}} */
225 // XServiceInfo
226 IMPLEMENT_SERVICE_INFO(OConnection, "com.sun.star.sdbc.drivers.mysqlc.OConnection", "com.sun.star.sdbc.Connection")
229 /* {{{ OConnection::createStatement() -I- */
230 Reference< XStatement > SAL_CALL OConnection::createStatement()
231 throw(SQLException, RuntimeException)
233 OSL_TRACE("OConnection::createStatement");
234 MutexGuard aGuard(m_aMutex);
235 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
237 // create a statement
238 Reference< XStatement > xReturn;
239 // the statement can only be executed once
240 try {
241 xReturn = new OStatement(this, m_settings.cppConnection->createStatement());
242 m_aStatements.push_back(WeakReferenceHelper(xReturn));
243 return xReturn;
244 } catch (const sql::SQLException & e) {
245 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
247 return xReturn;
249 /* }}} */
252 /* {{{ OConnection::createStatement() -I- */
253 Reference< XPreparedStatement > SAL_CALL OConnection::prepareStatement(const OUString& _sSql)
254 throw(SQLException, RuntimeException)
256 OSL_TRACE("OConnection::prepareStatement");
257 MutexGuard aGuard(m_aMutex);
258 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
259 const OUString sSqlStatement = transFormPreparedStatement( _sSql );
261 Reference< XPreparedStatement > xStatement;
262 try {
263 // create a statement
264 // the statement can only be executed more than once
265 xStatement = new OPreparedStatement(this,
266 m_settings.cppConnection->prepareStatement(OUStringToOString(sSqlStatement, getConnectionEncoding()).getStr()));
267 m_aStatements.push_back( WeakReferenceHelper( xStatement ) );
268 } catch (const sql::SQLException & e) {
269 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
271 return xStatement;
273 /* }}} */
276 /* {{{ OConnection::prepareCall() -U- */
277 Reference< XPreparedStatement > SAL_CALL OConnection::prepareCall(const OUString& /*_sSql*/ )
278 throw(SQLException, RuntimeException)
280 OSL_TRACE("OConnection::prepareCall");
281 MutexGuard aGuard(m_aMutex);
282 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
284 mysqlc_sdbc_driver::throwFeatureNotImplementedException("OConnection::prepareCall", *this);
285 return Reference< XPreparedStatement >();
287 /* }}} */
290 /* {{{ OConnection::nativeSQL() -I- */
291 OUString SAL_CALL OConnection::nativeSQL(const OUString& _sSql)
292 throw(SQLException, RuntimeException)
294 OSL_TRACE("OConnection::nativeSQL");
295 MutexGuard aGuard(m_aMutex);
297 const OUString sSqlStatement = transFormPreparedStatement( _sSql );
298 OUString sNativeSQL;
299 try {
300 sNativeSQL = mysqlc_sdbc_driver::convert(m_settings.cppConnection->nativeSQL(mysqlc_sdbc_driver::convert(sSqlStatement, getConnectionEncoding())),
301 getConnectionEncoding());
302 } catch (const sql::SQLException & e) {
303 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
305 return sNativeSQL;
307 /* }}} */
310 /* {{{ OConnection::setAutoCommit() -I- */
311 void SAL_CALL OConnection::setAutoCommit(sal_Bool autoCommit)
312 throw(SQLException, RuntimeException)
314 OSL_TRACE("OConnection::setAutoCommit");
315 MutexGuard aGuard(m_aMutex);
316 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
317 try {
318 m_settings.cppConnection->setAutoCommit(autoCommit == sal_True? true:false);
319 } catch (const sql::SQLException & e) {
320 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
323 /* }}} */
326 /* {{{ OConnection::getAutoCommit() -I- */
327 sal_Bool SAL_CALL OConnection::getAutoCommit()
328 throw(SQLException, RuntimeException)
330 OSL_TRACE("OConnection::getAutoCommit");
331 // you have to distinguish which if you are in autocommit mode or not
332 // at normal case true should be fine here
334 MutexGuard aGuard(m_aMutex);
335 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
337 sal_Bool autoCommit = sal_False;
338 try {
339 autoCommit = m_settings.cppConnection->getAutoCommit() == true ? sal_True : sal_False;
340 } catch (const sql::SQLException & e) {
341 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
343 return autoCommit;
345 /* }}} */
348 /* {{{ OConnection::commit() -I- */
349 void SAL_CALL OConnection::commit()
350 throw(SQLException, RuntimeException)
352 OSL_TRACE("OConnection::commit");
353 MutexGuard aGuard(m_aMutex);
354 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
355 try {
356 m_settings.cppConnection->commit();
357 } catch (const sql::SQLException & e) {
358 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
361 /* }}} */
364 /* {{{ OConnection::rollback() -I- */
365 void SAL_CALL OConnection::rollback()
366 throw(SQLException, RuntimeException)
368 OSL_TRACE("OConnection::rollback");
369 MutexGuard aGuard(m_aMutex);
370 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
371 try {
372 m_settings.cppConnection->rollback();
373 } catch (const sql::SQLException & e) {
374 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
377 /* }}} */
380 /* {{{ OConnection::isClosed() -I- */
381 sal_Bool SAL_CALL OConnection::isClosed()
382 throw(SQLException, RuntimeException)
384 OSL_TRACE("OConnection::isClosed");
385 MutexGuard aGuard(m_aMutex);
387 // just simple -> we are close when we are disposed that means someone called dispose(); (XComponent)
388 return (OConnection_BASE::rBHelper.bDisposed);
390 /* }}} */
393 /* {{{ OConnection::createStatement() -I- */
394 Reference< XDatabaseMetaData > SAL_CALL OConnection::getMetaData()
395 throw(SQLException, RuntimeException)
397 OSL_TRACE("OConnection::getMetaData");
398 MutexGuard aGuard(m_aMutex);
399 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
401 Reference< XDatabaseMetaData > xMetaData = m_xMetaData;
402 if (!xMetaData.is()) {
403 try {
404 xMetaData = new ODatabaseMetaData(*this); // need the connection because it can return it
405 } catch (const sql::SQLException & e) {
406 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
408 m_xMetaData = xMetaData;
411 return xMetaData;
413 /* }}} */
416 /* {{{ OConnection::createStatement() -I- */
417 void SAL_CALL OConnection::setReadOnly(sal_Bool readOnly)
418 throw(SQLException, RuntimeException)
420 OSL_TRACE("OConnection::setReadOnly");
421 MutexGuard aGuard(m_aMutex);
422 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
424 m_settings.readOnly = readOnly;
426 /* }}} */
429 /* {{{ OConnection::createStatement() -I- */
430 sal_Bool SAL_CALL OConnection::isReadOnly()
431 throw(SQLException, RuntimeException)
433 OSL_TRACE("OConnection::isReadOnly");
434 MutexGuard aGuard(m_aMutex);
435 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
437 // return if your connection to readonly
438 return (m_settings.readOnly);
440 /* }}} */
443 /* {{{ OConnection::createStatement() -I- */
444 void SAL_CALL OConnection::setCatalog(const OUString& catalog)
445 throw(SQLException, RuntimeException)
447 OSL_TRACE("OConnection::setCatalog");
448 MutexGuard aGuard(m_aMutex);
449 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
451 try {
452 // m_settings.cppConnection->setCatalog(OUStringToOString(catalog, m_settings.encoding).getStr());
453 m_settings.cppConnection->setSchema(OUStringToOString(catalog, getConnectionEncoding()).getStr());
454 } catch (sql::SQLException & e) {
455 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
458 /* }}} */
461 /* {{{ OConnection::createStatement() -I- */
462 OUString SAL_CALL OConnection::getCatalog()
463 throw(SQLException, RuntimeException)
465 OSL_TRACE("OConnection::getCatalog");
466 MutexGuard aGuard(m_aMutex);
467 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
469 OUString catalog;
470 try {
471 catalog = mysqlc_sdbc_driver::convert(m_settings.cppConnection->getSchema(), getConnectionEncoding());
472 } catch (const sql::SQLException & e) {
473 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
475 return catalog;
477 /* }}} */
480 /* {{{ OConnection::createStatement() -I- */
481 void SAL_CALL OConnection::setTransactionIsolation(sal_Int32 level)
482 throw(SQLException, RuntimeException)
484 OSL_TRACE("OConnection::setTransactionIsolation");
485 MutexGuard aGuard(m_aMutex);
486 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
488 sql::enum_transaction_isolation cpplevel = sql::TRANSACTION_SERIALIZABLE;
490 switch (level) {
491 case TransactionIsolation::READ_UNCOMMITTED:
492 cpplevel = sql::TRANSACTION_READ_UNCOMMITTED;
493 break;
494 case TransactionIsolation::READ_COMMITTED:
495 cpplevel = sql::TRANSACTION_READ_COMMITTED;
496 break;
497 case TransactionIsolation::REPEATABLE_READ:
498 cpplevel = sql::TRANSACTION_REPEATABLE_READ;
499 break;
500 case TransactionIsolation::SERIALIZABLE:
501 cpplevel = sql::TRANSACTION_SERIALIZABLE;
502 break;
503 case TransactionIsolation::NONE:
504 cpplevel = sql::TRANSACTION_SERIALIZABLE;
505 break;
506 default:;
507 /* XXX: Exception ?? */
509 try {
510 m_settings.cppConnection->setTransactionIsolation(cpplevel);
511 } catch (const sql::SQLException & e) {
512 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
515 /* }}} */
518 /* {{{ OConnection::createStatement() -I- */
519 sal_Int32 SAL_CALL OConnection::getTransactionIsolation()
520 throw(SQLException, RuntimeException)
522 OSL_TRACE("OConnection::getTransactionIsolation");
523 MutexGuard aGuard(m_aMutex);
524 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
526 try {
527 switch (m_settings.cppConnection->getTransactionIsolation()) {
528 case sql::TRANSACTION_SERIALIZABLE: return TransactionIsolation::SERIALIZABLE;
529 case sql::TRANSACTION_REPEATABLE_READ: return TransactionIsolation::REPEATABLE_READ;
530 case sql::TRANSACTION_READ_COMMITTED: return TransactionIsolation::READ_COMMITTED;
531 case sql::TRANSACTION_READ_UNCOMMITTED: return TransactionIsolation::READ_UNCOMMITTED;
532 default:
535 } catch (const sql::SQLException & e) {
536 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
538 return TransactionIsolation::NONE;
540 /* }}} */
543 /* {{{ OConnection::getTypeMap() -I- */
544 Reference<XNameAccess> SAL_CALL OConnection::getTypeMap()
545 throw(SQLException, RuntimeException)
547 OSL_TRACE("OConnection::getTypeMap");
548 MutexGuard aGuard(m_aMutex);
549 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
551 Reference<XNameAccess > t;
553 t = m_typeMap;
555 return (t);
557 /* }}} */
560 /* {{{ OConnection::setTypeMap() -I- */
561 void SAL_CALL OConnection::setTypeMap(const Reference<XNameAccess >& typeMap)
562 throw(SQLException, RuntimeException)
564 OSL_TRACE("OConnection::setTypeMap");
565 MutexGuard aGuard(m_aMutex);
566 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
568 m_typeMap = typeMap;
570 /* }}} */
573 // XCloseable
574 /* {{{ OConnection::close() -I- */
575 void SAL_CALL OConnection::close()
576 throw(SQLException, RuntimeException)
578 OSL_TRACE("OConnection::close");
580 we need block, because the mutex is a local variable,
581 which will guard the block
584 // we just dispose us
585 MutexGuard aGuard(m_aMutex);
586 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
588 dispose();
590 /* }}} */
593 // XWarningsSupplier
594 /* {{{ OConnection::getWarnings() -I- */
595 Any SAL_CALL OConnection::getWarnings()
596 throw(SQLException, RuntimeException)
598 Any x = Any();
599 OSL_TRACE("OConnection::getWarnings");
600 // when you collected some warnings -> return it
601 return x;
603 /* }}} */
606 /* {{{ OConnection::clearWarnings() -I- */
607 void SAL_CALL OConnection::clearWarnings()
608 throw(SQLException, RuntimeException)
610 OSL_TRACE("OConnection::clearWarnings");
611 // you should clear your collected warnings here#
613 /* }}} */
616 /* {{{ OConnection::buildTypeInfo() -I- */
617 void OConnection::buildTypeInfo()
618 throw(SQLException)
620 OSL_TRACE("OConnection::buildTypeInfo");
622 /* }}} */
625 /* {{{ OConnection::disposing() -I- */
626 void OConnection::disposing()
628 OSL_TRACE("OConnection::disposing");
629 // we noticed that we should be destroied in near future so we have to dispose our statements
630 MutexGuard aGuard(m_aMutex);
632 for (OWeakRefArray::iterator i = m_aStatements.begin(); i != m_aStatements.end() ; ++i) {
633 Reference< XComponent > xComp(i->get(), UNO_QUERY);
634 if (xComp.is()) {
635 xComp->dispose();
638 m_aStatements.clear();
640 m_bClosed = sal_True;
641 m_xMetaData = WeakReference< XDatabaseMetaData >();
643 dispose_ChildImpl();
644 OConnection_BASE::disposing();
646 /* }}} */
649 /* ToDo - upcast the connection to MySQL_Connection and use ::getSessionVariable() */
651 /* {{{ OConnection::getMysqlVariable() -I- */
652 OUString OConnection::getMysqlVariable(const char *varname)
653 throw(SQLException, RuntimeException)
655 OSL_TRACE("OConnection::getMysqlVariable");
656 MutexGuard aGuard(m_aMutex);
657 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
659 OUString ret;
660 OUStringBuffer aStatement;
661 aStatement.appendAscii( "SHOW SESSION VARIABLES LIKE '" );
662 aStatement.appendAscii( varname );
663 aStatement.append( sal_Unicode( '\'' ) );
665 try {
666 XStatement * stmt = new OStatement(this, m_settings.cppConnection->createStatement());
667 Reference< XResultSet > rs = stmt->executeQuery( aStatement.makeStringAndClear() );
668 if (rs.is() && rs->next()) {
669 Reference< XRow > xRow(rs, UNO_QUERY);
670 ret = xRow->getString(2);
672 } catch (const sql::SQLException & e) {
673 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
676 return ret;
678 /* }}} */
681 /* {{{ OConnection::getMysqlVersion() -I- */
682 sal_Int32 OConnection::getMysqlVersion()
683 throw(SQLException, RuntimeException)
685 OSL_TRACE("OConnection::getMysqlVersion");
686 MutexGuard aGuard(m_aMutex);
687 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
689 sal_Int32 version(0);
690 try {
691 version = 10000 * m_settings.cppConnection->getMetaData()->getDatabaseMajorVersion();
692 version += 100 * m_settings.cppConnection->getMetaData()->getDatabaseMinorVersion();
693 version += m_settings.cppConnection->getMetaData()->getDatabasePatchVersion();
694 } catch (const sql::SQLException & e) {
695 mysqlc_sdbc_driver::translateAndThrow(e, *this, getConnectionEncoding());
697 return version;
699 /* }}} */
702 /* {{{ OConnection::sdbcColumnType() -I- */
703 // TODO: Not used
704 //sal_Int32 OConnection::sdbcColumnType(OUString typeName)
706 // OSL_TRACE("OConnection::sdbcColumnType");
707 // int i = 0;
708 // while (mysqlc_types[i].typeName) {
709 // if (OUString::createFromAscii(mysqlc_types[i].typeName).equals(
710 // typeName.toAsciiUpperCase()))
711 // {
712 // return mysqlc_types[i].dataType;
713 // }
714 // i++;
715 // }
716 // return 0;
718 // -----------------------------------------------------------------------------
719 OUString OConnection::transFormPreparedStatement(const OUString& _sSQL)
721 OUString sSqlStatement = _sSQL;
722 if ( !m_xParameterSubstitution.is() ) {
723 try {
724 Sequence< Any > aArgs(1);
725 Reference< XConnection> xCon = this;
726 aArgs[0] <<= NamedValue(OUString("ActiveConnection"), makeAny(xCon));
728 m_xParameterSubstitution.set(m_rDriver.getFactory()->createInstanceWithArguments(OUString("org.openoffice.comp.helper.ParameterSubstitution"),aArgs),UNO_QUERY);
729 } catch(const Exception&) {}
731 if ( m_xParameterSubstitution.is() ) {
732 try {
733 sSqlStatement = m_xParameterSubstitution->substituteVariables(sSqlStatement,sal_True);
734 } catch(const Exception&) { }
736 return sSqlStatement;
739 /* }}} */
742 * Local variables:
743 * tab-width: 4
744 * c-basic-offset: 4
745 * End:
746 * vim600: noet sw=4 ts=4 fdm=marker
747 * vim<600: noet sw=4 ts=4
750 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */