mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-14 23:07:56 +02:00
MySQL fixes/additions/improvements
- fixed GH #187: MySQL: allow access to the underlying connection handle - added GH #186: MySQL: support for MYSQL_SECURE_AUTH - fixed GH #174: MySQL: 4GB allocated when reading any largetext or largeblob field
This commit is contained in:
@@ -189,11 +189,15 @@ void ResultMetadata::init(MYSQL_STMT* stmt)
|
||||
|
||||
{for (std::size_t i = 0; i < count; i++)
|
||||
{
|
||||
unsigned long size = fieldSize(fields[i]);
|
||||
unsigned long zero = 0;
|
||||
if (size == ~zero) size = 0;
|
||||
|
||||
_columns.push_back(MetaColumn(
|
||||
i, // position
|
||||
fields[i].name, // name
|
||||
fieldType(fields[i]), // type
|
||||
fieldSize(fields[i]), // length
|
||||
size, // length
|
||||
0, // TODO: precision
|
||||
!IS_NOT_NULL(fields[i].flags) // nullable
|
||||
));
|
||||
@@ -208,19 +212,19 @@ void ResultMetadata::init(MYSQL_STMT* stmt)
|
||||
|
||||
std::size_t offset = 0;
|
||||
|
||||
{for (std::size_t i = 0; i < count; i++)
|
||||
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 = static_cast<unsigned int>(_columns[i].length());
|
||||
_row[i].buffer = &_buffer[0] + offset;
|
||||
_row[i].buffer_length = len;
|
||||
_row[i].buffer = (len > 0) ? (&_buffer[0] + offset) : 0;
|
||||
_row[i].length = &_lengths[i];
|
||||
_row[i].is_null = &_isNull[i];
|
||||
_row[i].is_unsigned = (fields[i].flags & UNSIGNED_FLAG) > 0;
|
||||
|
||||
offset += _row[i].buffer_length;
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t ResultMetadata::columnsReturned() const
|
||||
|
@@ -95,8 +95,13 @@ void SessionHandle::options(mysql_option opt, unsigned int i)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
|
@@ -72,10 +72,8 @@ SessionImpl::SessionImpl(const std::string& connectionString, std::size_t loginT
|
||||
_connected(false),
|
||||
_inTransaction(false)
|
||||
{
|
||||
addProperty("insertId",
|
||||
&SessionImpl::setInsertId,
|
||||
&SessionImpl::getInsertId);
|
||||
|
||||
addProperty("insertId", &SessionImpl::setInsertId, &SessionImpl::getInsertId);
|
||||
setProperty("handle", static_cast<MYSQL*>(_handle));
|
||||
open();
|
||||
setConnectionTimeout(CONNECTION_TIMEOUT_DEFAULT);
|
||||
}
|
||||
@@ -109,6 +107,7 @@ void SessionImpl::open(const std::string& connect)
|
||||
options["db"] = "";
|
||||
options["compress"] = "";
|
||||
options["auto-reconnect"] = "";
|
||||
options["secure-auth"] = "";
|
||||
|
||||
const std::string& connString = connectionString();
|
||||
for (std::string::const_iterator start = connString.begin();;)
|
||||
@@ -126,7 +125,7 @@ void SessionImpl::open(const std::string& connect)
|
||||
start = finish + 1;
|
||||
}
|
||||
|
||||
if (options["user"] == "")
|
||||
if (options["user"].empty())
|
||||
throw MySQLException("create session: specify user name");
|
||||
|
||||
const char * db = NULL;
|
||||
@@ -141,16 +140,23 @@ void SessionImpl::open(const std::string& connect)
|
||||
_handle.options(MYSQL_OPT_COMPRESS);
|
||||
else if (options["compress"] == "false")
|
||||
;
|
||||
else if (options["compress"] != "")
|
||||
else if (!options["compress"].empty())
|
||||
throw MySQLException("create session: specify correct compress option (true or false) or skip it");
|
||||
|
||||
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"] != "")
|
||||
else if (!options["auto-reconnect"].empty())
|
||||
throw MySQLException("create session: specify correct auto-reconnect option (true or false) or skip it");
|
||||
|
||||
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) or skip it");
|
||||
|
||||
// Real connect
|
||||
_handle.connect(options["host"].c_str(),
|
||||
options["user"].c_str(),
|
||||
|
@@ -36,7 +36,7 @@
|
||||
|
||||
#include <mysql.h>
|
||||
#include "Poco/Data/MySQL/StatementExecutor.h"
|
||||
#include <sstream>
|
||||
#include "Poco/Format.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
@@ -120,7 +120,7 @@ void StatementExecutor::execute()
|
||||
|
||||
my_ulonglong affectedRows = mysql_affected_rows(_pSessionHandle);
|
||||
if (affectedRows != ((my_ulonglong) - 1))
|
||||
_affectedRowCount = affectedRows; //Was really a DELETE, UPDATE or INSERT statement
|
||||
_affectedRowCount = static_cast<std::size_t>(affectedRows); //Was really a DELETE, UPDATE or INSERT statement
|
||||
}
|
||||
|
||||
|
||||
@@ -131,10 +131,11 @@ bool StatementExecutor::fetch()
|
||||
|
||||
int res = mysql_stmt_fetch(_pHandle);
|
||||
|
||||
if ((res != 0) && (res != MYSQL_NO_DATA))
|
||||
// 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);
|
||||
return (res == 0) || (res == MYSQL_DATA_TRUNCATED);
|
||||
}
|
||||
|
||||
|
||||
@@ -146,11 +147,7 @@ bool StatementExecutor::fetchColumn(std::size_t n, MYSQL_BIND *bind)
|
||||
int res = mysql_stmt_fetch_column(_pHandle, bind, static_cast<unsigned int>(n), 0);
|
||||
|
||||
if ((res != 0) && (res != MYSQL_NO_DATA))
|
||||
{
|
||||
std::ostringstream msg;
|
||||
msg << "mysql_stmt_fetch_column(" << n << ") error";
|
||||
throw StatementException(msg.str(), _pHandle, _query);
|
||||
}
|
||||
throw StatementException(Poco::format("mysql_stmt_fetch_column(%z) error", n), _pHandle, _query);
|
||||
|
||||
return (res == 0);
|
||||
}
|
||||
|
88
Data/MySQL/src/Utility.cpp
Normal file
88
Data/MySQL/src/Utility.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
//
|
||||
// Utility.cpp
|
||||
//
|
||||
// $Id: //poco/Main/Data/MySQL/src/Utility.cpp#5 $
|
||||
//
|
||||
// Library: MySQL
|
||||
// Package: MySQL
|
||||
// Module: Utility
|
||||
//
|
||||
// Implementation of Utility
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#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
|
Reference in New Issue
Block a user