Skip to content

Commit

Permalink
The most important changes involve the relocation and redefinition of…
Browse files Browse the repository at this point in the history
… macros, and the modification of function parameters and SQL command string construction in various files.

1. The `PKEY` macro definition has been moved from `BaseCommand.h`, `BaseQuery.h` to `ApplicationApi.h`. It is now defined as "Id" instead of its previous definition. This change affects all files and functions that use this macro.

2. The `ASYNC` macro has been defined in `ApplicationApi.h` and removed from `BaseQuery.h`. This change affects all files and functions that use this macro.

3. The `UpdateApplicationUser` and `DeleteApplicationUser` functions in `ApplicationUserServices.cpp` have been modified to use `Id` instead of `id` as a parameter. The `Update` function call within these functions now uses `EQ(Id)` instead of `id`.

4. The `Update` and `Delete` functions in `BaseCommand.h` have been modified to use `query` instead of `id` as a parameter. The SQL command string construction within these functions has been modified accordingly.

5. The `UpdateTodoItem` and `DeleteTodoItem` functions in `TodoItemService.cpp`, and the `UpdateTodoList` and `DeleteTodoList` functions in `TodoListService.cpp` have been modified in a similar way to `UpdateApplicationUser` and `DeleteApplicationUser`.

6. Various changes have been made in `BaseQuery.h` to replace usage of `id` with `query` in SQL command string construction. This change affects all functions that use SQL command strings constructed in this file.

References to the code changes can be found in the respective files: `ApplicationApi.h`, `BaseCommand.h`, `BaseQuery.h`, `ApplicationUserServices.cpp`, `TodoItemService.cpp`, and `TodoListService.cpp`.
  • Loading branch information
k-nero committed Feb 24, 2024
1 parent c7518ce commit 30e2a00
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 42 deletions.
6 changes: 6 additions & 0 deletions Application/ApplicationApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
#define APPLICATION_API __declspec(dllimport)
#endif

#ifndef PKEY
#define PKEY "Id"
#endif // !PKEY

#define ASYNC

#define EQ(x) _EQ(x, x)
#define NEQ(x) _NEQ(x, x)
#define GT(x) _GT(x, x)
Expand Down
8 changes: 4 additions & 4 deletions Application/ApplicationUserServices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ std::string ApplicationUserService::CreateApplicationUser(ApplicationUser* appli
return id;
}

int ApplicationUserService::UpdateApplicationUser(ApplicationUser* applicationUser, const std::string& id) noexcept(false)
int ApplicationUserService::UpdateApplicationUser(ApplicationUser* applicationUser, const std::string& Id) noexcept(false)
{
ApplicationUserCommand cmd;
return cmd.Update(applicationUser, id);
return cmd.Update(applicationUser, EQ(Id));
}

int ApplicationUserService::DeleteApplicationUser(const std::string& id)
int ApplicationUserService::DeleteApplicationUser(const std::string& Id)
{
ApplicationUserCommand cmd;
return cmd.Delete(id);
return cmd.Delete(EQ(Id));
}

ApplicationUserService::~ApplicationUserService()
Expand Down
30 changes: 11 additions & 19 deletions Application/BaseCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
#include "CoreHelper.h"
#include "TypeCheck.h"

#ifndef PKEY
#define PKEY "Id"
#endif // !PKEY

template <typename T, class D = boost::describe::describe_members<T, boost::describe::mod_any_access | boost::describe::mod_inherited>>
class APPLICATION_API BaseCommand : public IBaseCommand<T>
Expand Down Expand Up @@ -124,16 +121,16 @@ class APPLICATION_API BaseCommand : public IBaseCommand<T>
return 0;
}

virtual int Update(T* item, const std::string& id) noexcept(false) override
virtual int Update(T* item, const std::string& query) noexcept(false) override
{
auto con = db->GetConnection();
try
{
std::string table_name = typeid(T).name();
table_name = table_name.substr(table_name.find_last_of(' ') + 1);
std::string query = "UPDATE [dbo].[" + table_name + "] SET ";
std::string command = "UPDATE [dbo].[" + table_name + "] SET ";
SACommand cmd(con);
cmd.setCommandText(_TSA(query.c_str()));
cmd.setCommandText(_TSA(command.c_str()));
std::vector<std::string> fields;
boost::mp11::mp_for_each<D>([&](auto D)
{
Expand All @@ -142,17 +139,17 @@ class APPLICATION_API BaseCommand : public IBaseCommand<T>
{
if (field == "ModifiedDate")
{
query += field + " = GETDATE(), ";
command += field + " = GETDATE(), ";
}
else
{
query += field + " = :" + field + ", ";
command += field + " = :" + field + ", ";
fields.push_back(field);
}
}
});
query = query.substr(0, query.length() - 2) + " WHERE Id = :id";
cmd.setCommandText(_TSA(query.c_str()));
command = command.substr(0, command.length() - 2) + " WHERE " + query;
cmd.setCommandText(_TSA(command.c_str()));
boost::mp11::mp_for_each<D>([&](auto D)
{
for (auto& f : fields)
Expand Down Expand Up @@ -192,7 +189,6 @@ class APPLICATION_API BaseCommand : public IBaseCommand<T>
}
}
});
cmd.Param(_TSA("id")).setAsString() = id.c_str();
cmd.Execute();
return (int)cmd.RowsAffected();
}
Expand All @@ -211,21 +207,17 @@ class APPLICATION_API BaseCommand : public IBaseCommand<T>
return 0;
}

