From e3a78a7a31eb25de1cf93a8f9a1e182f49041c63 Mon Sep 17 00:00:00 2001 From: Zwelf Date: Wed, 15 Jul 2020 00:12:08 +0200 Subject: [PATCH] Add BindBlob and BindFloat function to IDbConnection interface --- src/engine/server/databases/connection.h | 2 ++ src/engine/server/databases/mysql.cpp | 20 ++++++++++++++++++++ src/engine/server/databases/mysql.h | 2 ++ src/engine/server/databases/sqlite.cpp | 14 ++++++++++++++ src/engine/server/databases/sqlite.h | 2 ++ 5 files changed, 40 insertions(+) diff --git a/src/engine/server/databases/connection.h b/src/engine/server/databases/connection.h index 17ad211d1..0285452fc 100644 --- a/src/engine/server/databases/connection.h +++ b/src/engine/server/databases/connection.h @@ -41,7 +41,9 @@ public: // PrepareStatement has to be called beforehand, virtual void BindString(int Idx, const char *pString) = 0; + virtual void BindBlob(int Idx, unsigned char *pBlob, int Size) = 0; virtual void BindInt(int Idx, int Value) = 0; + virtual void BindFloat(int Idx, float Value) = 0; // executes the query and returns if a result row exists and selects it // when called multiple times the next row is selected diff --git a/src/engine/server/databases/mysql.cpp b/src/engine/server/databases/mysql.cpp index 149d9148e..998c0cfa9 100644 --- a/src/engine/server/databases/mysql.cpp +++ b/src/engine/server/databases/mysql.cpp @@ -4,6 +4,8 @@ #include #endif +#include + lock CMysqlConnection::m_SqlDriverLock; CMysqlConnection::CMysqlConnection( @@ -205,6 +207,16 @@ void CMysqlConnection::BindString(int Idx, const char *pString) #endif } +void CMysqlConnection::BindBlob(int Idx, unsigned char *pBlob, int Size) +{ +#if defined(CONF_SQL) + // copy blob into string + auto Blob = std::string(pBlob, pBlob+Size); + m_pPreparedStmt->setString(Idx, Blob); + m_NewQuery = true; +#endif +} + void CMysqlConnection::BindInt(int Idx, int Value) { #if defined(CONF_SQL) @@ -213,6 +225,14 @@ void CMysqlConnection::BindInt(int Idx, int Value) #endif } +void CMysqlConnection::BindFloat(int Idx, float Value) +{ +#if defined(CONF_SQL) + m_pPreparedStmt->setDouble(Idx, (double)Value); + m_NewQuery = true; +#endif +} + bool CMysqlConnection::Step() { #if defined(CONF_SQL) diff --git a/src/engine/server/databases/mysql.h b/src/engine/server/databases/mysql.h index 50d6c475b..b7c1a78be 100644 --- a/src/engine/server/databases/mysql.h +++ b/src/engine/server/databases/mysql.h @@ -39,7 +39,9 @@ public: virtual void PrepareStatement(const char *pStmt); virtual void BindString(int Idx, const char *pString); + virtual void BindBlob(int Idx, unsigned char *pBlob, int Size); virtual void BindInt(int Idx, int Value); + virtual void BindFloat(int Idx, float Value); virtual bool Step(); diff --git a/src/engine/server/databases/sqlite.cpp b/src/engine/server/databases/sqlite.cpp index 6e12da6b4..b8e4c6bff 100644 --- a/src/engine/server/databases/sqlite.cpp +++ b/src/engine/server/databases/sqlite.cpp @@ -118,6 +118,13 @@ void CSqliteConnection::BindString(int Idx, const char *pString) m_Done = false; } +void CSqliteConnection::BindBlob(int Idx, unsigned char *pBlob, int Size) +{ + int Result = sqlite3_bind_blob(m_pStmt, Idx, pBlob, Size, NULL); + ExceptionOnError(Result); + m_Done = false; +} + void CSqliteConnection::BindInt(int Idx, int Value) { int Result = sqlite3_bind_int(m_pStmt, Idx, Value); @@ -125,6 +132,13 @@ void CSqliteConnection::BindInt(int Idx, int Value) m_Done = false; } +void CSqliteConnection::BindFloat(int Idx, float Value) +{ + int Result = sqlite3_bind_double(m_pStmt, Idx, (double)Value); + ExceptionOnError(Result); + m_Done = false; +} + bool CSqliteConnection::Step() { if(m_Done) diff --git a/src/engine/server/databases/sqlite.h b/src/engine/server/databases/sqlite.h index d31eb2442..0d6763a37 100644 --- a/src/engine/server/databases/sqlite.h +++ b/src/engine/server/databases/sqlite.h @@ -26,7 +26,9 @@ public: virtual void PrepareStatement(const char *pStmt); virtual void BindString(int Idx, const char *pString); + virtual void BindBlob(int Idx, unsigned char *pBlob, int Size); virtual void BindInt(int Idx, int Value); + virtual void BindFloat(int Idx, float Value); virtual bool Step();