Implement dump_sqlserver

This commit is contained in:
Zwelf 2020-08-03 16:18:22 +02:00
parent 0fa13f6bf9
commit b2cf3cafc5
8 changed files with 50 additions and 23 deletions

View file

@ -3,6 +3,8 @@
#include <base/system.h>
class IConsole;
// can hold one PreparedStatement with Results
class IDbConnection
{
@ -13,6 +15,7 @@ public:
}
virtual ~IDbConnection() {}
IDbConnection& operator=(const IDbConnection&) = delete;
virtual void Print(IConsole *pConsole, const char *Mode) = 0;
// copies the credentials, not the active connection
virtual IDbConnection *Copy() = 0;

View file

@ -1,5 +1,6 @@
#include "connection_pool.h"
#include <engine/console.h>
#if defined(CONF_SQL)
#include <cppconn/exception.h>
#endif
@ -67,6 +68,15 @@ CDbConnectionPool::~CDbConnectionPool()
{
}
void CDbConnectionPool::Print(IConsole *pConsole, Mode DatabaseMode)
{
const char *ModeDesc[] = {"Read", "Write", "WriteBackup"};
for(unsigned int i = 0; i < m_aapDbConnections[DatabaseMode].size(); i++)
{
m_aapDbConnections[DatabaseMode][i]->Print(pConsole, ModeDesc[DatabaseMode]);
}
}
void CDbConnectionPool::RegisterDatabase(std::unique_ptr<IDbConnection> pDatabase, Mode DatabaseMode)
{
if(DatabaseMode < 0 || NUM_MODES <= DatabaseMode)

View file

@ -13,6 +13,8 @@ struct ISqlData
virtual ~ISqlData() {};
};
class IConsole;
class CDbConnectionPool
{
public:
@ -31,6 +33,8 @@ public:
NUM_MODES,
};
void Print(IConsole *pConsole, Mode DatabaseMode);
void RegisterDatabase(std::unique_ptr<IDbConnection> pDatabase, Mode DatabaseMode);
void Execute(

View file

@ -1,5 +1,6 @@
#include "mysql.h"
#include <engine/console.h>
#if defined(CONF_SQL)
#include <cppconn/driver.h>
#endif
@ -43,6 +44,15 @@ CMysqlConnection::~CMysqlConnection()
#endif
}
void CMysqlConnection::Print(IConsole *pConsole, const char *Mode)
{
char aBuf[512];
str_format(aBuf, sizeof(aBuf),
"MySQL-%s: DB: '%s' Prefix: '%s' User: '%s' Pass: '%s' IP: <{'%s'}> Port: %d",
Mode, m_aDatabase, GetPrefix(), m_aUser, m_aPass, m_aIp, m_Port);
pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf);
}
CMysqlConnection *CMysqlConnection::Copy()
{
return new CMysqlConnection(m_aDatabase, GetPrefix(), m_aUser, m_aPass, m_aIp, m_Port, m_Setup);

View file

@ -25,6 +25,7 @@ public:
int Port,
bool Setup);
virtual ~CMysqlConnection();
virtual void Print(IConsole *pConsole, const char *Mode);
virtual CMysqlConnection *Copy();

View file

@ -1,6 +1,7 @@
#include "sqlite.h"
#include <base/math.h>
#include <engine/console.h>
#include <stdexcept>
#include <sqlite3.h>
@ -25,6 +26,17 @@ CSqliteConnection::~CSqliteConnection()
m_pDb = nullptr;
}
void CSqliteConnection::Print(IConsole *pConsole, const char *Mode)
{
char aBuf[512];
str_format(aBuf, sizeof(aBuf),
"SQLite-%s: DB: '%s'",
Mode, m_aFilename);
pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf);
}
void CSqliteConnection::ToUnixTimestamp(const char *pTimestamp, char *aBuf, unsigned int BufferSize)
{
str_format(aBuf, BufferSize, "strftime('%%s', %s)", pTimestamp);

View file

@ -12,6 +12,7 @@ class CSqliteConnection : public IDbConnection
public:
CSqliteConnection(const char *pFilename, bool Setup);
virtual ~CSqliteConnection();
virtual void Print(IConsole *pConsole, const char *Mode);
virtual CSqliteConnection *Copy();

View file

@ -3127,42 +3127,28 @@ void CServer::ConAddSqlServer(IConsole::IResult *pResult, void *pUserData)
pResult->GetString(1), pResult->GetString(2), pResult->GetString(3),
pResult->GetString(5), pResult->GetInteger(6));
pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf);
if(SetUpDb)
{
/* TODO
if(!pSqlServers->CreateTables())
pSelf->SetErrorShutdown("database create tables failed");
*/
}
pSelf->DbPool()->RegisterDatabase(std::move(pSqlServers), ReadOnly ? CDbConnectionPool::READ : CDbConnectionPool::WRITE);
}
void CServer::ConDumpSqlServers(IConsole::IResult *pResult, void *pUserData)
{
/* TODO
CServer *pSelf = (CServer *)pUserData;
bool ReadOnly;
if (str_comp_nocase(pResult->GetString(0), "w") == 0)
ReadOnly = false;
else if (str_comp_nocase(pResult->GetString(0), "r") == 0)
ReadOnly = true;
if(str_comp_nocase(pResult->GetString(0), "w") == 0)
{
pSelf->DbPool()->Print(pSelf->Console(), CDbConnectionPool::WRITE);
pSelf->DbPool()->Print(pSelf->Console(), CDbConnectionPool::WRITE_BACKUP);
}
else if(str_comp_nocase(pResult->GetString(0), "r") == 0)
{
pSelf->DbPool()->Print(pSelf->Console(), CDbConnectionPool::READ);
}
else
{
pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", "choose either 'r' for SqlReadServer or 'w' for SqlWriteServer");
return;
}
CSqlServer** apSqlServers = ReadOnly ? pSelf->m_apSqlReadServers : pSelf->m_apSqlWriteServers;
for (int i = 0; i < MAX_SQLSERVERS; i++)
if (apSqlServers[i])
{
char aBuf[512];
str_format(aBuf, sizeof(aBuf), "SQL-%s %d: DB: '%s' Prefix: '%s' User: '%s' Pass: '%s' IP: <{'%s'}> Port: %d", ReadOnly ? "Read" : "Write", i, apSqlServers[i]->GetDatabase(), apSqlServers[i]->GetPrefix(), apSqlServers[i]->GetUser(), apSqlServers[i]->GetPass(), apSqlServers[i]->GetIP(), apSqlServers[i]->GetPort());
pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf);
}
*/
}
void CServer::ConchainSpecialInfoupdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)