From fd432b29f61065a577638e488a99a3d28e48625d Mon Sep 17 00:00:00 2001 From: amass <168062547@qq.com> Date: Sat, 30 Dec 2023 01:19:36 +0800 Subject: [PATCH] Add task. --- .gitignore | 1 + CMakeLists.txt | 4 +- Server/CMakeLists.txt | 15 +++++- Server/Database.cpp | 51 ++++++++++++++++++- Server/Database.h | 15 +++++- Server/ThirdParty/CMakeLists.txt | 5 -- Server/main.cpp | 4 ++ ThirdParty/CMakeLists.txt | 7 +++ .../ThirdParty => ThirdParty}/sqlite/shell.c | 0 .../sqlite/sqlite3.c | 0 .../sqlite/sqlite3.h | 0 .../sqlite/sqlite3ext.h | 0 UnitTest/CMakeLists.txt | 18 +++++++ UnitTest/DatabaseTest.cpp | 18 +++++++ UnitTest/main.cpp | 2 + resource/deploy.sh | 6 ++- 16 files changed, 136 insertions(+), 10 deletions(-) delete mode 100644 Server/ThirdParty/CMakeLists.txt create mode 100644 ThirdParty/CMakeLists.txt rename {Server/ThirdParty => ThirdParty}/sqlite/shell.c (100%) rename {Server/ThirdParty => ThirdParty}/sqlite/sqlite3.c (100%) rename {Server/ThirdParty => ThirdParty}/sqlite/sqlite3.h (100%) rename {Server/ThirdParty => ThirdParty}/sqlite/sqlite3ext.h (100%) create mode 100644 UnitTest/CMakeLists.txt create mode 100644 UnitTest/DatabaseTest.cpp create mode 100644 UnitTest/main.cpp diff --git a/.gitignore b/.gitignore index 16171bc..a407a4f 100644 --- a/.gitignore +++ b/.gitignore @@ -41,5 +41,6 @@ target_wrapper.* # QtCreator CMake CMakeLists.txt.user* build +logs Server/logs Server/conf/cert \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d8ba49..80ad66d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,4 +8,6 @@ FetchContent_Declare(Kylin ) FetchContent_MakeAvailable(Kylin) -add_subdirectory(Server) \ No newline at end of file +add_subdirectory(Server) +add_subdirectory(ThirdParty) +add_subdirectory(UnitTest) \ No newline at end of file diff --git a/Server/CMakeLists.txt b/Server/CMakeLists.txt index 8db33f8..3ef736c 100644 --- a/Server/CMakeLists.txt +++ b/Server/CMakeLists.txt @@ -1,5 +1,18 @@ find_package(Boost COMPONENTS program_options json REQUIRED) +add_library(Database + Database.h Database.cpp +) + +target_include_directories(Database + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} +) + +target_link_libraries(Database + PUBLIC sqlite3 + PUBLIC Universal +) + add_executable(Server main.cpp HttpSession.h HttpSession.cpp Listener.h Listener.cpp @@ -15,8 +28,8 @@ add_executable(Server main.cpp ) target_link_libraries(Server - PRIVATE Universal PRIVATE HttpProxy + PRIVATE Database PRIVATE ${Boost_LIBRARIES} ) diff --git a/Server/Database.cpp b/Server/Database.cpp index 5f3b3d4..1c40030 100644 --- a/Server/Database.cpp +++ b/Server/Database.cpp @@ -1,7 +1,56 @@ #include "Database.h" +#include "BoostLog.h" +#include +#include bool Database::open(const std::string &path) { + bool ret = true; + int result = sqlite3_open(path.c_str(), &m_sqlite3); + if (result != SQLITE_OK) { + ret = false; + LOG(error) << "open database failed."; + } + initialize(); + return ret; } -void Database::addTask(uint64_t createTime, const std::string &content) { +void Database::addTask(uint64_t createTime, const std::string &content, bool finished) { + std::ostringstream oss; + oss << "INSERT INTO tasks (create_time,content,finished) VALUES (" << createTime << ",\"" << content << "\"," + << finished << ");"; + auto sql = oss.str(); + int result = sqlite3_exec(m_sqlite3, sql.c_str(), NULL, NULL, NULL); + if (result != SQLITE_OK) { + LOG(error) << "add task failed: " << sqlite3_errmsg(m_sqlite3) << ", sql: " << sql; + return; + } +} + +void Database::setTaskFinished(int id, bool finished, uint64_t finishedTime) { + std::ostringstream oss; + oss << "UPDATE tasks SET finished = " << finished << ", finished_time = " << finishedTime << " WHERE id = " << id; + auto sql = oss.str(); + int result = sqlite3_exec(m_sqlite3, sql.c_str(), NULL, NULL, NULL); + if (result != SQLITE_OK) { + LOG(error) << "add task failed: " << sqlite3_errmsg(m_sqlite3) << ", sql: " << sql; + return; + } +} + +void Database::initialize() { + const char *sql = + "CREATE TABLE IF NOT EXISTS tasks (id INTEGER PRIMARY KEY AUTOINCREMENT, create_time INTEGER NOT NULL, " + "parent_id INTEGER, content VARCHAR(512) NOT NULL, finished BOLL, finished_time INTEGER);"; + int result = sqlite3_exec(m_sqlite3, sql, NULL, NULL, NULL); + if (result != SQLITE_OK) { + LOG(error) << "Failed to create table: " << sqlite3_errmsg(m_sqlite3); + return; + } +} + +Database::~Database() { + if (m_sqlite3 != nullptr) { + sqlite3_close(m_sqlite3); + m_sqlite3 = nullptr; + } } diff --git a/Server/Database.h b/Server/Database.h index 5360634..c141357 100644 --- a/Server/Database.h +++ b/Server/Database.h @@ -1,12 +1,25 @@ #ifndef __DATABASE_H__ #define __DATABASE_H__ +#include "Singleton.h" #include +struct sqlite3; + class Database { + friend class Amass::Singleton; + public: + ~Database(); bool open(const std::string &path); - void addTask(uint64_t createTime, const std::string &content); + void addTask(uint64_t createTime, const std::string &content, bool finished = false); + void setTaskFinished(int id, bool finished, uint64_t finishedTime); + +protected: + void initialize(); + +private: + sqlite3 *m_sqlite3 = nullptr; }; #endif // __DATABASE_H__ \ No newline at end of file diff --git a/Server/ThirdParty/CMakeLists.txt b/Server/ThirdParty/CMakeLists.txt deleted file mode 100644 index 324ce39..0000000 --- a/Server/ThirdParty/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ - - -add_library(sqlite - sqlite/sqlite3.c -) \ No newline at end of file diff --git a/Server/main.cpp b/Server/main.cpp index 479aa7e..f8125f5 100644 --- a/Server/main.cpp +++ b/Server/main.cpp @@ -11,6 +11,7 @@ #include #include #include +#include "Database.h" void initSettings(); @@ -56,6 +57,9 @@ int main(int argc, char const *argv[]) { } initSettings(); + auto database = Amass::Singleton::instance(); + database->open("database.sqlite"); + std::string server = "0.0.0.0"; boost::property_tree::ptree ptree; diff --git a/ThirdParty/CMakeLists.txt b/ThirdParty/CMakeLists.txt new file mode 100644 index 0000000..0208476 --- /dev/null +++ b/ThirdParty/CMakeLists.txt @@ -0,0 +1,7 @@ +add_library(sqlite3 + sqlite/sqlite3.c +) + +target_include_directories(sqlite3 + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/sqlite +) \ No newline at end of file diff --git a/Server/ThirdParty/sqlite/shell.c b/ThirdParty/sqlite/shell.c similarity index 100% rename from Server/ThirdParty/sqlite/shell.c rename to ThirdParty/sqlite/shell.c diff --git a/Server/ThirdParty/sqlite/sqlite3.c b/ThirdParty/sqlite/sqlite3.c similarity index 100% rename from Server/ThirdParty/sqlite/sqlite3.c rename to ThirdParty/sqlite/sqlite3.c diff --git a/Server/ThirdParty/sqlite/sqlite3.h b/ThirdParty/sqlite/sqlite3.h similarity index 100% rename from Server/ThirdParty/sqlite/sqlite3.h rename to ThirdParty/sqlite/sqlite3.h diff --git a/Server/ThirdParty/sqlite/sqlite3ext.h b/ThirdParty/sqlite/sqlite3ext.h similarity index 100% rename from Server/ThirdParty/sqlite/sqlite3ext.h rename to ThirdParty/sqlite/sqlite3ext.h diff --git a/UnitTest/CMakeLists.txt b/UnitTest/CMakeLists.txt new file mode 100644 index 0000000..09697b5 --- /dev/null +++ b/UnitTest/CMakeLists.txt @@ -0,0 +1,18 @@ +find_package(Boost REQUIRED COMPONENTS unit_test_framework) + +# --detect_memory_leak=0 --run_test=MarkdownParserTest,ProcessUtilityTest +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +add_executable(UnitTest main.cpp + DatabaseTest.cpp +) + +target_compile_definitions(UnitTest + PUBLIC LOG_FILTER_LEVEL=1 +) + +target_link_libraries(UnitTest + PRIVATE Boost::unit_test_framework + PRIVATE Database +) diff --git a/UnitTest/DatabaseTest.cpp b/UnitTest/DatabaseTest.cpp new file mode 100644 index 0000000..d19fe6d --- /dev/null +++ b/UnitTest/DatabaseTest.cpp @@ -0,0 +1,18 @@ +#include "Database.h" +#include "BoostLog.h" +#include + +static constexpr auto path = "build/database.sqlite"; +using namespace std::chrono; + +BOOST_AUTO_TEST_CASE(DatabaseTest) { + Database database; + BOOST_TEST(database.open(path)); + + database.addTask(1234, "Hello"); + + database.addTask(1234, "这是一个测试", true); + + auto now = duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); + database.setTaskFinished(1, true, now); +} \ No newline at end of file diff --git a/UnitTest/main.cpp b/UnitTest/main.cpp new file mode 100644 index 0000000..ebf9395 --- /dev/null +++ b/UnitTest/main.cpp @@ -0,0 +1,2 @@ +#define BOOST_TEST_MODULE OlderTest +#include "boost/test/included/unit_test.hpp" diff --git a/resource/deploy.sh b/resource/deploy.sh index cfc2a62..a7401e0 100755 --- a/resource/deploy.sh +++ b/resource/deploy.sh @@ -27,6 +27,10 @@ function build() { /opt/Qt/Tools/CMake/bin/cmake \ --build ${build_path} \ --target all + if [ $? -ne 0 ]; then + exit 1 + fi + build/UnitTest/UnitTest } function deploy_backend() { @@ -66,4 +70,4 @@ main $@ # curl -k --insecure https://127.0.0.1/lua # openresty -p Server -# sudo openresty -p Server -s reload \ No newline at end of file +# sudo openresty -p Server -s reload