1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-17 07:37:13 +02:00

Major plugin refactor and cleanup.

Switched to POCO library for unified platform/library interface.
Deprecated the external module API. It was creating more problems than solving.
Removed most built-in libraries in favor of system libraries for easier maintenance.
Cleaned and secured code with help from static analyzers.
This commit is contained in:
Sandu Liviu Catalin
2021-01-30 08:51:39 +02:00
parent e0e34b4030
commit 4a6bfc086c
6219 changed files with 1209835 additions and 454916 deletions

636
vendor/POCO/Data/MySQL/src/Binder.cpp vendored Normal file
View File

@ -0,0 +1,636 @@
//
// MySQLException.cpp
//
// Library: Data/MySQL
// Package: MySQL
// Module: Binder
//
// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Data/MySQL/Binder.h"
namespace Poco {
namespace Data {
namespace MySQL {
Binder::Binder()
{
}
Binder::~Binder()
{
for (std::vector<MYSQL_TIME*>::iterator it = _dates.begin(); it != _dates.end(); ++it)
{
delete *it;
*it = 0;
}
}
void Binder::bind(std::size_t pos, const Poco::Int8& val, Direction dir)
{
poco_assert(dir == PD_IN);
realBind(pos, MYSQL_TYPE_TINY, &val, 0);
}
void Binder::bind(std::size_t pos, const Poco::UInt8& val, Direction dir)
{
poco_assert(dir == PD_IN);
realBind(pos, MYSQL_TYPE_TINY, &val, 0, true);
}
void Binder::bind(std::size_t pos, const Poco::Int16& val, Direction dir)
{
poco_assert(dir == PD_IN);
realBind(pos, MYSQL_TYPE_SHORT, &val, 0);
}
void Binder::bind(std::size_t pos, const Poco::UInt16& val, Direction dir)
{
poco_assert(dir == PD_IN);
realBind(pos, MYSQL_TYPE_SHORT, &val, 0, true);
}
void Binder::bind(std::size_t pos, const Poco::Int32& val, Direction dir)
{
poco_assert(dir == PD_IN);
realBind(pos, MYSQL_TYPE_LONG, &val, 0);
}
void Binder::bind(std::size_t pos, const Poco::UInt32& val, Direction dir)
{
poco_assert(dir == PD_IN);
realBind(pos, MYSQL_TYPE_LONG, &val, 0, true);
}
void Binder::bind(std::size_t pos, const Poco::Int64& val, Direction dir)
{
poco_assert(dir == PD_IN);
realBind(pos, MYSQL_TYPE_LONGLONG, &val, 0);
}
void Binder::bind(std::size_t pos, const Poco::UInt64& val, Direction dir)
{
poco_assert(dir == PD_IN);
realBind(pos, MYSQL_TYPE_LONGLONG, &val, 0, true);
}
#ifndef POCO_INT64_IS_LONG
void Binder::bind(std::size_t pos, const long& val, Direction dir)
{
poco_assert(dir == PD_IN);
realBind(pos, MYSQL_TYPE_LONG, &val, 0);
}
void Binder::bind(std::size_t pos, const unsigned long& val, Direction dir)
{
poco_assert(dir == PD_IN);
realBind(pos, MYSQL_TYPE_LONG, &val, 0, true);
}
#endif // POCO_LONG_IS_64_BIT
void Binder::bind(std::size_t pos, const bool& val, Direction dir)
{
poco_assert(dir == PD_IN);
realBind(pos, MYSQL_TYPE_TINY, &val, 0);
}
void Binder::bind(std::size_t pos, const float& val, Direction dir)
{
poco_assert(dir == PD_IN);
realBind(pos, MYSQL_TYPE_FLOAT, &val, 0);
}
void Binder::bind(std::size_t pos, const double& val, Direction dir)
{
poco_assert(dir == PD_IN);
realBind(pos, MYSQL_TYPE_DOUBLE, &val, 0);
}
void Binder::bind(std::size_t pos, const char& val, Direction dir)
{
poco_assert(dir == PD_IN);
realBind(pos, MYSQL_TYPE_TINY, &val, 0);
}
void Binder::bind(std::size_t pos, const std::string& val, Direction dir)
{
poco_assert(dir == PD_IN);
realBind(pos, MYSQL_TYPE_STRING, val.c_str(), static_cast<int>(val.length()));
}
void Binder::bind(std::size_t pos, const Poco::Data::BLOB& val, Direction dir)
{
poco_assert(dir == PD_IN);
realBind(pos, MYSQL_TYPE_BLOB, val.rawContent(), static_cast<int>(val.size()));
}
void Binder::bind(std::size_t pos, const Poco::Data::CLOB& val, Direction dir)
{
poco_assert(dir == PD_IN);
realBind(pos, MYSQL_TYPE_BLOB, val.rawContent(), static_cast<int>(val.size()));
}
void Binder::bind(std::size_t pos, const DateTime& val, Direction dir)
{
poco_assert(dir == PD_IN);
MYSQL_TIME mt = {0};
mt.year = val.year();
mt.month = val.month();
mt.day = val.day();
mt.hour = val.hour();
mt.minute = val.minute();
mt.second = val.second();
mt.second_part = val.millisecond() * 1000 + val.microsecond();
mt.time_type = MYSQL_TIMESTAMP_DATETIME;
_dates.push_back(new MYSQL_TIME(mt));
realBind(pos, MYSQL_TYPE_DATETIME, _dates.back(), sizeof(MYSQL_TIME));
}
void Binder::bind(std::size_t pos, const Date& val, Direction dir)
{
poco_assert(dir == PD_IN);
MYSQL_TIME mt = {0};
mt.year = val.year();
mt.month = val.month();
mt.day = val.day();
mt.time_type = MYSQL_TIMESTAMP_DATE;
_dates.push_back(new MYSQL_TIME(mt));
realBind(pos, MYSQL_TYPE_DATE, _dates.back(), sizeof(MYSQL_TIME));
}
void Binder::bind(std::size_t pos, const Time& val, Direction dir)
{
poco_assert(dir == PD_IN);
MYSQL_TIME mt = {0};
mt.hour = val.hour();
mt.minute = val.minute();
mt.second = val.second();
mt.time_type = MYSQL_TIMESTAMP_TIME;
_dates.push_back(new MYSQL_TIME(mt));
realBind(pos, MYSQL_TYPE_TIME, _dates.back(), sizeof(MYSQL_TIME));
}
void Binder::bind(std::size_t pos, const NullData&, Direction dir)
{
poco_assert(dir == PD_IN);
realBind(pos, MYSQL_TYPE_NULL, 0, 0);
}
std::size_t Binder::size() const
{
return static_cast<std::size_t>(_bindArray.size());
}
MYSQL_BIND* Binder::getBindArray() const
{
if (_bindArray.size() == 0)
{
return 0;
}
return const_cast<MYSQL_BIND*>(&_bindArray[0]);
}
/*void Binder::updateDates()
{
for (std::size_t i = 0; i < _dates.size(); i++)
{
switch (_dates[i].mt.time_type)
{
case MYSQL_TIMESTAMP_DATE:
_dates[i].mt.year = _dates[i].link.date->year();
_dates[i].mt.month = _dates[i].link.date->month();
_dates[i].mt.day = _dates[i].link.date->day();
break;
case MYSQL_TIMESTAMP_DATETIME:
_dates[i].mt.year = _dates[i].link.dateTime->year();
_dates[i].mt.month = _dates[i].link.dateTime->month();
_dates[i].mt.day = _dates[i].link.dateTime->day();
_dates[i].mt.hour = _dates[i].link.dateTime->hour();
_dates[i].mt.minute = _dates[i].link.dateTime->minute();
_dates[i].mt.second = _dates[i].link.dateTime->second();
_dates[i].mt.second_part = _dates[i].link.dateTime->millisecond();
break;
case MYSQL_TIMESTAMP_TIME:
_dates[i].mt.hour = _dates[i].link.time->hour();
_dates[i].mt.minute = _dates[i].link.time->minute();
_dates[i].mt.second = _dates[i].link.time->second();
break;
}
}
}*/
///////////////////
//
// Private
//
////////////////////
void Binder::realBind(std::size_t pos, enum_field_types type, const void* buffer, int length, bool isUnsigned)
{
if (pos >= _bindArray.size())
{
std::size_t s = static_cast<std::size_t>(_bindArray.size());
_bindArray.resize(pos + 1);
std::memset(&_bindArray[s], 0, sizeof(MYSQL_BIND) * (_bindArray.size() - s));
}
MYSQL_BIND b = {0};
b.buffer_type = type;
b.buffer = const_cast<void*>(buffer);
b.buffer_length = length;
b.is_unsigned = isUnsigned;
_bindArray[pos] = b;
}
void Binder::bind(std::size_t pos, const std::vector<Poco::Int8>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::deque<Poco::Int8>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::list<Poco::Int8>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::vector<Poco::UInt8>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::deque<Poco::UInt8>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::list<Poco::UInt8>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::vector<Poco::Int16>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::deque<Poco::Int16>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::list<Poco::Int16>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::vector<Poco::UInt16>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::deque<Poco::UInt16>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::list<Poco::UInt16>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::vector<Poco::Int32>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::deque<Poco::Int32>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::list<Poco::Int32>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::vector<Poco::UInt32>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::deque<Poco::UInt32>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::list<Poco::UInt32>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::vector<Poco::Int64>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::deque<Poco::Int64>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::list<Poco::Int64>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::vector<Poco::UInt64>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::deque<Poco::UInt64>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::list<Poco::UInt64>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::vector<bool>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::deque<bool>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::list<bool>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::vector<float>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::deque<float>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::list<float>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::vector<double>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::deque<double>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::list<double>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::vector<char>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::deque<char>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::list<char>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::vector<Poco::Data::BLOB>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::deque<Poco::Data::BLOB>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::list<Poco::Data::BLOB>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::vector<Poco::Data::CLOB>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::deque<Poco::Data::CLOB>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::list<Poco::Data::CLOB>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::vector<Poco::DateTime>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::deque<Poco::DateTime>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::list<Poco::DateTime>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::vector<Poco::Data::Date>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::deque<Poco::Data::Date>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::list<Poco::Data::Date>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::vector<Poco::Data::Time>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::deque<Poco::Data::Time>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::list<Poco::Data::Time>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::vector<Poco::Data::NullData>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::deque<Poco::Data::NullData>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::list<Poco::Data::NullData>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::vector<std::string>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::deque<std::string>& val, Direction dir)
{
throw NotImplementedException();
}
void Binder::bind(std::size_t pos, const std::list<std::string>& val, Direction dir)
{
throw NotImplementedException();
}
} } } // namespace Poco::Data::MySQL

