Skip to content

Commit

Permalink
Fixed original sql string not being passed to error callback
Browse files Browse the repository at this point in the history
  • Loading branch information
FredyH committed Sep 1, 2022
1 parent b1d29b5 commit 17483ee
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/lua/LuaIQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void LuaIQuery::runAbortedCallback(ILuaBase *LUA, const std::shared_ptr<IQueryDa
LuaObject::pcallWithErrorReporter(LUA, 1);
}

void LuaIQuery::runErrorCallback(ILuaBase *LUA, const std::shared_ptr<IQueryData> &data) {
void LuaIQuery::runErrorCallback(ILuaBase *LUA, const std::shared_ptr<IQuery> &iQuery, const std::shared_ptr<IQueryData> &data) {
if (data->m_tableReference == 0) return;

if (!LuaIQuery::pushCallbackReference(LUA, data->m_errorReference, data->m_tableReference,
Expand All @@ -87,7 +87,8 @@ void LuaIQuery::runErrorCallback(ILuaBase *LUA, const std::shared_ptr<IQueryData
LUA->ReferencePush(data->m_tableReference);
auto error = data->getError();
LUA->PushString(error.c_str());
LuaObject::pcallWithErrorReporter(LUA, 2);
LUA->PushString(iQuery->getSQLString().c_str());
LuaObject::pcallWithErrorReporter(LUA, 3);
}

void LuaIQuery::addMetaTableFunctions(ILuaBase *LUA) {
Expand Down Expand Up @@ -160,7 +161,7 @@ void LuaIQuery::runCallback(ILuaBase *LUA, const std::shared_ptr<IQuery> &iQuery
case QUERY_NONE:
break; //Should not happen
case QUERY_ERROR:
runErrorCallback(LUA, data);
runErrorCallback(LUA, iQuery, data);
break;
case QUERY_SUCCESS:
if (auto query = std::dynamic_pointer_cast<Query>(iQuery)) {
Expand Down
2 changes: 1 addition & 1 deletion src/lua/LuaIQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class LuaIQuery : public LuaObject {

static void runAbortedCallback(ILuaBase *LUA, const std::shared_ptr<IQueryData> &data);

static void runErrorCallback(ILuaBase *LUA, const std::shared_ptr<IQueryData> &data);
static void runErrorCallback(ILuaBase *LUA, const std::shared_ptr<IQuery> &iQuery, const std::shared_ptr<IQueryData> &data);

static void runCallback(ILuaBase *LUA, const std::shared_ptr<IQuery> &query, const std::shared_ptr<IQueryData> &data);

Expand Down
2 changes: 2 additions & 0 deletions src/mysql/IQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class IQuery : public std::enable_shared_from_this<IQuery> {

std::vector<std::shared_ptr<IQueryData>> abort();

virtual std::string getSQLString() = 0;

void wait(bool shouldSwap);

bool hasCallbackData() const {
Expand Down
18 changes: 13 additions & 5 deletions src/mysql/PingQuery.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#ifndef PINGQUERY_
#define PINGQUERY_

#include <unordered_map>
#include "Query.h"
#include "MySQLHeader.h"
Expand All @@ -9,12 +10,19 @@


class PingQuery : public Query {
friend class Database;
friend class Database;

public:
~PingQuery() override;
~PingQuery() override;

protected:
explicit PingQuery(const std::shared_ptr<Database>& dbase);
void executeStatement(Database &database, MYSQL* m_sql, const std::shared_ptr<IQueryData> &data) override;
bool pingSuccess = false;
explicit PingQuery(const std::shared_ptr<Database> &dbase);

std::string getSQLString() override { return ""; };

void executeStatement(Database &database, MYSQL *m_sql, const std::shared_ptr<IQueryData> &data) override;

bool pingSuccess = false;
};

#endif
8 changes: 6 additions & 2 deletions src/mysql/Query.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Query : public IQuery {
public:
~Query() override;

void executeStatement(Database &database, MYSQL *m_sql, const std::shared_ptr<IQueryData>& data) override;
void executeStatement(Database &database, MYSQL *m_sql, const std::shared_ptr<IQueryData> &data) override;

my_ulonglong lastInsert();

Expand All @@ -32,7 +32,10 @@ class Query : public IQuery {

int m_dataReference = 0;

static std::shared_ptr<Query> create(const std::shared_ptr<Database> &dbase, const std::string& query);
std::string getSQLString() override { return m_query; };

static std::shared_ptr<Query> create(const std::shared_ptr<Database> &dbase, const std::string &query);

protected:
Query(const std::shared_ptr<Database> &dbase, std::string query);

Expand Down Expand Up @@ -75,6 +78,7 @@ class QueryData : public IQueryData {
std::deque<my_ulonglong> m_affectedRows;
std::deque<my_ulonglong> m_insertIds;
std::deque<ResultData> m_results;

QueryData() = default;
};

Expand Down
7 changes: 5 additions & 2 deletions src/mysql/Transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ class Transaction : public IQuery {

static std::shared_ptr<Transaction> create(const std::shared_ptr<Database> &database);

std::string getSQLString() override { return ""; };

protected:
void executeStatement(Database &database, MYSQL *connection, const std::shared_ptr<IQueryData>& data) override;
void executeStatement(Database &database, MYSQL *connection, const std::shared_ptr<IQueryData> &data) override;

explicit Transaction(const std::shared_ptr<Database> &database) : IQuery(database) {

}

private:
static void applyChildResultStatus(const std::shared_ptr<TransactionData>& data);
static void applyChildResultStatus(const std::shared_ptr<TransactionData> &data);

static void mysqlAutocommit(MYSQL *sql, bool auto_mode);

Expand Down

0 comments on commit 17483ee

Please sign in to comment.