diff --git a/hikyuu_cpp/hikyuu/utilities/db_connect/TableMacro.h b/hikyuu_cpp/hikyuu/utilities/db_connect/TableMacro.h index 7a73aa0dc..38ccb1314 100644 --- a/hikyuu_cpp/hikyuu/utilities/db_connect/TableMacro.h +++ b/hikyuu_cpp/hikyuu/utilities/db_connect/TableMacro.h @@ -10,6 +10,7 @@ #ifndef HIKYUU_DB_CONNECT_TABLE_MACRO_H #define HIKYUU_DB_CONNECT_TABLE_MACRO_H +#include #include "DBConnectBase.h" #include "SQLStatementBase.h" @@ -26,6 +27,9 @@ public: \ void id(uint64_t val) { \ m_id = val; \ } \ + static std::string getTableName() { \ + return #table; \ + } \ static const char* getInsertSQL() { \ return "insert into `" #table "` (`" #f1 "`) values (?)"; \ } \ @@ -56,6 +60,9 @@ public: \ void id(uint64_t val) { \ m_id = val; \ } \ + static std::string getTableName() { \ + return #table; \ + } \ static const char* getInsertSQL() { \ return "insert into `" #table "` (`" #f1 "`,`" #f2 "`) values (?,?)"; \ } \ @@ -86,6 +93,9 @@ public: void id(uint64_t val) { \ m_id = val; \ } \ + static std::string getTableName() { \ + return #table; \ + } \ static const char* getInsertSQL() { \ return "insert into `" #table "` (`" #f1 "`,`" #f2 "`,`" #f3 "`) values (?,?,?)"; \ } \ @@ -116,6 +126,9 @@ public: void id(uint64_t val) { \ m_id = val; \ } \ + static std::string getTableName() { \ + return #table; \ + } \ static const char* getInsertSQL() { \ return "insert into `" #table "` (`" #f1 "`,`" #f2 "`,`" #f3 "`,`" #f4 \ "`) values (?,?,?,?)"; \ @@ -148,6 +161,9 @@ public: void id(uint64_t val) { \ m_id = val; \ } \ + static std::string getTableName() { \ + return #table; \ + } \ static const char* getInsertSQL() { \ return "insert into `" #table "` (`" #f1 "`,`" #f2 "`,`" #f3 "`,`" #f4 "`,`" #f5 \ "`) values (?,?,?,?,?)"; \ @@ -180,6 +196,9 @@ public: void id(uint64_t val) { \ m_id = val; \ } \ + static std::string getTableName() { \ + return #table; \ + } \ static const char* getInsertSQL() { \ return "insert into `" #table "` (`" #f1 "`,`" #f2 "`,`" #f3 "`,`" #f4 "`,`" #f5 "`,`" #f6 \ "`) values (?,?,?,?,?,?)"; \ @@ -213,6 +232,9 @@ public: void id(uint64_t val) { \ m_id = val; \ } \ + static std::string getTableName() { \ + return #table; \ + } \ static const char* getInsertSQL() { \ return "insert into `" #table "` (`" #f1 "`,`" #f2 "`,`" #f3 "`,`" #f4 "`,`" #f5 "`,`" #f6 \ "`,`" #f7 "`) values (?,?,?,?,?,?,?)"; \ @@ -246,6 +268,9 @@ public: void id(uint64_t val) { \ m_id = val; \ } \ + static std::string getTableName() { \ + return #table; \ + } \ static const char* getInsertSQL() { \ return "insert into `" #table "` (`" #f1 "`,`" #f2 "`,`" #f3 "`,`" #f4 "`,`" #f5 "`,`" #f6 \ "`,`" #f7 "`,`" #f8 "`) values (?,?,?,?,?,?,?,?)"; \ @@ -279,6 +304,9 @@ public: void id(uint64_t val) { \ m_id = val; \ } \ + static std::string getTableName() { \ + return #table; \ + } \ static const char* getInsertSQL() { \ return "insert into `" #table "` (`" #f1 "`,`" #f2 "`,`" #f3 "`,`" #f4 "`,`" #f5 "`,`" #f6 \ "`,`" #f7 "`,`" #f8 "`,`" #f9 "`) values (?,?,?,?,?,?,?,?,?)"; \ @@ -312,6 +340,9 @@ public: void id(uint64_t val) { \ m_id = val; \ } \ + static std::string getTableName() { \ + return #table; \ + } \ static const char* getInsertSQL() { \ return "insert into `" #table "` (`" #f1 "`,`" #f2 "`,`" #f3 "`,`" #f4 "`,`" #f5 "`,`" #f6 \ "`,`" #f7 "`,`" #f8 "`,`" #f9 "`,`" #f10 "`) values (?,?,?,?,?,?,?,?,?,?)"; \ @@ -345,6 +376,9 @@ public: void id(uint64_t val) { \ m_id = val; \ } \ + static std::string getTableName() { \ + return #table; \ + } \ static const char* getInsertSQL() { \ return "insert into `" #table "` (`" #f1 "`,`" #f2 "`,`" #f3 "`,`" #f4 "`,`" #f5 "`,`" #f6 \ "`,`" #f7 "`,`" #f8 "`,`" #f9 "`,`" #f10 "`,`" #f11 \ @@ -380,6 +414,9 @@ public: void id(uint64_t val) { \ m_id = val; \ } \ + static std::string getTableName() { \ + return #table; \ + } \ static const char* getInsertSQL() { \ return "insert into `" #table "` (`" #f1 "`,`" #f2 "`,`" #f3 "`,`" #f4 "`,`" #f5 "`,`" #f6 \ "`,`" #f7 "`,`" #f8 "`,`" #f9 "`,`" #f10 "`,`" #f11 "`,`" #f12 \ @@ -415,6 +452,9 @@ public: void id(uint64_t val) { \ m_id = val; \ } \ + static std::string getTableName() { \ + return #table; \ + } \ static const char* getInsertSQL() { \ return "insert into `" #table "` (`" #f1 "`,`" #f2 "`,`" #f3 "`,`" #f4 "`,`" #f5 "`,`" #f6 \ "`,`" #f7 "`,`" #f8 "`,`" #f9 "`,`" #f10 "`,`" #f11 "`,`" #f12 "`,`" #f13 \ @@ -451,6 +491,9 @@ public: void id(uint64_t val) { \ m_id = val; \ } \ + static std::string getTableName() { \ + return #table; \ + } \ static const char* getInsertSQL() { \ return "insert into `" #table "` (`" #f1 "`,`" #f2 "`,`" #f3 "`,`" #f4 "`,`" #f5 "`,`" #f6 \ "`,`" #f7 "`,`" #f8 "`,`" #f9 "`,`" #f10 "`,`" #f11 "`,`" #f12 "`,`" #f13 \ @@ -487,6 +530,9 @@ public: void id(uint64_t val) { \ m_id = val; \ } \ + static std::string getTableName() { \ + return #table; \ + } \ static const char* getInsertSQL() { \ return "insert into `" #table "` (`" #f1 "`,`" #f2 "`,`" #f3 "`,`" #f4 "`,`" #f5 "`,`" #f6 \ "`,`" #f7 "`,`" #f8 "`,`" #f9 "`,`" #f10 "`,`" #f11 "`,`" #f12 "`,`" #f13 \ @@ -523,6 +569,9 @@ public: void id(uint64_t val) { \ m_id = val; \ } \ + static std::string getTableName() { \ + return #table; \ + } \ static const char* getInsertSQL() { \ return "insert into `" #table "` (`" #f1 "`,`" #f2 "`,`" #f3 "`,`" #f4 "`,`" #f5 "`,`" #f6 \ "`,`" #f7 "`,`" #f8 "`,`" #f9 "`,`" #f10 "`,`" #f11 "`,`" #f12 "`,`" #f13 \ @@ -562,6 +611,9 @@ public: void id(uint64_t val) { \ m_id = val; \ } \ + static std::string getTableName() { \ + return #table; \ + } \ static const char* getInsertSQL() { \ return "insert into `" #table "` (`" #f1 "`,`" #f2 "`,`" #f3 "`,`" #f4 "`,`" #f5 "`,`" #f6 \ "`,`" #f7 "`,`" #f8 "`,`" #f9 "`,`" #f10 "`,`" #f11 "`,`" #f12 "`,`" #f13 \ @@ -603,6 +655,9 @@ public: void id(uint64_t val) { \ m_id = val; \ } \ + static std::string getTableName() { \ + return #table; \ + } \ static const char* getInsertSQL() { \ return "insert into `" #table "` (`" #f1 "`,`" #f2 "`,`" #f3 "`,`" #f4 "`,`" #f5 "`,`" #f6 \ "`,`" #f7 "`,`" #f8 "`,`" #f9 "`,`" #f10 "`,`" #f11 "`,`" #f12 "`,`" #f13 \ @@ -645,6 +700,9 @@ public: void id(uint64_t val) { \ m_id = val; \ } \ + static std::string getTableName() { \ + return #table; \ + } \ static const char* getInsertSQL() { \ return "insert into `" #table "` (`" #f1 "`,`" #f2 "`,`" #f3 "`,`" #f4 "`,`" #f5 "`,`" #f6 \ "`,`" #f7 "`,`" #f8 "`,`" #f9 "`,`" #f10 "`,`" #f11 "`,`" #f12 "`,`" #f13 \ @@ -687,6 +745,9 @@ public: void id(uint64_t val) { \ m_id = val; \ } \ + static std::string getTableName() { \ + return #table; \ + } \ static const char* getInsertSQL() { \ return "insert into `" #table "` (`" #f1 "`,`" #f2 "`,`" #f3 "`,`" #f4 "`,`" #f5 "`,`" #f6 \ "`,`" #f7 "`,`" #f8 "`,`" #f9 "`,`" #f10 "`,`" #f11 "`,`" #f12 "`,`" #f13 \ diff --git a/hikyuu_cpp/hikyuu_server/common/uuid.h b/hikyuu_cpp/hikyuu_server/common/uuid.h new file mode 100644 index 000000000..7c782371f --- /dev/null +++ b/hikyuu_cpp/hikyuu_server/common/uuid.h @@ -0,0 +1,22 @@ +/* + * Copyright(C) 2021 hikyuu.org + * + * Create on: 2021-04-12 + * Author: fasiondog + */ + +#pragma once + +#include +#include +#include +#include + +namespace hku { + +inline std::string UUID() { + static auto rgen = boost::uuids::random_generator(); + return boost::uuids::to_string(rgen()); +} + +} // namespace hku \ No newline at end of file diff --git a/hikyuu_cpp/hikyuu_server/http/HttpError.h b/hikyuu_cpp/hikyuu_server/http/HttpError.h index 9f7d6818a..6352a6f1c 100644 --- a/hikyuu_cpp/hikyuu_server/http/HttpError.h +++ b/hikyuu_cpp/hikyuu_server/http/HttpError.h @@ -30,11 +30,11 @@ enum HttpErrorCode { WRONG_PARAMETER_TYPE // 参数类型错误(各个业务接口返回各个接口的参数) }; -#define HTTP_VALID_CHECK(expr, errcode, errmsg) \ - { \ - if (!(expr)) { \ - throw HttpError(errcode, errmsg); \ - } \ +#define HTTP_VALID_CHECK(expr, errcode, ...) \ + { \ + if (!(expr)) { \ + throw HttpError(errcode, fmt::format(__VA_ARGS__)); \ + } \ } class HttpError : public HttpException { diff --git a/hikyuu_cpp/hikyuu_server/service/RestErrorCode.h b/hikyuu_cpp/hikyuu_server/service/RestErrorCode.h new file mode 100644 index 000000000..92e088762 --- /dev/null +++ b/hikyuu_cpp/hikyuu_server/service/RestErrorCode.h @@ -0,0 +1,14 @@ +/* + * Copyright(C) 2021 hikyuu.org + * + * Create on: 2021-04-13 + * Author: fasiondog + */ + +#pragma once + +namespace hku { + +enum TradeErrorCode { TD_ACCOUNT_REPETITION = 20000 }; + +} \ No newline at end of file diff --git a/hikyuu_cpp/hikyuu_server/service/RestHandle.h b/hikyuu_cpp/hikyuu_server/service/RestHandle.h index e94ee622c..c9bc6f603 100644 --- a/hikyuu_cpp/hikyuu_server/service/RestHandle.h +++ b/hikyuu_cpp/hikyuu_server/service/RestHandle.h @@ -8,6 +8,7 @@ #pragma once #include "http/HttpHandle.h" +#include "RestErrorCode.h" namespace hku { diff --git a/hikyuu_cpp/hikyuu_server/service/trade/TradeAccountHandle.cpp b/hikyuu_cpp/hikyuu_server/service/trade/TradeAccountHandle.cpp new file mode 100644 index 000000000..176d03037 --- /dev/null +++ b/hikyuu_cpp/hikyuu_server/service/trade/TradeAccountHandle.cpp @@ -0,0 +1,43 @@ +/* + * Copyright(C) 2021 hikyuu.org + * + * Create on: 2021-04-12 + * Author: fasiondog + */ + +#include "common/uuid.h" +#include "TradeAccountHandle.h" +#include "TradeService.h" + +namespace hku { + +void AddTradeAccountHandle::run() { + json req = getReqJson(); + HTTP_VALID_CHECK(req.contains("name"), HttpErrorCode::MISS_PARAMETER, + R"(Missing param "name")"); + HTTP_VALID_CHECK(req.contains("type"), HttpErrorCode::MISS_PARAMETER, + R"(Missing param "type")"); + TradeAccountModel account; + std::string name = req["name"].get(); + account.setName(name); + account.setType(req["type"].get()); + account.setAccount(UUID()); + auto con = TradeService::getDBConnect(); + { + TransAction trans(con); + HTTP_VALID_CHECK(!TradeAccountModel::isExistName(con, name), + TradeErrorCode::TD_ACCOUNT_REPETITION, "Name repetition"); + con->save(account, false); + } + json res; + to_json(res, account); + setResData(res); +} + +void GetTradeAccountHandle::run() {} + +void ModTradeAccountHandle::run() {} + +void DelTradeAccountHandle::run() {} + +} // namespace hku \ No newline at end of file diff --git a/hikyuu_cpp/hikyuu_server/service/trade/TradeAccountHandle.h b/hikyuu_cpp/hikyuu_server/service/trade/TradeAccountHandle.h index 1e3d695e4..7e3b461b1 100644 --- a/hikyuu_cpp/hikyuu_server/service/trade/TradeAccountHandle.h +++ b/hikyuu_cpp/hikyuu_server/service/trade/TradeAccountHandle.h @@ -7,6 +7,7 @@ #pragma once +#include #include "../RestHandle.h" #include "model/TradeAccountModel.h" @@ -14,37 +15,22 @@ namespace hku { class AddTradeAccountHandle : public RestHandle { REST_HANDLE_IMP(AddTradeAccountHandle) - - virtual void run() override { - json req = getReqJson(); - HTTP_VALID_CHECK(req.contains("name"), HttpErrorCode::MISS_PARAMETER, - R"(Missing param "name")"); - HTTP_VALID_CHECK(req.contains("type"), HttpErrorCode::MISS_PARAMETER, - R"(Missing param "type")"); - TradeAccountModel account; - account.setName(req["name"].get()); - account.setType(req["type"].get()); - LOG_INFO("{}", req); - setResData(R"([{"result": true, "account": "12345"}])"); - } + virtual void run() override; }; class GetTradeAccountHandle : public RestHandle { REST_HANDLE_IMP(GetTradeAccountHandle) - - virtual void run() override {} + virtual void run() override; }; class ModTradeAccountHandle : public RestHandle { REST_HANDLE_IMP(ModTradeAccountHandle) - - virtual void run() override {} + virtual void run() override; }; class DelTradeAccountHandle : public RestHandle { REST_HANDLE_IMP(DelTradeAccountHandle) - - virtual void run() override {} + virtual void run() override; }; } // namespace hku \ No newline at end of file diff --git a/hikyuu_cpp/hikyuu_server/service/trade/TradeService.h b/hikyuu_cpp/hikyuu_server/service/trade/TradeService.h index 4ca81b5f4..d61d3b6a7 100644 --- a/hikyuu_cpp/hikyuu_server/service/trade/TradeService.h +++ b/hikyuu_cpp/hikyuu_server/service/trade/TradeService.h @@ -36,7 +36,7 @@ class TradeService : public HttpService { } public: - DBConnectPtr getDBConnect(); + static DBConnectPtr getDBConnect(); private: static void initTradeServiceSqlite(const Parameter ¶m); diff --git a/hikyuu_cpp/hikyuu_server/service/trade/db/sqlite/create.cpp b/hikyuu_cpp/hikyuu_server/service/trade/db/sqlite/create.cpp index d7ded486a..8fddecca0 100644 --- a/hikyuu_cpp/hikyuu_server/service/trade/db/sqlite/create.cpp +++ b/hikyuu_cpp/hikyuu_server/service/trade/db/sqlite/create.cpp @@ -14,13 +14,16 @@ const char *g_sqlite_create_db{ CREATE TABLE "td_account" ( "id" INTEGER NOT NULL UNIQUE, "account" TEXT NOT NULL UNIQUE, - "name" TEXT NOT NULL, + "name" TEXT NOT NULL UNIQUE, "type" TEXT NOT NULL, PRIMARY KEY("id" AUTOINCREMENT) ); CREATE UNIQUE INDEX "ix_td_account_on_account" ON "td_account" ( "account" ASC ); +CREATE UNIQUE INDEX "ix_td_account_on_name" ON "td_account" ( + "name" ASC +); CREATE TABLE "td_funds" ( "id" INTEGER NOT NULL UNIQUE, "td_id" INTEGER NOT NULL, diff --git a/hikyuu_cpp/hikyuu_server/service/trade/model/TradeAccountModel.h b/hikyuu_cpp/hikyuu_server/service/trade/model/TradeAccountModel.h index 5d316941e..891a5ed10 100644 --- a/hikyuu_cpp/hikyuu_server/service/trade/model/TradeAccountModel.h +++ b/hikyuu_cpp/hikyuu_server/service/trade/model/TradeAccountModel.h @@ -8,7 +8,7 @@ #pragma once #include -#include +#include using nlohmann::json; @@ -17,6 +17,18 @@ namespace hku { class TradeAccountModel { TABLE_BIND3(td_account, account, name, type) +public: + static bool isExistName(DBConnectPtr con, const std::string& name) { + SQLStatementPtr st = con->getStatement( + fmt::format(R"(select count(id) from {} where name="{}")", getTableName(), name)); + st->exec(); + st->moveNext(); + int result = 0; + st->getColumn(0, result); + return result != 0; + } + +public: std::string getAccount() const { return account; } @@ -51,11 +63,10 @@ class TradeAccountModel { }; inline void to_json(json& j, const TradeAccountModel& p) { - j = json{{"id", p.m_id}, {"account", p.account}, {"name", p.name}, {"type", p.type}}; + j = json{{"account", p.account}, {"name", p.name}, {"type", p.type}}; } inline void from_json(const json& j, TradeAccountModel& p) { - j.at("id").get_to(p.m_id); j.at("account").get_to(p.account); j.at("name").get_to(p.name); j.at("type").get_to(p.type); diff --git a/test_data/trader.db b/test_data/trader.db index f0d03d2e3..00d550412 100644 Binary files a/test_data/trader.db and b/test_data/trader.db differ