View File

@ -0,0 +1,72 @@
//
// MySQLException.cpp
//
// Library: Data/MySQL
// Package: MySQL
// Module: Connector
//
// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Data/MySQL/Connector.h"
#include "Poco/Data/MySQL/SessionImpl.h"
#include "Poco/Data/SessionFactory.h"
#include "Poco/Exception.h"
#include <mysql.h>
namespace Poco {
namespace Data {
namespace MySQL {
std::string Connector::KEY("mysql");
Connector::Connector()
{
}
Connector::~Connector()
{
}
const std::string& Connector::name() const
{
return KEY;
}
Poco::AutoPtr<Poco::Data::SessionImpl> Connector::createSession(const std::string& connectionString,
std::size_t timeout)
{
return Poco::AutoPtr<Poco::Data::SessionImpl>(new SessionImpl(connectionString, timeout));
}
void Connector::registerConnector()
{
if (mysql_library_init(0, 0, 0) != 0)
{
throw Exception("mysql_library_init error");
}
Poco::Data::SessionFactory::instance().add(new Connector());
}
void Connector::unregisterConnector()
{
Poco::Data::SessionFactory::instance().remove(KEY);
mysql_library_end();
}
} } } // namespace Poco::Data::MySQL

640
vendor/POCO/Data/MySQL/src/Extractor.cpp vendored Normal file
View File

