diff --git a/orm_lib/tests/db_test.cc b/orm_lib/tests/db_test.cc index 21a70a15..a8199fb2 100644 --- a/orm_lib/tests/db_test.cc +++ b/orm_lib/tests/db_test.cc @@ -1477,6 +1477,7 @@ DROGON_TEST(SQLite3Test) " avatar_id varchar(32)," " salt character varchar(20)," " admin boolean DEFAULT false," + " create_time datetime," " CONSTRAINT user_id_org UNIQUE(user_id, org_name)" ")" >> [TEST_CTX](const Result &r) { SUCCESS(); } >> @@ -1486,29 +1487,49 @@ DROGON_TEST(SQLite3Test) }; /// Test1:DbClient streaming-type interface /// 1.1 insert,non-blocking - *clientPtr << "insert into users (user_id,user_name,password,org_name) " - "values(?,?,?,?)" + *clientPtr << "insert into users " + "(user_id,user_name,password,org_name,create_time) " + "values(?,?,?,?,?)" << "pg" << "postgresql" << "123" - << "default" >> + << "default" << trantor::Date::now() >> [TEST_CTX](const Result &r) { MANDATE(r.insertId() == 1ULL); } >> [TEST_CTX](const DrogonDbException &e) { FAULT("sqlite3 - DbClient streaming-type interface(0) what():", e.base().what()); }; /// 1.2 insert,blocking - *clientPtr << "insert into users (user_id,user_name,password,org_name) " - "values(?,?,?,?)" + *clientPtr << "insert into users " + "(user_id,user_name,password,org_name,create_time) " + "values(?,?,?,?,?)" << "pg1" << "postgresql1" << "123" - << "default" << Mode::Blocking >> + << "default" << trantor::Date::now() << Mode::Blocking >> [TEST_CTX](const Result &r) { MANDATE(r.insertId() == 2ULL); } >> [TEST_CTX](const DrogonDbException &e) { FAULT("sqlite3 - DbClient streaming-type interface(1) what():", e.base().what()); }; + *clientPtr << "select * from users where user_id=?;" + << "pg1" << Mode::Blocking >> + [TEST_CTX](const Result &r) { + MANDATE(r.size() == 1); + MANDATE(r[0]["user_id"].as() == "pg1"); + using namespace drogon_model::sqlite3; + Users user(r[0]); + MANDATE(user.getValueOfUserId() == "pg1"); + // LOG_INFO << "user:" << user.toJson().toStyledString(); + MANDATE(trantor::Date::now().secondsSinceEpoch() - + user.getValueOfCreateTime().secondsSinceEpoch() <= + 1); + } >> + [TEST_CTX](const DrogonDbException &e) { + FAULT("sqlite3 - DbClient streaming-type interface(2) what():", + e.base().what()); + }; + /// 1.3 query,no-blocking *clientPtr << "select * from users where 1 = 1" << Mode::NonBlocking >> [TEST_CTX](const Result &r) { MANDATE(r.size() == 2UL); } >> @@ -1596,8 +1617,8 @@ DROGON_TEST(SQLite3Test) /// 2.1 insert clientPtr->execSqlAsync( "insert into users " - "(user_id,user_name,password,org_name) " - "values(?,?,?,?)", + "(user_id,user_name,password,org_name,create_time) " + "values(?,?,?,?,?)", [TEST_CTX](const Result &r) { MANDATE(r.insertId() == 1ULL); }, [TEST_CTX](const DrogonDbException &e) { FAULT("sqlite3 - DbClient asynchronous interface(0) what():", @@ -1606,12 +1627,13 @@ DROGON_TEST(SQLite3Test) "pg", "postgresql", "123", - "default"); + "default", + trantor::Date::now()); /// 2.2 insert clientPtr->execSqlAsync( "insert into users " - "(user_id,user_name,password,org_name) " - "values(?,?,?,?)", + "(user_id,user_name,password,org_name,create_time) " + "values(?,?,?,?,?)", [TEST_CTX](const Result &r) { MANDATE(r.affectedRows() == 1UL); }, [TEST_CTX](const DrogonDbException &e) { FAULT("sqlite3 - DbClient asynchronous interface(1) what():", @@ -1620,7 +1642,8 @@ DROGON_TEST(SQLite3Test) "pg1", "postgresql1", "123", - "default"); + "default", + trantor::Date::now()); /// 2.3 query clientPtr->execSqlAsync( "select * from users where 1 = 1", @@ -1692,12 +1715,14 @@ DROGON_TEST(SQLite3Test) try { auto r = clientPtr->execSqlSync( - "insert into users (user_id,user_name,password,org_name) " - "values(?,?,?,?)", + "insert into users " + "(user_id,user_name,password,org_name,create_time) " + "values(?,?,?,?,?)", "pg", "postgresql", "123", - "default"); + "default", + trantor::Date::now()); MANDATE(r.insertId() == 1ULL); } catch (const DrogonDbException &e) @@ -1712,12 +1737,14 @@ DROGON_TEST(SQLite3Test) drogon::string_view sv1("postgresql1"); drogon::string_view sv2("123"); auto r = clientPtr->execSqlSync( - "insert into users (user_id,user_name,password,org_name) " - "values(?,?,?,?)", + "insert into users " + "(user_id,user_name,password,org_name,create_time) " + "values(?,?,?,?,?)", sv, (const drogon::string_view &)sv1, std::move(sv2), - drogon::string_view("default")); + drogon::string_view("default"), + trantor::Date::now()); MANDATE(r.affectedRows() == 1UL); } catch (const DrogonDbException &e) @@ -1789,12 +1816,13 @@ DROGON_TEST(SQLite3Test) /// Test future interface /// 4.1 insert auto f = clientPtr->execSqlAsyncFuture( - "insert into users (user_id,user_name,password,org_name) " - "values(?,?,?,?) ", + "insert into users (user_id,user_name,password,org_name,create_time) " + "values(?,?,?,?,?) ", "pg", "postgresql", "123", - "default"); + "default", + trantor::Date::now()); try { auto r = f.get(); @@ -1807,12 +1835,13 @@ DROGON_TEST(SQLite3Test) } /// 4.2 insert f = clientPtr->execSqlAsyncFuture( - "insert into users (user_id,user_name,password,org_name) " - "values(?,?,?,?)", + "insert into users (user_id,user_name,password,org_name,create_time) " + "values(?,?,?,?,?)", "pg1", "postgresql1", "123", - "default"); + "default", + trantor::Date::now()); try { auto r = f.get(); @@ -1910,12 +1939,14 @@ DROGON_TEST(SQLite3Test) try { auto r = clientPtr->execSqlSync( - "insert into users (user_id,user_name,password,org_name) " - "values(?,?,?,?)", + "insert into users " + "(user_id,user_name,password,org_name,create_time) " + "values(?,?,?,?,?)", "pg", "postgresql", "123", - "default"); + "default", + trantor::Date::now()); MANDATE(r.insertId() == 1ULL); } catch (const DrogonDbException &e) @@ -1978,6 +2009,7 @@ DROGON_TEST(SQLite3Test) user.setUserName("postgres"); user.setPassword("123"); user.setOrgName("default"); + user.setCreateTime(trantor::Date::now()); mapper.insert( user, [TEST_CTX](Users ret) { MANDATE(ret.getPrimaryKey() == 1UL); }, diff --git a/orm_lib/tests/sqlite3/Users.cc b/orm_lib/tests/sqlite3/Users.cc index 45901f77..872185af 100644 --- a/orm_lib/tests/sqlite3/Users.cc +++ b/orm_lib/tests/sqlite3/Users.cc @@ -10,6 +10,7 @@ #include using namespace drogon; +using namespace drogon::orm; using namespace drogon_model::sqlite3; const std::string Users::Cols::_id = "id"; @@ -21,6 +22,7 @@ const std::string Users::Cols::_signature = "signature"; const std::string Users::Cols::_avatar_id = "avatar_id"; const std::string Users::Cols::_salt = "salt"; const std::string Users::Cols::_admin = "admin"; +const std::string Users::Cols::_create_time = "create_time"; const std::string Users::primaryKeyName = "id"; const bool Users::hasPrimaryKey = true; const std::string Users::tableName = "users"; @@ -33,8 +35,9 @@ const std::vector Users::metaData_ = { {"org_name", "std::string", "varchar(20)", 0, 0, 0, 0}, {"signature", "std::string", "varchar(50)", 0, 0, 0, 0}, {"avatar_id", "std::string", "varchar(32)", 0, 0, 0, 0}, - {"salt", "std::string", "character", 0, 0, 0, 0}, - {"admin", "std::string", "boolean", 0, 0, 0, 0}}; + {"salt", "std::string", "character varchar(20)", 0, 0, 0, 0}, + {"admin", "std::string", "boolean", 0, 0, 0, 0}, + {"create_time", "::trantor::Date", "datetime", 0, 0, 0, 0}}; const std::string &Users::getColumnName(size_t index) noexcept(false) { assert(index < metaData_.size()); @@ -87,11 +90,34 @@ Users::Users(const Row &r, const ssize_t indexOffset) noexcept admin_ = std::make_shared(r["admin"].as()); } + if (!r["create_time"].isNull()) + { + auto timeStr = r["create_time"].as(); + struct tm stm; + memset(&stm, 0, sizeof(stm)); + auto p = strptime(timeStr.c_str(), "%Y-%m-%d %H:%M:%S", &stm); + time_t t = mktime(&stm); + size_t decimalNum = 0; + if (p) + { + if (*p == '.') + { + std::string decimals(p + 1, &timeStr[timeStr.length()]); + while (decimals.length() < 6) + { + decimals += "0"; + } + decimalNum = (size_t)atol(decimals.c_str()); + } + createTime_ = + std::make_shared<::trantor::Date>(t * 1000000 + decimalNum); + } + } } else { size_t offset = (size_t)indexOffset; - if (offset + 9 > r.size()) + if (offset + 10 > r.size()) { LOG_FATAL << "Invalid SQL result for this model"; return; @@ -147,6 +173,30 @@ Users::Users(const Row &r, const ssize_t indexOffset) noexcept { admin_ = std::make_shared(r[index].as()); } + index = offset + 9; + if (!r[index].isNull()) + { + auto timeStr = r[index].as(); + struct tm stm; + memset(&stm, 0, sizeof(stm)); + auto p = strptime(timeStr.c_str(), "%Y-%m-%d %H:%M:%S", &stm); + time_t t = mktime(&stm); + size_t decimalNum = 0; + if (p) + { + if (*p == '.') + { + std::string decimals(p + 1, &timeStr[timeStr.length()]); + while (decimals.length() < 6) + { + decimals += "0"; + } + decimalNum = (size_t)atol(decimals.c_str()); + } + createTime_ = + std::make_shared<::trantor::Date>(t * 1000000 + decimalNum); + } + } } } @@ -154,7 +204,7 @@ Users::Users( const Json::Value &pJson, const std::vector &pMasqueradingVector) noexcept(false) { - if (pMasqueradingVector.size() != 9) + if (pMasqueradingVector.size() != 10) { LOG_ERROR << "Bad masquerading vector"; return; @@ -249,6 +299,34 @@ Users::Users( pJson[pMasqueradingVector[8]].asString()); } } + if (!pMasqueradingVector[9].empty() && + pJson.isMember(pMasqueradingVector[9])) + { + dirtyFlag_[9] = true; + if (!pJson[pMasqueradingVector[9]].isNull()) + { + auto timeStr = pJson[pMasqueradingVector[9]].asString(); + struct tm stm; + memset(&stm, 0, sizeof(stm)); + auto p = strptime(timeStr.c_str(), "%Y-%m-%d %H:%M:%S", &stm); + time_t t = mktime(&stm); + size_t decimalNum = 0; + if (p) + { + if (*p == '.') + { + std::string decimals(p + 1, &timeStr[timeStr.length()]); + while (decimals.length() < 6) + { + decimals += "0"; + } + decimalNum = (size_t)atol(decimals.c_str()); + } + createTime_ = + std::make_shared<::trantor::Date>(t * 1000000 + decimalNum); + } + } + } } Users::Users(const Json::Value &pJson) noexcept(false) @@ -331,13 +409,40 @@ Users::Users(const Json::Value &pJson) noexcept(false) admin_ = std::make_shared(pJson["admin"].asString()); } } + if (pJson.isMember("create_time")) + { + dirtyFlag_[9] = true; + if (!pJson["create_time"].isNull()) + { + auto timeStr = pJson["create_time"].asString(); + struct tm stm; + memset(&stm, 0, sizeof(stm)); + auto p = strptime(timeStr.c_str(), "%Y-%m-%d %H:%M:%S", &stm); + time_t t = mktime(&stm); + size_t decimalNum = 0; + if (p) + { + if (*p == '.') + { + std::string decimals(p + 1, &timeStr[timeStr.length()]); + while (decimals.length() < 6) + { + decimals += "0"; + } + decimalNum = (size_t)atol(decimals.c_str()); + } + createTime_ = + std::make_shared<::trantor::Date>(t * 1000000 + decimalNum); + } + } + } } void Users::updateByMasqueradedJson( const Json::Value &pJson, const std::vector &pMasqueradingVector) noexcept(false) { - if (pMasqueradingVector.size() != 9) + if (pMasqueradingVector.size() != 10) { LOG_ERROR << "Bad masquerading vector"; return; @@ -431,6 +536,34 @@ void Users::updateByMasqueradedJson( pJson[pMasqueradingVector[8]].asString()); } } + if (!pMasqueradingVector[9].empty() && + pJson.isMember(pMasqueradingVector[9])) + { + dirtyFlag_[9] = true; + if (!pJson[pMasqueradingVector[9]].isNull()) + { + auto timeStr = pJson[pMasqueradingVector[9]].asString(); + struct tm stm; + memset(&stm, 0, sizeof(stm)); + auto p = strptime(timeStr.c_str(), "%Y-%m-%d %H:%M:%S", &stm); + time_t t = mktime(&stm); + size_t decimalNum = 0; + if (p) + { + if (*p == '.') + { + std::string decimals(p + 1, &timeStr[timeStr.length()]); + while (decimals.length() < 6) + { + decimals += "0"; + } + decimalNum = (size_t)atol(decimals.c_str()); + } + createTime_ = + std::make_shared<::trantor::Date>(t * 1000000 + decimalNum); + } + } + } } void Users::updateByJson(const Json::Value &pJson) noexcept(false) @@ -512,6 +645,33 @@ void Users::updateByJson(const Json::Value &pJson) noexcept(false) admin_ = std::make_shared(pJson["admin"].asString()); } } + if (pJson.isMember("create_time")) + { + dirtyFlag_[9] = true; + if (!pJson["create_time"].isNull()) + { + auto timeStr = pJson["create_time"].asString(); + struct tm stm; + memset(&stm, 0, sizeof(stm)); + auto p = strptime(timeStr.c_str(), "%Y-%m-%d %H:%M:%S", &stm); + time_t t = mktime(&stm); + size_t decimalNum = 0; + if (p) + { + if (*p == '.') + { + std::string decimals(p + 1, &timeStr[timeStr.length()]); + while (decimals.length() < 6) + { + decimals += "0"; + } + decimalNum = (size_t)atol(decimals.c_str()); + } + createTime_ = + std::make_shared<::trantor::Date>(t * 1000000 + decimalNum); + } + } + } } const uint64_t &Users::getValueOfId() const noexcept @@ -525,6 +685,16 @@ const std::shared_ptr &Users::getId() const noexcept { return id_; } +void Users::setId(const uint64_t &pId) noexcept +{ + id_ = std::make_shared(pId); + dirtyFlag_[0] = true; +} +void Users::setIdToNull() noexcept +{ + id_.reset(); + dirtyFlag_[0] = true; +} const typename Users::PrimaryKeyType &Users::getPrimaryKey() const { assert(id_); @@ -552,7 +722,6 @@ void Users::setUserId(std::string &&pUserId) noexcept userId_ = std::make_shared(std::move(pUserId)); dirtyFlag_[1] = true; } - void Users::setUserIdToNull() noexcept { userId_.reset(); @@ -580,7 +749,6 @@ void Users::setUserName(std::string &&pUserName) noexcept userName_ = std::make_shared(std::move(pUserName)); dirtyFlag_[2] = true; } - void Users::setUserNameToNull() noexcept { userName_.reset(); @@ -608,7 +776,6 @@ void Users::setPassword(std::string &&pPassword) noexcept password_ = std::make_shared(std::move(pPassword)); dirtyFlag_[3] = true; } - void Users::setPasswordToNull() noexcept { password_.reset(); @@ -636,7 +803,6 @@ void Users::setOrgName(std::string &&pOrgName) noexcept orgName_ = std::make_shared(std::move(pOrgName)); dirtyFlag_[4] = true; } - void Users::setOrgNameToNull() noexcept { orgName_.reset(); @@ -664,7 +830,6 @@ void Users::setSignature(std::string &&pSignature) noexcept signature_ = std::make_shared(std::move(pSignature)); dirtyFlag_[5] = true; } - void Users::setSignatureToNull() noexcept { signature_.reset(); @@ -692,7 +857,6 @@ void Users::setAvatarId(std::string &&pAvatarId) noexcept avatarId_ = std::make_shared(std::move(pAvatarId)); dirtyFlag_[6] = true; } - void Users::setAvatarIdToNull() noexcept { avatarId_.reset(); @@ -720,7 +884,6 @@ void Users::setSalt(std::string &&pSalt) noexcept salt_ = std::make_shared(std::move(pSalt)); dirtyFlag_[7] = true; } - void Users::setSaltToNull() noexcept { salt_.reset(); @@ -748,13 +911,34 @@ void Users::setAdmin(std::string &&pAdmin) noexcept admin_ = std::make_shared(std::move(pAdmin)); dirtyFlag_[8] = true; } - void Users::setAdminToNull() noexcept { admin_.reset(); dirtyFlag_[8] = true; } +const ::trantor::Date &Users::getValueOfCreateTime() const noexcept +{ + const static ::trantor::Date defaultValue = ::trantor::Date(); + if (createTime_) + return *createTime_; + return defaultValue; +} +const std::shared_ptr<::trantor::Date> &Users::getCreateTime() const noexcept +{ + return createTime_; +} +void Users::setCreateTime(const ::trantor::Date &pCreateTime) noexcept +{ + createTime_ = std::make_shared<::trantor::Date>(pCreateTime); + dirtyFlag_[9] = true; +} +void Users::setCreateTimeToNull() noexcept +{ + createTime_.reset(); + dirtyFlag_[9] = true; +} + void Users::updateId(const uint64_t id) { id_ = std::make_shared(id); @@ -769,7 +953,8 @@ const std::vector &Users::insertColumns() noexcept "signature", "avatar_id", "salt", - "admin"}; + "admin", + "create_time"}; return inCols; } @@ -863,17 +1048,57 @@ void Users::outputArgs(drogon::orm::internal::SqlBinder &binder) const binder << nullptr; } } + if (dirtyFlag_[9]) + { + if (getCreateTime()) + { + binder << getValueOfCreateTime(); + } + else + { + binder << nullptr; + } + } } const std::vector Users::updateColumns() const { std::vector ret; - for (size_t i = 0; i < sizeof(dirtyFlag_); i++) + if (dirtyFlag_[1]) { - if (dirtyFlag_[i]) - { - ret.push_back(getColumnName(i)); - } + ret.push_back(getColumnName(1)); + } + if (dirtyFlag_[2]) + { + ret.push_back(getColumnName(2)); + } + if (dirtyFlag_[3]) + { + ret.push_back(getColumnName(3)); + } + if (dirtyFlag_[4]) + { + ret.push_back(getColumnName(4)); + } + if (dirtyFlag_[5]) + { + ret.push_back(getColumnName(5)); + } + if (dirtyFlag_[6]) + { + ret.push_back(getColumnName(6)); + } + if (dirtyFlag_[7]) + { + ret.push_back(getColumnName(7)); + } + if (dirtyFlag_[8]) + { + ret.push_back(getColumnName(8)); + } + if (dirtyFlag_[9]) + { + ret.push_back(getColumnName(9)); } return ret; } @@ -968,6 +1193,17 @@ void Users::updateArgs(drogon::orm::internal::SqlBinder &binder) const binder << nullptr; } } + if (dirtyFlag_[9]) + { + if (getCreateTime()) + { + binder << getValueOfCreateTime(); + } + else + { + binder << nullptr; + } + } } Json::Value Users::toJson() const { @@ -1044,6 +1280,14 @@ Json::Value Users::toJson() const { ret["admin"] = Json::Value(); } + if (getCreateTime()) + { + ret["create_time"] = getCreateTime()->toDbStringLocal(); + } + else + { + ret["create_time"] = Json::Value(); + } return ret; } @@ -1051,7 +1295,7 @@ Json::Value Users::toMasqueradedJson( const std::vector &pMasqueradingVector) const { Json::Value ret; - if (pMasqueradingVector.size() == 9) + if (pMasqueradingVector.size() == 10) { if (!pMasqueradingVector[0].empty()) { @@ -1152,6 +1396,18 @@ Json::Value Users::toMasqueradedJson( ret[pMasqueradingVector[8]] = Json::Value(); } } + if (!pMasqueradingVector[9].empty()) + { + if (getCreateTime()) + { + ret[pMasqueradingVector[9]] = + getCreateTime()->toDbStringLocal(); + } + else + { + ret[pMasqueradingVector[9]] = Json::Value(); + } + } return ret; } LOG_ERROR << "Masquerade failed"; @@ -1227,6 +1483,14 @@ Json::Value Users::toMasqueradedJson( { ret["admin"] = Json::Value(); } + if (getCreateTime()) + { + ret["create_time"] = getCreateTime()->toDbStringLocal(); + } + else + { + ret["create_time"] = Json::Value(); + } return ret; } @@ -1277,6 +1541,12 @@ bool Users::validateJsonForCreation(const Json::Value &pJson, std::string &err) if (!validJsonOfField(8, "admin", pJson["admin"], err, true)) return false; } + if (pJson.isMember("create_time")) + { + if (!validJsonOfField( + 9, "create_time", pJson["create_time"], err, true)) + return false; + } return true; } bool Users::validateMasqueradedJsonForCreation( @@ -1284,118 +1554,138 @@ bool Users::validateMasqueradedJsonForCreation( const std::vector &pMasqueradingVector, std::string &err) { - if (pMasqueradingVector.size() != 9) + if (pMasqueradingVector.size() != 10) { err = "Bad masquerading vector"; return false; } - if (!pMasqueradingVector[0].empty()) + try { - if (pJson.isMember(pMasqueradingVector[0])) + if (!pMasqueradingVector[0].empty()) { - if (!validJsonOfField(0, - pMasqueradingVector[0], - pJson[pMasqueradingVector[0]], - err, - true)) - return false; + if (pJson.isMember(pMasqueradingVector[0])) + { + if (!validJsonOfField(0, + pMasqueradingVector[0], + pJson[pMasqueradingVector[0]], + err, + true)) + return false; + } + } + if (!pMasqueradingVector[1].empty()) + { + if (pJson.isMember(pMasqueradingVector[1])) + { + if (!validJsonOfField(1, + pMasqueradingVector[1], + pJson[pMasqueradingVector[1]], + err, + true)) + return false; + } + } + if (!pMasqueradingVector[2].empty()) + { + if (pJson.isMember(pMasqueradingVector[2])) + { + if (!validJsonOfField(2, + pMasqueradingVector[2], + pJson[pMasqueradingVector[2]], + err, + true)) + return false; + } + } + if (!pMasqueradingVector[3].empty()) + { + if (pJson.isMember(pMasqueradingVector[3])) + { + if (!validJsonOfField(3, + pMasqueradingVector[3], + pJson[pMasqueradingVector[3]], + err, + true)) + return false; + } + } + if (!pMasqueradingVector[4].empty()) + { + if (pJson.isMember(pMasqueradingVector[4])) + { + if (!validJsonOfField(4, + pMasqueradingVector[4], + pJson[pMasqueradingVector[4]], + err, + true)) + return false; + } + } + if (!pMasqueradingVector[5].empty()) + { + if (pJson.isMember(pMasqueradingVector[5])) + { + if (!validJsonOfField(5, + pMasqueradingVector[5], + pJson[pMasqueradingVector[5]], + err, + true)) + return false; + } + } + if (!pMasqueradingVector[6].empty()) + { + if (pJson.isMember(pMasqueradingVector[6])) + { + if (!validJsonOfField(6, + pMasqueradingVector[6], + pJson[pMasqueradingVector[6]], + err, + true)) + return false; + } + } + if (!pMasqueradingVector[7].empty()) + { + if (pJson.isMember(pMasqueradingVector[7])) + { + if (!validJsonOfField(7, + pMasqueradingVector[7], + pJson[pMasqueradingVector[7]], + err, + true)) + return false; + } + } + if (!pMasqueradingVector[8].empty()) + { + if (pJson.isMember(pMasqueradingVector[8])) + { + if (!validJsonOfField(8, + pMasqueradingVector[8], + pJson[pMasqueradingVector[8]], + err, + true)) + return false; + } + } + if (!pMasqueradingVector[9].empty()) + { + if (pJson.isMember(pMasqueradingVector[9])) + { + if (!validJsonOfField(9, + pMasqueradingVector[9], + pJson[pMasqueradingVector[9]], + err, + true)) + return false; + } } } - if (!pMasqueradingVector[1].empty()) + catch (const Json::LogicError &e) { - if (pJson.isMember(pMasqueradingVector[1])) - { - if (!validJsonOfField(1, - pMasqueradingVector[1], - pJson[pMasqueradingVector[1]], - err, - true)) - return false; - } - } - if (!pMasqueradingVector[2].empty()) - { - if (pJson.isMember(pMasqueradingVector[2])) - { - if (!validJsonOfField(2, - pMasqueradingVector[2], - pJson[pMasqueradingVector[2]], - err, - true)) - return false; - } - } - if (!pMasqueradingVector[3].empty()) - { - if (pJson.isMember(pMasqueradingVector[3])) - { - if (!validJsonOfField(3, - pMasqueradingVector[3], - pJson[pMasqueradingVector[3]], - err, - true)) - return false; - } - } - if (!pMasqueradingVector[4].empty()) - { - if (pJson.isMember(pMasqueradingVector[4])) - { - if (!validJsonOfField(4, - pMasqueradingVector[4], - pJson[pMasqueradingVector[4]], - err, - true)) - return false; - } - } - if (!pMasqueradingVector[5].empty()) - { - if (pJson.isMember(pMasqueradingVector[5])) - { - if (!validJsonOfField(5, - pMasqueradingVector[5], - pJson[pMasqueradingVector[5]], - err, - true)) - return false; - } - } - if (!pMasqueradingVector[6].empty()) - { - if (pJson.isMember(pMasqueradingVector[6])) - { - if (!validJsonOfField(6, - pMasqueradingVector[6], - pJson[pMasqueradingVector[6]], - err, - true)) - return false; - } - } - if (!pMasqueradingVector[7].empty()) - { - if (pJson.isMember(pMasqueradingVector[7])) - { - if (!validJsonOfField(7, - pMasqueradingVector[7], - pJson[pMasqueradingVector[7]], - err, - true)) - return false; - } - } - if (!pMasqueradingVector[8].empty()) - { - if (pJson.isMember(pMasqueradingVector[8])) - { - if (!validJsonOfField(8, - pMasqueradingVector[8], - pJson[pMasqueradingVector[8]], - err, - true)) - return false; - } + err = e.what(); + return false; } return true; } @@ -1453,6 +1743,12 @@ bool Users::validateJsonForUpdate(const Json::Value &pJson, std::string &err) if (!validJsonOfField(8, "admin", pJson["admin"], err, false)) return false; } + if (pJson.isMember("create_time")) + { + if (!validJsonOfField( + 9, "create_time", pJson["create_time"], err, false)) + return false; + } return true; } bool Users::validateMasqueradedJsonForUpdate( @@ -1460,108 +1756,126 @@ bool Users::validateMasqueradedJsonForUpdate( const std::vector &pMasqueradingVector, std::string &err) { - if (pMasqueradingVector.size() != 9) + if (pMasqueradingVector.size() != 10) { err = "Bad masquerading vector"; return false; } - if (!pMasqueradingVector[0].empty() && - pJson.isMember(pMasqueradingVector[0])) + try { - if (!validJsonOfField(0, - pMasqueradingVector[0], - pJson[pMasqueradingVector[0]], - err, - false)) + if (!pMasqueradingVector[0].empty() && + pJson.isMember(pMasqueradingVector[0])) + { + if (!validJsonOfField(0, + pMasqueradingVector[0], + pJson[pMasqueradingVector[0]], + err, + false)) + return false; + } + else + { + err = + "The value of primary key must be set in the json object for " + "update"; return false; + } + if (!pMasqueradingVector[1].empty() && + pJson.isMember(pMasqueradingVector[1])) + { + if (!validJsonOfField(1, + pMasqueradingVector[1], + pJson[pMasqueradingVector[1]], + err, + false)) + return false; + } + if (!pMasqueradingVector[2].empty() && + pJson.isMember(pMasqueradingVector[2])) + { + if (!validJsonOfField(2, + pMasqueradingVector[2], + pJson[pMasqueradingVector[2]], + err, + false)) + return false; + } + if (!pMasqueradingVector[3].empty() && + pJson.isMember(pMasqueradingVector[3])) + { + if (!validJsonOfField(3, + pMasqueradingVector[3], + pJson[pMasqueradingVector[3]], + err, + false)) + return false; + } + if (!pMasqueradingVector[4].empty() && + pJson.isMember(pMasqueradingVector[4])) + { + if (!validJsonOfField(4, + pMasqueradingVector[4], + pJson[pMasqueradingVector[4]], + err, + false)) + return false; + } + if (!pMasqueradingVector[5].empty() && + pJson.isMember(pMasqueradingVector[5])) + { + if (!validJsonOfField(5, + pMasqueradingVector[5], + pJson[pMasqueradingVector[5]], + err, + false)) + return false; + } + if (!pMasqueradingVector[6].empty() && + pJson.isMember(pMasqueradingVector[6])) + { + if (!validJsonOfField(6, + pMasqueradingVector[6], + pJson[pMasqueradingVector[6]], + err, + false)) + return false; + } + if (!pMasqueradingVector[7].empty() && + pJson.isMember(pMasqueradingVector[7])) + { + if (!validJsonOfField(7, + pMasqueradingVector[7], + pJson[pMasqueradingVector[7]], + err, + false)) + return false; + } + if (!pMasqueradingVector[8].empty() && + pJson.isMember(pMasqueradingVector[8])) + { + if (!validJsonOfField(8, + pMasqueradingVector[8], + pJson[pMasqueradingVector[8]], + err, + false)) + return false; + } + if (!pMasqueradingVector[9].empty() && + pJson.isMember(pMasqueradingVector[9])) + { + if (!validJsonOfField(9, + pMasqueradingVector[9], + pJson[pMasqueradingVector[9]], + err, + false)) + return false; + } } - else + catch (const Json::LogicError &e) { - err = - "The value of primary key must be set in the json object for " - "update"; + err = e.what(); return false; } - if (!pMasqueradingVector[1].empty() && - pJson.isMember(pMasqueradingVector[1])) - { - if (!validJsonOfField(1, - pMasqueradingVector[1], - pJson[pMasqueradingVector[1]], - err, - false)) - return false; - } - if (!pMasqueradingVector[2].empty() && - pJson.isMember(pMasqueradingVector[2])) - { - if (!validJsonOfField(2, - pMasqueradingVector[2], - pJson[pMasqueradingVector[2]], - err, - false)) - return false; - } - if (!pMasqueradingVector[3].empty() && - pJson.isMember(pMasqueradingVector[3])) - { - if (!validJsonOfField(3, - pMasqueradingVector[3], - pJson[pMasqueradingVector[3]], - err, - false)) - return false; - } - if (!pMasqueradingVector[4].empty() && - pJson.isMember(pMasqueradingVector[4])) - { - if (!validJsonOfField(4, - pMasqueradingVector[4], - pJson[pMasqueradingVector[4]], - err, - false)) - return false; - } - if (!pMasqueradingVector[5].empty() && - pJson.isMember(pMasqueradingVector[5])) - { - if (!validJsonOfField(5, - pMasqueradingVector[5], - pJson[pMasqueradingVector[5]], - err, - false)) - return false; - } - if (!pMasqueradingVector[6].empty() && - pJson.isMember(pMasqueradingVector[6])) - { - if (!validJsonOfField(6, - pMasqueradingVector[6], - pJson[pMasqueradingVector[6]], - err, - false)) - return false; - } - if (!pMasqueradingVector[7].empty() && - pJson.isMember(pMasqueradingVector[7])) - { - if (!validJsonOfField(7, - pMasqueradingVector[7], - pJson[pMasqueradingVector[7]], - err, - false)) - return false; - } - if (!pMasqueradingVector[8].empty() && - pJson.isMember(pMasqueradingVector[8])) - { - if (!validJsonOfField(8, - pMasqueradingVector[8], - pJson[pMasqueradingVector[8]], - err, - false)) - return false; - } return true; } bool Users::validJsonOfField(size_t index, @@ -1676,11 +1990,20 @@ bool Users::validJsonOfField(size_t index, return false; } break; - + case 9: + if (pJson.isNull()) + { + return true; + } + if (!pJson.isString()) + { + err = "Type error in the " + fieldName + " field"; + return false; + } + break; default: err = "Internal error in the server"; return false; - break; } return true; } diff --git a/orm_lib/tests/sqlite3/Users.h b/orm_lib/tests/sqlite3/Users.h index 754d08ec..983608d6 100644 --- a/orm_lib/tests/sqlite3/Users.h +++ b/orm_lib/tests/sqlite3/Users.h @@ -24,7 +24,6 @@ #include #include -using namespace drogon::orm; namespace drogon { namespace orm @@ -51,6 +50,7 @@ class Users static const std::string _avatar_id; static const std::string _salt; static const std::string _admin; + static const std::string _create_time; }; const static int primaryKeyNumber; @@ -68,7 +68,8 @@ class Users * @note If the SQL is not a style of 'select * from table_name ...' (select * all columns by an asterisk), please set the offset to -1. */ - explicit Users(const Row &r, const ssize_t indexOffset = 0) noexcept; + explicit Users(const drogon::orm::Row &r, + const ssize_t indexOffset = 0) noexcept; /** * @brief constructor @@ -115,6 +116,9 @@ class Users /// Return a shared_ptr object pointing to the column const value, or an /// empty shared_ptr object if the column is null const std::shared_ptr &getId() const noexcept; + /// Set the value of the column id + void setId(const uint64_t &pId) noexcept; + void setIdToNull() noexcept; /** For column user_id */ /// Get the value of the column user_id, returns the default value if the @@ -212,9 +216,20 @@ class Users void setAdmin(std::string &&pAdmin) noexcept; void setAdminToNull() noexcept; + /** For column create_time */ + /// Get the value of the column create_time, returns the default value if + /// the column is null + const ::trantor::Date &getValueOfCreateTime() const noexcept; + /// Return a shared_ptr object pointing to the column const value, or an + /// empty shared_ptr object if the column is null + const std::shared_ptr<::trantor::Date> &getCreateTime() const noexcept; + /// Set the value of the column create_time + void setCreateTime(const ::trantor::Date &pCreateTime) noexcept; + void setCreateTimeToNull() noexcept; + static size_t getColumnNumber() noexcept { - return 9; + return 10; } static const std::string &getColumnName(size_t index) noexcept(false); @@ -223,9 +238,9 @@ class Users const std::vector &pMasqueradingVector) const; /// Relationship interfaces private: - friend Mapper; + friend drogon::orm::Mapper; #ifdef __cpp_impl_coroutine - friend CoroMapper; + friend drogon::orm::CoroMapper; #endif static const std::vector &insertColumns() noexcept; void outputArgs(drogon::orm::internal::SqlBinder &binder) const; @@ -242,6 +257,7 @@ class Users std::shared_ptr avatarId_; std::shared_ptr salt_; std::shared_ptr admin_; + std::shared_ptr<::trantor::Date> createTime_; struct MetaData { const std::string colName_; @@ -253,7 +269,7 @@ class Users const bool notNull_; }; static const std::vector metaData_; - bool dirtyFlag_[9] = {false}; + bool dirtyFlag_[10] = {false}; public: static const std::string &sqlForFindingByPrimaryKey() @@ -314,6 +330,15 @@ class Users sql += "admin,"; ++parametersCount; } + if (!dirtyFlag_[8]) + { + needSelection = true; + } + if (dirtyFlag_[9]) + { + sql += "create_time,"; + ++parametersCount; + } if (parametersCount > 0) { sql[sql.length() - 1] = ')'; @@ -354,6 +379,10 @@ class Users { sql.append("?,"); } + if (dirtyFlag_[9]) + { + sql.append("?,"); + } if (parametersCount > 0) { sql.resize(sql.length() - 1);