1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-27 22:57:45 +00:00

Blob fields can now be returned as vector.

This commit is contained in:
Mordred 2017-09-20 18:25:51 +02:00
parent 6f62b2f13b
commit 271c857d40
5 changed files with 50 additions and 19 deletions

View file

@ -8,13 +8,14 @@
Core::Db::Connection::Connection( MySqlBase * pBase,
const std::string& hostName,
const std::string& userName,
const std::string& password ) :
const std::string& password,
uint16_t port ) :
m_pBase( pBase ),
m_bConnected( false )
{
m_pRawCon = mysql_init( nullptr );
if( mysql_real_connect( m_pRawCon, hostName.c_str(), userName.c_str(), password.c_str(),
nullptr, 3306, nullptr, 0) == nullptr )
nullptr, port, nullptr, 0) == nullptr )
throw std::runtime_error( mysql_error( m_pRawCon ) );
m_bConnected = true;
@ -24,7 +25,8 @@ Core::Db::Connection::Connection( MySqlBase * pBase,
const std::string& hostName,
const std::string& userName,
const std::string& password,
const optionMap& options ) :
const optionMap& options,
uint16_t port ) :
m_pBase( pBase )
{
m_pRawCon = mysql_init( nullptr );
@ -100,7 +102,7 @@ Core::Db::Connection::Connection( MySqlBase * pBase,
if( mysql_real_connect( m_pRawCon, hostName.c_str(), userName.c_str(), password.c_str(),
nullptr, 3306, nullptr, 0) == nullptr )
nullptr, port, nullptr, 0) == nullptr )
throw std::runtime_error( mysql_error( m_pRawCon ) );
}

View file

@ -18,20 +18,19 @@ namespace Db
class Connection
{
//Statement * createServiceStmt();
public:
public:
Connection( MySqlBase * pBase,
const std::string& hostName,
const std::string& userName,
const std::string& password );
const std::string& password,
uint16_t port = 3306);
Connection( MySqlBase * pBase,
const std::string& hostName,
const std::string& userName,
const std::string& password,
const optionMap& options );
const optionMap& options,
uint16_t port = 3306 );
virtual ~Connection();
@ -53,20 +52,14 @@ namespace Db
Statement * createStatement();
//// implemented up to this point
void beginTransaction();
void commitTransaction();
void rollbackTransaction();
std::string getSchema();
//DatabaseMetaData * getMetaData();
//enum_transaction_isolation getTransactionIsolation();
std::string getError();
bool isReadOnly();
@ -90,11 +83,11 @@ namespace Db
std::string getLastStatementInfo();
MYSQL * getRawCon();
MYSQL* getRawCon();
private:
MySqlBase * m_pBase;
MYSQL * m_pRawCon;
MySqlBase* m_pBase;
MYSQL* m_pRawCon;
bool m_bConnected;
Connection( const Connection& );

View file

@ -5,6 +5,7 @@
#include <cctype>
#include <clocale>
#include <vector>
#include <boost/scoped_ptr.hpp>
Core::Db::ResultSet::ResultSet( MYSQL_RES *res, Core::Db::Statement *par )
{
@ -272,3 +273,25 @@ std::istream* Core::Db::ResultSet::getBlob( const std::string& columnLabel ) con
return new std::istringstream( getString( columnLabel ) );
}
std::vector< char > Core::Db::ResultSet::getBlobVector( uint32_t columnIndex ) const
{
if( columnIndex == 0 || columnIndex > m_numFields )
throw std::runtime_error( "ResultSet::getBlobVector: invalid value of 'columnIndex'" );
boost::scoped_ptr< std::istream > inStr( getBlob( columnIndex ) );
char buff[4196];
std::string s;
std::vector< char > data;
inStr->read( buff, sizeof( buff ) );
if( inStr->gcount() )
{
data.resize( inStr->gcount() );
memcpy( data.data(), buff, inStr->gcount() );
}
return data;
}
std::vector< char > Core::Db::ResultSet::getBlobVector( const std::string& columnLabel ) const
{
return getBlobVector( findColumn( columnLabel ) );
}

View file

@ -4,6 +4,7 @@
#include <boost/shared_ptr.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <vector>
#include "ResultSetBase.h"
#include <mysql.h>
@ -52,6 +53,9 @@ namespace Core
std::istream * getBlob( uint32_t columnIndex ) const;
std::istream * getBlob( const std::string& columnLabel ) const;
std::vector< char > getBlobVector( uint32_t columnIndex ) const;
std::vector< char > getBlobVector( const std::string& columnLabel ) const;
bool getBoolean( uint32_t columnIndex ) const;
bool getBoolean( const std::string& columnLabel ) const;

View file

@ -213,6 +213,15 @@ bool Core::ServerZone::loadSettings( int32_t argc, char* argv[] )
}
boost::scoped_ptr< Core::Db::Statement > stmt3( con->createStatement() );
boost::scoped_ptr< Core::Db::ResultSet > res1( stmt3->executeQuery( "SELECT * FROM charabase" ) );
while( res1->next() )
{
auto blob = res1->getBlobVector( "Customize" );
}
}
catch( std::runtime_error e )
{