@ -0,0 +1,640 @@
//
// MySQLException.cpp
//
// Library: Data/MySQL
// Package: MySQL
// Module: Extractor
//
// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Data/MySQL/Extractor.h"
#include "Poco/Data/Date.h"
#include "Poco/Data/Time.h"
namespace Poco {
namespace Data {
namespace MySQL {
Extractor::Extractor(StatementExecutor& st, ResultMetadata& md): _stmt(st), _metadata(md)
{
}
Extractor::~Extractor()
{
}
bool Extractor::extract(std::size_t pos, Poco::Int8& val)
{
return realExtractFixed(pos, MYSQL_TYPE_TINY, &val);
}
bool Extractor::extract(std::size_t pos, Poco::UInt8& val)
{
return realExtractFixed(pos, MYSQL_TYPE_TINY, &val, true);
}
bool Extractor::extract(std::size_t pos, Poco::Int16& val)
{
return realExtractFixed(pos, MYSQL_TYPE_SHORT, &val);
}
bool Extractor::extract(std::size_t pos, Poco::UInt16& val)
{
return realExtractFixed(pos, MYSQL_TYPE_SHORT, &val, true);
}
bool Extractor::extract(std::size_t pos, Poco::Int32& val)
{
return realExtractFixed(pos, MYSQL_TYPE_LONG, &val);
}
bool Extractor::extract(std::size_t pos, Poco::UInt32& val)
{
return realExtractFixed(pos, MYSQL_TYPE_LONG, &val, true);
}
bool Extractor::extract(std::size_t pos, Poco::Int64& val)
{
return realExtractFixed(pos, MYSQL_TYPE_LONGLONG, &val);
}
bool Extractor::extract(std::size_t pos, Poco::UInt64& val)
{
return realExtractFixed(pos, MYSQL_TYPE_LONGLONG, &val, true);
}
#ifndef POCO_INT64_IS_LONG
bool Extractor::extract(std::size_t pos, long& val)
{
return realExtractFixed(pos, MYSQL_TYPE_LONG, &val);
}
bool Extractor::extract(std::size_t pos, unsigned long& val)
{
return realExtractFixed(pos, MYSQL_TYPE_LONG, &val, true);
}
#endif
bool Extractor::extract(std::size_t pos, bool& val)
{
return realExtractFixed(pos, MYSQL_TYPE_TINY, &val);
}
bool Extractor::extract(std::size_t pos, float& val)
{
return realExtractFixed(pos, MYSQL_TYPE_FLOAT, &val);
}
bool Extractor::extract(std::size_t pos, double& val)
{
return realExtractFixed(pos, MYSQL_TYPE_DOUBLE, &val);
}
bool Extractor::extract(std::size_t pos, char& val)
{
return realExtractFixed(pos, MYSQL_TYPE_TINY, &val);
}
bool Extractor::extract(std::size_t pos, std::string& val)
{
if (_metadata.columnsReturned() <= pos)
throw MySQLException("Extractor: attempt to extract more parameters, than query result contain");
if (_metadata.isNull(static_cast<Poco::UInt32>(pos)))
return false;
//mysql reports TEXT types as FDT_BLOB when being extracted
MetaColumn::ColumnDataType columnType = _metadata.metaColumn(static_cast<Poco::UInt32>(pos)).type();
if (columnType != Poco::Data::MetaColumn::FDT_STRING && columnType != Poco::Data::MetaColumn::FDT_BLOB)
throw MySQLException("Extractor: not a string");
val.assign(reinterpret_cast<const char*>(_metadata.rawData(pos)), _metadata.length(pos));
return true;
}
bool Extractor::extract(std::size_t pos, Poco::Data::BLOB& val)
{
if (_metadata.columnsReturned() <= pos)
throw MySQLException("Extractor: attempt to extract more parameters, than query result contain");
if (_metadata.isNull(static_cast<Poco::UInt32>(pos)))
return false;
if (_metadata.metaColumn(static_cast<Poco::UInt32>(pos)).type() != Poco::Data::MetaColumn::FDT_BLOB)
throw MySQLException("Extractor: not a blob");
val.assignRaw(_metadata.rawData(pos), _metadata.length(pos));
return true;
}
bool Extractor::extract(std::size_t pos, Poco::Data::CLOB& val)
{
if (_metadata.columnsReturned() <= pos)
throw MySQLException("Extractor: attempt to extract more parameters, than query result contain");
if (_metadata.isNull(static_cast<Poco::UInt32>(pos)))
return false;
if (_metadata.metaColumn(static_cast<Poco::UInt32>(pos)).type() != Poco::Data::MetaColumn::FDT_BLOB)
throw MySQLException("Extractor: not a blob");
val.assignRaw(reinterpret_cast<const char*>(_metadata.rawData(pos)), _metadata.length(pos));
return true;
}
bool Extractor::extract(std::size_t pos, DateTime& val)
{
MYSQL_TIME mt = {0};
if (!realExtractFixed(pos, MYSQL_TYPE_DATETIME, &mt))
return false;
val.assign(mt.year, mt.month, mt.day, mt.hour, mt.minute, mt.second, mt.second_part / 1000, mt.second_part % 1000);
return true;
}
bool Extractor::extract(std::size_t pos, Date& val)
{
MYSQL_TIME mt = {0};
if (!realExtractFixed(pos, MYSQL_TYPE_DATE, &mt))
return false;
val.assign(mt.year, mt.month, mt.day);
return true;
}
bool Extractor::extract(std::size_t pos, Time& val)
{
MYSQL_TIME mt = {0};
if (!realExtractFixed(pos, MYSQL_TYPE_TIME, &mt))
return false;
val.assign(mt.hour, mt.minute, mt.second);
return true;
}
bool Extractor::extract(std::size_t pos, Any& val)
{
return false;
}
bool Extractor::extract(std::size_t pos, Dynamic::Var& val)
{
return false;
}
bool Extractor::isNull(std::size_t col, std::size_t row)
{
poco_assert(row == POCO_DATA_INVALID_ROW);
if (_metadata.columnsReturned() <= col)
throw MySQLException("Extractor: attempt to extract more parameters, than query result contain");
if (_metadata.isNull(static_cast<Poco::UInt32>(col)))
return true;
return false;
}
void Extractor::reset()
{
AbstractExtractor::reset();
}
bool Extractor::realExtractFixed(std::size_t pos, enum_field_types type, void* buffer, bool isUnsigned)
{
MYSQL_BIND bind = {0};
my_bool isNull = 0;
bind.is_null = &isNull;
bind.buffer_type = type;
bind.buffer = buffer;
bind.is_unsigned = isUnsigned;
if (!_stmt.fetchColumn(pos, &bind))
return false;
return isNull == 0;
}
//////////////
// Not implemented
//////////////
bool Extractor::extract(std::size_t , std::vector<Poco::Int8>& )
{
throw NotImplementedException("std::vector extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::deque<Poco::Int8>& )
{
throw NotImplementedException("std::deque extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::list<Poco::Int8>& )
{
throw NotImplementedException("std::list extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::vector<Poco::UInt8>& )
{
throw NotImplementedException("std::vector extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::deque<Poco::UInt8>& )
{
throw NotImplementedException("std::deque extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::list<Poco::UInt8>& )
{
throw NotImplementedException("std::list extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::vector<Poco::Int16>& )
{
throw NotImplementedException("std::vector extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::deque<Poco::Int16>& )
{
throw NotImplementedException("std::deque extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::list<Poco::Int16>& )
{
throw NotImplementedException("std::list extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::vector<Poco::UInt16>& )
{
throw NotImplementedException("std::vector extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::deque<Poco::UInt16>& )
{
throw NotImplementedException("std::deque extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::list<Poco::UInt16>& )
{
throw NotImplementedException("std::list extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::vector<Poco::Int32>& )
{
throw NotImplementedException("std::vector extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::deque<Poco::Int32>& )
{
throw NotImplementedException("std::deque extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::list<Poco::Int32>& )
{
throw NotImplementedException("std::list extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::vector<Poco::UInt32>& )
{
throw NotImplementedException("std::vector extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::deque<Poco::UInt32>& )
{
throw NotImplementedException("std::deque extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::list<Poco::UInt32>& )
{
throw NotImplementedException("std::list extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::vector<Poco::Int64>& )
{
throw NotImplementedException("std::vector extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::deque<Poco::Int64>& )
{
throw NotImplementedException("std::deque extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::list<Poco::Int64>& )
{
throw NotImplementedException("std::list extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::vector<Poco::UInt64>& )
{
throw NotImplementedException("std::vector extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::deque<Poco::UInt64>& )
{
throw NotImplementedException("std::deque extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::list<Poco::UInt64>& )
{
throw NotImplementedException("std::list extractor must be implemented.");
}
#ifndef POCO_INT64_IS_LONG
bool Extractor::extract(std::size_t , std::vector<long>& )
{
throw NotImplementedException("std::vector extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::deque<long>& )
{
throw NotImplementedException("std::deque extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::list<long>& )
{
throw NotImplementedException("std::list extractor must be implemented.");
}
#endif
bool Extractor::extract(std::size_t , std::vector<bool>& )
{
throw NotImplementedException("std::vector extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::deque<bool>& )
{
throw NotImplementedException("std::deque extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::list<bool>& )
{
throw NotImplementedException("std::list extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::vector<float>& )
{
throw NotImplementedException("std::vector extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::deque<float>& )
{
throw NotImplementedException("std::deque extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::list<float>& )
{
throw NotImplementedException("std::list extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::vector<double>& )
{
throw NotImplementedException("std::vector extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::deque<double>& )
{
throw NotImplementedException("std::deque extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::list<double>& )
{
throw NotImplementedException("std::list extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::vector<char>& )
{
throw NotImplementedException("std::vector extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::deque<char>& )
{
throw NotImplementedException("std::deque extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::list<char>& )
{
throw NotImplementedException("std::list extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::vector<std::string>& )
{
throw NotImplementedException("std::vector extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::deque<std::string>& )
{
throw NotImplementedException("std::deque extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::list<std::string>& )
{
throw NotImplementedException("std::list extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::vector<BLOB>& )
{
throw NotImplementedException("std::vector extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::deque<BLOB>& )
{
throw NotImplementedException("std::deque extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::list<BLOB>& )
{
throw NotImplementedException("std::list extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::vector<CLOB>& )
{
throw NotImplementedException("std::vector extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::deque<CLOB>& )
{
throw NotImplementedException("std::deque extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::list<CLOB>& )
{
throw NotImplementedException("std::list extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::vector<DateTime>& )
{
throw NotImplementedException("std::vector extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::deque<DateTime>& )
{
throw NotImplementedException("std::deque extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::list<DateTime>& )
{
throw NotImplementedException("std::list extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::vector<Date>& )
{
throw NotImplementedException("std::vector extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::deque<Date>& )
{
throw NotImplementedException("std::deque extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::list<Date>& )
{
throw NotImplementedException("std::list extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::vector<Time>& )
{
throw NotImplementedException("std::vector extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::deque<Time>& )
{
throw NotImplementedException("std::deque extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::list<Time>& )
{
throw NotImplementedException("std::list extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::vector<Any>& )
{
throw NotImplementedException("std::vector extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::deque<Any>& )
{
throw NotImplementedException("std::deque extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::list<Any>& )
{
throw NotImplementedException("std::list extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::vector<Dynamic::Var>& )
{
throw NotImplementedException("std::vector extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::deque<Dynamic::Var>& )
{
throw NotImplementedException("std::deque extractor must be implemented.");
}
bool Extractor::extract(std::size_t , std::list<Dynamic::Var>& )
{
throw NotImplementedException("std::list extractor must be implemented.");
}
} } } // namespace Poco::Data::MySQL

View File

@ -0,0 +1,141 @@
//
// MySQLException.cpp
//
// Library: Data/MySQL
// Package: MySQL
// Module: MySQLException
//
// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Data/MySQL/MySQLException.h"
#include <mysql.h>
#include <stdio.h>
namespace Poco {
namespace Data {
namespace MySQL {
MySQLException::MySQLException(const std::string& msg) : Poco::Data::DataException(std::string("[MySQL]: ") + msg)
{
}
MySQLException::MySQLException(const MySQLException& exc) : Poco::Data::DataException(exc)
{
}
MySQLException::MySQLException(const std::string& msg, int code) : Poco::Data::DataException(std::string("[MySQL]: ") + msg, code)
{
}
MySQLException::~MySQLException() noexcept
{
}
//
// ConnectionException
//
ConnectionException::ConnectionException(const std::string& msg) : MySQLException(msg)
{
}
ConnectionException::ConnectionException(const std::string& text, MYSQL* h) : MySQLException(compose(text, h))
{
}
std::string ConnectionException::compose(const std::string& text, MYSQL* h)
{
std::string str;
str += "[Comment]: ";
str += text;
str += "\t[mysql_error]: ";
str += mysql_error(h);
str += "\t[mysql_errno]: ";
char buff[30];
sprintf(buff, "%d", mysql_errno(h));
str += buff;
str += "\t[mysql_sqlstate]: ";
str += mysql_sqlstate(h);
return str;
}
//
// TransactionException
//
TransactionException::TransactionException(const std::string& msg) : ConnectionException(msg)
{
}
TransactionException::TransactionException(const std::string& text, MYSQL* h) : ConnectionException(text, h)
{
}
/////
//
// StatementException
//
/////
StatementException::StatementException(const std::string& msg) : MySQLException(msg)
{
}
StatementException::StatementException(const std::string& text, MYSQL_STMT* h, const std::string& stmt) : MySQLException(compose(text, h, stmt))
{
}
std::string StatementException::compose(const std::string& text, MYSQL_STMT* h, const std::string& stmt)
{
std::string str;
str += "[Comment]: ";
str += text;
if (h != 0)
{
str += "\t[mysql_stmt_error]: ";
str += mysql_stmt_error(h);
str += "\t[mysql_stmt_errno]: ";
char buff[30];
sprintf(buff, "%d", mysql_stmt_errno(h));
str += buff;
str += "\t[mysql_stmt_sqlstate]: ";
str += mysql_stmt_sqlstate(h);
}
if (stmt.length() > 0)
{
str += "\t[statemnt]: ";
str += stmt;
}
return str;
}
} } } // namespace Poco::Data::MySQL

View File

@ -0,0 +1,179 @@
//
// MySQLException.cpp
//
// Library: Data/MySQL
// Package: MySQL
// Module: MySQLStatementImpl
//
// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Data/MySQL/MySQLStatementImpl.h"
namespace Poco {
namespace Data {
namespace MySQL {
MySQLStatementImpl::MySQLStatementImpl(SessionImpl& h) :
Poco::Data::StatementImpl(h),
_stmt(h.handle()),
_pBinder(new Binder),
_pExtractor(new Extractor(_stmt, _metadata)),
_hasNext(NEXT_DONTKNOW)
{
}
MySQLStatementImpl::~MySQLStatementImpl()
{
}
std::size_t MySQLStatementImpl::columnsReturned() const
{
return _metadata.columnsReturned();
}
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)
{
if (_metadata.columnsReturned() == 0)
{
return false;
}
if (_stmt.fetch())
{
_hasNext = NEXT_TRUE;
return true;
}
_hasNext = NEXT_FALSE;
return false;
}
else if (_hasNext == NEXT_TRUE)
{
return true;
}
return false;
}
std::size_t MySQLStatementImpl::next()
{
if (!hasNext())
throw StatementException("No data received");
Poco::Data::AbstractExtractionVec::iterator it = extractions().begin();
Poco::Data::AbstractExtractionVec::iterator itEnd = extractions().end();
std::size_t pos = 0;
for (; it != itEnd; ++it)
{
(*it)->extract(pos);
pos += (*it)->numOfColumnsHandled();
}
_hasNext = NEXT_DONTKNOW;
return 1;
}
bool MySQLStatementImpl::canBind() const
{
bool ret = false;
if ((_stmt.state() >= StatementExecutor::STMT_COMPILED) && !bindings().empty())
ret = (*bindings().begin())->canBind();
return ret;
}
bool MySQLStatementImpl::canCompile() const
{
return (_stmt.state() < StatementExecutor::STMT_COMPILED);
}
void MySQLStatementImpl::compileImpl()
{
try
{
_metadata.reset();
_stmt.prepare(toString());
_metadata.init(_stmt);
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);
}
void MySQLStatementImpl::bindImpl()
{
Poco::Data::AbstractBindingVec& binds = bindings();
std::size_t pos = 0;
Poco::Data::AbstractBindingVec::iterator it = binds.begin();
Poco::Data::AbstractBindingVec::iterator itEnd = binds.end();
for (; it != itEnd && (*it)->canBind(); ++it)
{
(*it)->bind(pos);
pos += (*it)->numOfColumnsHandled();
}
_stmt.bindParams(_pBinder->getBindArray(), _pBinder->size());
try
{
_stmt.execute();
}
catch (MySQLException& exc)
{
static_cast<SessionImpl&>(session()).setLastError(exc.code());
throw;
}
_hasNext = NEXT_DONTKNOW;
static_cast<SessionImpl&>(session()).setLastError(0);
}
Poco::Data::AbstractExtractor::Ptr MySQLStatementImpl::extractor()
{
return _pExtractor;
}
Poco::Data::AbstractBinder::Ptr MySQLStatementImpl::binder()
{
return _pBinder;
}
} } } // namespace Poco::Data::MySQL

View File

@ -0,0 +1,247 @@
//
// MySQLException.cpp
//
// Library: Data/MySQL
// Package: MySQL
// Module: ResultMetadata
//
// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Data/MySQL/ResultMetadata.h"
#include "Poco/Data/MySQL/MySQLException.h"
#include <cstring>
namespace
{
class ResultMetadataHandle
/// Simple exception-safe wrapper
{
public:
explicit ResultMetadataHandle(MYSQL_STMT* stmt)
{
h = mysql_stmt_result_metadata(stmt);
}
~ResultMetadataHandle()
{
if (h)
{
mysql_free_result(h);
}
}
operator MYSQL_RES* ()
{
return h;
}
private:
MYSQL_RES* h;
};
std::size_t fieldSize(const MYSQL_FIELD& field)
/// Convert field MySQL-type and field MySQL-length to actual field length
{
switch (field.type)
{
case MYSQL_TYPE_TINY: return sizeof(char);
case MYSQL_TYPE_SHORT: return sizeof(short);
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_LONG: return sizeof(Poco::Int32);
case MYSQL_TYPE_FLOAT: return sizeof(float);
case MYSQL_TYPE_DOUBLE: return sizeof(double);
case MYSQL_TYPE_LONGLONG: return sizeof(Poco::Int64);
case MYSQL_TYPE_DATE:
case MYSQL_TYPE_TIME:
case MYSQL_TYPE_DATETIME:
return sizeof(MYSQL_TIME);
case MYSQL_TYPE_DECIMAL:
case MYSQL_TYPE_NEWDECIMAL:
case MYSQL_TYPE_STRING:
case MYSQL_TYPE_VAR_STRING:
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_BLOB:
return field.length;
default:
throw Poco::Data::MySQL::StatementException("unknown field type");
}
}
Poco::Data::MetaColumn::ColumnDataType fieldType(const MYSQL_FIELD& field)
/// Convert field MySQL-type to Poco-type
{
bool unsig = ((field.flags & UNSIGNED_FLAG) == UNSIGNED_FLAG);
switch (field.type)
{
case MYSQL_TYPE_TINY:
if (unsig) return Poco::Data::MetaColumn::FDT_UINT8;
return Poco::Data::MetaColumn::FDT_INT8;
case MYSQL_TYPE_SHORT:
if (unsig) return Poco::Data::MetaColumn::FDT_UINT16;
return Poco::Data::MetaColumn::FDT_INT16;
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_LONG:
if (unsig) return Poco::Data::MetaColumn::FDT_UINT32;
return Poco::Data::MetaColumn::FDT_INT32;
case MYSQL_TYPE_FLOAT:
return Poco::Data::MetaColumn::FDT_FLOAT;
case MYSQL_TYPE_DECIMAL:
case MYSQL_TYPE_NEWDECIMAL:
case MYSQL_TYPE_DOUBLE:
return Poco::Data::MetaColumn::FDT_DOUBLE;
case MYSQL_TYPE_LONGLONG:
if (unsig) return Poco::Data::MetaColumn::FDT_UINT64;
return Poco::Data::MetaColumn::FDT_INT64;
case MYSQL_TYPE_DATE:
return Poco::Data::MetaColumn::FDT_DATE;
case MYSQL_TYPE_TIME:
return Poco::Data::MetaColumn::FDT_TIME;
case MYSQL_TYPE_DATETIME:
return Poco::Data::MetaColumn::FDT_TIMESTAMP;
case MYSQL_TYPE_STRING:
case MYSQL_TYPE_VAR_STRING:
return Poco::Data::MetaColumn::FDT_STRING;
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_BLOB:
return Poco::Data::MetaColumn::FDT_BLOB;
default:
return Poco::Data::MetaColumn::FDT_UNKNOWN;
}
}
} // namespace
namespace Poco {
namespace Data {
namespace MySQL {
void ResultMetadata::reset()
{
_columns.resize(0);
_row.resize(0);
_buffer.resize(0);
_lengths.resize(0);
_isNull.resize(0);
}
void ResultMetadata::init(MYSQL_STMT* stmt)
{
ResultMetadataHandle h(stmt);
if (!h)
{
// all right, it is normal
// querys such an "INSERT INTO" just does not have result at all
reset();
return;
}
std::size_t count = mysql_num_fields(h);
MYSQL_FIELD* fields = mysql_fetch_fields(h);
std::size_t commonSize = 0;
_columns.reserve(count);
for (std::size_t i = 0; i < count; i++)
{
std::size_t size = fieldSize(fields[i]);
if (size == 0xFFFFFFFF) size = 0;
_columns.push_back(MetaColumn(
i, // position
fields[i].name, // name
fieldType(fields[i]), // type
size, // length
0, // TODO: precision
!IS_NOT_NULL(fields[i].flags) // nullable
));
commonSize += _columns[i].length();
}
_buffer.resize(commonSize);
_row.resize(count);
_lengths.resize(count);
_isNull.resize(count);
std::size_t offset = 0;
for (std::size_t i = 0; i < count; i++)
{
std::memset(&_row[i], 0, sizeof(MYSQL_BIND));
unsigned int len = static_cast<unsigned int>(_columns[i].length());
_row[i].buffer_type = fields[i].type;
_row[i].buffer_length = len;
_row[i].buffer = (len > 0) ? (&_buffer[0] + offset) : 0;
_row[i].length = &_lengths[i];
_row[i].is_null = reinterpret_cast<my_bool*>(&_isNull[i]); // workaround to make it work with both MySQL 8 and earlier
_row[i].is_unsigned = (fields[i].flags & UNSIGNED_FLAG) > 0;
offset += _row[i].buffer_length;
}
}
std::size_t ResultMetadata::columnsReturned() const
{
return static_cast<std::size_t>(_columns.size());
}
const MetaColumn& ResultMetadata::metaColumn(std::size_t pos) const
{
return _columns[pos];
}
MYSQL_BIND* ResultMetadata::row()
{
return &_row[0];
}
std::size_t ResultMetadata::length(std::size_t pos) const
{
return _lengths[pos];
}
const unsigned char* ResultMetadata::rawData(std::size_t pos) const
{
return reinterpret_cast<const unsigned char*>(_row[pos].buffer);
}
bool ResultMetadata::isNull(std::size_t pos) const
{
return (_isNull[pos] != 0);
}
} } } // namespace Poco::Data::MySQL

View File

@ -0,0 +1,202 @@
//
// 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/Data/MySQL/SessionHandle.h"
#include "Poco/Data/DataException.h"
#include "Poco/SingletonHolder.h"
#ifdef POCO_OS_FAMILY_UNIX
#include <pthread.h>
#endif
#if LIBMYSQL_VERSION_ID >= 80000
typedef bool my_bool; // Workaround to make library work with MySQL client 8.0 as well as earlier versions
#endif
#define POCO_MYSQL_VERSION_NUMBER ((NDB_VERSION_MAJOR<<16) | (NDB_VERSION_MINOR<<8) | (NDB_VERSION_BUILD&0xFF))
namespace Poco {
namespace Data {
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);
}
void SessionHandle::reset()
{
#if ((defined (MYSQL_VERSION_ID)) && (MYSQL_VERSION_ID >= 50700)) || ((defined (MARIADB_PACKAGE_VERSION_ID)) && (MARIADB_PACKAGE_VERSION_ID >= 30000))
if (mysql_reset_connection(_pHandle) != 0)
#else
if (mysql_refresh(_pHandle, REFRESH_TABLES | REFRESH_STATUS | REFRESH_THREADS | REFRESH_READ_LOCK) != 0)
#endif
throw TransactionException("Reset connection failed.", _pHandle);
}
bool SessionHandle::ping()
{
int rc = mysql_ping(_pHandle);
return rc == 0;
}
} } } // namespace Poco::Data::MySQL

View File

@ -0,0 +1,353 @@
//
// SessionImpl.cpp
//
// Library: Data/MySQL
// Package: MySQL
// Module: SessionImpl
//
// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Data/MySQL/SessionImpl.h"
#include "Poco/Data/MySQL/MySQLStatementImpl.h"
#include "Poco/Data/Session.h"
#include "Poco/NumberParser.h"
#include "Poco/String.h"
namespace
{
std::string copyStripped(std::string::const_iterator from, std::string::const_iterator to)
{
// skip leading spaces
while ((from != to) && isspace(*from)) from++;
// skip trailing spaces
while ((from != to) && isspace(*(to - 1))) to--;
return std::string(from, to);
}
}
namespace Poco {
namespace Data {
namespace MySQL {
const std::string SessionImpl::MYSQL_READ_UNCOMMITTED = "READ UNCOMMITTED";
const std::string SessionImpl::MYSQL_READ_COMMITTED = "READ COMMITTED";
const std::string SessionImpl::MYSQL_REPEATABLE_READ = "REPEATABLE READ";
const std::string SessionImpl::MYSQL_SERIALIZABLE = "SERIALIZABLE";
SessionImpl::SessionImpl(const std::string& connectionString, std::size_t loginTimeout) :
Poco::Data::AbstractSessionImpl<SessionImpl>(connectionString, loginTimeout),
_connector("MySQL"),
_handle(0),
_reset(false),
_connected(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();
}
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>(getLoginTimeout());
_handle.options(MYSQL_OPT_CONNECT_TIMEOUT, timeout);
std::map<std::string, std::string> options;
// Default values
options["host"] = "localhost";
options["port"] = "3306";
options["user"] = "";
options["password"] = "";
options["db"] = "";
options["compress"] = "";
options["auto-reconnect"] = "";
options["secure-auth"] = "";
options["character-set"] = "utf8";
options["reset"] = "";
options["fail-readonly"] = "";
const std::string& connString = connectionString();
for (std::string::const_iterator start = connString.begin();;)
{
std::string::const_iterator finish = std::find(start, connString.end(), ';');
std::string::const_iterator middle = std::find(start, finish, '=');
if (middle == finish)
throw MySQLException("create session: bad connection string format, can not find '='");
options[copyStripped(start, middle)] = copyStripped(middle + 1, finish);
if ((finish == connString.end()) || (finish + 1 == connString.end())) break;
start = finish + 1;
}
if (options["user"].empty())
throw MySQLException("create session: specify user name");
const char * db = NULL;
if (!options["db"].empty())
db = options["db"].c_str();
unsigned int port = 0;
if (!NumberParser::tryParseUnsigned(options["port"], port) || 0 == port || port > 65535)
throw MySQLException("create session: specify correct port (numeric in decimal notation)");
if (options["compress"] == "true")
_handle.options(MYSQL_OPT_COMPRESS);
else if (options["compress"] == "false")
;
else if (!options["compress"].empty())
throw MySQLException("create session: specify correct compress option (true or false)");
if (options["auto-reconnect"] == "true")
_handle.options(MYSQL_OPT_RECONNECT, true);
else if (options["auto-reconnect"] == "false")
_handle.options(MYSQL_OPT_RECONNECT, false);
else if (!options["auto-reconnect"].empty())
throw MySQLException("create session: specify correct auto-reconnect option (true or false)");
#ifdef MYSQL_SECURE_AUTH
if (options["secure-auth"] == "true")
_handle.options(MYSQL_SECURE_AUTH, true);
else if (options["secure-auth"] == "false")
_handle.options(MYSQL_SECURE_AUTH, false);
else if (!options["secure-auth"].empty())
throw MySQLException("create session: specify correct secure-auth option (true or false)");
#endif
if (!options["character-set"].empty())
_handle.options(MYSQL_SET_CHARSET_NAME, options["character-set"].c_str());
if (options["reset"] == "true")
_reset = true;
else if (options["reset"] == "false")
_reset = false;
else if (!options["reset"].empty())
throw MySQLException("create session: specify correct reset option (true or false)");
if (options["fail-readonly"] == "true")
_failIfInnoReadOnly = true;
else if (options["fail-readonly"] == "false")
_failIfInnoReadOnly = false;
else if (!options["fail-readonly"].empty())
throw MySQLException("create session: specify correct fail-readonly option (true or false)");
// Real connect
_handle.connect(options["host"].c_str(),
options["user"].c_str(),
options["password"].c_str(),
db,
port);
addFeature("autoCommit",
&SessionImpl::autoCommit,
&SessionImpl::isAutoCommit);
_connected = true;
}
SessionImpl::~SessionImpl()
{
close();
}
Poco::Data::StatementImpl::Ptr SessionImpl::createStatementImpl()
{
return new MySQLStatementImpl(*this);
}
void SessionImpl::begin()
{
Poco::FastMutex::ScopedLock l(_mutex);
if (_inTransaction)
throw Poco::InvalidAccessException("Already in transaction.");
_handle.startTransaction();
_inTransaction = true;
}
void SessionImpl::commit()
{
_handle.commit();
_inTransaction = false;
}
void SessionImpl::rollback()
{
_handle.rollback();
_inTransaction = false;
}
void SessionImpl::autoCommit(const std::string&, bool val)
{
StatementExecutor ex(_handle);
ex.prepare(Poco::format("SET autocommit=%d", val ? 1 : 0));
ex.execute();
}
bool SessionImpl::isAutoCommit(const std::string&) const
{
int ac = 0;
return 1 == getSetting("autocommit", ac);
}
void SessionImpl::setTransactionIsolation(Poco::UInt32 ti)
{
std::string isolation;
switch (ti)
{
case Session::TRANSACTION_READ_UNCOMMITTED:
isolation = MYSQL_READ_UNCOMMITTED; break;
case Session::TRANSACTION_READ_COMMITTED:
isolation = MYSQL_READ_COMMITTED; break;
case Session::TRANSACTION_REPEATABLE_READ:
isolation = MYSQL_REPEATABLE_READ; break;
case Session::TRANSACTION_SERIALIZABLE:
isolation = MYSQL_SERIALIZABLE; break;
default:
throw Poco::InvalidArgumentException("setTransactionIsolation()");
}
StatementExecutor ex(_handle);
ex.prepare(Poco::format("SET SESSION TRANSACTION ISOLATION LEVEL %s", isolation));
ex.execute();
}
Poco::UInt32 SessionImpl::getTransactionIsolation() const
{
std::string isolation;
getSetting("tx_isolation", isolation);
Poco::replaceInPlace(isolation, "-", " ");
if (MYSQL_READ_UNCOMMITTED == isolation)
return Session::TRANSACTION_READ_UNCOMMITTED;
else if (MYSQL_READ_COMMITTED == isolation)
return Session::TRANSACTION_READ_COMMITTED;
else if (MYSQL_REPEATABLE_READ == isolation)
return Session::TRANSACTION_REPEATABLE_READ;
else if (MYSQL_SERIALIZABLE == isolation)
return Session::TRANSACTION_SERIALIZABLE;
throw InvalidArgumentException("getTransactionIsolation()");
}
bool SessionImpl::hasTransactionIsolation(Poco::UInt32 ti) const
{
return Session::TRANSACTION_READ_UNCOMMITTED == ti ||
Session::TRANSACTION_READ_COMMITTED == ti ||
Session::TRANSACTION_REPEATABLE_READ == ti ||
Session::TRANSACTION_SERIALIZABLE == ti;
}
void SessionImpl::reset()
{
if (_connected && _reset)
{
_handle.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)
{
_handle.close();
_connected = false;
}
}
void SessionImpl::setConnectionTimeout(std::size_t timeout)
{
_handle.options(MYSQL_OPT_READ_TIMEOUT, static_cast<unsigned int>(timeout));
_handle.options(MYSQL_OPT_WRITE_TIMEOUT, static_cast<unsigned int>(timeout));
_timeout = timeout;
}
} } } // namespace Poco::Data::MySQL

View File

@ -0,0 +1,149 @@
//
// StatementExecutor.cpp
//
// Library: Data/MySQL
// Package: MySQL
// Module: StatementExecutor
//
// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include <mysql.h>
#include "Poco/Data/MySQL/StatementExecutor.h"
#include "Poco/Format.h"
namespace Poco {
namespace Data {
namespace MySQL {
StatementExecutor::StatementExecutor(MYSQL* mysql)
: _pSessionHandle(mysql)
, _affectedRowCount(0)
{
if (!(_pHandle = mysql_stmt_init(mysql)))
throw StatementException("mysql_stmt_init error");
_state = STMT_INITED;
}
StatementExecutor::~StatementExecutor()
{
mysql_stmt_close(_pHandle);
}
int StatementExecutor::state() const
{
return _state;
}
void StatementExecutor::prepare(const std::string& query)
{
if (_state >= STMT_COMPILED)
{
_state = STMT_COMPILED;
return;
}
int rc = mysql_stmt_prepare(_pHandle, query.c_str(), static_cast<unsigned int>(query.length()));
if (rc != 0)
{
// retry if connection lost
int err = mysql_errno(_pSessionHandle);
if (err == 2006 /* CR_SERVER_GONE_ERROR */ || err == 2013 /* CR_SERVER_LOST */)
{
rc = mysql_stmt_prepare(_pHandle, query.c_str(), static_cast<unsigned int>(query.length()));
}
}
if (rc != 0) throw StatementException("mysql_stmt_prepare error", _pHandle, query);
_query = query;
_state = STMT_COMPILED;
}
void StatementExecutor::bindParams(MYSQL_BIND* params, std::size_t count)
{
if (_state < STMT_COMPILED)
throw StatementException("Statement is not compiled yet");
if (count != mysql_stmt_param_count(_pHandle))
throw StatementException("wrong bind parameters count", 0, _query);
if (count == 0) return;
if (mysql_stmt_bind_param(_pHandle, params) != 0)
throw StatementException("mysql_stmt_bind_param() error ", _pHandle, _query);
}
void StatementExecutor::bindResult(MYSQL_BIND* result)
{
if (_state < STMT_COMPILED)
throw StatementException("Statement is not compiled yet");
if (mysql_stmt_bind_result(_pHandle, result) != 0)
throw StatementException("mysql_stmt_bind_result error ", _pHandle, _query);
}
void StatementExecutor::execute()
{
if (_state < STMT_COMPILED)
throw StatementException("Statement is not compiled yet");
if (mysql_stmt_execute(_pHandle) != 0)
throw StatementException("mysql_stmt_execute error", _pHandle, _query);
_state = STMT_EXECUTED;
my_ulonglong affectedRows = mysql_affected_rows(_pSessionHandle);
if (affectedRows != ((my_ulonglong) - 1))
_affectedRowCount = static_cast<std::size_t>(affectedRows); //Was really a DELETE, UPDATE or INSERT statement
}
bool StatementExecutor::fetch()
{
if (_state < STMT_EXECUTED)
throw StatementException("Statement is not executed yet");
int res = mysql_stmt_fetch(_pHandle);
// we have specified zero buffers for BLOBs, so DATA_TRUNCATED is normal in this case
if ((res != 0) && (res != MYSQL_NO_DATA) && (res != MYSQL_DATA_TRUNCATED))
throw StatementException("mysql_stmt_fetch error", _pHandle, _query);
return (res == 0) || (res == MYSQL_DATA_TRUNCATED);
}
bool StatementExecutor::fetchColumn(std::size_t n, MYSQL_BIND *bind)
{
if (_state < STMT_EXECUTED)
throw StatementException("Statement is not executed yet");
int res = mysql_stmt_fetch_column(_pHandle, bind, static_cast<unsigned int>(n), 0);
if ((res != 0) && (res != MYSQL_NO_DATA))
throw StatementException(Poco::format("mysql_stmt_fetch_column(%z) error", n), _pHandle, _query);
return (res == 0);
}
int StatementExecutor::getAffectedRowCount() const
{
return static_cast<int>(_affectedRowCount);
}
} } } // namespace Poco::Data::MySQL

66
vendor/POCO/Data/MySQL/src/Utility.cpp vendored Normal file
View File

@ -0,0 +1,66 @@
//
// Utility.cpp
//
// Library: Data/MySQL
// Package: MySQL
// Module: Utility
//
// Implementation of Utility
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Data/MySQL/Utility.h"
#include <mysql.h>
namespace Poco {
namespace Data {
namespace MySQL {
std::string Utility::serverInfo(MYSQL* pHandle)
{
std::string info(mysql_get_server_info(pHandle));
return info;
}
std::string Utility::serverInfo(Session& session)
{
std::string info(mysql_get_server_info(handle(session)));
return info;
}
unsigned long Utility::serverVersion(MYSQL* pHandle)
{
return mysql_get_server_version(pHandle);
}
unsigned long Utility::serverVersion(Session& session)
{
return mysql_get_server_version(handle(session));
}
std::string Utility::hostInfo(MYSQL* pHandle)
{
std::string info(mysql_get_host_info(pHandle));
return info;
}
std::string Utility::hostInfo(Session& session)
{
std::string info(mysql_get_host_info(handle(session)));
return info;
}
} } } // namespace Poco::Data::MySQL