diff --git a/Server/Database/CMakeLists.txt b/Server/Database/CMakeLists.txt index 4c74e86..2e67e0a 100644 --- a/Server/Database/CMakeLists.txt +++ b/Server/Database/CMakeLists.txt @@ -1,6 +1,7 @@ add_library(Database Database.h Database.cpp Task.h Task.cpp + HomeBox.h HomeBox.cpp ) target_include_directories(Database diff --git a/Server/Database/Database.cpp b/Server/Database/Database.cpp index fd93b63..e9831e9 100644 --- a/Server/Database/Database.cpp +++ b/Server/Database/Database.cpp @@ -106,6 +106,51 @@ void Database::setTaskFinished(int id, bool finished, uint64_t finishedTime) { } } +bool Database::addHomeBoxItem(const std::string &name, const std::string &location, int cost) { + bool ret = true; + std::ostringstream oss; + oss << "INSERT INTO homebox (name,location,cost) VALUES (\"" << name << "\",\"" << location << "\"," << cost + << ");"; + auto sql = oss.str(); + char *error = nullptr; + int result = sqlite3_exec(m_sqlite3, sql.c_str(), NULL, NULL, &error); + if (result != SQLITE_OK) { + LOG(error) << "add task failed: " << error << ", sql: " << sql; + sqlite3_free(error); + ret = false; + } + return ret; +} + +static int selectHomeBoxItemCallback(void *data, int argc, char **argv, char **columnName) { + auto items = reinterpret_cast(data); + HomeBox::Item item; + for (int i = 0; i < argc; i++) { + if (argv[i] == nullptr) continue; + if (strcmp(columnName[i], "id") == 0) { + item.id = std::atol(argv[i]); + } else if (strcmp(columnName[i], "name") == 0) { + item.name = argv[i]; + } else if (strcmp(columnName[i], "location") == 0) { + item.location = argv[i]; + } else if (strcmp(columnName[i], "cost") == 0) { + item.cost = std::atol(argv[i]); + } + } + items->push_back(item); + return 0; +} + +HomeBox::Items Database::homeBoxItems() { + HomeBox::Items ret; + char *error = nullptr; + if (sqlite3_exec(m_sqlite3, "select * from homebox", selectHomeBoxItemCallback, &ret, &error) != SQLITE_OK) { + LOG(error) << "sqlite3_exec() failed: " << error << std::endl; + sqlite3_free(error); + } + return ret; +} + void Database::initialize() { const char *sql = "CREATE TABLE IF NOT EXISTS tasks (id INTEGER PRIMARY KEY AUTOINCREMENT, create_time INTEGER NOT NULL, " @@ -116,6 +161,14 @@ void Database::initialize() { LOG(error) << "Failed to create table: " << sqlite3_errmsg(m_sqlite3); return; } + + const char *homeBoxSql = "CREATE TABLE IF NOT EXISTS homebox (id INTEGER PRIMARY KEY AUTOINCREMENT, " + "name VARCHAR(512) NOT NULL, location VARCHAR(512) NOT NULL, cost INTEGER);"; + result = sqlite3_exec(m_sqlite3, homeBoxSql, NULL, NULL, NULL); + if (result != SQLITE_OK) { + LOG(error) << "Failed to create table: " << sqlite3_errmsg(m_sqlite3); + return; + } } Database::~Database() { diff --git a/Server/Database/Database.h b/Server/Database/Database.h index 9906f81..048d904 100644 --- a/Server/Database/Database.h +++ b/Server/Database/Database.h @@ -1,6 +1,7 @@ #ifndef __DATABASE_H__ #define __DATABASE_H__ +#include "HomeBox.h" #include "Singleton.h" #include "Task.h" #include @@ -19,6 +20,9 @@ public: bool removeTask(int id); void setTaskFinished(int id, bool finished, uint64_t finishedTime); + HomeBox::Items homeBoxItems(); + bool addHomeBoxItem(const std::string &name, const std::string &location, int cost); + protected: void initialize(); diff --git a/Server/Database/HomeBox.cpp b/Server/Database/HomeBox.cpp new file mode 100644 index 0000000..6e66874 --- /dev/null +++ b/Server/Database/HomeBox.cpp @@ -0,0 +1 @@ +#include "HomeBox.h" \ No newline at end of file diff --git a/Server/Database/HomeBox.h b/Server/Database/HomeBox.h new file mode 100644 index 0000000..c394a28 --- /dev/null +++ b/Server/Database/HomeBox.h @@ -0,0 +1,20 @@ +#ifndef __HOMEBOX_H__ +#define __HOMEBOX_H__ + +#include +#include + +class HomeBox { +public: + class Item { + public: + int id = -1; + std::string name; + std::string location; + int cost; + }; + + using Items = std::list; +}; + +#endif // __HOMEBOX_H__ \ No newline at end of file diff --git a/UnitTest/DatabaseTest.cpp b/UnitTest/DatabaseTest.cpp index f668138..f798b84 100644 --- a/UnitTest/DatabaseTest.cpp +++ b/UnitTest/DatabaseTest.cpp @@ -1,17 +1,25 @@ #include "Database.h" #include "BoostLog.h" #include +#include static constexpr auto path = "build/database.sqlite"; using namespace std::chrono; BOOST_AUTO_TEST_CASE(DatabaseTest) { + if (std::filesystem::exists(path)) { + std::filesystem::remove(path); + } Database database; BOOST_TEST(database.open(path)); database.addTask(1234, "Hello"); - database.addTask(1234, "这是一个测试","", true); + database.addTask(1234, "这是一个测试", "", true); + + database.addHomeBoxItem("手机", "抽屉", 1499); + auto items = database.homeBoxItems(); + BOOST_CHECK_EQUAL(items.size(), 1); auto now = duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); database.setTaskFinished(1, true, now); diff --git a/resource/build.sh b/resource/build.sh index bf1e5c4..c96ce63 100755 --- a/resource/build.sh +++ b/resource/build.sh @@ -72,3 +72,4 @@ main $@ # curl -k --insecure https://127.0.0.1/lua # openresty -p Server # sudo openresty -p Server -s reload +# export LD_LIBRARY_PATH=/opt/Libraries/boost_1_85_0/lib:$LD_LIBRARY_PATH \ No newline at end of file