virtual int Delete(const std::string& id) noexcept(false) override
virtual int Delete(const std::string& query) noexcept(false) override
{
auto con = db->GetConnection();
try
{
if (id.empty())
{
throw std::exception("Internal error! Id is empty");
}

std::string table_name = typeid(T).name();
table_name = table_name.substr(table_name.find_last_of(' ') + 1);
std::string query = "DELETE FROM [dbo].[" + table_name + "] WHERE Id = :id";
std::string command = "DELETE FROM [dbo].[" + table_name + "] WHERE " + query;
SACommand cmd(con);
cmd.setCommandText(_TSA(query.c_str()));
cmd.Param(_TSA("id")).setAsString() = id.c_str();
cmd.setCommandText(_TSA(command.c_str()));
cmd.Execute();
return (int)cmd.RowsAffected();
}
Expand Down
11 changes: 0 additions & 11 deletions Application/BaseQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
#include <string>
#include <vector>

#define ASYNC

#ifndef PKEY
#define PKEY "Id"
#endif // !PKEY

//TODO: Apply concepts
template <typename T, typename Z = std::is_base_of<BaseEntity, T>::type>
class APPLICATION_API BaseQuery
Expand All @@ -46,7 +40,6 @@ class APPLICATION_API BaseQuery
{
throw std::exception("Id is empty");
}

std::string table_name = typeid(K).name();
table_name = table_name.substr(table_name.find_last_of(' ') + 1);
std::string query = "SELECT * FROM [dbo].[" + table_name + "] WHERE " PKEY " = :id";
Expand Down Expand Up @@ -76,7 +69,6 @@ class APPLICATION_API BaseQuery
auto inner_items = inner_items_future.get();
#else
auto inner_items = GetAll<inner_elem_type>(table_name + "Id = '" + std::string(cmd.Field(PKEY).asString().GetMultiByteChars()) + "'", includes);

#endif // ASYNC
(item.get()->*(D).pointer) = std::any_cast<type>(inner_items);
}
Expand All @@ -99,9 +91,7 @@ class APPLICATION_API BaseQuery
(item.get()->*(D).pointer) = std::any_cast<type>(inner_item);
}
}

}

});
}
return item;
Expand Down Expand Up @@ -364,7 +354,6 @@ class APPLICATION_API BaseQuery

base_query = "SELECT * FROM ( SELECT ROW_NUMBER() OVER (ORDER BY CreatedDate) AS RowNum, * FROM [dbo].[" + table_name + "]";


if (query.length() > 1)
{
base_query += " WHERE " + query;
Expand Down
8 changes: 4 additions & 4 deletions Application/TodoItemService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ std::string TodoItemService::CreateTodoItem(TodoItem* TodoItem) noexcept(false)
return TodoItem->GetId();
}

int TodoItemService::UpdateTodoItem(TodoItem* TodoItem, const std::string& id) noexcept(false)
int TodoItemService::UpdateTodoItem(TodoItem* TodoItem, const std::string& Id) noexcept(false)
{
TodoItemCommand cmd;
return cmd.Update(TodoItem, id);
return cmd.Update(TodoItem, EQ(Id));
}

int TodoItemService::DeleteTodoItem(const std::string& id)
int TodoItemService::DeleteTodoItem(const std::string& Id)
{
TodoItemCommand cmd;
return cmd.Delete(id);
return cmd.Delete(EQ(Id));
}

TodoItemService::~TodoItemService()
Expand Down
8 changes: 4 additions & 4 deletions Application/TodoListService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ std::string TodoListService::CreateTodoList(TodoList* todo_list) noexcept(false)
return todo_list->GetId();
}

int TodoListService::UpdateTodoList(TodoList* todo_list, const std::string& id) noexcept(false)
int TodoListService::UpdateTodoList(TodoList* todo_list, const std::string& Id) noexcept(false)
{
TodoListCommand cmd;
return cmd.Update(todo_list, id);
return cmd.Update(todo_list, EQ(Id));

}

int TodoListService::DeleteTodoList(const std::string& id)
int TodoListService::DeleteTodoList(const std::string& Id)
{
TodoListCommand cmd;
return cmd.Delete(id);
return cmd.Delete(EQ(Id));
}

TodoListService::~TodoListService()
Expand Down

0 comments on commit 30e2a00

Please sign in to comment.