mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-14 23:07:56 +02:00
SF [2643953] Improve Data::Session connection
This commit is contained in:
@@ -64,9 +64,10 @@ const std::string& Connector::name() const
|
||||
return KEY;
|
||||
}
|
||||
|
||||
Poco::AutoPtr<Poco::Data::SessionImpl> Connector::createSession(const std::string& connectionString)
|
||||
Poco::AutoPtr<Poco::Data::SessionImpl> Connector::createSession(const std::string& connectionString,
|
||||
std::size_t timeout)
|
||||
{
|
||||
return Poco::AutoPtr<Poco::Data::SessionImpl>(new SessionImpl(connectionString));
|
||||
return Poco::AutoPtr<Poco::Data::SessionImpl>(new SessionImpl(connectionString, timeout));
|
||||
}
|
||||
|
||||
|
||||
|
@@ -35,6 +35,7 @@
|
||||
|
||||
|
||||
#include "Poco/Data/MySQL/SessionHandle.h"
|
||||
#include "Poco/Data/DataException.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
@@ -42,10 +43,20 @@ namespace Data {
|
||||
namespace MySQL {
|
||||
|
||||
|
||||
SessionHandle::SessionHandle(MYSQL* mysql)
|
||||
SessionHandle::SessionHandle(MYSQL* mysql): _pHandle(0)
|
||||
{
|
||||
if (!(_pHandle = mysql_init(mysql)))
|
||||
throw ConnectionException("mysql_init error");
|
||||
init(mysql);
|
||||
}
|
||||
|
||||
|
||||
void SessionHandle::init(MYSQL* mysql)
|
||||
{
|
||||
if (!_pHandle)
|
||||
{
|
||||
_pHandle = mysql_init(mysql);
|
||||
if (!_pHandle)
|
||||
throw ConnectionException("mysql_init error");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -69,11 +80,17 @@ void SessionHandle::options(mysql_option opt, bool b)
|
||||
throw ConnectionException("mysql_options error", _pHandle);
|
||||
}
|
||||
|
||||
void SessionHandle::options(mysql_option opt, unsigned int i)
|
||||
{
|
||||
if (mysql_options(_pHandle, opt, &i) != 0)
|
||||
throw ConnectionException("mysql_options error", _pHandle);
|
||||
}
|
||||
|
||||
|
||||
void SessionHandle::connect(const char* host, const char* user, const char* password, const char* db, unsigned int port)
|
||||
{
|
||||
if (!mysql_real_connect(_pHandle, host, user, password, db, port, 0, 0))
|
||||
throw ConnectionException("create session: mysql_real_connect error", _pHandle);
|
||||
throw ConnectionFailedException("mysql_real_connect error");
|
||||
}
|
||||
|
||||
|
||||
|
@@ -66,8 +66,8 @@ const std::string SessionImpl::MYSQL_REPEATABLE_READ = "REPEATABLE READ";
|
||||
const std::string SessionImpl::MYSQL_SERIALIZABLE = "SERIALIZABLE";
|
||||
|
||||
|
||||
SessionImpl::SessionImpl(const std::string& connectionString) :
|
||||
Poco::Data::AbstractSessionImpl<SessionImpl>(toLower(connectionString)),
|
||||
SessionImpl::SessionImpl(const std::string& connectionString, std::size_t timeout) :
|
||||
Poco::Data::AbstractSessionImpl<SessionImpl>(toLower(connectionString), timeout),
|
||||
_handle(0),
|
||||
_connected(false),
|
||||
_inTransaction(false)
|
||||
@@ -76,6 +76,28 @@ SessionImpl::SessionImpl(const std::string& connectionString) :
|
||||
&SessionImpl::setInsertId,
|
||||
&SessionImpl::getInsertId);
|
||||
|
||||
open();
|
||||
}
|
||||
|
||||
|
||||
void SessionImpl::open(const std::string& connect)
|
||||
{
|
||||
if (connect != connectionString())
|
||||
{
|
||||
if (isConnected())
|
||||
throw InvalidAccessException("Session already connected");
|
||||
|
||||
if (!connect.empty())
|
||||
setConnectionString(connect);
|
||||
}
|
||||
|
||||
poco_assert_dbg (!connectionString().empty());
|
||||
|
||||
_handle.init();
|
||||
|
||||
unsigned int timeout = static_cast<unsigned int>(getTimeout());
|
||||
_handle.options(MYSQL_OPT_CONNECT_TIMEOUT, timeout);
|
||||
|
||||
std::map<std::string, std::string> options;
|
||||
|
||||
// Default values
|
||||
@@ -87,9 +109,10 @@ SessionImpl::SessionImpl(const std::string& connectionString) :
|
||||
options["compress"] = "";
|
||||
options["auto-reconnect"] = "";
|
||||
|
||||
for (std::string::const_iterator start = connectionString.begin();;)
|
||||
const std::string& connString = connectionString();
|
||||
for (std::string::const_iterator start = connString.begin();;)
|
||||
{
|
||||
std::string::const_iterator finish = std::find(start, connectionString.end(), ';');
|
||||
std::string::const_iterator finish = std::find(start, connString.end(), ';');
|
||||
std::string::const_iterator middle = std::find(start, finish, '=');
|
||||
|
||||
if (middle == finish)
|
||||
@@ -97,7 +120,7 @@ SessionImpl::SessionImpl(const std::string& connectionString) :
|
||||
|
||||
options[copyStripped(start, middle)] = copyStripped(middle + 1, finish);
|
||||
|
||||
if ((finish == connectionString.end()) || (finish + 1 == connectionString.end())) break;
|
||||
if ((finish == connString.end()) || (finish + 1 == connString.end())) break;
|
||||
|
||||
start = finish + 1;
|
||||
}
|
||||
@@ -127,8 +150,7 @@ SessionImpl::SessionImpl(const std::string& connectionString) :
|
||||
throw MySQLException("create session: specify correct auto-reconnect option (true or false) or skip it");
|
||||
|
||||
// Real connect
|
||||
_handle.connect(
|
||||
options["host"].c_str(),
|
||||
_handle.connect(options["host"].c_str(),
|
||||
options["user"].c_str(),
|
||||
options["password"].c_str(),
|
||||
options["db"].c_str(),
|
||||
|
Reference in New Issue
Block a user