added Poco::Data::Session::isGood()

This commit is contained in:
Günter Obiltschnig
2020-01-13 20:13:27 +01:00
parent c04a1f28d9
commit 43b79ffdbe
13 changed files with 200 additions and 44 deletions

View File

@@ -32,6 +32,11 @@ MySQLException::MySQLException(const MySQLException& exc) : Poco::Data::DataExce
}
MySQLException::MySQLException(const std::string& msg, int code) : Poco::Data::DataException(std::string("[MySQL]: ") + msg, code)
{
}
MySQLException::~MySQLException() noexcept
{
}

View File

@@ -21,10 +21,10 @@ namespace MySQL {
MySQLStatementImpl::MySQLStatementImpl(SessionImpl& h) :
Poco::Data::StatementImpl(h),
_stmt(h.handle()),
Poco::Data::StatementImpl(h),
_stmt(h.handle()),
_pBinder(new Binder),
_pExtractor(new Extractor(_stmt, _metadata)),
_pExtractor(new Extractor(_stmt, _metadata)),
_hasNext(NEXT_DONTKNOW)
{
}
@@ -46,13 +46,13 @@ int MySQLStatementImpl::affectedRowCount() const
return _stmt.getAffectedRowCount();
}
const MetaColumn& MySQLStatementImpl::metaColumn(std::size_t pos) const
{
return _metadata.metaColumn(pos);
}
bool MySQLStatementImpl::hasNext()
{
if (_hasNext == NEXT_DONTKNOW)
@@ -79,11 +79,11 @@ bool MySQLStatementImpl::hasNext()
return false;
}
std::size_t MySQLStatementImpl::next()
{
if (!hasNext())
throw StatementException("No data received");
throw StatementException("No data received");
Poco::Data::AbstractExtractionVec::iterator it = extractions().begin();
Poco::Data::AbstractExtractionVec::iterator itEnd = extractions().end();
@@ -119,12 +119,21 @@ bool MySQLStatementImpl::canCompile() const
void MySQLStatementImpl::compileImpl()
{
_metadata.reset();
_stmt.prepare(toString());
_metadata.init(_stmt);
try
{
_metadata.reset();
_stmt.prepare(toString());
_metadata.init(_stmt);
if (_metadata.columnsReturned() > 0)
_stmt.bindResult(_metadata.row());
if (_metadata.columnsReturned() > 0)
_stmt.bindResult(_metadata.row());
}
catch (MySQLException& exc)
{
static_cast<SessionImpl&>(session()).setLastError(exc.code());
throw;
}
static_cast<SessionImpl&>(session()).setLastError(0);
}
@@ -141,8 +150,17 @@ void MySQLStatementImpl::bindImpl()
}
_stmt.bindParams(_pBinder->getBindArray(), _pBinder->size());
_stmt.execute();
try
{
_stmt.execute();
}
catch (MySQLException& exc)
{
static_cast<SessionImpl&>(session()).setLastError(exc.code());
throw;
}
_hasNext = NEXT_DONTKNOW;
static_cast<SessionImpl&>(session()).setLastError(0);
}

View File

@@ -42,23 +42,23 @@ public:
if (pthread_key_create(&_key, &ThreadCleanupHelper::cleanup) != 0)
throw Poco::SystemException("cannot create TLS key for mysql cleanup");
}
void init()
{
if (pthread_setspecific(_key, reinterpret_cast<void*>(1)))
throw Poco::SystemException("cannot set TLS key for mysql cleanup");
}
static ThreadCleanupHelper& instance()
{
return *_sh.get();
}
static void cleanup(void* data)
{
mysql_thread_end();
}
private:
pthread_key_t _key;
static Poco::SingletonHolder<ThreadCleanupHelper> _sh;
@@ -192,4 +192,11 @@ void SessionHandle::reset()
}
bool SessionHandle::ping()
{
int rc = mysql_ping(_pHandle);
return rc == 0;
}
} } } // namespace Poco::Data::MySQL

View File

@@ -49,10 +49,13 @@ SessionImpl::SessionImpl(const std::string& connectionString, std::size_t loginT
_connector("MySQL"),
_handle(0),
_connected(false),
_inTransaction(false)
_inTransaction(false),
_failIfInnoReadOnly(false),
_lastError(0)
{
addProperty("insertId", &SessionImpl::setInsertId, &SessionImpl::getInsertId);
setProperty("handle", static_cast<MYSQL*>(_handle));
addFeature("failIfInnoReadOnly", &SessionImpl::setFailIfInnoReadOnly, &SessionImpl::getFailIfInnoReadOnly);
open();
}
@@ -129,7 +132,7 @@ void SessionImpl::open(const std::string& connect)
else if (!options["auto-reconnect"].empty())
throw MySQLException("create session: specify correct auto-reconnect option (true or false) or skip it");
#ifdef MYSQL_SECURE_AUTH
#ifdef MYSQL_SECURE_AUTH
if (options["secure-auth"] == "true")
_handle.options(MYSQL_SECURE_AUTH, true);
else if (options["secure-auth"] == "false")
@@ -266,6 +269,50 @@ void SessionImpl::reset()
}
inline bool SessionImpl::isConnected() const
{
return _connected;
}
bool SessionImpl::isGood() const
{
if (_connected)
{
if (_lastError)
{
if (_failIfInnoReadOnly)
{
try
{
int ro = 0;
if (0 == getSetting("innodb_read_only", ro))
{
_lastError = 0;
return true;
}
}
catch (...)
{
}
return false;
}
else
{
if (_handle.ping())
{
_lastError = 0;
return true;
}
return false;
}
}
else return true;
}
else return false;
}
void SessionImpl::close()
{
if (_connected)