From: <ibr...@us...> - 2011-12-09 22:18:17
|
Revision: 4180 http://tora.svn.sourceforge.net/tora/?rev=4180&view=rev Author: ibre5041 Date: 2011-12-09 22:18:09 +0000 (Fri, 09 Dec 2011) Log Message: ----------- Modified Paths: -------------- branches/tora-trotl/sandbox/tora3/cmake/modules/FindOracle.cmake branches/tora-trotl/sandbox/tora3/src/connection/tooracleconnection.cpp branches/tora-trotl/sandbox/tora3/src/connection/tooracleconnection.h branches/tora-trotl/sandbox/tora3/src/connection/tooraclefind.cpp branches/tora-trotl/sandbox/tora3/src/connection/tooracleprovider.cpp branches/tora-trotl/sandbox/tora3/src/connection/toteradataprovider.cpp branches/tora-trotl/sandbox/tora3/src/core/tocache.cpp branches/tora-trotl/sandbox/tora3/src/core/toconnection.cpp branches/tora-trotl/sandbox/tora3/src/core/toconnection.h branches/tora-trotl/sandbox/tora3/src/core/toconnectionprovider.cpp branches/tora-trotl/sandbox/tora3/src/core/toconnectionprovider.h branches/tora-trotl/sandbox/tora3/src/core/utils.cpp Modified: branches/tora-trotl/sandbox/tora3/cmake/modules/FindOracle.cmake =================================================================== --- branches/tora-trotl/sandbox/tora3/cmake/modules/FindOracle.cmake 2011-12-08 17:03:06 UTC (rev 4179) +++ branches/tora-trotl/sandbox/tora3/cmake/modules/FindOracle.cmake 2011-12-09 22:18:09 UTC (rev 4180) @@ -80,7 +80,7 @@ IF (ORACLE_USE_CLNTSH) FIND_LIBRARY( ORACLE_LIBRARY_CLNTSH - NAMES libclntsh clntsh oci + NAMES libclntsh libclntsh.so.11.1 clntsh oci PATHS ${ORACLE_LIB_LOCATION} # NO_DEFAULT_PATH NO_CMAKE_ENVIRONMENT_PATH NO_CMAKE_PATH NO_SYSTEM_ENVIRONMENT_PATH NO_CMAKE_SYSTEM_PATH ) Modified: branches/tora-trotl/sandbox/tora3/src/connection/tooracleconnection.cpp =================================================================== --- branches/tora-trotl/sandbox/tora3/src/connection/tooracleconnection.cpp 2011-12-08 17:03:06 UTC (rev 4179) +++ branches/tora-trotl/sandbox/tora3/src/connection/tooracleconnection.cpp 2011-12-09 22:18:09 UTC (rev 4180) @@ -1,11 +1,244 @@ +#include "core/utils.h" +#include "core/tologger.h" +#include "core/toconfiguration.h" + #include "connection/tooracleconnection.h" +#include "trotl.h" +#include "trotl_convertor.h" + +#include <QInputDialog> +#include <QLineEdit> +#include <QApplication> +#include <iomanip> + +void ThrowException(const ::trotl::OciException &exc) +{ + TLOG(0,toDecorator,__HERE__) + << "Exception thrown, what:" << exc.what() << std::endl + << exc.get_sql() << std::endl + << "--------------------------------------------------------------------------------" + << std::endl; + + toConnection::exception ret(QString::fromUtf8(exc.get_mesg())); + throw ret; +} + +toOracleConnectionImpl::toOracleConnectionImpl(toConnection &conn, ::trotl::OciEnv &env) + : toConnection::connectionImpl(conn) + , _env(env) +{ +} + toConnectionSub* toOracleConnectionImpl::createConnection(void) { - return NULL; + ::trotl::OciConnection *conn = NULL; + ::trotl::OciLogin *login = NULL; + QString oldSid; + + QSet<QString> options = connection().options(); + + bool sqlNet = (options.find("SQL*Net") != options.end()); + if (!sqlNet) + { + oldSid = getenv("ORACLE_SID"); + Utils::toSetEnv("ORACLE_SID", connection().database()); + } + try + { + int session_mode = OCI_DEFAULT; + if (options.find("SYS_OPER") != options.end()) + session_mode = OCI_SYSOPER; + else if (options.find("SYS_DBA") != options.end()) + session_mode = OCI_SYSDBA; + else if (options.find("SYS_ASM") != options.end()) + session_mode = OCI_SYSASM; + + do + { + /* TODO + if (!sqlNet) + conn->server_attach(); + else + */ + QString user = connection().user(); + QString pass = connection().password(); + + try + { + // TODO what does _login destructor? and where is it? + /*::trotl::OciLogin */ login = new ::trotl::OciLogin(_env, + ::trotl::LoginPara( + user.isEmpty() ? "" : user.toUtf8().constData(), + pass.isEmpty() ? "" : pass.toUtf8().constData(), + connection().database().toUtf8().constData() + ), + (ub4) session_mode); + conn = new ::trotl::OciConnection(_env, *login); + + TLOG(0,toDecorator,__HERE__) << "Oracle database version: " + << ::std::hex << ::std::showbase << ::std::setw(10) + << ::std::setfill('0') << ::std::internal + << login->_server._version << ::std::endl + << login->_server._version_string << ::std::endl + << login->_server.versionNumber() << "." + << login->_server.releaseNumber() << "." + << login->_server.updateNumber() << "." + << login->_server.portReleaseNumber() << "." + << login->_server.portUpdateNumber() + << ::std::dec << ::std::endl; + } + catch (const ::trotl::OciException &exc) + { + TLOG(0,toDecorator,__HERE__) << "TODO: catch" << std::endl << __HERE__ << std::endl; + + if (toThread::mainThread() && exc.get_code() == 28001) + { + bool ok = false; + QString newpass = QInputDialog::getText( + toMainWidget(), + qApp->translate("toOracleConnection", "Password expired"), + qApp->translate("toOracleConnection", "Enter new password"), + QLineEdit::Password, + QString::null, + &ok); + if (!ok) + throw exc; + + QString newpass2 = QInputDialog::getText( + toMainWidget(), + qApp->translate("toOracleConnection", "Password expired"), + qApp->translate("toOracleConnection", "Enter password again for confirmation"), + QLineEdit::Password, + QString::null, + &ok); + + if (!ok) + throw exc; + + if (newpass2 != newpass) + throw qApp->translate("toOracleConnection", "The two passwords doesn't match"); + QString nputf = newpass; + if( login ) delete login; + login = new ::trotl::OciLogin(_env, + ::trotl::LoginAndPChangePara( + user.isEmpty() ? "" : user.toUtf8().constData(), + pass.isEmpty() ? "" : pass.toUtf8().constData(), + newpass.isEmpty() ? "" : newpass.toUtf8().constData(), + connection().database().toUtf8().constData() + ), + (ub4) session_mode); + conn = new ::trotl::OciConnection(_env, *login); + + connection().setPassword(newpass); + } else { + TLOG(0,toDecorator,__HERE__) << std::endl; + throw exc; + } // (toThread::mainThread() && exc.get_code() == 28001) + } // catch (const ::trotl::OciException &exc) + } + while (!conn); + } + catch (const ::trotl::OciException &exc) + { + if (!sqlNet) + { + if (oldSid.isNull()) + Utils::toUnSetEnv("ORACLE_SID"); + else + Utils::toSetEnv("ORACLE_SID", oldSid.toLatin1()); + } + delete conn; + ThrowException(exc); + } + if (!sqlNet) + { + if (oldSid.isNull()) + Utils::toUnSetEnv("ORACLE_SID"); + else + { + Utils::toSetEnv("ORACLE_SID", oldSid.toLatin1()); + } + } + + try + { + QString str = QString::fromLatin1("ALTER SESSION SET NLS_DATE_FORMAT = '"); + str += toConfigurationSingle::Instance().dateFormat(); + str += QString::fromLatin1("'"); + ////query oracleQuery::trotlQuery date(*conn, ::std::string(str.toAscii().constData())); + } + catch (...) + { + TLOG(0,toDecorator,__HERE__) + << "Failed to set new default date format for session: " + << toConfigurationSingle::Instance().dateFormat() + << std::endl; + Utils::toStatusMessage(QObject::tr("Failed to set new default date format for session: %1") + .arg(toConfigurationSingle::Instance().dateFormat())); + } + + try + { + QString str = QString::fromLatin1("ALTER SESSION SET NLS_TIMESTAMP_FORMAT = '"); + str += toConfigurationSingle::Instance().timestampFormat(); + str += QString::fromLatin1("'"); + ////query oracleQuery::trotlQuery timestmp(*conn, ::std::string(str.toAscii().constData())); + } + catch (...) + { + TLOG(0,toDecorator,__HERE__) + << "Failed to set new default timestmp format for session: " + << toConfigurationSingle::Instance().timestampFormat() + << std::endl; + Utils::toStatusMessage(QObject::tr("Failed to set new default timestamp format for session: %1") + .arg(toConfigurationSingle::Instance().timestampFormat())); + } + + try + { + //query oracleQuery::trotlQuery info(*conn, std::string( + // "BEGIN\n" + // " SYS.DBMS_APPLICATION_INFO.SET_CLIENT_INFO('" TOAPPNAME + // " (http://tora.sf.net)" + // "');\n" + // " SYS.DBMS_APPLICATION_INFO.SET_MODULE('" TOAPPNAME "','Access Database');\n" + // "END;")); + } + catch (::trotl::OciException const& e) + { + TLOG(0,toDecorator,__HERE__) << "Failed to set client info for session:\n" << e.what(); + } + catch (...) + { + TLOG(0,toDecorator,__HERE__) << "Failed to set client info for session\n"; + } + + return new toOracleConnectionSub(conn, login); } void toOracleConnectionImpl::closeConnection(toConnectionSub *) { +} -} \ No newline at end of file +toOracleConnectionSub::toOracleConnectionSub(::trotl::OciConnection *conn, ::trotl::OciLogin *login) + : _conn(conn) + , _login(login) +{ +} + +toOracleConnectionSub::~toOracleConnectionSub() +{ +} + +void toOracleConnectionSub::close() +{ +} + +void toOracleConnectionSub::commit() +{ +} + +void toOracleConnectionSub::rollback() +{ +} Modified: branches/tora-trotl/sandbox/tora3/src/connection/tooracleconnection.h =================================================================== --- branches/tora-trotl/sandbox/tora3/src/connection/tooracleconnection.h 2011-12-08 17:03:06 UTC (rev 4179) +++ branches/tora-trotl/sandbox/tora3/src/connection/tooracleconnection.h 2011-12-09 22:18:09 UTC (rev 4180) @@ -5,25 +5,44 @@ class toOracleProvider; +namespace trotl { + class OciEnv; + class OciConnection; + class OciLogin; + class OciException; +}; + +void ThrowException(const ::trotl::OciException &exc); + class toOracleConnectionImpl: public toConnection::connectionImpl { friend class toOracleProvider; typedef toConnection::connectionImpl super; protected: - toOracleConnectionImpl(toConnection& con) : super(con) - { - }; + toOracleConnectionImpl(toConnection &conn, ::trotl::OciEnv &env); public: /** Create a new connection to the database. */ virtual toConnectionSub *createConnection(void); /** Close a connection to the database. */ virtual void closeConnection(toConnectionSub *); + + ::trotl::OciEnv &_env; }; class toOracleConnectionSub: public toConnectionSub { +public: + toOracleConnectionSub(::trotl::OciConnection *, ::trotl::OciLogin *); + virtual ~toOracleConnectionSub(); + virtual void close(); + virtual void commit(); + virtual void rollback(); + +private: + ::trotl::OciConnection *_conn; + ::trotl::OciLogin *_login; }; Modified: branches/tora-trotl/sandbox/tora3/src/connection/tooraclefind.cpp =================================================================== --- branches/tora-trotl/sandbox/tora3/src/connection/tooraclefind.cpp 2011-12-08 17:03:06 UTC (rev 4179) +++ branches/tora-trotl/sandbox/tora3/src/connection/tooraclefind.cpp 2011-12-09 22:18:09 UTC (rev 4180) @@ -264,7 +264,7 @@ continue; } - version = clientVersion(sLibPath); + //version = clientVersion(sLibPath); TLOG(5, toNoDecorator, __HERE__) << "adding: " << sLibPath << ':' << version << std::endl; ohome.insert("PATH", QDir::toNativeSeparators(d.canonicalPath())); ohome.insert("ORACLE_HOME", QDir::toNativeSeparators(d.canonicalPath())); @@ -307,7 +307,7 @@ TLOG(5, toNoDecorator, __HERE__) << "Loading:" << libPath.absoluteFilePath() << std::endl; Utils::toLibrary::LHandle hmoduleOCI = Utils::toLibrary::loadLibrary(libPath); if ( hmoduleOCI) - TLOG(5, toNoDecorator, __HERE__) << "OK" << std::endl; + TLOG(5, toNoDecorator, __HERE__) << "OK" << std::endl; TLOG(5, toNoDecorator, __HERE__) << "Loading: " TROTL_LIB << std::endl; Utils::toLibrary::LHandle hmoduleTrotl = Utils::toLibrary::loadLibrary(QFileInfo(TROTL_LIB)); Modified: branches/tora-trotl/sandbox/tora3/src/connection/tooracleprovider.cpp =================================================================== --- branches/tora-trotl/sandbox/tora3/src/connection/tooracleprovider.cpp 2011-12-08 17:03:06 UTC (rev 4179) +++ branches/tora-trotl/sandbox/tora3/src/connection/tooracleprovider.cpp 2011-12-09 22:18:09 UTC (rev 4180) @@ -44,6 +44,7 @@ #include "core/toconnectionprovider.h" #include "core/tologger.h" #include "core/utils.h" +#include "core/toconfiguration.h" #include "core/tooracleconst.h" #include "connection/absfact.h" @@ -63,8 +64,11 @@ class toOracleProvider : public toConnectionProvider { public: - toOracleProvider(toConnectionProviderFinder::ConnectionProvirerParams const& p) : toConnectionProvider(p){}; + toOracleProvider(toConnectionProviderFinder::ConnectionProvirerParams const& p); + /** see: @ref toConnectionProvider::initialize() */ + virtual bool initialize(); + /** see: @ref toConnectionProvider::name() */ virtual QString const& name() const { return m_name; }; @@ -85,10 +89,53 @@ private: static QString m_name; + ::trotl::OciEnv *_envp; + qint64 toMaxLong; + QByteArray dateFormat; }; QString toOracleProvider::m_name = ORACLE_PROVIDER; +toOracleProvider::toOracleProvider(toConnectionProviderFinder::ConnectionProvirerParams const& p) : toConnectionProvider(p) +{ +} + +bool toOracleProvider::initialize() +{ + try { + QString nls = getenv("NLS_LANG"); + if (nls.isEmpty()) + nls = "AMERICAN_AMERICA.AL32UTF8"; + else + { + int pos = nls.lastIndexOf('.'); + if (pos > 0) + nls = nls.left(pos); + nls += ".AL32UTF8"; + } + Utils::toSetEnv("NLS_LANG", nls); + + toMaxLong = toConfigurationSingle::Instance().maxLong() == -1 ? 0x80000000 : toConfigurationSingle::Instance().maxLong(); + ::trotl::g_OCIPL_MAX_LONG = toMaxLong; + + dateFormat = toConfigurationSingle::Instance().dateFormat().toAscii(); + ::trotl::g_TROTL_DEFAULT_DATE_FTM = const_cast<char*>(dateFormat.constData()); + ::trotl::OciEnvAlloc *_envallocp = new ::trotl::OciEnvAlloc; + + _envp = new ::trotl::OciEnv(*_envallocp); + // Catch trotl OCI exception and re-throw toConnection::exception + } catch (const ::trotl::OciException &exc) + { + TLOG(5, toNoDecorator, __HERE__) + << "Error loading Oracle provider: " + << exc.what() + << std::endl; + //ThrowException(exc); + return false; + } + return true; +} + QList<QString> toOracleProvider::hosts() { QList<QString> ret = QList<QString>() << QString::null << "SQL*Net"; @@ -258,7 +305,7 @@ toConnection::connectionImpl* toOracleProvider::createConnectionImpl(toConnection &con) { - return new toOracleConnectionImpl(con); + return new toOracleConnectionImpl(con, *_envp); } Util::RegisterInFactory<toOracleProvider, ConnectionProvirerFactory> regToOracleInstantProvider(ORACLE_PROVIDER); Modified: branches/tora-trotl/sandbox/tora3/src/connection/toteradataprovider.cpp =================================================================== --- branches/tora-trotl/sandbox/tora3/src/connection/toteradataprovider.cpp 2011-12-08 17:03:06 UTC (rev 4179) +++ branches/tora-trotl/sandbox/tora3/src/connection/toteradataprovider.cpp 2011-12-09 22:18:09 UTC (rev 4180) @@ -62,6 +62,9 @@ public: toTeradataProvider(toConnectionProviderFinder::ConnectionProvirerParams const& p) : toConnectionProvider(p){}; + /** see: @ref toConnectionProvider::initialize() */ + virtual bool initialize(); + /** see: @ref toConnectionProvider::name() */ virtual QString const& name() const { return m_name; }; @@ -77,12 +80,20 @@ /** see: @ref toConnectionProvider::configurationTab() */ virtual QWidget *configurationTab(QWidget *parent); + /** see: @ref toConnection */ + virtual toConnection::connectionImpl* createConnectionImpl(toConnection&); + private: static QString m_name; }; QString toTeradataProvider::m_name = "Teradata"; +bool toTeradataProvider::initialize() +{ + return true; +} + QList<QString> toTeradataProvider::hosts() { QList<QString> ret = QList<QString>() << ":1025"; // default port @@ -107,4 +118,10 @@ return NULL; } +toConnection::connectionImpl* toTeradataProvider::createConnectionImpl(toConnection &con) +{ + throw toConnection::exception("Not implemented yet."); + return NULL; +} + Util::RegisterInFactory<toTeradataProvider, ConnectionProvirerFactory> regToTeradataProvider("Teradata"); Modified: branches/tora-trotl/sandbox/tora3/src/core/tocache.cpp =================================================================== --- branches/tora-trotl/sandbox/tora3/src/core/tocache.cpp 2011-12-08 17:03:06 UTC (rev 4179) +++ branches/tora-trotl/sandbox/tora3/src/core/tocache.cpp 2011-12-09 22:18:09 UTC (rev 4180) @@ -38,7 +38,7 @@ * * END_COMMON_COPYRIGHT_HEADER */ -#include "core/toCache.h" +#include "core/tocache.h" #include "core/toconfiguration.h" #include "core/utils.h" Modified: branches/tora-trotl/sandbox/tora3/src/core/toconnection.cpp =================================================================== --- branches/tora-trotl/sandbox/tora3/src/core/toconnection.cpp 2011-12-08 17:03:06 UTC (rev 4179) +++ branches/tora-trotl/sandbox/tora3/src/core/toconnection.cpp 2011-12-09 22:18:09 UTC (rev 4180) @@ -105,8 +105,8 @@ { toLocker clock(ConnectionLock); - if (toConfigurationSingle::Instance().objectCache() == toConfiguration::ON_CONNECT) - Cache->readObjects(new cacheObjectsNew(this)); + // if (toConfigurationSingle::Instance().objectCache() == toConfiguration::ON_CONNECT) + // Cache->readObjects(new cacheObjectsNew(this)); } } Modified: branches/tora-trotl/sandbox/tora3/src/core/toconnection.h =================================================================== --- branches/tora-trotl/sandbox/tora3/src/core/toconnection.h 2011-12-08 17:03:06 UTC (rev 4179) +++ branches/tora-trotl/sandbox/tora3/src/core/toconnection.h 2011-12-09 22:18:09 UTC (rev 4180) @@ -361,13 +361,15 @@ inline toQuery *query() { return Query; } /** Get time when last query on this connection has finished */ - QDateTime lastUsed(void) { return LastUsed; } + inline QDateTime lastUsed(void) { return LastUsed; } // SETTERS + /** Set query currently running on connection. NULL means none. */ inline void setQuery(toQuery *query) { Query = query; } // ACTIONS + /** Cancel anything running on this sub. */ virtual void cancel(void) { }; @@ -378,7 +380,7 @@ virtual void rollback(void) = 0; /** Set time when last query on this connection has finished to "now" */ - void setLastUsed(void) { LastUsed = QDateTime::currentDateTime(); } + inline void setLastUsed(void) { LastUsed = QDateTime::currentDateTime(); } }; #endif Modified: branches/tora-trotl/sandbox/tora3/src/core/toconnectionprovider.cpp =================================================================== --- branches/tora-trotl/sandbox/tora3/src/core/toconnectionprovider.cpp 2011-12-08 17:03:06 UTC (rev 4179) +++ branches/tora-trotl/sandbox/tora3/src/core/toconnectionprovider.cpp 2011-12-09 22:18:09 UTC (rev 4180) @@ -56,10 +56,13 @@ std::auto_ptr<toConnectionProviderFinder> finder = ConnectionProvirerFinderFactory::Instance().create(finderName.toStdString(),0); finder->load(providerParams); - /* dll are loaded. Create instance of the toConnectioProvider */ + /* dlls are loaded. Create instance of the toConnectioProvider */ std::auto_ptr<toConnectionProvider> pProvider = ConnectionProvirerFactory::Instance().create(providerName.toStdString(), providerParams); - m_registry.insert(providerName, pProvider.release()); + if( pProvider->initialize()) + { + m_registry.insert(providerName, pProvider.release()); + } }; toConnectionProvider& toConnectionProviderRegistry::get(QString const& providerName) @@ -84,4 +87,4 @@ QList<QString> toConnectionProviderRegistry::providers() const { return m_registry.keys(); -} \ No newline at end of file +} Modified: branches/tora-trotl/sandbox/tora3/src/core/toconnectionprovider.h =================================================================== --- branches/tora-trotl/sandbox/tora3/src/core/toconnectionprovider.h 2011-12-08 17:03:06 UTC (rev 4179) +++ branches/tora-trotl/sandbox/tora3/src/core/toconnectionprovider.h 2011-12-09 22:18:09 UTC (rev 4180) @@ -157,6 +157,10 @@ public: virtual ~toConnectionProvider() {}; + /** initialize connection provider + */ + virtual bool initialize() = 0; + /** @return name of connection provider class */ virtual QString const& name () const = 0; Modified: branches/tora-trotl/sandbox/tora3/src/core/utils.cpp =================================================================== --- branches/tora-trotl/sandbox/tora3/src/core/utils.cpp 2011-12-08 17:03:06 UTC (rev 4179) +++ branches/tora-trotl/sandbox/tora3/src/core/utils.cpp 2011-12-09 22:18:09 UTC (rev 4180) @@ -1410,7 +1410,7 @@ QString oldDir = QDir::currentPath(); QDir::setCurrent(path.absolutePath()); #ifndef Q_OS_WIN32 - handle = dlopen(path.fileName().toStdString().c_str(), RTLD_LAZY | RTLD_GLOBAL); + handle = dlopen(path.fileName().toStdString().c_str(), RTLD_NOW | RTLD_GLOBAL); if( !handle) { QDir::setCurrent(oldDir); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |