mirror of
https://github.com/Telecominfraproject/wlan-cloud-lib-poco.git
synced 2025-11-06 13:38:06 +00:00
Data2sql (#2070)
* remove leftover progen files * remove Data sources and turn headers into forwards * add SQL files #2059 * Data2sql: adjust Travis, AppVeyor & Makefiles (#2069) * Replace Data by SQL * Replace Data by SQL * Replace Data by SQL * Replace Data by SQL * fix header forwarding * Data2sql: Fixes for complete Travis CI success (#2071) * Replace Data by SQL * Replace Data by SQL * Replace Data by SQL * Replace Data by SQL * Replace Data by SQL * Restore DataFormatException * Replace Data by SQL * Replace Data by SQL * Replace Data by SQL * Replace Data by SQL * construct RowFilter from RecordSet reference instead of pointer * pass Container ref instead of ptr to Column * elimitate g++ warnings * SQL: remove raw pointers from interfaces #2094; add constness and move ops where appropriate * tidy up Postgres * ODBC fixes
This commit is contained in:
committed by
GitHub
parent
7726d024ab
commit
b49ac67225
179
SQL/MySQL/src/SessionHandle.cpp
Normal file
179
SQL/MySQL/src/SessionHandle.cpp
Normal file
@@ -0,0 +1,179 @@
|
||||
//
|
||||
// SesssionHandle.cpp
|
||||
//
|
||||
// Library: Data/MySQL
|
||||
// Package: MySQL
|
||||
// Module: SessionHandle
|
||||
//
|
||||
// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/SQL/MySQL/SessionHandle.h"
|
||||
#include "Poco/SQL/SQLException.h"
|
||||
#include "Poco/SingletonHolder.h"
|
||||
#ifdef POCO_OS_FAMILY_UNIX
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
|
||||
#define POCO_MYSQL_VERSION_NUMBER ((NDB_VERSION_MAJOR<<16) | (NDB_VERSION_MINOR<<8) | (NDB_VERSION_BUILD&0xFF))
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace SQL {
|
||||
namespace MySQL {
|
||||
|
||||
|
||||
#ifdef POCO_OS_FAMILY_UNIX
|
||||
class ThreadCleanupHelper
|
||||
{
|
||||
public:
|
||||
ThreadCleanupHelper()
|
||||
{
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
Poco::SingletonHolder<ThreadCleanupHelper> ThreadCleanupHelper::_sh;
|
||||
#endif
|
||||
|
||||
|
||||
SessionHandle::SessionHandle(MYSQL* mysql): _pHandle(0)
|
||||
{
|
||||
init(mysql);
|
||||
#ifdef POCO_OS_FAMILY_UNIX
|
||||
ThreadCleanupHelper::instance().init();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void SessionHandle::init(MYSQL* mysql)
|
||||
{
|
||||
if (!_pHandle)
|
||||
{
|
||||
_pHandle = mysql_init(mysql);
|
||||
if (!_pHandle)
|
||||
throw ConnectionException("mysql_init error");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
SessionHandle::~SessionHandle()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
|
||||
void SessionHandle::options(mysql_option opt)
|
||||
{
|
||||
if (mysql_options(_pHandle, opt, 0) != 0)
|
||||
throw ConnectionException("mysql_options error", _pHandle);
|
||||
}
|
||||
|
||||
|
||||
void SessionHandle::options(mysql_option opt, bool b)
|
||||
{
|
||||
my_bool tmp = b;
|
||||
if (mysql_options(_pHandle, opt, &tmp) != 0)
|
||||
throw ConnectionException("mysql_options error", _pHandle);
|
||||
}
|
||||
|
||||
|
||||
void SessionHandle::options(mysql_option opt, const char* c)
|
||||
{
|
||||
if (mysql_options(_pHandle, opt, c) != 0)
|
||||
throw ConnectionException("mysql_options error", _pHandle);
|
||||
}
|
||||
|
||||
|
||||
void SessionHandle::options(mysql_option opt, unsigned int i)
|
||||
{
|
||||
#if (POCO_MYSQL_VERSION_NUMBER < 0x050108)
|
||||
const char* tmp = (const char *)&i;
|
||||
#else
|
||||
const void* tmp = (const void *)&i;
|
||||
#endif
|
||||
if (mysql_options(_pHandle, opt, tmp) != 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)
|
||||
{
|
||||
#ifdef HAVE_MYSQL_REAL_CONNECT
|
||||
if (!mysql_real_connect(_pHandle, host, user, password, db, port, 0, 0))
|
||||
throw ConnectionFailedException(mysql_error(_pHandle));
|
||||
#else
|
||||
if (!mysql_connect(_pHandle, host, user, password))
|
||||
throw ConnectionFailedException(mysql_error(_pHandle))
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void SessionHandle::close()
|
||||
{
|
||||
if (_pHandle)
|
||||
{
|
||||
mysql_close(_pHandle);
|
||||
_pHandle = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void SessionHandle::startTransaction()
|
||||
{
|
||||
int rc = mysql_autocommit(_pHandle, false);
|
||||
if (rc != 0)
|
||||
{
|
||||
// retry if connection lost
|
||||
int err = mysql_errno(_pHandle);
|
||||
if (err == 2006 /* CR_SERVER_GONE_ERROR */ || err == 2013 /* CR_SERVER_LOST */)
|
||||
{
|
||||
rc = mysql_autocommit(_pHandle, false);
|
||||
}
|
||||
}
|
||||
if (rc != 0) throw TransactionException("Start transaction failed.", _pHandle);
|
||||
}
|
||||
|
||||
|
||||
void SessionHandle::commit()
|
||||
{
|
||||
if (mysql_commit(_pHandle) != 0)
|
||||
throw TransactionException("Commit failed.", _pHandle);
|
||||
}
|
||||
|
||||
|
||||
void SessionHandle::rollback()
|
||||
{
|
||||
if (mysql_rollback(_pHandle) != 0)
|
||||
throw TransactionException("Rollback failed.", _pHandle);
|
||||
}
|
||||
|
||||
|
||||
}}} // Poco::SQL::MySQL
|
||||
Reference in New Issue
Block a user